import {z} from 'zod' export type GetApiV1LabelsArgs = {includeArchived?: boolean | undefined, pageSize?: number | undefined, pageToken?: string | undefined, tagIds?: Array | undefined}; export type GetApiV1LabelsOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const v1LabelTag = z.object({labelTagId: z.string().optional(), customerId: z.string().optional(), name: z.string().optional(), color: z.string().optional(), created: z.string().optional(), updated: z.string().optional(), expunged: z.boolean().optional()}); export const v1LabelImportProvenance = z.object({libraryLabelId: z.string().optional(), libraryVersionId: z.string().optional(), libraryName: z.string().optional(), importedAt: z.string().optional()}); export const sharedv1Label = z.object({labelId: z.string().optional(), name: z.string().optional(), customerId: z.string().optional(), policyId: z.string().optional(), disabled: z.boolean().optional(), created: z.string().optional(), updated: z.string().optional(), expunged: z.boolean().optional(), description: z.string().optional(), archivedAt: z.string().optional(), tags: z.array(v1LabelTag).optional(), importProvenance: v1LabelImportProvenance.optional()}); export const v1ListLabelsResponse = z.object({labels: z.array(sharedv1Label).optional(), nextPageToken: z.string().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 labels * * GET /v1/labels */ export const getApiV1Labels = async (args: GetApiV1LabelsArgs, options?: GetApiV1LabelsOptions) => { const {baseUrl = "https://gateway.app.clavata.ai:8443", fetch: fetchFn = fetch, BearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.includeArchived != null) query.append("includeArchived", String(args.includeArchived)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) for (const item of args.tagIds ?? []) query.append("tagIds", String(item)) if (BearerToken !== undefined) headers.set('authorization', `Bearer ${BearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/labels${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(v1ListLabelsResponse, await res.json()) };