import {z} from 'zod' export type PeakAPIsLibraryModelEcoSystemResponseProductAPIUnitModel = {id?: string | undefined, code?: string | undefined, nameTh?: string | undefined, nameEn?: string | undefined}; export type CreateApiApiV1ServicesArgs = {'Time-Stamp': string, 'Time-Signature': string, 'User-Token': string, 'Client-Token': string, body: {peakServices: {services: Array<{id?: string | undefined, name: string, type: 0 | 1 | 2, code?: string | undefined, purchaseValue?: number | undefined, purchaseVatType?: (1 | 2 | 3) | undefined, purchaseAccount?: string | undefined, sellValue?: number | undefined, sellVatType?: (1 | 2 | 3) | undefined, sellAccount?: string | undefined, description?: string | undefined, carryingBalanceValue?: number | undefined, carryingBalanceAmount?: number | undefined, unit?: PeakAPIsLibraryModelEcoSystemResponseProductAPIUnitModel | undefined}>}}}; export type CreateApiApiV1ServicesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const peakWCFModelsAPIAPIProductsServicesAPIUnitModel = z.object({id: z.string().optional(), nameTh: z.string().optional(), nameEn: z.string().optional(), code: z.string().optional()}); export const peakWCFModelsAPIAPIProductsServicesAPIProductsServicesModel = z.object({resCode: z.string().optional(), resDesc: z.string().optional(), id: z.string().optional(), name: z.string().optional(), code: z.string().optional(), type: z.number().int().optional(), purchaseValue: z.number().optional(), purchaseVatType: z.number().int().optional(), purchaseAccount: z.string().optional(), sellValue: z.number().optional(), sellVatType: z.number().int().optional(), sellAccount: z.string().optional(), description: z.string().optional(), carryingBalanceValue: z.number().optional(), carryingBalanceAmount: z.number().optional(), remainingBalanceAmount: z.number().optional(), unit: peakWCFModelsAPIAPIProductsServicesAPIUnitModel.optional()}); export const peakWCFModelsAPIBodyAPIBodyService = z.object({services: z.array(peakWCFModelsAPIAPIProductsServicesAPIProductsServicesModel).optional(), totalService: z.number().int().optional(), resDesc: z.string().optional(), resCode: z.string().optional()}); export const peakWCFModelsAPIResponseResponseServiceModel = z.object({peakServices: peakWCFModelsAPIBodyAPIBodyService.optional(), eventType: z.string().optional(), apiType: 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 Service * * POST /api/v1/Services */ export const createApiApiV1Services = async (args: CreateApiApiV1ServicesArgs, options?: CreateApiApiV1ServicesOptions) => { const {baseUrl = "https://api.peakaccount.com", 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['Time-Stamp'] != null) headers.set("Time-Stamp", String(args['Time-Stamp'])) if (args['Time-Signature'] != null) headers.set("Time-Signature", String(args['Time-Signature'])) if (args['User-Token'] != null) headers.set("User-Token", String(args['User-Token'])) if (args['Client-Token'] != null) headers.set("Client-Token", String(args['Client-Token'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/Services${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(peakWCFModelsAPIResponseResponseServiceModel, await res.json()) };