-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for OpenAPI 3 #181
Draft
tpburch
wants to merge
15
commits into
krakenjs:master
Choose a base branch
from
tpburch:oas3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+5,101
−1,601
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1000716
Initial refactor into api mapper
0a84173
added test module for api-dto-mapper
0f05b0d
Initial refactor into api mapper
7b8abf3
added test module for api-dto-mapper
2680479
update Routes to use apiDto
ef89d87
Merge branch 'oas3' of https://github.com/tpburch/hapi-openapi into oas3
96a2f06
add oas3 mapper
8b5afc5
'clean up local file changes'
3cec211
change private field to underscored field
e77efac
update eslint ecmaVersion and fix lint errors
20c1b1b
Branch fixtures and update tests
d5de80b
remove unused variable
e5d64c2
add oas3 test for validation special cases
5b8bfef
handle full url for oas3 server
3c4a798
handle oas3 http auth schemes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
package-lock=false | ||
package-lock=true | ||
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 |
---|---|---|
|
@@ -15,4 +15,4 @@ | |
"console": "internalConsole" | ||
} | ||
] | ||
} | ||
} |
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,48 @@ | ||
'use strict'; | ||
|
||
const Oas2Strategy = require('./mapping-strategies/oas2'); | ||
const Oas3Strategy = require('./mapping-strategies/oas3'); | ||
|
||
class ApiDto { | ||
constructor(spec, baseDir, mappingStrategy) { | ||
|
||
this.baseDir = baseDir; | ||
this.spec = spec; | ||
this._mappingStrategy = mappingStrategy; | ||
} | ||
|
||
get basePath() { | ||
|
||
return this._mappingStrategy.getBasePath.call(this); | ||
} | ||
|
||
get customAuthSchemes() { | ||
|
||
return this._mappingStrategy.getCustomAuthSchemes.call(this); | ||
} | ||
|
||
get customAuthStrategies() { | ||
|
||
return this._mappingStrategy.getCustomAuthStrategies.call(this); | ||
} | ||
|
||
get operations() { | ||
|
||
return this._mappingStrategy.getOperations.call(this); | ||
} | ||
|
||
getOperation(method, path) { | ||
|
||
return this.operations.find((op) => op.method === method && op.path === path); | ||
} | ||
} | ||
|
||
const toDto = (spec, baseDir) => { | ||
|
||
const mappingStrategy = spec.swagger ? Oas2Strategy : Oas3Strategy; | ||
return new ApiDto(spec, baseDir, mappingStrategy); | ||
}; | ||
|
||
module.exports = { | ||
toDto | ||
}; |
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
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,77 @@ | ||
'use strict'; | ||
|
||
const Path = require('path'); | ||
const Utils = require('../../lib/utils'); | ||
|
||
const getBasePath = function () { | ||
|
||
return Utils.unsuffix(Utils.prefix(this.spec.basePath || '/', '/'), '/'); | ||
}; | ||
|
||
const getCustomAuthSchemes = function () { | ||
|
||
const schemes = this.spec['x-hapi-auth-schemes'] || {}; | ||
|
||
return Object.entries(schemes) | ||
.map(([scheme, pathToScheme]) => ({ scheme, path: Path.join(this.baseDir, pathToScheme) }) | ||
); | ||
}; | ||
|
||
const getCustomAuthStrategies = function () { | ||
|
||
const strategies = this.spec.securityDefinitions || {}; | ||
|
||
return Object.entries(strategies) | ||
.filter(([, config]) => config['x-hapi-auth-strategy']) | ||
.map(([strategy, { 'x-hapi-auth-strategy': pathToStrategy, ...rest }]) => | ||
({ | ||
strategy, | ||
config: { | ||
...rest, | ||
path: Path.join(this.baseDir, pathToStrategy) | ||
} | ||
}) | ||
); | ||
}; | ||
|
||
const mapSecurity = function (security = []) { | ||
|
||
return security.flatMap((auth) => | ||
|
||
Object.entries(auth).map(([strategy, scopes]) => ({ strategy, scopes })) | ||
); | ||
}; | ||
|
||
const getOperations = function () { | ||
|
||
const globalSecurity = this.spec.security; | ||
const paths = this.spec.paths || {}; | ||
|
||
return Object.entries(paths).flatMap(([path, operations]) => | ||
|
||
Object.entries(operations) | ||
.filter(([method]) => Utils.isHttpMethod(method)) | ||
.map(([method, operation]) => ({ | ||
path, | ||
method, | ||
description: operation.description, | ||
operationId: operation.operationId, | ||
tags: operation.tags, | ||
security: mapSecurity(operation.security || globalSecurity), | ||
mediaTypes: { | ||
request: operation.consumes || this.spec.consumes | ||
}, | ||
parameters: [...(operations.parameters || []), ...(operation.parameters || [])], | ||
handler: operations['x-hapi-handler'] && Path.join(this.baseDir, operations['x-hapi-handler']), | ||
responses: operation.responses, | ||
customOptions: operation['x-hapi-options'] | ||
})) | ||
); | ||
}; | ||
|
||
module.exports = { | ||
getBasePath, | ||
getCustomAuthSchemes, | ||
getCustomAuthStrategies, | ||
getOperations | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can just remove this file, this is the default. Never seen a project with a .npmrc file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have create a new branch
next
to land changes in.In the case of this particular
.npmrc
file, I think it can be removed. Checking one in can be useful at times.