import {z} from 'zod' export type GetApiApiMetaSitesV1AppsWithMetaSitesArgs = {offset?: number | undefined, limit?: number | undefined, search?: string | undefined, sort?: ('name' | 'type' | 'created_at' | '-name' | '-type' | '-created_at') | undefined, action?: ('view' | 'edit') | undefined}; export type GetApiApiMetaSitesV1AppsWithMetaSitesOptions = {baseUrl?: string | undefined, OAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiMetaSitesV1AppsWithMetaSitesResponse = z.object({meta: z.object({total: z.number().int()}), data: z.array(z.object({type: z.enum(["ppms/app", "ppms/meta-site"]), id: z.string(), attributes: z.object({name: z.string(), created_at: z.string()})}))}); 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 }; /** * List apps & meta sites * * GET /api/meta-sites/v1/apps-with-meta-sites */ export const getApiApiMetaSitesV1AppsWithMetaSites = async (args: GetApiApiMetaSitesV1AppsWithMetaSitesArgs, options?: GetApiApiMetaSitesV1AppsWithMetaSitesOptions) => { 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 != null) query.append("search", String(args.search)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.action != null) query.append("action", String(args.action)) if (OAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/meta-sites/v1/apps-with-meta-sites${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(getApiApiMetaSitesV1AppsWithMetaSitesResponse, await res.json()) };