import {z} from 'zod' export type GetApiV2TestValidateArgs = {id: number, tag?: (Array<'one' | 'two'>) | undefined, enabled?: boolean | undefined, 'user-agent'?: string | undefined}; export type GetApiV2TestValidateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2TestValidateResponse = z.object({header: z.object({"user-agent": z.string().optional()}).optional(), query: z.object({id: z.number().int(), enabled: z.boolean().optional(), tag: z.array(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 }; /** * Validate input * * GET /v2/test/validate */ export const getApiV2TestValidate = async (args: GetApiV2TestValidateArgs, options?: GetApiV2TestValidateOptions) => { const {baseUrl = "https://api.shutterstock.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.id != null) query.append("id", String(args.id)) for (const item of args.tag ?? []) query.append("tag", String(item)) if (args.enabled != null) query.append("enabled", String(args.enabled)) if (args['user-agent'] != null) headers.set("user-agent", String(args['user-agent'])) const url = `${baseUrl.replace(/\/+$/, '')}/v2/test/validate${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(getApiV2TestValidateResponse, await res.json()) };