import {z} from 'zod' export type GetApiProjectsArgs = {projectName?: string | undefined, teamId?: string | undefined, showAlsoDeletedProjects?: boolean | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiProjectsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const cxProjectManagementPresentationDtosProjectCustomFieldDtoV5 = z.object({isMandatory: z.boolean().optional(), projectId: z.number().int().optional(), id: z.number().int().optional(), value: z.string().optional(), name: z.string().optional()}); export const cxSuperTypesAPIDtosLinkDtoBase = z.object({rel: z.string().optional(), uri: z.string().optional()}); export const cxProjectManagementPresentationDtosProjectQueueSettingsDto = z.object({queueKeepMode: z.enum(["KeepAll", "KeepOld", "KeepNew"]).optional(), scansType: z.enum(["All", "OnlyFull", "OnlyIncremental"]).optional(), includeScansInProcess: z.boolean().optional(), identicalCodeOnly: z.boolean().optional()}); export const cxProjectManagementPresentationDtosProjectBaseDtoV5 = z.object({id: z.number().int().optional(), teamId: z.number().int().optional(), name: z.string().optional(), isPublic: z.boolean().optional(), customFields: z.array(cxProjectManagementPresentationDtosProjectCustomFieldDtoV5).optional(), links: z.array(cxSuperTypesAPIDtosLinkDtoBase).optional(), owner: z.string().optional(), isDeprecated: z.boolean().optional(), projectQueueSettings: cxProjectManagementPresentationDtosProjectQueueSettingsDto.optional(), isBranched: z.boolean().optional(), originalProjectId: z.string().optional(), branchedOnScanId: z.string().optional(), relatedProjects: z.array(z.number().int()).optional()}); export const getApiProjectsResponse = z.array(cxProjectManagementPresentationDtosProjectBaseDtoV5); 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 }; /** * Gets details of all existing projects * * GET /projects */ export const getApiProjects = async (args: GetApiProjectsArgs, options?: GetApiProjectsOptions) => { const {baseUrl = "https://desktop-rmvpboc/cxrestapi/help", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json;v=5.3") if (args.projectName != null) query.append("projectName", String(args.projectName)) if (args.teamId != null) query.append("teamId", String(args.teamId)) if (args.showAlsoDeletedProjects != null) query.append("showAlsoDeletedProjects", String(args.showAlsoDeletedProjects)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/projects${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(getApiProjectsResponse, await res.json()) };