import {z} from 'zod' export type PlatformProvider = 'eposnow' | 'shopify' | 'clover' | 'wordpress' | 'square' | 'lightspeed'; export type InitMerchantSessionRequest = {appToken: string, merchantId: string, name?: (string | null) | undefined, platform?: PlatformProvider | undefined, locationId?: (string | null) | undefined, locationName?: (string | null) | undefined, programId?: (string | null) | undefined, userId?: (string | null) | undefined, role?: (string | null) | undefined, cashierId?: (string | null) | undefined, terminalId?: (string | null) | undefined}; export type CreateApiV2GiftcardsMerchantsSessionArgs = {body: InitMerchantSessionRequest}; export type CreateApiV2GiftcardsMerchantsSessionOptions = {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 }; /** * Initmerchantsession * * POST /v2/giftcards/merchants/session */ export const createApiV2GiftcardsMerchantsSession = async (args: CreateApiV2GiftcardsMerchantsSessionArgs, options?: CreateApiV2GiftcardsMerchantsSessionOptions) => { 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${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(merchantSessionResponse, await res.json()) };