@@ -8,8 +8,7 @@ import type {
8
8
ParameterObject ,
9
9
ResponseObject ,
10
10
RequestBodyObject ,
11
- SchemaObject ,
12
- ParameterLocation
11
+ SchemaObject
13
12
} from 'openapi-directory' ;
14
13
import * as Ajv from 'ajv' ;
15
14
@@ -24,7 +23,15 @@ import { firstMatch, empty, lastHeader } from '../../util';
24
23
import { formatAjvError } from '../../util/json-schema' ;
25
24
import { reportError } from '../../errors' ;
26
25
27
- import { ApiMetadata } from './build-openapi' ;
26
+ import {
27
+ ApiExchange ,
28
+ ApiService ,
29
+ ApiOperation ,
30
+ ApiRequest ,
31
+ ApiResponse ,
32
+ ApiParameter
33
+ } from './api-interfaces' ;
34
+ import { OpenApiMetadata } from './build-openapi' ;
28
35
import { fromMarkdown } from '../markdown' ;
29
36
30
37
const paramValidator = new Ajv ( {
@@ -35,7 +42,10 @@ const paramValidator = new Ajv({
35
42
// If we match multiple APIs, build all of them, try to match each one
36
43
// against the request, and return only the matching one. This happens if
37
44
// multiple services have the exact same base request URL (rds.amazonaws.com)
38
- export function findBestMatchingApi ( apis : ApiMetadata [ ] , request : HtkRequest ) : ApiMetadata | undefined {
45
+ export function findBestMatchingApi (
46
+ apis : OpenApiMetadata [ ] ,
47
+ request : HtkRequest
48
+ ) : OpenApiMetadata | undefined {
39
49
const matchingApis = apis . filter ( ( api ) => matchOperation ( api , request ) . matched ) ;
40
50
41
51
// If we've successfully found one matching API, return it
@@ -56,7 +66,7 @@ export function findBestMatchingApi(apis: ApiMetadata[], request: HtkRequest): A
56
66
return _ . maxBy ( matchingApis , a => a . spec . paths . length ) ! ;
57
67
}
58
68
59
- function getPath ( api : ApiMetadata , request : HtkRequest ) : {
69
+ function getPath ( api : OpenApiMetadata , request : HtkRequest ) : {
60
70
pathSpec : PathObject ,
61
71
path : string
62
72
} | undefined {
@@ -94,7 +104,7 @@ export function getParameters(
94
104
path : string ,
95
105
parameters : ParameterObject [ ] ,
96
106
request : HtkRequest
97
- ) : Parameter [ ] {
107
+ ) : ApiParameter [ ] {
98
108
if ( ! parameters ) return [ ] ;
99
109
100
110
const query = request . parsedUrl . searchParams ;
@@ -270,7 +280,7 @@ export function getBodySchema(
270
280
) ;
271
281
}
272
282
273
- function getDummyPath ( api : ApiMetadata , request : HtkRequest ) : string {
283
+ function getDummyPath ( api : OpenApiMetadata , request : HtkRequest ) : string {
274
284
const { parsedUrl } = request ;
275
285
const url = `${ parsedUrl . protocol } //${ parsedUrl . hostname } ${ parsedUrl . pathname } ` ;
276
286
const serverMatch = api . serverMatcher . exec ( url ) ;
@@ -292,16 +302,16 @@ function stripTags(input: string | undefined): string | undefined {
292
302
return input . replace ( / ( < ( [ ^ > ] + ) > ) / ig, '' ) ;
293
303
}
294
304
295
- export class ApiExchange {
296
- constructor ( api : ApiMetadata , exchange : HttpExchange ) {
305
+ export class OpenApiExchange implements ApiExchange {
306
+ constructor ( api : OpenApiMetadata , exchange : HttpExchange ) {
297
307
const { request } = exchange ;
298
- this . service = new ApiService ( api . spec ) ;
308
+ this . service = new OpenApiService ( api . spec ) ;
299
309
300
310
this . _spec = api . spec ;
301
311
this . _opSpec = matchOperation ( api , request ) ;
302
312
303
- this . operation = new ApiOperation ( this . _opSpec ) ;
304
- this . request = new ApiRequest ( api . spec , this . _opSpec , request ) ;
313
+ this . operation = new OpenApiOperation ( this . _opSpec ) ;
314
+ this . request = new OpenApiRequest ( api . spec , this . _opSpec , request ) ;
305
315
306
316
if ( exchange . response ) {
307
317
this . updateWithResponse ( exchange . response ) ;
@@ -319,22 +329,22 @@ export class ApiExchange {
319
329
320
330
updateWithResponse ( response : HtkResponse | 'aborted' | undefined ) : void {
321
331
if ( response === 'aborted' || response === undefined ) return ;
322
- this . response = new ApiResponse ( this . _spec , this . _opSpec , response ) ;
332
+ this . response = new OpenApiResponse ( this . _spec , this . _opSpec , response ) ;
323
333
}
324
334
325
335
matchedOperation ( ) {
326
336
return this . _opSpec . matched ;
327
337
}
328
338
}
329
339
330
- class ApiService {
340
+ class OpenApiService implements ApiService {
331
341
constructor ( spec : OpenAPIObject ) {
332
342
const { info : service } = spec ;
333
343
334
344
this . name = service . title ;
335
345
this . logoUrl = service [ 'x-logo' ] ?. url
336
346
this . description = fromMarkdown ( service . description ) ;
337
- this . docsUrl = get ( spec , ' externalDocs' , ' url' ) ;
347
+ this . docsUrl = spec ?. externalDocs ?. url ;
338
348
}
339
349
340
350
public readonly name : string ;
@@ -343,7 +353,7 @@ class ApiService {
343
353
public readonly docsUrl ?: string ;
344
354
}
345
355
346
- function matchOperation ( api : ApiMetadata , request : HtkRequest ) {
356
+ function matchOperation ( api : OpenApiMetadata , request : HtkRequest ) {
347
357
const matchingPath = getPath ( api , request ) ;
348
358
349
359
const { pathSpec, path } = matchingPath || {
@@ -368,7 +378,7 @@ function matchOperation(api: ApiMetadata, request: HtkRequest) {
368
378
369
379
type MatchedOperation = ReturnType < typeof matchOperation > ;
370
380
371
- class ApiOperation {
381
+ class OpenApiOperation implements ApiOperation {
372
382
constructor (
373
383
op : MatchedOperation
374
384
) {
@@ -411,7 +421,7 @@ class ApiOperation {
411
421
warnings : string [ ] = [ ] ;
412
422
}
413
423
414
- class ApiRequest {
424
+ class OpenApiRequest implements ApiRequest {
415
425
constructor (
416
426
spec : OpenAPIObject ,
417
427
op : MatchedOperation ,
@@ -430,24 +440,11 @@ class ApiRequest {
430
440
) ;
431
441
}
432
442
433
- parameters : Parameter [ ] ;
443
+ parameters : ApiParameter [ ] ;
434
444
bodySchema ?: SchemaObject ;
435
445
}
436
446
437
- export interface Parameter {
438
- name : string ;
439
- description ?: Html ;
440
- value ?: unknown ;
441
- defaultValue ?: unknown ;
442
- enum ?: unknown [ ] ;
443
- type ?: string ;
444
- in : ParameterLocation ;
445
- required : boolean ;
446
- deprecated : boolean ;
447
- warnings : string [ ] ;
448
- }
449
-
450
- class ApiResponse {
447
+ class OpenApiResponse implements ApiResponse {
451
448
constructor (
452
449
spec : OpenAPIObject ,
453
450
op : MatchedOperation ,
0 commit comments