import {z} from 'zod' export type Expiration = {year?: number | undefined, month?: number | undefined}; export type Segment = 'SEGMENT_A' | 'SEGMENT_B' | 'SEGMENT_C' | 'SEGMENT_D' | 'SEGMENT_E' | 'SEGMENT_F'; 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 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 ProductCode = string; export type Country = string; export type Address = {address1: string, address2?: string | undefined, address3?: string | undefined, address4?: string | undefined, city: string, country: Country, region?: string | undefined, zipCode: string}; export type AllowedPurchaseCategories = 'UNRESTRICTED' | 'ALL_FUELS_INCL_VEHICLE_GOODS_AND_SERVICES' | 'ALL_FUELS' | 'RESTRICTED_FUELS' | 'NO_PRODUCTS'; export type ExtraTerminalInformation = 'NONE' | 'VEHICLE_ID_AND_ODOMETER' | 'VEHICLE_ID' | 'ODOMETER'; export type CreateFleetResourceBody = {expiration?: Expiration | undefined, segment?: Segment | undefined, pinStatus?: PinStatus | undefined, digitalLayoutCode?: DigitalLayoutCode | undefined, scheduledClosing?: ScheduledClosing | undefined, additionalValues?: AdditionalValues | undefined, encryptedData?: string | undefined, embossing: Embossing, productCode?: ProductCode | undefined, pinAddress?: Address | undefined, cardAddress?: Address | undefined, allowedPurchaseCategories?: AllowedPurchaseCategories | undefined, branchId?: string | undefined, branchName?: string | undefined, extraTerminalInformation?: ExtraTerminalInformation | undefined}; export type CreateApiV4FLEETCustomerCustomerIdArgs = {auditUser: string, customerId: unknown, body: CreateFleetResourceBody}; export type CreateApiV4FLEETCustomerCustomerIdOptions = {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 }; /** * Create fleet account * * POST /v4/FLEET/customer/{customerId} */ export const createApiV4FLEETCustomerCustomerId = async (args: CreateApiV4FLEETCustomerCustomerIdArgs, options: CreateApiV4FLEETCustomerCustomerIdOptions) => { 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/FLEET/customer/${encodeURIComponent(String(args.customerId))}${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(resourceResponse, await res.json()) };