import {z} from 'zod' export type GetApiApiV1ProjectsArgs = {orgId: number, page?: number | undefined, pageSize?: number | undefined, textSearch?: string | undefined, projectDescription?: string | undefined}; export type GetApiApiV1ProjectsOptions = {baseUrl?: string | undefined, cookieAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const project = z.object({projectId: z.number().int().optional(), projectName: z.string().optional(), projectDescription: z.string().nullable().optional(), createdOn: z.string().optional(), createdBy: z.string().optional(), orgId: z.number().int().optional()}); export const organization = z.object({orgId: z.number().int().optional(), orgName: z.string().optional(), createdBy: z.number().int().optional(), createdOn: z.string().optional()}); export const getApiApiV1ProjectsResponse = z.object({data: z.object({projects: z.array(project).optional(), org: organization.optional()}).optional(), status: z.number().int().optional()}); 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 }; /** * Get all projects * * GET /api/v1/projects * * Also accepts auth: bearerAuth */ export const getApiApiV1Projects = async (args: GetApiApiV1ProjectsArgs, options?: GetApiApiV1ProjectsOptions) => { const {baseUrl = "http://localhost:3000", fetch: fetchFn = fetch, cookieAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.orgId != null) query.append("orgId", String(args.orgId)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.textSearch != null) query.append("textSearch", String(args.textSearch)) if (args.projectDescription != null) query.append("projectDescription", String(args.projectDescription)) if (cookieAuth !== undefined) cookies.push("user_session=" + encodeURIComponent(cookieAuth)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/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(getApiApiV1ProjectsResponse, await res.json()) };