import {z} from 'zod' export type CreateApiV2StorageBucketsBucketIdShareOrganizationArgs = {bucketId: string}; export type CreateApiV2StorageBucketsBucketIdShareOrganizationOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const bucketShareOrganizationResponse = z.object({id: z.number().int(), status: z.string(), url: z.string(), tableId: z.string().nullable(), operationName: z.string(), operationParams: z.record(z.string(), z.unknown()), createdTime: z.string(), startTime: z.string().nullable(), endTime: z.string().nullable(), runId: z.string().nullable(), results: z.object({}).nullable(), creatorToken: z.object({id: z.number().int(), description: z.string().nullable()}), metrics: z.record(z.string(), z.unknown()), error: z.object({code: z.string(), message: z.string(), exceptionId: z.string()}).optional()}); 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 }; /** * Share Bucket To Organization * * POST /v2/storage/buckets/{bucketId}/share-organization * * Also accepts auth: ManageKey, BearerAuth */ export const createApiV2StorageBucketsBucketIdShareOrganization = async (args: CreateApiV2StorageBucketsBucketIdShareOrganizationArgs, options?: CreateApiV2StorageBucketsBucketIdShareOrganizationOptions) => { 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 (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/buckets/${encodeURIComponent(String(args.bucketId))}/share-organization${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(bucketShareOrganizationResponse, await res.json()) };