import {z} from 'zod' export type GetApiInviteJiraProgressIdArgs = {id: number}; export type GetApiInviteJiraProgressIdOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const tableFetchingProgress = z.object({fetched: z.number().int().gte(0), name: z.string(), total: z.number().int().gte(1)}); export const installationProgress = z.object({started_date: z.string(), finished_date: z.string().nullable(), owner: z.string().optional(), repositories: z.number().int().gte(0).optional(), tables: z.array(tableFetchingProgress)}); 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 }; /** * Return the current JIRA installation progress in Athenian. * * GET /invite/jira_progress/{id} * * Also accepts auth: apiKeyAuth */ export const getApiInviteJiraProgressId = async (args: GetApiInviteJiraProgressIdArgs, options: GetApiInviteJiraProgressIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/invite/jira_progress/${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(installationProgress, await res.json()) };