import {z} from 'zod' export type GetApiV2StorageBranchBranchIdComponentsArgs = {branchId: string, componentType?: (string | null) | undefined, include?: (string | null) | undefined, isDeleted?: (string | null) | undefined}; export type GetApiV2StorageBranchBranchIdComponentsOptions = {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 componentWithConfigurationsResponse = z.object({id: z.string(), type: z.string(), name: z.string(), description: z.string(), longDescription: z.string().nullable(), version: z.number().int(), complexity: z.enum(["low", "medium", "high"]).nullable(), categories: z.array(z.string()), hasUI: z.boolean(), hasRun: z.boolean(), ico32: z.string().nullable(), ico64: z.string().nullable(), ico128: z.string().nullable(), data: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown()), z.string()]), flags: z.array(z.string()), configurationSchema: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown())]), configurationRowSchema: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown())]), emptyConfiguration: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown())]), emptyConfigurationRow: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown())]), createConfigurationRowSchema: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown())]), uiOptions: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown())]), configurationDescription: z.string().nullable(), features: z.array(z.string()), expiredOn: z.string().nullable(), uri: z.string().nullable(), documentationUrl: z.string().optional(), dataTypesConfiguration: z.union([z.object({dataTypesSupport: z.string().optional()}).and(z.record(z.string(), z.unknown())).nullable(), z.array(z.unknown())]), processorConfiguration: z.union([z.object({allowedProcessorPosition: z.string().optional()}).and(z.record(z.string(), z.unknown())).nullable(), z.array(z.unknown())]), configurations: z.array(configurationPartialResponse)}); export const getApiV2StorageBranchBranchIdComponentsResponse = z.array(componentWithConfigurationsResponse); 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 }; /** * Get components * * GET /v2/storage/branch/{branchId}/components * * Also accepts auth: ManageKey, BearerAuth */ export const getApiV2StorageBranchBranchIdComponents = async (args: GetApiV2StorageBranchBranchIdComponentsArgs, options?: GetApiV2StorageBranchBranchIdComponentsOptions) => { const {baseUrl = "https://connection.keboola.com", fetch: fetchFn = fetch, StorageKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.componentType != null) query.append("componentType", String(args.componentType)) if (args.include != null) query.append("include", String(args.include)) if (args.isDeleted != null) query.append("isDeleted", String(args.isDeleted)) if (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/branch/${encodeURIComponent(String(args.branchId))}/components${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(getApiV2StorageBranchBranchIdComponentsResponse, await res.json()) };