import {z} from 'zod' export type LogicalOperators = 'or' | 'and'; export type GetApiApiV1ArtifactsArgs = {hydrate?: boolean | undefined, sort_by?: string | undefined, logical_operator?: LogicalOperators | undefined, page?: number | undefined, size?: number | undefined, id?: (string | string | null) | undefined, created?: (string | string | null) | undefined, updated?: (string | string | null) | undefined, scope_workspace?: (string | null) | undefined, tag?: (string | null) | undefined, name?: (string | null) | undefined, has_custom_name?: (boolean | null) | undefined}; export type GetApiApiV1ArtifactsOptions = {baseUrl: string, CookieOAuth2TokenBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const colorVariants = z.enum(["grey", "purple", "red", "green", "yellow", "orange", "lime", "teal", "turquoise", "magenta", "blue"]); export const tagResponseBody = z.object({created: z.string(), updated: z.string(), color: colorVariants.optional(), tagged_count: z.number().int()}); export const baseResponseMetadata = z.object({}); export const tagResponseResources = z.record(z.string(), z.unknown()); export const tagResponse = z.object({body: tagResponseBody.nullable().optional(), metadata: baseResponseMetadata.nullable().optional(), resources: tagResponseResources.nullable().optional(), id: z.string(), permission_denied: z.boolean().optional(), name: z.string().max(255)}); export const artifactResponseBody = z.object({created: z.string(), updated: z.string(), tags: z.array(tagResponse), latest_version_name: z.string().nullable().optional(), latest_version_id: z.string().nullable().optional()}); export const artifactResponseMetadata = z.object({has_custom_name: z.boolean().optional()}); export const artifactResponseResources = z.record(z.string(), z.unknown()); export const artifactResponse = z.object({body: artifactResponseBody.nullable().optional(), metadata: artifactResponseMetadata.nullable().optional(), resources: artifactResponseResources.nullable().optional(), id: z.string(), permission_denied: z.boolean().optional(), name: z.string().max(255)}); export const pageArtifactResponse = z.object({index: z.number().int().gt(0), max_size: z.number().int().gt(0), total_pages: z.number().int().gte(0), total: z.number().int().gte(0), items: z.array(artifactResponse)}); 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 Artifacts * * GET /api/v1/artifacts */ export const getApiApiV1Artifacts = async (args: GetApiApiV1ArtifactsArgs, options: GetApiApiV1ArtifactsOptions) => { const {baseUrl, fetch: fetchFn = fetch, CookieOAuth2TokenBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.hydrate != null) query.append("hydrate", String(args.hydrate)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.logical_operator != null) query.append("logical_operator", String(args.logical_operator)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.id != null) query.append("id", String(args.id)) if (args.created != null) query.append("created", String(args.created)) if (args.updated != null) query.append("updated", String(args.updated)) if (args.scope_workspace != null) query.append("scope_workspace", String(args.scope_workspace)) if (args.tag != null) query.append("tag", String(args.tag)) if (args.name != null) query.append("name", String(args.name)) if (args.has_custom_name != null) query.append("has_custom_name", String(args.has_custom_name)) if (CookieOAuth2TokenBearer !== undefined) headers.set('authorization', `Bearer ${CookieOAuth2TokenBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/artifacts${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(pageArtifactResponse, await res.json()) };