import { FactVariant, FlatOpenAPIV3, FlatOpenAPIV3_1, OpenApiKind, OpenAPIV3, } from '@useoptic/openapi-utilities'; export type OpenAPIDocument = | OpenAPIV3.Document | FlatOpenAPIV3.Document | FlatOpenAPIV3_1.Document; // Value constructs - these are types that represent the node you are working on export type RuleContext = { specification: Specification & { change: 'added' | 'changed' | 'removed' | null; versionChange: 'major' | 'minor' | 'patch' | null; }; operation: Operation & { change: 'added' | 'changed' | 'removed' | null; }; custom: any; // user defined context }; export type Specification = FactVariant & { raw: OpenAPIV3.Document; }; export type Operation = FactVariant & { raw: OpenAPIV3.OperationObject; security: OpenAPIV3.OperationObject['security'] | null; polymorphicSchemas: { before: Set; after: Set }; path: string; method: string; queryParameters: Map; pathParameters: Map; headerParameters: Map; cookieParameters: Map; requests: RequestBody[]; responses: Map; }; export type QueryParameter = FactVariant & { raw: OpenAPIV3.ParameterObject; }; export type PathParameter = FactVariant & { raw: OpenAPIV3.ParameterObject; }; export type HeaderParameter = FactVariant & { raw: OpenAPIV3.ParameterObject; }; export type CookieParameter = FactVariant & { raw: OpenAPIV3.ParameterObject; }; export type RequestBody = FactVariant & { raw: OpenAPIV3.MediaTypeObject; required?: boolean; contentType: string; properties: Map; schemas: Map; }; export type Property = FactVariant & { raw: OpenAPIV3.SchemaObject; parent: OpenAPIV3.NonArraySchemaObject; }; export type Field = Property; export type Schema = FactVariant & { raw: OpenAPIV3.SchemaObject; }; export type ResponseHeader = FactVariant & { raw: OpenAPIV3.HeaderObject; }; export type Response = FactVariant & { raw: OpenAPIV3.ResponseObject; statusCode: string; headers: Map; bodies: ResponseBody[]; }; export type ResponseBody = FactVariant & { raw: OpenAPIV3.MediaTypeObject; contentType: string; statusCode: string; properties: Map; schemas: Map; }; // Assertions export type AssertionType = | 'specification' | 'operation' | 'query-parameter' | 'path-parameter' | 'header-parameter' | 'cookie-parameter' | 'response' | 'response-header' | 'request-body' | 'response-body' | 'property' | 'schema'; export type AssertionTypeToValue = { specification: Specification; operation: Operation; 'query-parameter': QueryParameter; 'path-parameter': PathParameter; 'header-parameter': HeaderParameter; 'cookie-parameter': CookieParameter; 'response-header': ResponseHeader; response: Response; 'request-body': RequestBody; 'response-body': ResponseBody; property: Field; schema: Schema; }; type MatchesFn = ( structure: any, options?: { strict?: boolean; errorMessage?: string; } ) => void; type MatchesOneOfFn = ( structures: any[], options?: { strict?: boolean; errorMessage?: string; } ) => void; type SpecificationAssertionHelpers = { not: SpecificationAssertionHelpers; matches: MatchesFn; matchesOneOf: MatchesOneOfFn; }; type OperationAssertionHelpers = { not: OperationAssertionHelpers; hasQueryParameterMatching: MatchesFn; hasPathParameterMatching: MatchesFn; hasHeaderParameterMatching: MatchesFn; hasCookieParameterMatching: MatchesFn; hasRequests: ( requests: { contentType: string; }[] ) => void; hasResponses: ( responses: { contentType?: string; statusCode: string; }[] ) => void; matches: MatchesFn; matchesOneOf: MatchesOneOfFn; }; type ResponseAssertionHelpers = { not: ResponseAssertionHelpers; hasResponseHeaderMatching: ( name: string, structure: any, options?: { strict?: boolean; } ) => void; }; type RequestBodyAssertionHelpers = { not: AssertionTypeToHelpers['request-body']; matches: MatchesFn; matchesOneOf: MatchesOneOfFn; }; type ResponseBodyAssertionHelpers = { not: AssertionTypeToHelpers['response-body']; matches: MatchesFn; matchesOneOf: MatchesOneOfFn; }; export type AssertionTypeToHelpers = { specification: SpecificationAssertionHelpers; operation: OperationAssertionHelpers; 'query-parameter': {}; 'path-parameter': {}; 'header-parameter': {}; 'cookie-parameter': {}; response: ResponseAssertionHelpers; 'response-header': {}; 'request-body': RequestBodyAssertionHelpers; 'response-body': ResponseBodyAssertionHelpers; schema: {}; property: {}; }; export type Assertion = ( value: AssertionTypeToValue[T] ) => void; export type RegisterAssertion = ( ...args: | [condition: string, assertion: Assertion] | [assertion: Assertion] ) => void; export type ChangedAssertion = ( before: AssertionTypeToValue[T], after: AssertionTypeToValue[T] ) => void; export type RegisterChangedAssertion = ( ...args: | [condition: string, assertion: ChangedAssertion] | [assertion: ChangedAssertion] ) => void; export type Assertions = { requirement: RegisterAssertion & AssertionTypeToHelpers[T]; added: RegisterAssertion & AssertionTypeToHelpers[T]; addedOrChanged: RegisterAssertion & AssertionTypeToHelpers[T]; changed: RegisterChangedAssertion & AssertionTypeToHelpers[T]; removed: RegisterAssertion & AssertionTypeToHelpers[T]; }; export type SpecificationAssertions = Assertions<'specification'>; export type OperationAssertions = Assertions<'operation'> & { queryParameter: Assertions<'query-parameter'>; pathParameter: Assertions<'path-parameter'>; headerParameter: Assertions<'header-parameter'>; cookieParameter: Assertions<'cookie-parameter'>; }; export type RequestAssertions = { body: Assertions<'request-body'>; property: Assertions<'property'>; schema: Assertions<'schema'>; }; export type ResponseAssertions = Assertions<'response'> & { header: Assertions<'response-header'>; }; export type ResponseBodyAssertions = { body: Assertions<'response-body'>; property: Assertions<'property'>; schema: Assertions<'schema'>; }; export type PropertyAssertions = Assertions<'property'>;