import {z} from 'zod' export type Coordinates = Array; export type Point = {coordinates: Coordinates, type: 'Point'}; export type DeviceCreate = {oldDeviceID?: string | undefined, appVersion?: string | undefined, deviceID?: string | undefined, deviceString?: string | undefined, browserName?: string | undefined, browserVersion?: string | undefined, allowedUI?: boolean | undefined, backgroundAppRefresh?: boolean | undefined, bluetoothEnabled?: boolean | undefined, locationServicesAuthStatus?: ('none' | 'use' | 'always') | undefined, locationServicesAccuracyAuth?: ('full' | 'reduced') | undefined, language?: string | undefined, location?: Point | undefined, locationAccuracy?: number | undefined, altitude?: number | undefined, course?: number | undefined, speed?: number | undefined, osString?: string | undefined, osVersion?: string | undefined, platform?: ('iOS' | 'tvOS' | 'Android' | 'Web' | 'Email' | 'SMS') | undefined, sdkVersion?: string | undefined, timeZoneOffset?: number | undefined, transport?: ('APNS' | 'GCM' | 'HMS' | 'WebPush' | 'WebsitePush' | 'Email' | 'SMS' | 'Notificare') | undefined, userID?: string | undefined}; export type CreateApiDeviceArgs = {body: DeviceCreate}; export type CreateApiDeviceOptions = {baseUrl?: string | undefined, ApplicationSecret?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const responseMessage = z.object({message: z.string()}); 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 }; /** * Register a device * * POST /device */ export const createApiDevice = async (args: CreateApiDeviceArgs, options?: CreateApiDeviceOptions) => { 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(/\/+$/, '')}/device${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(responseMessage, await res.json()) };