import {z} from 'zod' export type GetApiCoaApiTagArgs = {exclude_tags?: string | undefined, group?: number | undefined, include_deleted?: boolean | undefined, include_inactive?: boolean | undefined, last_modified_at__gte?: string | undefined, last_modified_at__lte?: string | undefined, limit?: number | undefined, no_group?: boolean | undefined, offset?: number | undefined, parent?: number | undefined, q?: string | undefined, sort?: string | undefined}; export type GetApiCoaApiTagOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionTag = z.object({id: z.number().int(), group_name: z.string(), parent_name: z.string().nullable(), parent: z.number().int().nullable().optional(), is_deleted: z.boolean(), deleted_at: z.string().nullable(), is_active: z.boolean().optional(), name: z.string().max(250).nullable(), created_at: z.string(), last_modified_at: z.string(), customer: z.number().int(), group: z.number().int().nullable().optional()}); export const paginatedTransactionTagList = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(transactionTag)}); 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 Custom Dimensions * * GET /coa/api/tag */ export const getApiCoaApiTag = async (args: GetApiCoaApiTagArgs, options?: GetApiCoaApiTagOptions) => { const {baseUrl = "https://api.meetcampfire.com", fetch: fetchFn = fetch, knoxApiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.exclude_tags != null) query.append("exclude_tags", String(args.exclude_tags)) if (args.group != null) query.append("group", String(args.group)) if (args.include_deleted != null) query.append("include_deleted", String(args.include_deleted)) if (args.include_inactive != null) query.append("include_inactive", String(args.include_inactive)) if (args.last_modified_at__gte != null) query.append("last_modified_at__gte", String(args.last_modified_at__gte)) if (args.last_modified_at__lte != null) query.append("last_modified_at__lte", String(args.last_modified_at__lte)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.no_group != null) query.append("no_group", String(args.no_group)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.parent != null) query.append("parent", String(args.parent)) if (args.q != null) query.append("q", String(args.q)) if (args.sort != null) query.append("sort", String(args.sort)) if (knoxApiToken !== undefined) headers.set("Authorization", knoxApiToken) const url = `${baseUrl.replace(/\/+$/, '')}/coa/api/tag${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(paginatedTransactionTagList, await res.json()) };