import {z} from 'zod' export type GetApiSqlArgs = {fields?: string | undefined, 'filter[name]'?: string | undefined, 'filter[active]'?: boolean | undefined, 'filter[tags][0][key]'?: string | undefined, 'filter[tags][0][value]'?: string | undefined, orderBy?: string | undefined, amount?: number | undefined, page?: number | undefined}; export type GetApiSqlOptions = {baseUrl?: string | undefined, ProfileToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiSqlResponse = z.object({status: z.boolean().optional(), result: z.array(z.object({id: z.string().optional(), name: z.string().optional(), tags: z.array(z.object({key: z.string().optional(), value: z.string().optional()})).optional(), active: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().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 Queries * * GET /sql */ export const getApiSql = async (args: GetApiSqlArgs, options?: GetApiSqlOptions) => { 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.fields != null) query.append("fields", String(args.fields)) if (args['filter[name]'] != null) query.append("filter[name]", String(args['filter[name]'])) if (args['filter[active]'] != null) query.append("filter[active]", String(args['filter[active]'])) if (args['filter[tags][0][key]'] != null) query.append("filter[tags][0][key]", String(args['filter[tags][0][key]'])) if (args['filter[tags][0][value]'] != null) query.append("filter[tags][0][value]", String(args['filter[tags][0][value]'])) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.amount != null) query.append("amount", String(args.amount)) if (args.page != null) query.append("page", String(args.page)) if (ProfileToken !== undefined) headers.set("Profile-Token", ProfileToken) const url = `${baseUrl.replace(/\/+$/, '')}/sql${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(getApiSqlResponse, await res.json()) };