import {z} from 'zod' export type OrganizationAddressSet = {address_line_1: string, address_line_2?: (string | null) | undefined, locality: string, admin_area: string, admin_area_2?: (string | null) | undefined, postal_code: string, country_code: string}; export type UpdateApiOrganizationAddressArgs = {body: OrganizationAddressSet}; export type UpdateApiOrganizationAddressOptions = {baseUrl: string, apiTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const organizationAddressDetails = z.object({address_line_1: z.string().min(1).max(512), address_line_2: z.string().max(512).nullable().optional(), locality: z.string().min(1).max(256), admin_area: z.string().min(1).max(256), admin_area_2: z.string().max(256).nullable().optional(), postal_code: z.string().min(1).max(32), country_code: z.string().min(2).max(2), id: z.string(), organization_id: z.string()}); 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 }; /** * Set organization address * * PUT /organization/address * * Also accepts auth: APIKeyHeader */ export const updateApiOrganizationAddress = async (args: UpdateApiOrganizationAddressArgs, options: UpdateApiOrganizationAddressOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiTokenAuth !== undefined) headers.set('authorization', `Bearer ${apiTokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/organization/address${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(organizationAddressDetails, await res.json()) };