import {z} from 'zod' export type ProductsPostRequest = {product?: ({name?: string | undefined, description?: string | undefined, prefix?: string | undefined}) | undefined}; export type CreateApiProductsArgs = {body: ProductsPostRequest}; export type CreateApiProductsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const productsPostResponse = z.object({product: z.object({id: z.string().optional(), reference_prefix: z.string().optional(), name: z.string().optional(), product_line: z.boolean().optional(), product_line_type: z.unknown(), created_at: z.string().optional(), updated_at: z.string().optional(), description: z.object({id: z.string().optional(), body: z.string().optional(), notable_id: z.string().optional(), notable_type: z.string().optional(), editor_version: z.number().int().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), attachments: z.array(z.unknown()).optional()}).optional(), url: z.string().optional(), resource: z.string().optional(), children: z.array(z.unknown()).optional(), custom_fields: z.array(z.unknown()).optional(), screen_definition_ids: z.object({}).optional(), screen_definitions: z.array(z.unknown()).optional(), has_ideas: z.boolean().optional(), has_master_features: z.boolean().optional(), has_epics: z.boolean().optional(), release_workflow: z.object({workflow_type: z.string().optional(), id: z.number().int().optional(), name: z.string().optional(), statusable_type: z.string().optional()}).optional(), feature_workflow: z.object({workflow_type: z.string().optional(), id: z.number().int().optional(), name: z.string().optional(), statusable_type: z.string().optional()}).optional(), initiative_workflow: z.object({workflow_type: z.string().optional(), id: z.number().int().optional(), name: z.string().optional(), statusable_type: z.string().optional()}).optional(), epic_workflow: z.object({workflow_type: z.string().optional(), id: z.number().int().optional(), name: z.string().optional(), statusable_type: z.string().optional()}).optional(), idea_workflow: z.object({workflow_type: z.string().optional(), id: z.number().int().optional(), name: z.string().optional(), statusable_type: z.string().optional()}).optional(), strategic_imperative_workflow: z.object({workflow_type: z.string().optional(), id: z.number().int().optional(), name: z.string().optional(), statusable_type: z.string().optional()}).optional(), page_workflow: z.object({workflow_type: z.string().optional(), id: z.number().int().optional(), name: z.string().optional(), statusable_type: z.string().optional()}).optional(), requirement_workflow: z.object({workflow_type: z.string().optional(), id: z.number().int().optional(), name: z.string().optional(), statusable_type: z.string().optional()}).optional(), key_result_workflow: z.object({workflow_type: z.string().optional(), id: z.number().int().optional(), name: z.string().optional(), statusable_type: z.string().optional()}).optional(), capacity_planning_enabled: z.boolean().optional(), default_capacity_units: z.number().int().optional(), enhanced_capacity_planning_enabled: z.boolean().optional(), workspace_type: z.string().optional(), color: z.string().optional()}).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 a product * * POST /products * * Also accepts auth: ApiKeyAuth, CookieAuth */ export const createApiProducts = async (args: CreateApiProductsArgs, options?: CreateApiProductsOptions) => { const {baseUrl = "https://company.aha.io/api/v1", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json; charset=utf-8") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/products${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(productsPostResponse, await res.json()) };