import {z} from 'zod' export type AttributeResourceTypeDTO = 'dataset' | 'check' | 'column'; export type GetApiApiV1AttributesArgs = {page?: number | undefined, resourceType?: AttributeResourceTypeDTO | undefined, size?: number | undefined}; export type GetApiApiV1AttributesOptions = {baseUrl?: string | undefined, basicAuthApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const attributeResourceTypeDTO = z.enum(["dataset", "check", "column"]); export const attributeTypeDTO = z.enum(["text", "number", "checkbox", "datetime", "singleSelect", "multiSelect"]); export const attributeContentDTO = z.object({allowedValues: z.array(z.string()).optional(), description: z.string().optional(), id: z.string(), label: z.string(), name: z.string(), resourceType: attributeResourceTypeDTO, type: attributeTypeDTO}); export const publicApiListAttributesResponse = z.object({content: z.array(attributeContentDTO), first: z.boolean(), last: z.boolean(), number: z.number().int(), size: z.number().int(), totalElements: z.number().int(), totalPages: z.number().int()}); 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 attributes * * GET /api/v1/attributes * * Also accepts auth: cookieToken */ export const getApiApiV1Attributes = async (args: GetApiApiV1AttributesArgs, options?: GetApiApiV1AttributesOptions) => { const {baseUrl = "https://cloud.soda.io", fetch: fetchFn = fetch, basicAuthApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.resourceType != null) query.append("resourceType", String(args.resourceType)) if (args.size != null) query.append("size", String(args.size)) if (basicAuthApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuthApiKey.username}:${basicAuthApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/attributes${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(publicApiListAttributesResponse, await res.json()) };