import {z} from 'zod' export type CategoryOptionsDetails = 0 | 1 | 2 | 4 | 8; export type KeywordMatchingType = 0 | 1; export type CategoryAugmentationType = 0 | 1 | 2 | 3; export type ParameterOperationType = 0 | 1 | 2 | 3 | 4; export type Argument = {value?: (string | null) | undefined, order?: number | undefined}; export type Operation = {type?: ParameterOperationType | undefined, arguments?: (Array | null) | undefined}; export type Parameter = {id?: (string | null) | undefined, name?: (string | null) | undefined, operation?: Operation | undefined, defaultValue?: (string | null) | undefined, eventSourceId?: (string | null) | undefined}; export type CategoryAugmentationDetails = {enabled?: boolean | undefined, name?: (string | null) | undefined, type: CategoryAugmentationType, value?: (string | null) | undefined, id?: (string | null) | undefined, parameters?: (Array | null) | undefined}; export type CategoryEnrichmentType = 0 | 1; export type CategoryEnrichmentDetails = {id?: (string | null) | undefined, name?: (string | null) | undefined, type: CategoryEnrichmentType, value?: (string | null) | undefined}; export type CategoryDetails = {id?: (string | null) | undefined, teamId?: (string | null) | undefined, isDefault?: boolean | undefined, options?: CategoryOptionsDetails | undefined, name: string, color: string, imageName: string, keywords: Array, keywordsExcluded?: (Array | null) | undefined, keywordMatching: KeywordMatchingType, augmentations?: (Array | null) | undefined, enrichments?: (Array | null) | undefined, order?: number | undefined}; export type CreateApiCategoriesTeamIdArgs = {teamId: string, body: CategoryDetails}; export type CreateApiCategoriesTeamIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const categoryOptionsDetails = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(4), z.literal(8)]); export const keywordMatchingType = z.union([z.literal(0), z.literal(1)]); export const categoryAugmentationType = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]); export const parameterOperationType = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const argument = z.object({value: z.string().nullable().optional(), order: z.number().int().optional()}); export const operation = z.object({type: parameterOperationType.optional(), arguments: z.array(argument).nullable().optional()}); export const parameter = z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional(), operation: operation.optional(), defaultValue: z.string().nullable().optional(), eventSourceId: z.string().nullable().optional()}); export const categoryAugmentationDetails = z.object({enabled: z.boolean().optional(), name: z.string().nullable().optional(), type: categoryAugmentationType, value: z.string().nullable().optional(), id: z.string().nullable().optional(), parameters: z.array(parameter).nullable().optional()}); export const categoryEnrichmentType = z.union([z.literal(0), z.literal(1)]); export const categoryEnrichmentDetails = z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional(), type: categoryEnrichmentType, value: z.string().nullable().optional()}); export const categoryDetails = z.object({id: z.string().nullable().optional(), teamId: z.string().nullable().optional(), isDefault: z.boolean().optional(), options: categoryOptionsDetails.optional(), name: z.string(), color: z.string(), imageName: z.string(), keywords: z.array(z.string()), keywordsExcluded: z.array(z.string()).nullable().optional(), keywordMatching: keywordMatchingType, augmentations: z.array(categoryAugmentationDetails).nullable().optional(), enrichments: z.array(categoryEnrichmentDetails).nullable().optional(), order: 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 }; /** * Create a new category * * POST /categories/{teamId} */ export const createApiCategoriesTeamId = async (args: CreateApiCategoriesTeamIdArgs, options?: CreateApiCategoriesTeamIdOptions) => { const {baseUrl = "https://connect.signl4.com/api", 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(/\/+$/, '')}/categories/${encodeURIComponent(String(args.teamId))}${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(categoryDetails, await res.json()) };