import {z} from 'zod' export type CreateApiV2UsersIdTokenArgs = {page?: number | undefined, per_page?: number | undefined, order_by?: string | undefined, direction?: string | undefined, 'x-customer-api-key'?: string | undefined}; export type CreateApiV2UsersIdTokenOptions = {baseUrl: string, defaultValue?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2UsersIdTokenResponse = z.object({data: z.object({user_id: z.number().int().optional(), token: z.string().optional(), created_at: z.string().optional(), expires_at: z.string().optional(), url: z.string().optional(), requested_by: z.number().int().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 }; /** * Create temporary auth token * * POST /v2/users/{id}/token */ export const createApiV2UsersIdToken = async (args: CreateApiV2UsersIdTokenArgs, options: CreateApiV2UsersIdTokenOptions) => { const {baseUrl, fetch: fetchFn = fetch, defaultValue} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.direction != null) query.append("direction", String(args.direction)) if (args['x-customer-api-key'] != null) headers.set("x-customer-api-key", String(args['x-customer-api-key'])) if (defaultValue !== undefined) headers.set("x-customer-api-key", defaultValue) const url = `${baseUrl.replace(/\/+$/, '')}/v2/users/${encodeURIComponent(String(args.id))}/token${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV2UsersIdTokenResponse, await res.json()) };