import {z} from 'zod' export type SaveMerchantSettingsRequest = {programId?: (string | null) | undefined, locationId?: (string | null) | undefined, merchantId?: (string | null) | undefined}; export type UpdateApiV2GiftcardsMerchantsSettingsArgs = {'X-Eposn-Merchant-Token'?: (string | null) | undefined, body: SaveMerchantSettingsRequest}; export type UpdateApiV2GiftcardsMerchantsSettingsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pydanticObjectId = z.string().min(24).max(24); export const merchantLocationResponse = z.object({locationId: z.string(), locationName: z.string().nullable(), lastSeenAt: z.string().nullable(), programId: z.string().nullable()}); export const merchantSessionResponse = z.object({id: z.string(), merchantId: pydanticObjectId.nullable(), name: z.string().nullable(), platform: z.string(), programId: z.string().nullable(), active: z.boolean(), locationId: z.string().nullable(), locationName: z.string().nullable(), locations: z.array(merchantLocationResponse), cashierId: z.string().nullable().optional(), terminalId: z.string().nullable().optional(), role: z.string().nullable().optional(), sessionToken: z.string().nullable().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 }; /** * Savemerchantsettings * * PUT /v2/giftcards/merchants/settings */ export const updateApiV2GiftcardsMerchantsSettings = async (args: UpdateApiV2GiftcardsMerchantsSettingsArgs, options?: UpdateApiV2GiftcardsMerchantsSettingsOptions) => { const {baseUrl = "https://api.loyalty.dog", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Eposn-Merchant-Token'] != null) headers.set("X-Eposn-Merchant-Token", String(args['X-Eposn-Merchant-Token'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/giftcards/merchants/settings${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(merchantSessionResponse, await res.json()) };