import {z} from 'zod' export type GetApiSkynetAssetListArgs = {key: unknown, tags?: string | undefined, pn?: number | undefined, ps?: number | undefined, sort?: unknown | undefined, include_all_of_attributes?: unknown | undefined, include_any_of_attributes?: unknown | undefined}; export type GetApiSkynetAssetListOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const commonStatus = z.string(); export const metaData = z.object({}); export const dtoAssetDetails = z.object({id: z.string().optional(), device_id: z.string().optional(), state: z.string().optional(), name: z.string().optional(), description: z.string().optional(), meta_data: metaData.optional(), created_at: z.number().int().optional(), updated_at: z.number().int().optional(), tracked_at: z.number().int().optional(), attributes: z.object({}).optional(), latest_location: z.object({location: z.object({lat: z.number().optional(), lon: z.number().optional()}).optional(), timestamp: z.number().int().optional(), accuracy: z.number().optional(), speed: z.number().optional(), bearing: z.number().optional(), altitude: z.number().optional()}).optional(), tags: z.array(z.string()).optional()}); export const dtoPagination = z.object({total: z.number().int().optional(), page: z.number().int().optional(), size: z.number().int().optional(), hasmore: z.boolean().optional()}); export const commonMessage = z.string(); export const dtoGetAssetListResp = z.object({status: commonStatus.optional(), data: z.object({list: z.array(dtoAssetDetails).optional(), page: dtoPagination.optional()}).optional(), message: commonMessage.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 }; /** * Get Asset List * * GET /skynet/asset/list */ export const getApiSkynetAssetList = async (args: GetApiSkynetAssetListArgs, options?: GetApiSkynetAssetListOptions) => { const {baseUrl = "https://api.nextbillion.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.key != null) query.append("key", String(args.key)) if (args.tags != null) query.append("tags", String(args.tags)) if (args.pn != null) query.append("pn", String(args.pn)) if (args.ps != null) query.append("ps", String(args.ps)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.include_all_of_attributes != null) query.append("include_all_of_attributes", String(args.include_all_of_attributes)) if (args.include_any_of_attributes != null) query.append("include_any_of_attributes", String(args.include_any_of_attributes)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/skynet/asset/list${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(dtoGetAssetListResp, await res.json()) };