import {z} from 'zod' export type ClassificationLevel = 'UNCLASSIFIED' | 'SECRET' | 'TOP_SECRET'; export type CLIN = {clin_number: string, type: 'CLOUD' | 'NON_CLOUD', classification_level?: ClassificationLevel | undefined, pop_start_date: string, pop_end_date: string}; export type TaskOrder = {id: string, task_order_number: string, clins: Array, pop_start_date: string, pop_end_date: string}; export type CreateApiPortfoliosPortfolioIdTaskOrdersArgs = {body: TaskOrder}; export type CreateApiPortfoliosPortfolioIdTaskOrdersOptions = {baseUrl: string, oidc?: string | undefined, fetch?: typeof fetch | undefined}; export const classificationLevel = z.enum(["UNCLASSIFIED", "SECRET", "TOP_SECRET"]); export const cLIN = z.object({clin_number: z.string(), type: z.enum(["CLOUD", "NON_CLOUD"]), classification_level: classificationLevel.optional(), pop_start_date: z.string(), pop_end_date: z.string()}); export const taskOrder = z.object({id: z.string(), task_order_number: z.string(), clins: z.array(cLIN), pop_start_date: z.string(), pop_end_date: z.string()}); 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 }; /** * Adds a new Task Order (TO) * * POST /portfolios/{portfolioId}/task-orders */ export const createApiPortfoliosPortfolioIdTaskOrders = async (args: CreateApiPortfoliosPortfolioIdTaskOrdersArgs, options: CreateApiPortfoliosPortfolioIdTaskOrdersOptions) => { const {baseUrl, fetch: fetchFn = fetch, oidc} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oidc !== undefined) headers.set('authorization', `Bearer ${oidc}`) const url = `${baseUrl.replace(/\/+$/, '')}/portfolios/${encodeURIComponent(String(args.portfolioId))}/task-orders${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(taskOrder, await res.json()) };