import {z} from 'zod' export type FleetTemplate = 'D_CARD' | 'FLEET_CARD' | 'FLEET_CARD2' | 'FLEET_CARD3' | 'FLEET_CARD4'; export type AccountId = string; export type CustomerId = string; export type Embossing = {additionalField1?: string | undefined, additionalField2?: string | undefined, additionalField3?: string | undefined, additionalField4?: string | undefined, additionalField5?: string | undefined, companyName?: string | undefined, externalLayoutCode?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, additionalEmbossingName?: string | undefined, manufacturer?: string | undefined, physical?: boolean | undefined}; export type Reason = string; export type Segment = 'SEGMENT_A' | 'SEGMENT_B' | 'SEGMENT_C' | 'SEGMENT_D' | 'SEGMENT_E' | 'SEGMENT_F'; export type CardStatusPatch = 'CARD_OK' | 'CARD_BLOCKED' | 'SUSPECTED_FRAUD' | 'CARD_CLOSED_DUE_TO_FRAUD' | 'CARD_NO_RENEWAL' | 'CARD_LOST' | 'CARD_STOLEN' | 'CARD_CLOSED'; 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 PinStatus = 'D' | 'W' | 'S'; export type DigitalLayoutCode = string; export type ScheduledClosingType = 'TIMEBASED'; export type ScheduledClosingTime = string; export type ScheduledClosing = {type?: ScheduledClosingType | undefined, time?: ScheduledClosingTime | undefined}; export type KeyValuePair = {key: string, value: string}; export type AdditionalValues = Array; export type AllowedPurchaseCategories = 'UNRESTRICTED' | 'ALL_FUELS_INCL_VEHICLE_GOODS_AND_SERVICES' | 'ALL_FUELS' | 'RESTRICTED_FUELS' | 'NO_PRODUCTS'; export type Country = string; export type UpdateAddress = {address1?: string | undefined, address2?: string | undefined, address3?: string | undefined, address4?: string | undefined, city?: string | undefined, country?: Country | undefined, region?: string | undefined, zipCode?: string | undefined}; export type ExtraTerminalInformation = 'NONE' | 'VEHICLE_ID_AND_ODOMETER' | 'VEHICLE_ID' | 'ODOMETER'; export type CardUsageBlocks = {cardPresent?: boolean | undefined, atmCashDisbursement?: boolean | undefined}; export type PatchFleetResourceBody = {accountId?: AccountId | undefined, customerId?: CustomerId | undefined, embossing?: Embossing | undefined, reason?: Reason | undefined, segment?: Segment | undefined, status?: CardStatusPatch | undefined, usageLimits?: Array | undefined, pinStatus?: PinStatus | undefined, digitalLayoutCode?: DigitalLayoutCode | undefined, scheduledClosing?: ScheduledClosing | undefined, additionalValues?: AdditionalValues | undefined, allowedPurchaseCategories?: AllowedPurchaseCategories | undefined, branchId?: string | undefined, branchName?: string | undefined, cardAddress?: UpdateAddress | undefined, extraTerminalInformation?: ExtraTerminalInformation | undefined, pinAddress?: UpdateAddress | undefined, cardUsageBlocks?: CardUsageBlocks | undefined}; export type PatchApiV4FleetTemplateIdArgs = {fleetTemplate: FleetTemplate, auditUser: string, id: unknown, body: PatchFleetResourceBody}; export type PatchApiV4FleetTemplateIdOptions = {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 a fleet card * * PATCH /v4/{fleetTemplate}/{id} */ export const patchApiV4FleetTemplateId = async (args: PatchApiV4FleetTemplateIdArgs, options: PatchApiV4FleetTemplateIdOptions) => { 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(/\/+$/, '')}/v4/${encodeURIComponent(String(args.fleetTemplate))}/${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()) };