Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit 8d4b3d8

Browse files
authored
Merge pull request #66 from netlify/feature/improve-open-api-logic
Improve OpenAPI logic
2 parents 0834a4c + be3ce94 commit 8d4b3d8

File tree

12 files changed

+62
-230
lines changed

12 files changed

+62
-230
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const client = new NetlifyAPI('1234myAccessToken')
1313
const sites = await client.listSites()
1414
```
1515

16-
## Using Open API methods
16+
## Using Open API operations
1717

1818
```js
1919
const NetlifyAPI = require('netlify')
@@ -67,9 +67,9 @@ A getter that returns the formatted base URL of the endpoint the client is confi
6767

6868
### Open API Client methods
6969

70-
The client is dynamically generated from the [open-api](https://github.com/netlify/open-api) definition file. Each method is is named after the `operationId` name of each endpoint action. **To see list of available operations see the [open-api website](https://open-api.netlify.com/)**.
70+
The client is dynamically generated from the [open-api](https://github.com/netlify/open-api) definition file. Each method is is named after the `operationId` name of each operation. **To see list of available operations see the [open-api website](https://open-api.netlify.com/)**.
7171

72-
Every open-api method has the following signature:
72+
Every open-api operation has the following signature:
7373

7474
#### `promise(response) = client.operationId([params], [opts])`
7575

@@ -103,7 +103,7 @@ Optional `opts` can include any property you want passed to `node-fetch`. The `h
103103
}
104104
```
105105

106-
All methods are conveniently consumed with async/await:
106+
All operations are conveniently consumed with async/await:
107107

108108
```js
109109
async function getSomeData() {
@@ -123,7 +123,7 @@ If the request response includes `json` in the `contentType` header, fetch will
123123

124124
### API Flow Methods
125125

126-
Some methods have been added in addition to the open API methods that make certain actions simpler to perform.
126+
Some methods have been added in addition to the open API operations that make certain actions simpler to perform.
127127

128128
#### `promise(accessToken) = client.getAccessToken(ticket, [opts])`
129129

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"lodash.set": "^4.3.2",
4242
"micro-api-client": "^3.3.0",
4343
"node-fetch": "^2.2.0",
44+
"omit.js": "^1.0.2",
4445
"p-map": "^2.1.0",
4546
"p-wait-for": "^2.0.0",
4647
"parallel-transform": "^1.1.0",

src/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const dfn = require('@netlify/open-api')
44
const pWaitFor = require('p-wait-for')
55
const debug = require('debug')('netlify')
66

7-
const { methods, generateMethod } = require('./open-api')
7+
const { generateOperation } = require('./open-api')
8+
const { getOperations } = require('./operations')
89
const deploy = require('./deploy')
910

1011
class NetlifyAPI {
@@ -107,12 +108,13 @@ class NetlifyAPI {
107108
}
108109
}
109110

110-
methods.forEach(method => {
111+
const operations = getOperations()
112+
operations.forEach(operation => {
111113
// Generate open-api methods
112114
/* {param1, param2, body, ... }, [opts] */
113-
NetlifyAPI.prototype[method.operationId] = generateMethod(method)
115+
NetlifyAPI.prototype[operation.operationId] = generateOperation(operation)
114116
})
115117

116118
module.exports = NetlifyAPI
117119

118-
module.exports.methods = methods
120+
module.exports.methods = operations
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,20 @@ const { JSONHTTPError, TextHTTPError } = require('micro-api-client')
1010
const debug = require('debug')('netlify:open-api')
1111
const isStream = require('is-stream')
1212

13-
const { sleep, unixNow } = require('./util')
14-
15-
exports.methods = require('./shape-swagger')
16-
1713
// open-api 2.0
18-
exports.generateMethod = method => {
14+
const generateOperation = function(operation) {
1915
//
2016
// Warning: Expects `this`. These methods expect to live on the client prototype
2117
//
2218
return async function(params, opts) {
2319
opts = Object.assign({}, opts)
2420
params = Object.assign({}, this.globalParams, params)
2521

26-
let path = this.basePath + method.path
22+
let path = this.basePath + operation.path
2723
debug(`path template: ${path}`)
2824

2925
// Path parameters
30-
Object.values(method.parameters.path).forEach(param => {
26+
Object.values(operation.parameters.path).forEach(param => {
3127
const val = params[param.name] || params[camelCase(param.name)]
3228
if (val != null) {
3329
path = path.replace(`{${param.name}}`, val)
@@ -39,7 +35,7 @@ exports.generateMethod = method => {
3935

4036
// qs parameters
4137
let qs
42-
Object.values(method.parameters.query).forEach(param => {
38+
Object.values(operation.parameters.query).forEach(param => {
4339
const val = params[param.name] || params[camelCase(param.name)]
4440
if (val != null) {
4541
if (!qs) qs = {}
@@ -58,7 +54,7 @@ exports.generateMethod = method => {
5854
let bodyType = 'json'
5955
if (params.body) {
6056
body = params.body
61-
Object.values(method.parameters.body).forEach(param => {
57+
Object.values(operation.parameters.body).forEach(param => {
6258
const type = get(param, 'schema.format')
6359
if (type === 'binary') {
6460
bodyType = 'binary'
@@ -86,8 +82,8 @@ exports.generateMethod = method => {
8682
debug('specialHeaders: %O', specialHeaders)
8783

8884
opts.headers = new Headers(Object.assign({}, this.defaultHeaders, specialHeaders, opts.headers))
89-
opts.method = method.verb.toUpperCase()
90-
debug(`method: ${opts.method}`)
85+
opts.method = operation.verb.toUpperCase()
86+
debug(`HTTP method: ${opts.method}`)
9187

9288
// TODO: Consider using micro-api-client when it supports node-fetch
9389

@@ -171,3 +167,9 @@ exports.generateMethod = method => {
171167
return text
172168
}
173169
}
170+
171+
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
172+
173+
const unixNow = () => Math.floor(new Date() / 1000)
174+
175+
module.exports = { generateOperation }

src/open-api/index.test.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/open-api/index.test.js.snap

-17 KB
Binary file not shown.

src/open-api/shape-swagger.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/open-api/util.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/operations.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const { paths } = require('@netlify/open-api')
2+
const omit = require('omit.js')
3+
4+
// Retrieve all OpenAPI operations
5+
const getOperations = function() {
6+
return [].concat(
7+
...Object.entries(paths).map(([path, pathItem]) => {
8+
const operations = omit(pathItem, ['parameters'])
9+
return Object.entries(operations).map(([method, operation]) => {
10+
const parameters = getParameters(pathItem.parameters, operation.parameters)
11+
return Object.assign({}, operation, { verb: method, path, parameters })
12+
})
13+
})
14+
)
15+
}
16+
17+
const getParameters = function(pathParameters = [], operationParameters = []) {
18+
const parameters = [...pathParameters, ...operationParameters]
19+
return parameters.reduce(addParameter, { path: {}, query: {}, body: {} })
20+
}
21+
22+
const addParameter = function(parameters, param) {
23+
return Object.assign({}, parameters, {
24+
[param.in]: Object.assign({}, parameters[param.in], { [param.name]: param })
25+
})
26+
}
27+
28+
module.exports = { getOperations }

src/operations.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const test = require('ava')
2+
3+
const { getOperations } = require('./operations')
4+
5+
test('Exported methods', t => {
6+
t.snapshot(getOperations())
7+
})

0 commit comments

Comments
 (0)