import {z} from 'zod' export type CompanyInfo = {companyType?: string | undefined, compnayName: string, companyNameEn?: string | undefined, companyAddress?: string | undefined, companyAddressEn?: string | undefined, companyZipCode?: string | undefined, companyTaxId?: string | undefined, companyBranch?: string | undefined, companyBranchEn?: string | undefined, companyBranchCode?: string | undefined, companyPhone?: string | undefined, companyMobile?: string | undefined, companyFax?: string | undefined, companyWebsite?: string | undefined}; export type UpdateApiCompanyInfoArgs = {Authorization: string, body: CompanyInfo}; export type UpdateApiCompanyInfoOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const companyInfoResponse = z.object({status: z.boolean().optional(), message: z.string().optional(), code: z.number().int().optional(), data: z.object({companyId: z.number().int().optional(), companyType: z.string().optional(), compnayName: z.string().optional(), companyNameEn: z.string().optional(), companyAddress: z.string().optional(), companyAddressEn: z.string().optional(), companyZipCode: z.string().optional(), companyTaxId: z.string().optional(), companyBranch: z.string().optional(), companyBranchEn: z.string().optional(), companyBranchCode: z.string().optional(), companyPhone: z.string().optional(), companyMobile: z.string().optional(), companyFax: z.string().optional(), companyWebsite: z.string().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 }; /** * Edit business infomation * * PUT /company/info */ export const updateApiCompanyInfo = async (args: UpdateApiCompanyInfoArgs, options?: UpdateApiCompanyInfoOptions) => { 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(/\/+$/, '')}/company/info${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(companyInfoResponse, await res.json()) };