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 CardId = string; export type DeviceId = string; export type Operation = 'READ_BALANCE' | 'UPDATE_BALANCE' | 'UPDATE_CONFIG' | 'UPDATE_PLAN' | 'ERASE_CARD' | 'PARK_CARD' | 'BLOCK_AND_READ_CARD'; export type Responses = Array<{id?: string | undefined, name?: string | undefined, value?: string | undefined}>; export type ResponseTimeMs = number; export type StatusCode = number; export type StatusMessage = string; export type GetCardCommandsReq = {requestHeader: RequestHeader, issuerId: IssuerId, cardId: CardId, purchaseId?: string | undefined, deviceId: DeviceId, operation?: Operation | undefined, responses?: Responses | undefined, responseTimeMs?: ResponseTimeMs | undefined, statusCode?: StatusCode | undefined, statusMessage?: StatusMessage | undefined}; export type CreateApiGetCardCommandsArgs = {'x-actor-id': string, body: GetCardCommandsReq}; export type CreateApiGetCardCommandsOptions = {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 responseHeader = z.object({responseId: responseId, sessionId: sessionId, generationDateTime: generationDateTime.optional(), statusCode: z.number(), statusMessage: z.string().optional()}); export const commands = z.array(z.object({id: z.string().min(1).max(64).optional(), name: z.string().min(1).max(100).optional(), value: z.string().min(1).max(512).optional()})); export const getCardCommandsRes = z.object({responseHeader: responseHeader, commands: commands.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 provide the next list of commands to be executed on a device * * POST /getCardCommands */ export const createApiGetCardCommands = async (args: CreateApiGetCardCommandsArgs, options?: CreateApiGetCardCommandsOptions) => { 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-actor-id'] != null) headers.set("x-actor-id", String(args['x-actor-id'])) const url = `${baseUrl.replace(/\/+$/, '')}/getCardCommands${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(getCardCommandsRes, await res.json()) };