import { type JsonValue } from 'type-fest'; import { type ExpressiveJSONSchema } from 'ya-json-schema-types'; import { type OpenAPI, type OpenAPIExtension, type OpenAPIOperation, type OpenAPIParameter, type OpenAPIPathItem, type OpenAPIHeader, type OpenAPIRequestBody, type OpenAPIResponse, type OpenAPIReference, type OpenAPICallback, isValidOpenAPIPath, PATH_ITEM_METHODS, OpenAPIMethod, } from 'ya-open-api-types'; import { YError } from 'yerror'; export interface WhookOpenAPIOperation extends Omit< OpenAPIOperation, 'operationId' > { operationId: string; } export interface WhookOpenAPI extends OpenAPI< ExpressiveJSONSchema, OpenAPIExtension > { paths?: Record< `/${string}`, Omit< OpenAPIPathItem, OpenAPIMethod > & Partial> >; } export type WhookSupportedParameter = Extract< OpenAPIParameter, { schema: ExpressiveJSONSchema | OpenAPIReference } >; export interface WhookAPISchemaDefinition< T extends JsonValue | undefined | unknown = unknown, > { name: string; schema: ExpressiveJSONSchema; example?: T; examples?: Record; } export interface WhookAPIParameterDefinition< T extends JsonValue | undefined | unknown = unknown, > { name: string; parameter: OpenAPIParameter; example?: T; examples?: Record; } export interface WhookAPIExampleDefinition { name: string; example: T; } export interface WhookAPIHeaderDefinition { name: string; header: | OpenAPIHeader | OpenAPIReference>; } export interface WhookAPIResponseDefinition { name: string; response: | OpenAPIResponse | OpenAPIReference>; } export interface WhookAPIRequestBodyDefinition { name: string; requestBody: | OpenAPIRequestBody | OpenAPIReference< OpenAPIRequestBody >; } export interface WhookAPICallbackDefinition { name: string; callback: | OpenAPICallback | OpenAPIReference>; } export function isValidWhookOpenAPI( api: WhookOpenAPI | OpenAPI, ): api is WhookOpenAPI { for (const path in api.paths) { if (isValidOpenAPIPath(path)) { const pathItem = api.paths[path]; if (typeof pathItem === 'undefined' || '$ref' in pathItem) { throw new YError('E_BAD_PATH_ITEM', [path, pathItem]); } for (const method of PATH_ITEM_METHODS) { if (pathItem[method]) { if ('$ref' in pathItem[method] || !pathItem[method].operationId) { throw new YError('E_BAD_OPERATION', [ path, method, pathItem[method], ]); } } } } } return true; }