import {z} from 'zod' export type CreateApiV1PostsPostIdPullRequestArgs = {postId: string, body: {integrationId?: string | undefined, github?: {owner: string, repo: string} | undefined}}; export type CreateApiV1PostsPostIdPullRequestOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1PostsPostIdPullRequestResponse = z.object({organization: z.object({id: z.string(), slug: z.string(), name: z.string(), logo: z.string().nullable()}), pullRequest: z.object({number: z.number().int().gt(0), url: z.string(), branchName: z.string(), path: z.string(), operation: z.enum(["created", "updated"])})}); 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 }; /** * Open a draft pull request for a post * * POST /v1/posts/{postId}/pull-request */ export const createApiV1PostsPostIdPullRequest = async (args: CreateApiV1PostsPostIdPullRequestArgs, options?: CreateApiV1PostsPostIdPullRequestOptions) => { const {baseUrl = "https://api.usenotra.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/posts/${encodeURIComponent(String(args.postId))}/pull-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(createApiV1PostsPostIdPullRequestResponse, await res.json()) };