import {z} from 'zod' export type MergeRequestCreateRequest = {branchFromId: number, branchIntoId: number, title: string, description?: (string | null) | undefined, externalId?: (string | null) | undefined, autoMergeStrategy?: ('immediately' | 'scheduled' | 'none' | null) | undefined, autoMergeAt?: (string | null) | undefined, reviewerIds?: Array | undefined}; export type CreateApiV2StorageMergeRequestArgs = {body: MergeRequestCreateRequest}; export type CreateApiV2StorageMergeRequestOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const mergeRequestResponse = z.object({id: z.number().int(), creator: z.object({id: z.number().int(), name: z.string()}), title: z.string(), description: z.string(), state: z.enum(["development", "in_review", "approved", "in_merge", "published", "canceled"]), branches: z.object({branchFromId: z.number().int().nullable(), branchIntoId: z.number().int().nullable()}), merge: z.object({mergedAt: z.string().nullable(), mergerId: z.number().int().nullable(), mergerName: z.string()}), createdAt: z.string(), externalId: z.string(), autoMergeStrategy: z.enum(["immediately", "scheduled", "none"]), autoMergeAt: z.string().nullable(), approvals: z.array(z.object({approverId: z.string(), approverName: z.string(), createdAt: z.string()})), reviewers: z.array(z.object({id: z.number().int(), name: z.string(), email: z.string(), status: z.enum(["approved", "rejected"]).nullable()}))}); 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 }; /** * Create a merge request * * POST /v2/storage/merge-request * * Also accepts auth: ManageKey, BearerAuth */ export const createApiV2StorageMergeRequest = async (args: CreateApiV2StorageMergeRequestArgs, options?: CreateApiV2StorageMergeRequestOptions) => { 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/merge-request${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(mergeRequestResponse, await res.json()) };