Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
feat(swagger): add a swagger-js based client (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
silasbw authored Dec 22, 2018
1 parent 519c712 commit 036307d
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class Component {
component.templatedEndpoints = null
}

component.operationIds = {}
component.parameters = options.parameters
component.template = options.template
component.splits = options.splits.slice()
Expand Down Expand Up @@ -206,6 +207,7 @@ class Component {
Object.keys(endpoint.pathItem)
.filter(key => endpoint.pathItem[key].operationId)
.forEach(method => {
component.operationIds[method] = endpoint.pathItem[method].operationId
component[method] = component['_' + method]
if (method === 'get') component.getStream = component._getStream
})
Expand All @@ -218,8 +220,10 @@ class Component {
* @returns {(Promise|Stream)} Promise
*/
_requestAsync (method, options) {
const operationId = this.operationIds[method.toLowerCase()]
return this.http.http(Object.assign({
method,
operationId,
pathname: this.getPath(),
pathnameParameters: this.parameters
}, options))
Expand Down
3 changes: 3 additions & 0 deletions lib/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ describe('list.loader', () => {
'/foo/bar/': {
get: {
operationId: 'fooBarGet'
},
post: {
operationId: 'fooBarPost'
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions lib/swagger-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

class SwaggerClient {
/**
* {@link https://github.com/swagger-api/swagger-js Swagger Client} client
* @param {object} options
* @param {function} options.client - Instance of a Swagger.
*/
constructor ({ client }) {
this.client = client
}

/**
* Invoke API request.
* @param {object} options - options object.
* @param {object} options.body - JSONifable object or undefined.
* @param {string} options.method - HTTP method.
* @param {string} optoins.operationId - Swagger/OpenAPI operation ID.
* @param {object} options.parameters - named query parameters.
* @param {object} options.qs - named query parameters (legacy).
* @param {string} options.pathname - URL pathname.
* @param {boolean} options.stream - true if called by a "stream method".
*/
http (options) {
const parameters = Object.assign(
{},
options.body,
options.qs || options.parameters,
options.pathnameParameters
)

return this.client.execute({
operationId: options.operationId,
parameters
})
}
}

module.exports = SwaggerClient
123 changes: 123 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"mocha": "^5.2.0",
"nock": "^10.0.4",
"node-fetch": "^2.3.0",
"sinon": "^7.2.2",
"standard": "^12.0.1",
"standard-version": "^4.4.0"
},
Expand Down

0 comments on commit 036307d

Please sign in to comment.