import {z} from 'zod' export type GetApiV2IdentitiesIdentifierDisplayMethodArgs = {identifier: string, page?: number | undefined, max_results?: number | undefined, sort?: (Array<'created_at' | '-created_at' | 'name' | '-name' | 'type' | '-type'>) | undefined}; export type GetApiV2IdentitiesIdentifierDisplayMethodOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const displayMethodEntity = z.object({id: z.string(), name: z.string(), url: z.string(), type: z.string()}); export const paginatedMetadata = z.object({total: z.number().int(), page: z.number().int(), max_results: z.number().int()}); export const displayMethodPaginated = z.object({items: z.array(displayMethodEntity), meta: paginatedMetadata}); 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 }; /** * Get All Display Methods * * GET /v2/identities/{identifier}/display-method */ export const getApiV2IdentitiesIdentifierDisplayMethod = async (args: GetApiV2IdentitiesIdentifierDisplayMethodArgs, options: GetApiV2IdentitiesIdentifierDisplayMethodOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = 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.max_results != null) query.append("max_results", String(args.max_results)) if (args.sort != null) query.append("sort", args.sort.join(',')) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/identities/${encodeURIComponent(String(args.identifier))}/display-method${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(displayMethodPaginated, await res.json()) };