import {z} from 'zod' export type UpdateTokenRequest = {description?: (string | null) | undefined, bucketPermissions?: ({[key: string]: 'read' | 'write'}) | undefined, componentAccess?: (Array | null) | undefined, canReadAllFileUploads?: (boolean | null) | undefined, canPurgeTrash?: (boolean | null) | undefined}; export type UpdateApiV2StorageTokensIdArgs = {id: number, body: UpdateTokenRequest}; export type UpdateApiV2StorageTokensIdOptions = {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 tokenCreateResponse = 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())}); 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 }; /** * Update token * * PUT /v2/storage/tokens/{id} * * Also accepts auth: ManageKey, BearerAuth */ export const updateApiV2StorageTokensId = async (args: UpdateApiV2StorageTokensIdArgs, options?: UpdateApiV2StorageTokensIdOptions) => { 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/tokens/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(tokenCreateResponse, await res.json()) };