import {z} from 'zod' export type GetApiV2StorageBranchBranchIdTokensIdArgs = {id: number, branchId: string}; export type GetApiV2StorageBranchBranchIdTokensIdOptions = {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 tokenVerifyResponse = z.object({id: z.string(), created: z.string().nullable(), refreshed: z.string().nullable(), description: z.string(), uri: z.string(), isMasterToken: z.boolean(), canManageBuckets: z.boolean(), canManageTokens: z.boolean(), canReadAllFileUploads: z.boolean(), canPurgeTrash: z.boolean(), canManageProtectedDefaultBranch: z.boolean(), canCreateJobs: z.boolean(), canReadAllProjectEvents: z.boolean(), canManageDevBranches: z.boolean(), expires: z.string().nullable(), isExpired: z.boolean(), isDisabled: z.boolean(), dailyCapacity: z.number().int(), token: z.string().optional(), creatorToken: z.object({id: z.number().int(), description: z.string()}).optional(), actor: actorResponse.nullable(), componentAccess: z.array(z.string()).optional(), bucketPermissions: z.record(z.string(), z.string()), owner: z.object({id: z.number().int(), name: z.string(), type: z.string(), region: z.string(), created: z.string().nullable(), expires: z.string().nullable(), features: z.array(z.string()), dataSizeBytes: z.number().int(), rowsCount: z.number().int(), hasMysql: z.boolean(), hasSynapse: z.boolean(), hasSnowflake: z.boolean(), hasExasol: z.boolean(), hasTeradata: z.boolean(), hasBigquery: z.boolean(), defaultBackend: z.string(), hasTryModeOn: z.string(), limits: z.record(z.string(), z.object({name: z.string(), value: z.number().int()})), metrics: z.record(z.string(), z.object({name: z.string(), value: z.number().int()})), isDisabled: z.boolean(), billedMonthlyPrice: z.number().int().nullable(), dataRetentionTimeInDays: z.number().int(), assignedBackends: z.array(z.string()), timezone: z.string().nullable(), isBYODB: z.boolean(), usersCount: z.number().int(), fileStorageProvider: z.string().optional(), payAsYouGo: z.object({purchasedCredits: z.number()}).optional(), disabled: z.object({reason: z.string(), estimatedEndTime: z.string().nullable()}).optional()}), isBYODB: z.boolean(), organization: z.object({id: z.string().nullable(), created: z.string().nullable()}), admin: z.object({name: z.string(), id: z.number().int(), features: z.array(z.string()), isOrganizationMember: z.boolean(), role: z.enum(["admin", "guest", "readOnly", "share", "developer", "reviewer", "productionManager"]).nullable(), isActivated: z.boolean(), samlParameters: z.union([z.record(z.string(), z.array(z.string())), z.array(z.unknown())]).optional()}).optional(), adminOwner: z.object({id: z.number().int(), name: z.string(), email: 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 }; /** * Token detail * * GET /v2/storage/branch/{branchId}/tokens/{id} * * Also accepts auth: ManageKey, BearerAuth */ export const getApiV2StorageBranchBranchIdTokensId = async (args: GetApiV2StorageBranchBranchIdTokensIdArgs, options?: GetApiV2StorageBranchBranchIdTokensIdOptions) => { 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/branch/${encodeURIComponent(String(args.branchId))}/tokens/${encodeURIComponent(String(args.id))}${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(tokenVerifyResponse, await res.json()) };