import {z} from 'zod' export type GetApiApiLorawanDevicesArgs = {searchText?: string | undefined, searchStatus?: boolean | undefined, searchState?: boolean | undefined, pageSize?: number | undefined, pageNumber?: number | undefined, orderBy?: Array | undefined, modelId?: string | undefined}; export type GetApiApiLorawanDevicesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const labelDto = z.object({name: z.string().min(1), color: z.string().min(1)}); export const deviceListItem = z.object({deviceID: z.string().nullable().optional(), deviceName: z.string().nullable().optional(), deviceModelId: z.string().nullable().optional(), image: z.string().nullable().optional(), isEnabled: z.boolean().optional(), supportLoRaFeatures: z.boolean().optional(), hasLoRaTelemetry: z.boolean().optional(), lastActivityTime: z.string().optional(), labels: z.array(labelDto).nullable().optional(), layerId: z.string().nullable().optional()}); export const deviceListItemPaginationResult = z.object({items: z.array(deviceListItem).nullable().optional(), totalItems: z.number().int().optional(), nextPage: z.string().nullable().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 }; /** * Gets the device list. * * GET /api/lorawan/devices */ export const getApiApiLorawanDevices = async (args: GetApiApiLorawanDevicesArgs, options: GetApiApiLorawanDevicesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.searchText != null) query.append("searchText", String(args.searchText)) if (args.searchStatus != null) query.append("searchStatus", String(args.searchStatus)) if (args.searchState != null) query.append("searchState", String(args.searchState)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.pageNumber != null) query.append("pageNumber", String(args.pageNumber)) for (const item of args.orderBy ?? []) query.append("orderBy", String(item)) if (args.modelId != null) query.append("modelId", String(args.modelId)) const url = `${baseUrl.replace(/\/+$/, '')}/api/lorawan/devices${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(deviceListItemPaginationResult, await res.json()) };