import {z} from 'zod' export type GetApiV3ServicesSupportedDocumentsArgs = {continent?: ('AFRICA' | 'ASIA' | 'EUROPE' | 'NORTH AMERICA' | 'OCEANIA' | 'SOUTH AMERICA') | undefined, country_code?: string | undefined, locale?: ('en-GB' | 'fr-FR' | 'ar-EG') | undefined}; export type GetApiV3ServicesSupportedDocumentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const country = z.object({code: z.string().optional(), name: z.string().optional(), continent: z.string().optional()}); export const iDType = z.object({code: z.string().optional(), name: z.string().optional(), has_back: z.boolean().optional()}); export const supportedDocument = z.object({country: country.optional(), id_types: z.array(iDType).optional()}); export const supportedDocumentsResponse = z.object({valid_documents: z.array(supportedDocument)}); 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 supported documents for verification * * GET /v3/services/supported_documents */ export const getApiV3ServicesSupportedDocuments = async (args: GetApiV3ServicesSupportedDocumentsArgs, options?: GetApiV3ServicesSupportedDocumentsOptions) => { const {baseUrl = "https://testapi.smileidentity.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.continent != null) query.append("continent", String(args.continent)) if (args.country_code != null) query.append("country_code", String(args.country_code)) if (args.locale != null) query.append("locale", String(args.locale)) const url = `${baseUrl.replace(/\/+$/, '')}/v3/services/supported_documents${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(supportedDocumentsResponse, await res.json()) };