import {z} from 'zod' export type ProgramSchemaType = 'stampCard' | 'tieredMembership' | 'specialOffers' | 'custom'; export type ProgramType = 'loyalty' | 'giftcard'; export type PassScannerType = 'Browser' | 'PassVerifier' | 'CodeReadr' | 'None'; export type TemplatePlaceholderType = 'text' | 'email' | 'number' | 'date' | 'selection' | 'json'; export type TemplatePlaceholder = {name: string, type?: TemplatePlaceholderType | undefined, displayName?: (string | null) | undefined, defaultValue?: (string | null) | undefined, points?: (number | null) | undefined, options?: (string | null) | undefined, required?: (boolean | null) | undefined}; export type CreateProgramPayload = {passTypeIdentifier: string, schema?: ProgramSchemaType | undefined, programType?: ProgramType | undefined, name: string, description?: (string | null) | undefined, language?: (string | null) | undefined, country?: (string | null) | undefined, company?: (string | null) | undefined, address?: (string | null) | undefined, website?: (string | null) | undefined, email?: (string | string | null) | undefined, phone?: (string | null) | undefined, terms?: (string | null) | undefined, pointsAddEmail?: (number | null) | undefined, pointsAddPhone?: (number | null) | undefined, pointsInstallPass?: (number | null) | undefined, pointsScanPass?: (number | null) | undefined, dynamicScanPoints?: (boolean | null) | undefined, pointsReferral?: (number | null) | undefined, referralRequiredPoints?: (number | null) | undefined, scannerType?: (PassScannerType | null) | undefined, customPointsChangeMessage?: (string | null) | undefined, disableCustomPointsChangeMessage?: (boolean | null) | undefined, customPointName?: (string | null) | undefined, customPointsName?: (string | null) | undefined, customFields?: Array | undefined, requiredFields?: Array | undefined, retention_period_days?: number | undefined}; export type CreateApiV2ProgramsArgs = {authorization?: (string | null) | undefined, body: CreateProgramPayload}; export type CreateApiV2ProgramsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2ProgramsResponse = z.unknown(); 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 }; /** * Create Loyalty Program * * POST /v2/programs */ export const createApiV2Programs = async (args: CreateApiV2ProgramsArgs, options?: CreateApiV2ProgramsOptions) => { 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.authorization != null) headers.set("authorization", String(args.authorization)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/programs${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(createApiV2ProgramsResponse, await res.json()) };