import {z} from 'zod' export type CreateAddress = {postalCode: string, countryName: string, countryCode?: string | undefined, administrativeAreaLevel1: string, locality: string, localityAreaLevel1: string, route: string, streetNumber: string, contactId?: string | undefined}; export type PatchApiApiStorageProfileSystemProfilesProfileIdAddressesAddressIdArgs = {'Content-Type': string, Accept: string, profileId: string, addressId: string, alternativeKey?: ('email' | 'document') | undefined, body: CreateAddress}; export type PatchApiApiStorageProfileSystemProfilesProfileIdAddressesAddressIdOptions = {baseUrl?: string | undefined, appKey?: string | undefined, appToken?: string | undefined, fetch?: typeof fetch | undefined}; export const addressId = z.string(); export const profileId = z.string(); export const address = z.object({postalCode: z.string(), countryName: z.string(), countryCode: z.string().optional(), administrativeAreaLevel1: z.string(), locality: z.string(), localityAreaLevel1: z.string(), route: z.string(), streetNumber: z.string(), profileId: profileId, contactId: z.string().optional()}); export const addressMeta = z.object({version: z.string(), author: z.string(), creationDate: z.string(), lastUpdateDate: z.string()}); export const unmaskedAddressResponse = z.object({id: addressId.optional(), document: address.optional(), meta: addressMeta.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 client address * * PATCH /api/storage/profile-system/profiles/{profileId}/addresses/{addressId} * * Also accepts auth: VtexIdclientAutCookie */ export const patchApiApiStorageProfileSystemProfilesProfileIdAddressesAddressId = async (args: PatchApiApiStorageProfileSystemProfilesProfileIdAddressesAddressIdArgs, options?: PatchApiApiStorageProfileSystemProfilesProfileIdAddressesAddressIdOptions) => { const {baseUrl = "https://apiexamples.vtexcommercestable.com.br", fetch: fetchFn = fetch, appKey, appToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (args.alternativeKey != null) query.append("alternativeKey", String(args.alternativeKey)) if (appKey !== undefined) headers.set("X-VTEX-API-AppKey", appKey) if (appToken !== undefined) headers.set("X-VTEX-API-AppToken", appToken) const url = `${baseUrl.replace(/\/+$/, '')}/api/storage/profile-system/profiles/${encodeURIComponent(String(args.profileId))}/addresses/${encodeURIComponent(String(args.addressId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(unmaskedAddressResponse, await res.json()) };