@@ -14,12 +14,9 @@ import {
14
14
MockttpBreakpointResponseResult ,
15
15
InputRuleEventDataMap
16
16
} from "../../types" ;
17
- import { UnreachableCheck } from '../../util/error' ;
18
- import { lazyObservablePromise } from "../../util/observable" ;
19
17
import { getHeaderValue } from '../../util/headers' ;
20
18
import { ParsedUrl } from '../../util/url' ;
21
19
22
- import { logError } from '../../errors' ;
23
20
24
21
import { MANUALLY_SENT_SOURCE , parseSource } from './sources' ;
25
22
import { getContentType } from '../events/content-types' ;
@@ -29,9 +26,7 @@ import { HandlerClassKey, HtkRule, getRulePartKey } from '../rules/rules';
29
26
30
27
import { ApiStore } from '../api/api-store' ;
31
28
import { 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' ;
35
30
36
31
import { HttpBody } from './http-body' ;
37
32
import {
@@ -189,7 +184,7 @@ export class HttpExchange extends HTKEventBase implements ViewableHttpExchange {
189
184
. toLowerCase ( ) ;
190
185
191
186
// 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 ) ;
193
188
}
194
189
195
190
public readonly id : string ;
@@ -326,15 +321,6 @@ export class HttpExchange extends HTKEventBase implements ViewableHttpExchange {
326
321
..._ . map ( response . headers , ( value , key ) => `${ key } : ${ value } ` ) ,
327
322
..._ . map ( response . trailers , ( value , key ) => `${ key } : ${ value } ` )
328
323
] . 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
- ) ;
338
324
}
339
325
340
326
// 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 {
356
342
357
343
// API metadata:
358
344
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 ;
392
346
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 ;
396
352
}
397
353
398
354
// Breakpoint data:
0 commit comments