import {z} from 'zod' export type AdminSettingsId = {id: string, entityType: 'ADMIN_SETTINGS'}; export type TenantId = {id: string, entityType: 'TENANT'}; export type JsonNode = unknown; export type AdminSettings = {id?: AdminSettingsId | undefined, createdTime?: number | undefined, tenantId?: TenantId | undefined, key?: string | undefined, jsonValue?: JsonNode | undefined}; export type CreateApiApiAdminSettingsArgs = {body: AdminSettings}; export type CreateApiApiAdminSettingsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const adminSettingsId = z.object({id: z.string(), entityType: z.literal("ADMIN_SETTINGS")}); export const tenantId = z.object({id: z.string(), entityType: z.literal("TENANT")}); export const jsonNode = z.unknown(); export const adminSettings = z.object({id: adminSettingsId.optional(), createdTime: z.number().int().optional(), tenantId: tenantId.optional(), key: z.string().optional(), jsonValue: jsonNode.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 or Updates the Administration Settings (saveAdminSettings) * * POST /api/admin/settings */ export const createApiApiAdminSettings = async (args: CreateApiApiAdminSettingsArgs, options?: CreateApiApiAdminSettingsOptions) => { const {baseUrl = "https://demo.thingsboard.io", 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(/\/+$/, '')}/api/admin/settings${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(adminSettings, await res.json()) };