import {z} from 'zod' export type GetApiV2StorageGlobalSearchArgs = {query: string, limit?: number | undefined, offset?: number | undefined, 'types[]'?: (Array<'flow' | 'bucket' | 'table' | 'transformation' | 'configuration' | 'configuration-row' | 'workspace' | 'shared-code'> | null) | undefined, 'projectIds[]'?: (Array | null) | undefined, 'branchTypes[]'?: (Array<'production' | 'development'> | null) | undefined, 'branchIds[]'?: (Array | null) | undefined, mode?: ('standard' | 'regex') | undefined}; export type GetApiV2StorageGlobalSearchOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const globalSearchItemBranch = z.object({id: z.number().int(), name: z.string(), isDefault: z.boolean(), isNative: z.boolean()}); export const globalSearchIdName = z.object({id: z.string(), name: z.string().nullable()}); export const globalSearchItemFullPath = z.object({project: z.object({id: z.number().int(), name: z.string()}), branch: globalSearchItemBranch, bucket: z.union([globalSearchIdName, z.array(z.unknown())]).optional(), component: z.union([globalSearchIdName, z.array(z.unknown())]).optional(), configuration: globalSearchIdName.optional()}); export const globalSearchItem = z.object({id: z.string(), type: z.enum(["bucket", "table", "configuration", "configuration-row"]), name: z.string(), fullPath: globalSearchItemFullPath, uri: z.string().optional(), componentId: z.string().optional(), matchedColumns: z.array(z.string()).optional(), organizationId: z.number().int(), projectId: z.number().int(), projectName: z.string().optional(), created: z.string()}); export const globalSearchResponse = z.object({all: z.number().int(), items: z.array(globalSearchItem), byType: z.object({total: z.number().int()}).and(z.record(z.string(), z.number().int())), byProject: z.union([z.record(z.string(), z.string()), z.array(z.unknown())])}); 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 }; /** * Search storage objects * * GET /v2/storage/global-search * * Also accepts auth: ManageKey, BearerAuth */ export const getApiV2StorageGlobalSearch = async (args: GetApiV2StorageGlobalSearchArgs, options?: GetApiV2StorageGlobalSearchOptions) => { 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.query != null) query.append("query", String(args.query)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) for (const item of args['types[]'] ?? []) query.append("types[]", String(item)) for (const item of args['projectIds[]'] ?? []) query.append("projectIds[]", String(item)) for (const item of args['branchTypes[]'] ?? []) query.append("branchTypes[]", String(item)) for (const item of args['branchIds[]'] ?? []) query.append("branchIds[]", String(item)) if (args.mode != null) query.append("mode", String(args.mode)) if (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/global-search${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(globalSearchResponse, await res.json()) };