import {z} from 'zod' export type Id = string; export type PlasticId = string; export type GetApiV3IdPlasticPlasticIdEmvScriptArgs = {auditUser: string, id: Id, plasticId: PlasticId, page?: number | undefined, size?: number | undefined}; export type GetApiV3IdPlasticPlasticIdEmvScriptOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const emvScript = z.object({id: z.string().optional(), code: z.enum(["BLOCK_CARD", "BLOCK_APPLICATION", "UNBLOCK_APPLICATION", "CHANGE_OFFLINE_PIN", "UNBLOCK_OFFLINE_PIN", "SET_OFFLINE_LIMIT", "GENERIC_OFFLINE_RISK_UPDATE", "MULTI_APPLICATION_CARD_UPDATE"]).optional(), scriptName: z.string().optional(), application: z.string().optional(), action: z.string().optional(), applicationName: z.string().optional(), created: z.string().optional(), updated: z.string().optional(), status: z.enum(["CLOSED", "REJECTED", "PENDING", "SUSPENDED", "ACCEPTED", "SENT"]).optional()}); export const emvScriptsStatusResponse = z.object({plasticId: z.string().optional(), cardId: z.string().optional(), emvScripts: z.array(emvScript).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 }; /** * Get a list of EMV scripts for a given plastic * * GET /v3/{id}/plastic/{plasticId}/emv_script */ export const getApiV3IdPlasticPlasticIdEmvScript = async (args: GetApiV3IdPlasticPlasticIdEmvScriptArgs, options: GetApiV3IdPlasticPlasticIdEmvScriptOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.auditUser != null) query.append("auditUser", String(args.auditUser)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) const url = `${baseUrl.replace(/\/+$/, '')}/v3/${encodeURIComponent(String(args.id))}/plastic/${encodeURIComponent(String(args.plasticId))}/emv_script${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(emvScriptsStatusResponse, await res.json()) };