import {z} from 'zod' export type KeyValuePair = {key: string, value: string}; export type AdditionalValues = Array; export type DigitalLayoutCode = string; export type RegionAndEcommBlocking = {ecomm?: boolean | undefined, africa?: boolean | undefined, asia?: boolean | undefined, europe?: boolean | undefined, home?: boolean | undefined, northAmerica?: boolean | undefined, oceania?: boolean | undefined, southAmerica?: boolean | undefined}; export type CardStatus = 'CARD_OK' | 'CARD_BLOCKED' | 'SUSPECTED_FRAUD' | 'CARD_CLOSED_DUE_TO_FRAUD' | 'CARD_NO_RENEWAL' | 'CARD_LOST' | 'CARD_STOLEN' | 'CARD_CLOSED' | 'CARD_EXPIRED'; export type LimitValues = {code?: ('ATM' | 'RETAIL' | 'ALL') | undefined, reset?: boolean | undefined, singleAmount?: number | undefined, count?: number | undefined, sumAmount?: number | undefined}; export type UsageLimit = {code?: ('24H' | 'DAILY' | 'MONTHLY' | 'WEEKLY' | 'ANNUAL_YEARLY' | 'ROLLING_YEARLY') | undefined, values?: Array | undefined}; export type CardUsageBlocks = {cardPresent?: boolean | undefined, atmCashDisbursement?: boolean | undefined}; export type PatchApplicationResourceBody = {applicationName?: string | undefined, enabled?: boolean | undefined, additionalValues?: AdditionalValues | undefined, printed?: boolean | undefined, contactPriorityLevel?: number | undefined, contactlessPriorityLevel?: number | undefined, digitalLayoutCode?: DigitalLayoutCode | undefined, regionAndEcommBlocking?: RegionAndEcommBlocking | undefined, status?: CardStatus | undefined, usageLimits?: Array | undefined, cardUsageBlocks?: CardUsageBlocks | undefined}; export type PatchApiV1ApplicationIdArgs = {auditUser: string, id: unknown, body: PatchApplicationResourceBody}; export type PatchApiV1ApplicationIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const resourceResponse = z.object({description: z.string().optional(), id: z.string().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 }; /** * Update an application of a multi-application card * * PATCH /v1/application/{id} */ export const patchApiV1ApplicationId = async (args: PatchApiV1ApplicationIdArgs, options: PatchApiV1ApplicationIdOptions) => { const {baseUrl, 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.auditUser != null) query.append("auditUser", String(args.auditUser)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/application/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(resourceResponse, await res.json()) };