import {z} from 'zod' export type Contact = {id?: number | undefined, contactGroup: number, contactType: number, contactCode?: number | undefined, contactName: string, contactAddress?: string | undefined, contactZipCode?: string | undefined, contactTaxId?: unknown | undefined, contactBranchCode?: string | undefined, contactBranch?: string | undefined, contactPerson?: string | undefined, contactEmail?: string | undefined, contactMobile?: string | undefined, contactBankId?: number | undefined, contactBankAccountNumber?: number | undefined, contactBankAccountName?: string | undefined, contactBankBranch?: string | undefined, contactBankBranchCode?: string | undefined, contactBankAccountType?: number | undefined, contactCreditDays?: number | undefined, contactOffice?: string | undefined, contactFax?: string | undefined, contactWebsite?: string | undefined, contactShippingAddress?: string | undefined, contactNote?: string | undefined}; export type UpdateApiContactsIdArgs = {Authorization: string, id: string, body: Contact}; export type UpdateApiContactsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const contact = z.object({id: z.number().int().optional(), contactGroup: z.number().int(), contactType: z.number().int(), contactCode: z.number().int().optional(), contactName: z.string(), contactAddress: z.string().optional(), contactZipCode: z.string().optional(), contactTaxId: z.string().optional(), contactBranchCode: z.string().optional(), contactBranch: z.string().optional(), contactPerson: z.string().optional(), contactEmail: z.string().optional(), contactMobile: z.string().optional(), contactBankId: z.number().int().optional(), contactBankAccountNumber: z.number().int().optional(), contactBankAccountName: z.string().optional(), contactBankBranch: z.string().optional(), contactBankBranchCode: z.string().optional(), contactBankAccountType: z.number().int().optional(), contactCreditDays: z.number().int().optional(), contactOffice: z.string().optional(), contactFax: z.string().optional(), contactWebsite: z.string().optional(), contactShippingAddress: z.string().optional(), contactNote: z.string().optional()}); export const contactResponse = z.object({status: z.boolean().optional(), message: z.string().optional(), code: z.number().int().optional(), data: z.object({total: z.number().int().optional(), currentPage: z.number().int().optional(), list: z.array(contact).optional(), isDB: z.boolean().optional()}).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 contacts. * * PUT /contacts/{id} */ export const updateApiContactsId = async (args: UpdateApiContactsIdArgs, options?: UpdateApiContactsIdOptions) => { const {baseUrl = "https://openapi.flowaccount.com/v1", 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.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/contacts/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(contactResponse, await res.json()) };