import {z} from 'zod' export type GetApiV0UsersUserIdApiKeysArgs = {sort?: ('-created-at' | 'created-at') | undefined, 'page[size]'?: number | undefined, 'page[after]'?: string | undefined, 'page[before]'?: string | undefined, 'user-id': string}; export type GetApiV0UsersUserIdApiKeysOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV0UsersUserIdApiKeysResponse = z.object({"api-keys": z.array(z.object({"api-key-url": z.union([z.string(), z.string()]), "api-key-name": z.string().min(1).max(64), "api-key-live?": z.boolean(), "organization-url": z.string(), "user-url": z.string(), "created-at": z.string()})), links: z.object({prev: z.string().nullable(), next: z.string().nullable()})}); 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 API keys * * GET /v0/users/{user-id}/api-keys */ export const getApiV0UsersUserIdApiKeys = async (args: GetApiV0UsersUserIdApiKeysArgs, options?: GetApiV0UsersUserIdApiKeysOptions) => { const {baseUrl = "https://api.griffin.com", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sort != null) query.append("sort", String(args.sort)) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (args['page[before]'] != null) query.append("page[before]", String(args['page[before]'])) if (apiKeyAuth !== undefined) headers.set("Authorization", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v0/users/${encodeURIComponent(String(args['user-id']))}/api-keys${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(getApiV0UsersUserIdApiKeysResponse, await res.json()) };