import {z} from 'zod' export type GetApiImplementationArgs = {sort?: (Array<'arch' | '-arch' | 'asciiArmor' | '-asciiArmor' | 'downloadUrl' | '-downloadUrl' | 'filename' | '-filename' | 'keyId' | '-keyId' | 'os' | '-os' | 'shasum' | '-shasum' | 'shasumsSignatureUrl' | '-shasumsSignatureUrl' | 'shasumsUrl' | '-shasumsUrl' | 'source' | '-source' | 'sourceUrl' | '-sourceUrl' | 'trustSignature' | '-trustSignature' | 'id' | '-id'>) | undefined, 'fields[implementation]'?: (Array<'arch' | 'asciiArmor' | 'downloadUrl' | 'filename' | 'keyId' | 'os' | 'shasum' | 'shasumsSignatureUrl' | 'shasumsUrl' | 'source' | 'sourceUrl' | 'trustSignature' | 'version'>) | undefined, include?: Array<'version'> | undefined, 'filter[implementation]'?: string | undefined, filter?: string | undefined, 'page[number]'?: number | undefined, 'page[size]'?: number | undefined, 'page[offset]'?: number | undefined, 'page[limit]'?: number | undefined, 'page[totals]'?: string | undefined}; export type GetApiImplementationOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const implementation = z.object({type: z.string().optional(), id: z.string().optional(), attributes: z.object({arch: z.string().optional(), asciiArmor: z.string().optional(), downloadUrl: z.string().optional(), filename: z.string().optional(), keyId: z.string().optional(), os: z.string().optional(), shasum: z.string().optional(), shasumsSignatureUrl: z.string().optional(), shasumsUrl: z.string().optional(), source: z.string().optional(), sourceUrl: z.string().optional(), trustSignature: z.string().optional()}).optional(), relationships: z.object({version: z.object({id: z.string().optional(), type: z.literal("version").optional()}).optional()}).optional()}); export const getApiImplementationResponse = z.object({data: z.array(implementation).optional(), included: z.array(z.object({attributes: z.object({}).optional(), id: z.string().optional(), relationships: z.object({}).optional(), type: z.string().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 }; /** * Returns the collection of type implementation * * GET /implementation */ export const getApiImplementation = async (args: GetApiImplementationArgs, options: GetApiImplementationOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") for (const item of args.sort ?? []) query.append("sort", String(item)) for (const item of args['fields[implementation]'] ?? []) query.append("fields[implementation]", String(item)) for (const item of args.include ?? []) query.append("include", String(item)) if (args['filter[implementation]'] != null) query.append("filter[implementation]", String(args['filter[implementation]'])) if (args.filter != null) query.append("filter", String(args.filter)) if (args['page[number]'] != null) query.append("page[number]", String(args['page[number]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['page[offset]'] != null) query.append("page[offset]", String(args['page[offset]'])) if (args['page[limit]'] != null) query.append("page[limit]", String(args['page[limit]'])) if (args['page[totals]'] != null) query.append("page[totals]", String(args['page[totals]'])) const url = `${baseUrl.replace(/\/+$/, '')}/implementation${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(getApiImplementationResponse, await res.json()) };