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

Commit

Permalink
Use published spec
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Jul 26, 2018
1 parent 41b9dad commit e0f50c7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2,044 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
"test": "ava --serial --verbose"
},
"homepage": "https://github.com/netlify/node-client",
"main": "lib/index.js",
"main": "src/index.js",
"dependencies": {
"@netlify/open-api": "^0.1.0",
"js-yaml": "^3.12.0",
"lodash.camelcase": "^4.3.0",
"lodash.flatten": "^4.4.0",
Expand Down
32 changes: 29 additions & 3 deletions src/generate-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = method => {
Object.values(method.parameters.path).forEach(param => {
const val = params[param.name] || params[camelCase(param.name)]
if (existy(val)) {
path.replace(`{${param.name}}`, val)
path = path.replace(`{${param.name}}`, val)
} else if (param.required) {
throw new Error(`Missing required param ${param.name}`)
}
Expand All @@ -34,18 +34,44 @@ module.exports = method => {
if (existy(val)) {
if (!qs) qs = {}
qs[param.name] = val
} else if (param.required) {
throw new Error(`Missing required param ${param.name}`)
}
})
if (qs) path = path += `?${queryString.stringify(qs)}`

let body

if (params.body) {
body = params.body
Object.values(method.parameters.body).forEach(param => {
const type = get(param, 'schema.format')
if (type === 'binary') {
opts.headers['Content-Type'] = 'application/octet-stream'
}
})
}
delete params.body

opts.headers = Object.assign({}, this.defaultHeaders, opts.headers)

if (body) opts.json = body
return await r2[method.verb](path, opts).response
const req = await r2[method.verb](path, opts)
const response = await req.response

if (response.status >= 400) {
const err = new Error(response.statusText)
err.status = response.status
err.response = response
err.path = path
err.opts = opts
throw err
}
const status = {
status: response.status,
statusText: response.statusText
}
const json = await req.json
Object.setPrototypeOf(json, status)
return json
}
}
31 changes: 0 additions & 31 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,7 @@
const NetlifyApiBase = require('./base')
const { methods } = require('./load-swagger')
const request = require('request')

class NetlifyApi extends NetlifyApiBase {
// Override to support streaming file reading
async uploadDeployFile(deployId, path, readStream) {
const reqOpts = {
url: methods.uploadDeployFile.path.replace('{deploy_id}', deployId).replace('{path}', path),
headers: Object.assign({}, this.defaultHeaders, {
'Content-Type': 'application/octet-stream'
}),
body: readStream
}

return new Promise((resolve, reject) => {
request.put(reqOpts, (err, res, body) => {
if (err) return reject(err)

if (res.statusCode >= 400) {
const apiError = new Error('There was an error with one of the file uploads')
apiError.response = res
return reject(apiError)
}

try {
body = JSON.parse(body)
} catch (_) {
// Ignore if body can't parse
}

resolve({ res, body })
})
})
}
}

module.exports = NetlifyApi
10 changes: 4 additions & 6 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ test.serial('can make basic requests', async t => {

await server.listen(port)

const response = await client.createTicket()
const json = await response.json()
t.is(response.status, 200)
t.deepEqual(json, { foo: 'bar' })
const body = await client.createTicket()
t.is(body.status, 200)
t.deepEqual(body, { foo: 'bar' })

await server.close()
})
Expand All @@ -54,9 +53,8 @@ test.serial('can make requests with a body', async t => {
another: 'one'
}
})
const json = await response.json()
t.is(response.status, 200)
t.deepEqual(json, { foo: 'bar' })
t.deepEqual(response, { foo: 'bar' })

await server.close()
})
Expand Down
6 changes: 1 addition & 5 deletions src/load-swagger.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
const fs = require('fs')
const path = require('path')
const yaml = require('js-yaml')
const swaggerYaml = fs.readFileSync(path.join(__dirname, 'swagger.yml'), 'utf8')
const dfn = yaml.safeLoad(swaggerYaml)
const dfn = require('@netlify/open-api')
const { sortParams, mergeParams } = require('./util')
const methods = []

Expand Down
Loading

0 comments on commit e0f50c7

Please sign in to comment.