import {z} from 'zod' export type EngineParamBindingValuePayloadV2 = {literal?: string | undefined, reference?: string | undefined}; export type EngineParamBindingPayloadV2 = {array_value?: Array | undefined, value?: EngineParamBindingValuePayloadV2 | undefined}; export type CatalogV2CreateEntryRequestBody = {aliases?: Array | undefined, attribute_values: {[key: string]: EngineParamBindingPayloadV2}, catalog_type_id: string, external_id?: string | undefined, name: string, rank?: number | undefined}; export type CreateApiV2CatalogEntriesArgs = {body: CatalogV2CreateEntryRequestBody}; export type CreateApiV2CatalogEntriesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const catalogEntryReferenceV2 = z.object({archived_at: z.string().optional(), catalog_entry_id: z.string(), catalog_entry_name: z.string(), catalog_type_id: z.string()}); export const catalogEntryEngineParamBindingValueV2 = z.object({catalog_entry: catalogEntryReferenceV2.optional(), helptext: z.string().optional(), image_url: z.string().optional(), is_image_slack_icon: z.boolean().optional(), label: z.string(), literal: z.string().optional(), reference: z.string().optional(), sort_key: z.string(), unavailable: z.boolean().optional(), value: z.string().optional()}); export const catalogEntryEngineParamBindingV2 = z.object({array_value: z.array(catalogEntryEngineParamBindingValueV2).optional(), value: catalogEntryEngineParamBindingValueV2.optional()}); export const catalogEntryV2 = z.object({aliases: z.array(z.string()), archived_at: z.string().optional(), attribute_values: z.record(z.string(), catalogEntryEngineParamBindingV2), catalog_type_id: z.string(), created_at: z.string(), external_id: z.string().optional(), id: z.string(), name: z.string(), rank: z.number().int(), updated_at: z.string()}); export const catalogV2CreateEntryResponseBody = z.object({catalog_entry: catalogEntryV2}); 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 }; /** * CreateEntry Catalog V2 * * POST /v2/catalog_entries */ export const createApiV2CatalogEntries = async (args: CreateApiV2CatalogEntriesArgs, options?: CreateApiV2CatalogEntriesOptions) => { const {baseUrl = "https:////api.incident.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/catalog_entries${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(catalogV2CreateEntryResponseBody, await res.json()) };