import {z} from 'zod' export type AddDeviceDetail = {user_id?: number | undefined, appname?: string | undefined, appversion?: string | undefined, deviceuid?: string | undefined, devicetoken?: string | undefined, devicename?: string | undefined, devicemodel?: string | undefined, deviceversion?: string | undefined, pushbadge?: string | undefined, pushalert?: string | undefined, pushsound?: string | undefined, development?: string | undefined, status?: string | undefined, latitude: number, longitude: number, devicetype: number}; export type CreateApiDeviceDetailsArgs = {body: AddDeviceDetail}; export type CreateApiDeviceDetailsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const deviceDetail = z.object({id: z.number().int().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), user_id: z.number().int().optional(), appname: z.string().optional(), appversion: z.string().optional(), deviceuid: z.string().optional(), devicetoken: z.string().optional(), devicename: z.string().optional(), devicemodel: z.string().optional(), deviceversion: z.string().optional(), pushbadge: z.string().optional(), pushalert: z.string().optional(), pushsound: z.string().optional(), development: z.string().optional(), status: z.string().optional(), latitude: z.number().optional(), longitude: z.number().optional(), devicetype: z.number().int().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 }; /** * Creates a new device detail * * POST /device_details */ export const createApiDeviceDetails = async (args: CreateApiDeviceDetailsArgs, options: CreateApiDeviceDetailsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/device_details${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(deviceDetail, await res.json()) };