import {z} from 'zod' export type CreateConfigurationRequest = {name: string, configurationId?: (string | null) | undefined, description?: (string | null) | undefined, configuration?: ({[key: string]: unknown} | null) | undefined, state?: ({[key: string]: unknown} | null) | undefined, changeDescription?: (string | null) | undefined, isDisabled?: (boolean | null) | undefined, folder?: (string | null) | undefined}; export type CreateApiV2StorageComponentsComponentIdConfigsArgs = {componentId: string, body: CreateConfigurationRequest}; export type CreateApiV2StorageComponentsComponentIdConfigsOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const actorResponse = z.object({type: z.enum(["token", "session", "pat", "system"]), id: z.string(), name: z.string(), adminId: z.number().int().nullable()}); export const configurationCreatorTokenResponse = z.object({id: z.number().int(), description: z.string().nullable()}); export const configurationRowInConfigurationResponse = z.object({id: z.string(), name: z.string(), description: z.string(), isDisabled: z.boolean(), version: z.number().int(), created: z.string(), creatorToken: configurationCreatorTokenResponse, actor: actorResponse.nullable(), changeDescription: z.string().nullable(), versionIdentifier: z.string().nullable(), state: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown()), z.string()]).optional(), configuration: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown()), z.string()]).optional()}); export const configurationCurrentVersionResponse = z.object({created: z.string(), creatorToken: configurationCreatorTokenResponse, actor: actorResponse.nullable(), changeDescription: z.string().nullable(), versionIdentifier: z.string().nullable()}); export const configurationPartialResponse = z.object({id: z.string(), name: z.string(), description: z.string(), created: z.string(), creatorToken: z.object({id: z.number().int(), description: z.string()}), actor: actorResponse.nullable(), version: z.number().int(), changeDescription: z.string().nullable(), isDisabled: z.boolean(), isDeleted: z.boolean(), folder: z.string().max(400).nullable(), folderUpdated: z.string().nullable(), configuration: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown()), z.string()]).optional(), rowsSortOrder: z.array(z.string()).optional(), rows: z.array(configurationRowInConfigurationResponse).optional(), state: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown()), z.string()]).optional(), currentVersion: configurationCurrentVersionResponse}); 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 Configuration * * POST /v2/storage/components/{componentId}/configs * * Also accepts auth: ManageKey, BearerAuth */ export const createApiV2StorageComponentsComponentIdConfigs = async (args: CreateApiV2StorageComponentsComponentIdConfigsArgs, options?: CreateApiV2StorageComponentsComponentIdConfigsOptions) => { const {baseUrl = "https://connection.keboola.com", fetch: fetchFn = fetch, StorageKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/components/${encodeURIComponent(String(args.componentId))}/configs${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(configurationPartialResponse, await res.json()) };