import {z} from 'zod' export type GetApiApiV0ReleasesArgs = {organization?: (string | null) | undefined, repository?: (string | null) | undefined, tag_name?: (string | null) | undefined, ordering?: ('created_at' | '-created_at' | 'published_at' | '-published_at' | null) | undefined, page?: number | undefined, page_size?: number | undefined}; export type GetApiApiV0ReleasesOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const release = z.object({created_at: z.string(), name: z.string(), published_at: z.string().nullable().optional(), tag_name: z.string()}); export const pagedRelease = z.object({current_page: z.number().int(), has_next: z.boolean(), has_previous: z.boolean(), items: z.array(release), 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 releases * * GET /api/v0/releases/ */ export const getApiApiV0Releases = async (args: GetApiApiV0ReleasesArgs, options?: GetApiApiV0ReleasesOptions) => { 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.tag_name != null) query.append("tag_name", String(args.tag_name)) 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/releases/${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(pagedRelease, await res.json()) };