import {z} from 'zod' export type GetApiSqlTablesArgs = {filter?: string | undefined, amount?: number | undefined, page?: number | undefined, entity_id?: string | undefined}; export type GetApiSqlTablesOptions = {baseUrl?: string | undefined, ProfileToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiSqlTablesResponse = z.object({status: z.boolean().optional(), result: z.object({tables: z.array(z.object({function: z.string().optional(), label: z.string().optional(), tag_form: z.string().optional(), columns: z.array(z.object({name: z.string().optional(), type: z.string().optional()})).optional(), dynamic: z.boolean().optional()})).optional(), functions: z.array(z.object({name: z.string().optional(), kind: z.enum(["aggregate", "session"]).optional(), args: z.array(z.string()).optional(), description: z.string().optional(), example: z.string().optional()})).optional(), resources: z.object({devices: z.array(z.object({id: z.string().optional(), name: z.string().optional()})).optional(), entities: z.array(z.object({id: z.string().optional(), name: z.string().optional()})).optional()}).optional()}).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 }; /** * List SQL Tables * * GET /sql/tables */ export const getApiSqlTables = async (args: GetApiSqlTablesArgs, options?: GetApiSqlTablesOptions) => { const {baseUrl = "https://api.us-e1.tago.io", fetch: fetchFn = fetch, ProfileToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter != null) query.append("filter", String(args.filter)) if (args.amount != null) query.append("amount", String(args.amount)) if (args.page != null) query.append("page", String(args.page)) if (args.entity_id != null) query.append("entity_id", String(args.entity_id)) if (ProfileToken !== undefined) headers.set("Profile-Token", ProfileToken) const url = `${baseUrl.replace(/\/+$/, '')}/sql/tables${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(getApiSqlTablesResponse, await res.json()) };