import {z} from 'zod' export type ObjectId = string; export type PassField = {key?: string | undefined, label?: string | undefined, value?: string | undefined, string?: boolean | undefined, number?: boolean | undefined, date?: boolean | undefined, dataDetectorTypes?: ('PKDataDetectorTypePhoneNumber' | 'PKDataDetectorTypeLink' | 'PKDataDetectorTypeAddress' | 'PKDataDetectorTypeCalendarEvent') | undefined, textAlignment?: ('PKTextAlignmentLeft' | 'PKTextAlignmentCenter' | 'PKTextAlignmentRight' | 'PKTextAlignmentNatural') | undefined, dateStyle?: ('PKDateStyleNone' | 'PKDateStyleShort' | 'PKDateStyleMedium' | 'PKDateStyleLong' | 'PKDateStyleFull') | undefined, timeStyle?: ('PKDateStyleNone' | 'PKDateStyleShort' | 'PKDateStyleMedium' | 'PKDateStyleLong' | 'PKDateStyleFull') | undefined, ignoresTimeZone?: boolean | undefined, isRelative?: boolean | undefined, currencyCode?: string | undefined, numberStyle?: ('PKNumberStyleDecimal' | 'PKNumberStylePercent' | 'PKNumberStyleScientific' | 'PKNumberStyleSpellOut') | undefined}; export type PassData = {auxiliaryFields?: Array | undefined, backFields?: Array | undefined, headerFields?: Array | undefined, primaryFields?: Array | undefined, secondaryFields?: Array | undefined, background?: string | undefined, backgroundColor?: string | undefined, barcode?: ({format?: string | undefined, messageEncoding?: string | undefined}) | undefined, description?: string | undefined, footer?: string | undefined, foregroundColor?: string | undefined, icon?: string | undefined, labelColor?: string | undefined, logo?: string | undefined, logoText?: string | undefined, strip?: string | undefined, suppressStripShine?: boolean | undefined, thumbnail?: string | undefined, transitType?: ('PKTransitTypeAir' | 'PKTransitTypeBoat' | 'PKTransitTypeTrain' | 'PKTransitTypeBus' | 'PKTransitTypeGeneric') | undefined}; export type PassCreate = {passbook: ObjectId, data: PassData, barcode: string}; export type CreateApiPassArgs = {body: PassCreate}; export type CreateApiPassOptions = {baseUrl?: string | undefined, ApplicationSecret?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const objectId = z.string(); export const responseCreated = z.object({_id: objectId.optional()}); export const createApiPassResponse = z.object({pass: responseCreated.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 }; /** * Create a new pass * * POST /pass */ export const createApiPass = async (args: CreateApiPassArgs, options?: CreateApiPassOptions) => { const {baseUrl = "https://cloud.notifica.re/api", fetch: fetchFn = fetch, ApplicationSecret} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json; charset=utf-8") if (ApplicationSecret !== undefined) headers.set('authorization', `Basic ${btoa(`${ApplicationSecret.username}:${ApplicationSecret.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/pass${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(createApiPassResponse, await res.json()) };