import {z} from 'zod' export type Implementation = {type?: string | undefined, id?: string | undefined, attributes?: ({arch?: string | undefined, asciiArmor?: string | undefined, downloadUrl?: string | undefined, filename?: string | undefined, keyId?: string | undefined, os?: string | undefined, shasum?: string | undefined, shasumsSignatureUrl?: string | undefined, shasumsUrl?: string | undefined, source?: string | undefined, sourceUrl?: string | undefined, trustSignature?: string | undefined}) | undefined, relationships?: ({version?: ({id?: string | undefined, type?: 'version' | undefined}) | undefined}) | undefined}; export type CreateApiImplementationArgs = {body: {data?: Implementation | undefined}}; export type CreateApiImplementationOptions = {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 createApiImplementationResponse = z.object({data: implementation.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 }; /** * Creates an item of type implementation * * POST /implementation */ export const createApiImplementation = async (args: CreateApiImplementationArgs, options: CreateApiImplementationOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") const url = `${baseUrl.replace(/\/+$/, '')}/implementation${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiImplementationResponse, await res.json()) };