import {z} from 'zod' export type GetApiApiV2OauthClientsArgs = {'page[before]'?: string | undefined, 'page[after]'?: string | undefined, 'page[size]'?: number | undefined}; export type GetApiApiV2OauthClientsOptions = {baseUrl?: string | undefined, apiTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const oAuthClientObject = z.object({company: z.string().optional(), created_at: z.string().optional(), description: z.string().optional(), global: z.boolean().optional(), id: z.number().int().optional(), identifier: z.string(), kind: z.string().optional(), logo_url: z.string().optional(), name: z.string(), redirect_uri: z.array(z.string()).optional(), secret: z.string().optional(), updated_at: z.string().optional(), url: z.string().optional(), user_id: z.number().int()}); export const oAuthClientsResponse = z.object({clients: z.array(oAuthClientObject).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 }; /** * List Clients * * GET /api/v2/oauth/clients */ export const getApiApiV2OauthClients = async (args: GetApiApiV2OauthClientsArgs, options?: GetApiApiV2OauthClientsOptions) => { const {baseUrl = "https://example.zendesk.com", fetch: fetchFn = fetch, apiTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['page[before]'] != null) query.append("page[before]", String(args['page[before]'])) if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (apiTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiTokenAuth.username}:${apiTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/oauth/clients${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(oAuthClientsResponse, await res.json()) };