import {z} from 'zod' export type CreateApiProjectsProjectIdItemsArgs = {projectId: string, body: {itemData: {id?: (string | null) | undefined, sourceId?: (string | null) | undefined, type: string, title: string, statusName: string, author?: ({id: string} | {email: string} | null) | undefined, reporter?: ({id: string} | {email: string} | null) | undefined, assignee?: ({id: string} | {email: string} | null) | undefined, introducedInVersion?: ({id: string} | null) | undefined, obsoleteInVersion?: ({id: string} | null) | undefined, selectedVersions?: (Array<{id: string}> | null) | undefined, fields?: (Array<{label: string, type: 'string' | 'select' | 'date', value: string} | {label: string, type: 'jdf', value: unknown} | {label: string, type: 'multiselect', value: Array}> | null) | undefined, relations?: (Array<{type: string, toItem: {id: string} | {sourceId: string} | {system: 'ADO' | 'BUILD' | 'GIT' | 'LINEAR' | 'JAMA' | 'JIRA' | 'CONFLUENCE' | 'KETRYX' | 'POLARION' | 'QMETRY' | 'TESTRAIL' | 'GHSA' | 'NVD' | 'JIRA_DATA_CENTER', value: string}}> | null) | undefined}}}; export type CreateApiProjectsProjectIdItemsOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiProjectsProjectIdItemsResponse = z.object({item: z.object({success: z.boolean(), id: z.string().nullable(), sourceId: z.string().min(1).optional(), currentRecord: z.object({id: z.string().min(1)}).nullable(), warnings: z.array(z.union([z.object({type: z.literal("UNKNOWN_ITEM_TYPE"), message: z.string(), typeName: z.string()}), z.object({type: z.literal("UNKNOWN_ID"), message: z.string(), key: z.object({value: z.string()})}), z.object({type: z.literal("UNKNOWN_FIELD"), message: z.string(), fieldKey: z.string()}), z.object({type: z.literal("NON_WRITABLE_FIELD"), message: z.string(), fieldKey: z.string()}), z.object({type: z.literal("COMPUTED_FIELD_ERROR"), message: z.string(), fieldKey: z.string()}), z.object({type: z.literal("INCOMPATIBLE_FIELD_TYPE"), message: z.string(), fieldKey: z.string(), actualType: z.string(), expectedType: z.string()}), z.object({type: z.literal("UNKNOWN_RELATION_TYPE"), message: z.string(), relationType: z.string()}), z.object({type: z.literal("SINGLE_SELECT_RELATION_CARDINALITY"), message: z.string(), relationType: z.string()}), z.object({type: z.enum(["UNKNOWN_RELATION_TARGET", "SELF_REFERENCE"]), message: z.string(), relationType: z.string(), targetKey: z.union([z.object({id: z.string()}), z.object({sourceId: z.string().min(1)}), z.object({system: z.enum(["ADO", "BUILD", "GIT", "LINEAR", "JAMA", "JIRA", "CONFLUENCE", "KETRYX", "POLARION", "QMETRY", "TESTRAIL", "GHSA", "NVD", "JIRA_DATA_CENTER"]), value: z.string().min(1)})])}), z.object({type: z.literal("STALE_DATA_DETECTED"), message: z.string()}), z.object({type: z.literal("DUPLICATE_ITEM_REFERENCE"), message: z.string()}), z.object({type: z.literal("INVALID_STATE_TRANSITION"), message: z.string()}), z.object({type: z.literal("UNEXPECTED_ERROR"), message: z.string()}), z.object({type: z.literal("EXTERNAL_WRITE_BACK_FAILED"), message: z.string()}), z.object({type: z.literal("EXTERNAL_RELATION_WRITE_BACK_FAILED"), message: z.string()})])).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 or patch an existing item in a project. A write that conflicts with another in-progress write on the same id or sourceId is rejected with 409 and should be retried (subject to change). * * POST /projects/{projectId}/items */ export const createApiProjectsProjectIdItems = async (args: CreateApiProjectsProjectIdItemsArgs, options?: CreateApiProjectsProjectIdItemsOptions) => { const {baseUrl = "https://app.ketryx.com/api/v1", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set('authorization', `Bearer ${Authorization}`) const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.projectId))}/items${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(createApiProjectsProjectIdItemsResponse, await res.json()) };