forked from kogosoftwarellc/open-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
openapi-framework: Allow injecting custom classes for features (kogos…
- Loading branch information
Showing
13 changed files
with
372 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/openapi-framework/test/sample-projects/paths-dir-with-custom-features/apiDoc.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
swagger: '2.0' | ||
info: | ||
title: sample api doc | ||
version: '3' | ||
paths: {} | ||
securityDefinitions: | ||
basic: | ||
type: basic |
16 changes: 16 additions & 0 deletions
16
...i-framework/test/sample-projects/paths-dir-with-custom-features/features/CustomCoercer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { | ||
IOpenAPIRequestCoercer, | ||
OpenAPIRequestCoercerArgs, | ||
} from 'openapi-request-coercer'; | ||
|
||
export default class CustomCoercer implements IOpenAPIRequestCoercer { | ||
private args; | ||
|
||
constructor(args: OpenAPIRequestCoercerArgs) { | ||
this.args = args; | ||
} | ||
|
||
coerce(request: any) { | ||
return; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ework/test/sample-projects/paths-dir-with-custom-features/features/CustomDefaultSetter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { | ||
IOpenAPIDefaultSetter, | ||
OpenAPIDefaultSetterArgs, | ||
} from 'openapi-default-setter'; | ||
import { OpenAPI } from 'openapi-types'; | ||
|
||
export default class CustomDefaultSetter implements IOpenAPIDefaultSetter { | ||
private args; | ||
|
||
constructor(args: OpenAPIDefaultSetterArgs) { | ||
this.args = args; | ||
} | ||
|
||
handle(request: OpenAPI.Request) { | ||
return; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...rk/test/sample-projects/paths-dir-with-custom-features/features/CustomRequestValidator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { | ||
IOpenAPIRequestValidator, | ||
OpenAPIRequestValidatorArgs, | ||
} from 'openapi-request-validator'; | ||
|
||
export default class CustomRequestValidator | ||
implements IOpenAPIRequestValidator { | ||
private args; | ||
|
||
constructor(args: OpenAPIRequestValidatorArgs) { | ||
this.args = args; | ||
} | ||
|
||
validateRequest(request: any) { | ||
return; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...k/test/sample-projects/paths-dir-with-custom-features/features/CustomResponseValidator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { | ||
IOpenAPIResponseValidator, | ||
OpenAPIResponseValidatorArgs, | ||
} from 'openapi-response-validator'; | ||
|
||
export default class CustomResponseValidator | ||
implements IOpenAPIResponseValidator { | ||
private args; | ||
|
||
constructor(args: OpenAPIResponseValidatorArgs) { | ||
this.args = args; | ||
} | ||
|
||
validateResponse(statusCode: any, response: any) { | ||
return { | ||
errors: [], | ||
message: 'Hello, world!', | ||
}; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...ork/test/sample-projects/paths-dir-with-custom-features/features/CustomSecurityHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { | ||
IOpenAPISecurityHandler, | ||
OpenAPISecurityHandlerArgs, | ||
} from 'openapi-security-handler'; | ||
|
||
export default class CustomSecurityHandler implements IOpenAPISecurityHandler { | ||
private args; | ||
|
||
constructor(args: OpenAPISecurityHandlerArgs) { | ||
this.args = args; | ||
} | ||
|
||
handle(request: any) { | ||
return new Promise<void>((resolve) => { | ||
resolve(); | ||
}); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/openapi-framework/test/sample-projects/paths-dir-with-custom-features/paths/foo.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module.exports = { | ||
GET, | ||
}; | ||
|
||
function GET() { | ||
return; | ||
} | ||
|
||
GET.apiDoc = { | ||
parameters: [ | ||
{ | ||
name: 'name', | ||
in: 'query', | ||
type: 'string', | ||
default: 'elvis', | ||
}, | ||
], | ||
responses: { | ||
default: { | ||
description: 'return foo', | ||
schema: {}, | ||
}, | ||
}, | ||
}; |
85 changes: 85 additions & 0 deletions
85
packages/openapi-framework/test/sample-projects/paths-dir-with-custom-features/spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* tslint:disable:no-unused-expression */ | ||
import { expect } from 'chai'; | ||
import OpenapiFramework from '../../../'; | ||
import CustomDefaultSetter from './features/CustomDefaultSetter'; | ||
import CustomCoercer from './features/CustomCoercer'; | ||
import CustomRequestValidator from './features/CustomRequestValidator'; | ||
import CustomResponseValidator from './features/CustomResponseValidator'; | ||
import CustomSecurityHandler from './features/CustomSecurityHandler'; | ||
const path = require('path'); | ||
|
||
describe(path.basename(__dirname), () => { | ||
let framework: OpenapiFramework; | ||
|
||
beforeEach(() => { | ||
framework = new OpenapiFramework({ | ||
apiDoc: path.resolve(__dirname, 'apiDoc.yml'), | ||
featureType: 'middleware', | ||
features: { | ||
coercer: CustomCoercer, | ||
defaultSetter: CustomDefaultSetter, | ||
requestValidator: CustomRequestValidator, | ||
responseValidator: CustomResponseValidator, | ||
securityHandler: CustomSecurityHandler, | ||
}, | ||
name: 'some-framework', | ||
paths: path.resolve(__dirname, './paths'), | ||
pathSecurity: [ | ||
[/.+/, [{ basic: [] }]], | ||
[/^awes/, [{ basic: [] }]], | ||
], | ||
securityHandlers: { | ||
basic() { | ||
return true; | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should instantiate custom features', () => { | ||
framework.initialize({ | ||
visitOperation(ctx) { | ||
expect(ctx.features.coercer).to.be.instanceof(CustomCoercer); | ||
expect(ctx.features.defaultSetter).to.be.instanceof( | ||
CustomDefaultSetter | ||
); | ||
expect(ctx.features.requestValidator).to.be.instanceof( | ||
CustomRequestValidator | ||
); | ||
expect(ctx.features.responseValidator).to.be.instanceof( | ||
CustomResponseValidator | ||
); | ||
expect(ctx.features.securityHandler).to.be.instanceof( | ||
CustomSecurityHandler | ||
); | ||
}, | ||
visitApi(ctx) { | ||
const apiDoc = ctx.getApiDoc(); | ||
expect(apiDoc.paths['/foo']).to.eql({ | ||
parameters: [], | ||
get: { | ||
parameters: [ | ||
{ | ||
name: 'name', | ||
in: 'query', | ||
type: 'string', | ||
default: 'elvis', | ||
}, | ||
], | ||
responses: { | ||
default: { | ||
description: 'return foo', | ||
schema: {}, | ||
}, | ||
}, | ||
security: [ | ||
{ | ||
basic: [], | ||
}, | ||
], | ||
}, | ||
}); | ||
}, | ||
}); | ||
}); | ||
}); |
8 changes: 8 additions & 0 deletions
8
packages/openapi-framework/test/sample-projects/paths-dir-with-default-features/apiDoc.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
swagger: '2.0' | ||
info: | ||
title: sample api doc | ||
version: '3' | ||
paths: {} | ||
securityDefinitions: | ||
basic: | ||
type: basic |
24 changes: 24 additions & 0 deletions
24
packages/openapi-framework/test/sample-projects/paths-dir-with-default-features/paths/foo.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module.exports = { | ||
GET, | ||
}; | ||
|
||
function GET() { | ||
return; | ||
} | ||
|
||
GET.apiDoc = { | ||
parameters: [ | ||
{ | ||
name: 'name', | ||
in: 'query', | ||
type: 'string', | ||
default: 'elvis', | ||
}, | ||
], | ||
responses: { | ||
default: { | ||
description: 'return foo', | ||
schema: {}, | ||
}, | ||
}, | ||
}; |
Oops, something went wrong.