import {z} from 'zod' export type GetApiFilterTemplatesFilterHandleArgs = {'filter-handle': string}; export type GetApiFilterTemplatesFilterHandleOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const filterComparison = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(15), z.literal(4), z.literal(16), z.literal(5), z.literal(10), z.literal(11), z.literal(12), z.literal(13), z.literal(53), z.literal(57), z.literal(6), z.literal(7), z.literal(8), z.literal(9), z.literal(14), z.literal(21), z.literal(22), z.literal(23), z.literal(24), z.literal(25), z.literal(54), z.literal(58), z.literal(26), z.literal(27), z.literal(28), z.literal(29), z.literal(30), z.literal(31), z.literal(32), z.literal(33), z.literal(34), z.literal(35), z.literal(36), z.literal(37), z.literal(38), z.literal(39), z.literal(40), z.literal(41), z.literal(42), z.literal(43), z.literal(44), z.literal(17), z.literal(18), z.literal(19), z.literal(20), z.literal(45), z.literal(46), z.literal(47), z.literal(48), z.literal(55), z.literal(59), z.literal(49), z.literal(50), z.literal(51), z.literal(52), z.literal(56), z.literal(60)]); export const filterOption = z.literal(1); export const filterTemplateLiteral = z.object({ColumnName: z.string().optional(), ColumnType: z.string().optional(), Comparison: filterComparison.optional(), Options: z.array(filterOption).optional(), Values: z.array(z.unknown()).optional()}); export const filterTemplateOrClause = z.object({Literals: z.array(filterTemplateLiteral).optional()}); export const filterCNFTemplate = z.object({AndClauses: z.array(filterTemplateOrClause).optional()}); export const filterSavedFilter = z.object({CreatedAt: z.string().optional(), Id: z.string().optional(), Name: z.string().optional(), Owner: z.string().optional(), Template: filterCNFTemplate.optional(), UpdatedAt: z.string().optional(), Version: z.number().int().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 }; /** * Describe saved filter * * GET /filter-templates/{filter-handle} */ export const getApiFilterTemplatesFilterHandle = async (args: GetApiFilterTemplatesFilterHandleArgs, options: GetApiFilterTemplatesFilterHandleOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-GIGASHEET-TOKEN", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/filter-templates/${encodeURIComponent(String(args['filter-handle']))}${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(filterSavedFilter, await res.json()) };