import {z} from 'zod' export type GetApiApiAppsV2Args = {offset?: number | undefined, limit?: number | undefined, search_query?: string | undefined, sort?: ('name' | 'addedAt' | 'updatedAt' | '-name' | '-addedAt' | '-updatedAt') | undefined, search?: string | undefined, permission?: ('view' | 'edit' | 'publish' | 'manage') | undefined}; export type GetApiApiAppsV2Options = {baseUrl?: string | undefined, OAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiAppsV2Response = z.object({meta: z.object({total: z.number().int().optional()}).optional(), data: z.array(z.object({type: z.literal("ppms/app").optional(), id: z.string().optional(), attributes: z.object({name: z.string().max(90).optional(), addedAt: z.string().optional(), updatedAt: z.string().optional()}).optional()})).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Apps list * * GET /api/apps/v2 */ export const getApiApiAppsV2 = async (args: GetApiApiAppsV2Args, options?: GetApiApiAppsV2Options) => { const {baseUrl = "https://example.piwik.pro", fetch: fetchFn = fetch, OAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.search_query != null) query.append("search_query", String(args.search_query)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.search != null) query.append("search", String(args.search)) if (args.permission != null) query.append("permission", String(args.permission)) if (OAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/apps/v2${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiAppsV2Response, await res.json()) };