import {z} from 'zod' export type RmUserGroup = {userGroupId?: (string | null) | undefined}; export type DemandOrgUnitOwnerSchema = {email: string, name?: (string | null) | undefined}; export type EntityTag = {type: 'user' | 'system', name: string, category?: ('inherited_tag' | 'null' | null) | undefined}; export type ExtDemandOrgUnitSchema = {status: 'active' | 'archived', updatedBy?: (string | null) | undefined, userGroups?: (Array | null) | undefined, extOrgUnitId: string, _type: 'program' | 'project', mainOwner?: (DemandOrgUnitOwnerSchema | null) | undefined, endDate?: (string | null) | undefined, createdBy: string, description?: (string | null) | undefined, otherOwners?: (Array | null) | undefined, parentExtId?: (string | null) | undefined, startDate: string, updatedAt?: (number | null) | undefined, customFields?: (Record | null) | undefined, title: string, tags?: (Array | null) | undefined}; export type CreateApiApiV2ResourceManagementExtSystemsResourcingExtDemandOrgUnitsArgs = {Authorization: string, body: ExtDemandOrgUnitSchema}; export type CreateApiApiV2ResourceManagementExtSystemsResourcingExtDemandOrgUnitsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const rmUserGroup = z.object({userGroupId: z.string().nullable().optional()}); export const demandOrgUnitOwnerSchema = z.object({email: z.string(), name: z.string().nullable().optional()}); export const entityTag = z.object({type: z.enum(["user", "system"]), name: z.string(), category: z.enum(["inherited_tag", "null"]).nullable().optional()}); export const extDemandOrgUnitSchema = z.object({status: z.enum(["active", "archived"]), updatedBy: z.string().nullable().optional(), userGroups: z.array(rmUserGroup).nullable().optional(), extOrgUnitId: z.string(), _type: z.enum(["program", "project"]), mainOwner: demandOrgUnitOwnerSchema.nullable().optional(), endDate: z.string().nullable().optional(), createdBy: z.string(), description: z.string().nullable().optional(), otherOwners: z.array(demandOrgUnitOwnerSchema).nullable().optional(), parentExtId: z.string().nullable().optional(), startDate: z.string(), updatedAt: z.number().int().nullable().optional(), customFields: z.object({}).nullable().optional(), title: z.string(), tags: z.array(entityTag).nullable().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 }; /** * Create External Demand Org Unit * * POST /api/v2/resource_management/ext-systems/resourcing/ext-demand-org-units */ export const createApiApiV2ResourceManagementExtSystemsResourcingExtDemandOrgUnits = async (args: CreateApiApiV2ResourceManagementExtSystemsResourcingExtDemandOrgUnitsArgs, options?: CreateApiApiV2ResourceManagementExtSystemsResourcingExtDemandOrgUnitsOptions) => { const {baseUrl = "https://apiv2.eightfold.ai", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/resource_management/ext-systems/resourcing/ext-demand-org-units${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(extDemandOrgUnitSchema, await res.json()) };