import {z} from 'zod' export type IssueState = 'open' | 'closed'; export type GetApiApiV0MilestonesArgs = {organization?: (string | null) | undefined, repository?: (string | null) | undefined, state?: (IssueState | null) | undefined, ordering?: ('created_at' | '-created_at' | 'updated_at' | '-updated_at' | null) | undefined, page?: number | undefined, page_size?: number | undefined}; export type GetApiApiV0MilestonesOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const issueState = z.enum(["open", "closed"]); export const milestone = z.object({created_at: z.string(), number: z.number().int(), state: issueState, title: z.string(), updated_at: z.string(), url: z.string()}); export const pagedMilestone = z.object({current_page: z.number().int(), has_next: z.boolean(), has_previous: z.boolean(), items: z.array(milestone), total_count: z.number().int(), total_pages: z.number().int()}); 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 }; /** * List milestones * * GET /api/v0/milestones/ */ export const getApiApiV0Milestones = async (args: GetApiApiV0MilestonesArgs, options?: GetApiApiV0MilestonesOptions) => { const {baseUrl = "https://nest.owasp.org", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.organization != null) query.append("organization", String(args.organization)) if (args.repository != null) query.append("repository", String(args.repository)) if (args.state != null) query.append("state", String(args.state)) if (args.ordering != null) query.append("ordering", String(args.ordering)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (ApiKey !== undefined) headers.set("X-API-Key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v0/milestones/${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(pagedMilestone, await res.json()) };