import {z} from 'zod' export type GetApiV3IdPlasticArgs = {auditUser: string, id: unknown}; export type GetApiV3IdPlasticOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const embossing = z.object({additionalField1: z.string().max(32).optional(), additionalField2: z.string().max(32).optional(), additionalField3: z.string().max(32).optional(), additionalField4: z.string().max(32).optional(), additionalField5: z.string().max(32).optional(), companyName: z.string().max(26).optional(), externalLayoutCode: z.string().max(32).optional(), firstName: z.string().max(26).optional(), lastName: z.string().max(26).optional(), additionalEmbossingName: z.string().max(26).optional(), manufacturer: z.string().optional(), physical: z.boolean().optional()}); export const expiration = z.object({year: z.number().int().optional(), month: z.number().int().optional()}); export const plasticId = z.string(); export const listPlasticResponse = z.object({cardId: z.string().optional(), createdDate: z.string().optional(), embossing: embossing.optional(), expiration: expiration.optional(), id: plasticId.optional(), productionDate: z.string().optional(), productionType: z.enum(["ALL", "NEW_PLASTIC", "NEW_PIN", "NEW_CVV", "PIN", "ADD_PARMS", "CHIP_DATA"]).optional(), sequenceNumber: z.number().int().optional(), status: z.enum(["ACTIVE", "CLOSED", "DEFERRED", "FROM_FILE", "INACTIVE", "LOCKED", "LOCKED_FOR_ONLINE", "REJECTED"]).optional()}); export const getApiV3IdPlasticResponse = z.array(listPlasticResponse); 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 plastics for a given card * * GET /v3/{id}/plastic */ export const getApiV3IdPlastic = async (args: GetApiV3IdPlasticArgs, options: GetApiV3IdPlasticOptions) => { 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)) const url = `${baseUrl.replace(/\/+$/, '')}/v3/${encodeURIComponent(String(args.id))}/plastic${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(getApiV3IdPlasticResponse, await res.json()) };