import {z} from 'zod' export type RequestId = string; export type SessionId = string; export type GenerationDateTime = string; export type RequestHeader = {requestId: RequestId, sessionId: SessionId, generationDateTime?: GenerationDateTime | undefined}; export type IssuerId = string; export type CardAccountDescriptor = Record; export type SeId = string; export type DeviceType = number; export type Language = string; export type PhoneNumberLastDigits = string; export type DeviceName = string; export type Device = {seId: SeId, deviceType: DeviceType, language: Language, phoneNumberLastDigits?: PhoneNumberLastDigits | undefined, deviceName?: DeviceName | undefined}; export type CardSource = 'ON-FILE' | 'CLIENT-SDK' | 'USER-INPUT' | 'READER-MODE'; export type CheckCardAccountReq = {requestHeader: RequestHeader, issuerId: IssuerId, cardAccountDescriptor: CardAccountDescriptor, device: Device, cardSource?: CardSource | undefined}; export type CreateApiCheckCardAccountArgs = {'x-pod': string, body: CheckCardAccountReq}; export type CreateApiCheckCardAccountOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const responseId = z.string().min(1).max(48); export const sessionId = z.string().min(1).max(48); export const generationDateTime = z.string(); export const cardArtManifest = z.array(z.string().max(64)); export const checkCardAccountRes = z.object({responseHeader: z.object({responseId: responseId, sessionId: sessionId, generationDateTime: generationDateTime.optional(), statusCode: z.number(), statusMessage: z.string().optional()}), termsAndConditionsId: z.string().max(64).optional(), cardArtManifest: cardArtManifest.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 }; /** * Request to check digital card account eligibility * * POST /checkCardAccount */ export const createApiCheckCardAccount = async (args: CreateApiCheckCardAccountArgs, options?: CreateApiCheckCardAccountOptions) => { const {baseUrl = "https://api.d1.thalescloud.io/authz/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-pod'] != null) headers.set("x-pod", String(args['x-pod'])) const url = `${baseUrl.replace(/\/+$/, '')}/checkCardAccount${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(checkCardAccountRes, await res.json()) };