@@ -14,12 +14,9 @@ import {
1414 MockttpBreakpointResponseResult ,
1515 InputRuleEventDataMap
1616} from "../../types" ;
17- import { UnreachableCheck } from '../../util/error' ;
18- import { lazyObservablePromise } from "../../util/observable" ;
1917import { getHeaderValue } from '../../util/headers' ;
2018import { ParsedUrl } from '../../util/url' ;
2119
22- import { logError } from '../../errors' ;
2320
2421import { MANUALLY_SENT_SOURCE , parseSource } from './sources' ;
2522import { getContentType } from '../events/content-types' ;
@@ -29,9 +26,7 @@ import { HandlerClassKey, HtkRule, getRulePartKey } from '../rules/rules';
2926
3027import { ApiStore } from '../api/api-store' ;
3128import { ApiExchange } from '../api/api-interfaces' ;
32- import { OpenApiExchange } from '../api/openapi' ;
33- import { parseRpcApiExchange } from '../api/jsonrpc' ;
34- import { ApiMetadata } from '../api/api-interfaces' ;
29+ import { ApiDetector } from './api-detector' ;
3530
3631import { HttpBody } from './http-body' ;
3732import {
@@ -189,7 +184,7 @@ export class HttpExchange extends HTKEventBase implements ViewableHttpExchange {
189184 . toLowerCase ( ) ;
190185
191186 // Start loading the relevant Open API specs for this request, if any.
192- this . _apiMetadataPromise = apiStore . getApi ( this . request ) ;
187+ this . _apiDetector = new ApiDetector ( this , apiStore ) ;
193188 }
194189
195190 public readonly id : string ;
@@ -326,15 +321,6 @@ export class HttpExchange extends HTKEventBase implements ViewableHttpExchange {
326321 ..._ . map ( response . headers , ( value , key ) => `${ key } : ${ value } ` ) ,
327322 ..._ . map ( response . trailers , ( value , key ) => `${ key } : ${ value } ` )
328323 ] . join ( '\n' ) . toLowerCase ( ) ;
329-
330- // Wrap the API promise to also add this response data (but lazily)
331- const requestApiPromise = this . _apiPromise ;
332- this . _apiPromise = lazyObservablePromise ( ( ) =>
333- requestApiPromise . then ( ( api ) => {
334- if ( api ) api . updateWithResponse ( this . response ! ) ;
335- return api ;
336- } )
337- ) ;
338324 }
339325
340326 // Must only be called when the exchange will no longer be used. Ensures that large data is
@@ -356,43 +342,13 @@ export class HttpExchange extends HTKEventBase implements ViewableHttpExchange {
356342
357343 // API metadata:
358344
359- // A convenient reference to the service-wide spec for this API - starts loading immediately
360- private _apiMetadataPromise : Promise < ApiMetadata | undefined > ;
361-
362- // Parsed API info for this specific request, loaded & parsed lazily, only if it's used
363- @observable . ref
364- private _apiPromise = lazyObservablePromise ( async ( ) : Promise < ApiExchange | undefined > => {
365- const apiMetadata = await this . _apiMetadataPromise ;
366-
367- if ( apiMetadata ) {
368- // We load the spec, but we don't try to parse API requests until we've received
369- // the whole thing (because e.g. JSON-RPC requests aren't parseable without the body)
370- await when ( ( ) => this . isCompletedRequest ( ) ) ;
371-
372- try {
373- if ( apiMetadata . type === 'openapi' ) {
374- return new OpenApiExchange ( apiMetadata , this ) ;
375- } else if ( apiMetadata . type === 'openrpc' ) {
376- return await parseRpcApiExchange ( apiMetadata , this ) ;
377- } else {
378- console . log ( 'Unknown API metadata type for host' , this . request . parsedUrl . hostname ) ;
379- console . log ( apiMetadata ) ;
380- throw new UnreachableCheck ( apiMetadata , m => m . type ) ;
381- }
382- } catch ( e ) {
383- logError ( e ) ;
384- throw e ;
385- }
386- } else {
387- return undefined ;
388- }
389- } ) ;
390-
391- // Fixed value for the parsed API data - returns the data or undefined, observably.
345+ private _apiDetector : ApiDetector ;
392346 get api ( ) {
393- if ( this . _apiPromise . state === 'fulfilled' ) {
394- return this . _apiPromise . value as ApiExchange | undefined ;
395- }
347+ return this . _apiDetector . parsedApi ;
348+ }
349+
350+ get apiSpec ( ) {
351+ return this . _apiDetector . apiMetadata ;
396352 }
397353
398354 // Breakpoint data:
0 commit comments