import {z} from 'zod' export type GetApiApiV10AppAppIdAddressFormArgs = {appId: string, language?: string | undefined}; export type GetApiApiV10AppAppIdAddressFormOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const dynamicFormRule = z.object({Value: z.object({}).optional(), Message: z.string().optional(), Scopes: z.string().optional()}); export const dynamicFormFieldOption = z.object({Display: z.string().optional(), Value: z.string().optional()}); export const dynamicFormRules = z.object({MaxLength: dynamicFormRule.optional(), MinLength: dynamicFormRule.optional(), Required: dynamicFormRule.optional(), Pattern: dynamicFormRule.optional(), Predefined: z.array(dynamicFormFieldOption).optional()}); export const dynamicFormField = z.object({Identifier: z.string().optional(), Label: z.string().optional(), Placeholder: z.string().optional(), Rules: dynamicFormRules.optional(), Mapping: z.record(z.string(), z.string()).optional(), Modifiers: z.array(z.string()).optional(), Value: z.object({}).optional(), Scopes: z.string().optional()}); export const addressFormDisplayFormat = z.object({OneLine: z.string().optional(), TwoLines: z.array(z.string()).optional()}); export const addressFormResponse = z.object({FormData: z.array(dynamicFormField).optional(), CountryCode: z.string().optional(), Language: z.string().optional(), DisplayFormat: addressFormDisplayFormat.optional()}); export const restApiResultAddressFormResponse = z.object({Data: addressFormResponse}); 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 }; /** * GET /api/v1.0/app/{appId}/address/form */ export const getApiApiV10AppAppIdAddressForm = async (args: GetApiApiV10AppAppIdAddressFormArgs, options?: GetApiApiV10AppAppIdAddressFormOptions) => { const {baseUrl = "https://api.flipdish.co", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.language != null) query.append("language", String(args.language)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1.0/app/${encodeURIComponent(String(args.appId))}/address/form${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(restApiResultAddressFormResponse, await res.json()) };