import {z} from 'zod' export type ValidateMerchantSessionRequest = {sessionToken: string}; export type CreateApiV2GiftcardsMerchantsSessionValidateArgs = {body: ValidateMerchantSessionRequest}; export type CreateApiV2GiftcardsMerchantsSessionValidateOptions = {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 validateMerchantSessionResponse = z.object({valid: z.boolean(), merchant: merchantSessionResponse.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 }; /** * Validatemerchantsession * * POST /v2/giftcards/merchants/session/validate */ export const createApiV2GiftcardsMerchantsSessionValidate = async (args: CreateApiV2GiftcardsMerchantsSessionValidateArgs, options?: CreateApiV2GiftcardsMerchantsSessionValidateOptions) => { 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/giftcards/merchants/session/validate${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(validateMerchantSessionResponse, await res.json()) };