@anvilco/anvil
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -10,6 +10,8 @@ # Changelog | ||
## [v2.0.0](https://github.com/anvilco/node-anvil/compare/v1.0.3...v2.0.0) - 2020-03-20 | ||
## [v2.1.0](https://github.com/anvilco/node-anvil/compare/v1.0.3...v2.1.0) - 2020-08-20 | ||
### Merged | ||
- replace `request` with `node-fetch` [`#13`](https://github.com/anvilco/node-anvil/pull/13) | ||
- Bump lodash from 4.17.15 to 4.17.19 [`#12`](https://github.com/anvilco/node-anvil/pull/12) | ||
- Add back in the dirName and add ability to autogen CHANGELOG [`#11`](https://github.com/anvilco/node-anvil/pull/11) | ||
@@ -25,3 +27,3 @@ - Use module.exports instead of import/export [`#10`](https://github.com/anvilco/node-anvil/pull/10) | ||
- bump deps to get minimist to >= 1.2.2 [`26e4cd1`](https://github.com/anvilco/node-anvil/commit/26e4cd1d9d2f3070309f85f3fb21e5f7cab484fa) | ||
- add changelog autogen capability [`e7a55ea`](https://github.com/anvilco/node-anvil/commit/e7a55eaeb4043557000d11a79c681445b42aa5ec) | ||
- added node-fetch. re-wrote to work with node-fetch. added support for stream. added example for it [`69b4d80`](https://github.com/anvilco/node-anvil/commit/69b4d80a1703e840b3c055c47c1af5f1ebb98799) | ||
@@ -28,0 +30,0 @@ ## [v1.0.3](https://github.com/anvilco/node-anvil/compare/v1.0.2...v1.0.3) - 2020-02-05 |
{ | ||
"name": "@anvilco/anvil", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Anvil API Client", | ||
@@ -53,3 +53,3 @@ "main": "src/index.js", | ||
"limiter": "^1.1.5", | ||
"request": "^2.88.0" | ||
"node-fetch": "^2.6.0" | ||
}, | ||
@@ -56,0 +56,0 @@ "resolutions": { |
@@ -45,3 +45,3 @@ # Anvil API Client for Node | ||
// Data will be the filled PDF raw byes | ||
// Data will be the filled PDF raw bytes | ||
fs.writeFileSync('output.pdf', data, { encoding: null }) | ||
@@ -52,14 +52,24 @@ ``` | ||
### new Anvil({ apiKey }) | ||
### new Anvil(options) | ||
Creates an Anvil client instance. | ||
* `apiKey` (String) - your API key from your Anvil organization settings | ||
* `options` (Object) - [Options](#options) for the Anvil Client instance. | ||
```js | ||
const anvilClient = new Anvil({ apiKey }) | ||
const anvilClient = new Anvil({ apiKey: 'abc123' }) | ||
``` | ||
### Anvil::fillPDF(pdfTemplateID, payload) | ||
### Options | ||
Options for the Anvil Client. Defaults are shown after each option key. | ||
```js | ||
{ | ||
apiKey: <your_api_key> // Required. Your API key from your Anvil organization settings | ||
} | ||
``` | ||
### Anvil::fillPDF(pdfTemplateID, payload[, options]) | ||
Fills a PDF with your JSON data. | ||
@@ -87,4 +97,8 @@ | ||
} | ||
// The 'options' parameter is optional | ||
const options = { | ||
"dataType": "buffer" | ||
} | ||
const anvilClient = new Anvil({ apiKey }) | ||
const { statusCode, data } = await anvilClient.fillPDF(pdfTemplateID, payload) | ||
const { statusCode, data } = await anvilClient.fillPDF(pdfTemplateID, payload, options) | ||
``` | ||
@@ -99,6 +113,7 @@ | ||
* For example `{ "someFieldId": "Hello World!" }` | ||
} | ||
* `options` (Object) - _optional_ Any additional options for the request | ||
* `dataType` (Enum[String]) - _optional_ Set the type of the `data` value that is returned in the resolved `Promise`. Defaults to `'buffer'`, but `'stream'` is also supported. | ||
* Returns a `Promise` that resolves to an `Object` | ||
* `statusCode` (Number) - the HTTP status code; `200` is success | ||
* `data` (Buffer) - The raw binary data of the filled PDF if success | ||
* `data` (Buffer | Stream) - The raw binary data of the filled PDF if success. Will be either a Buffer or a Stream, depending on `dataType` option supplied to the request. | ||
* `errors` (Array of Objects) - Will be present if status >= 400. See Errors | ||
@@ -105,0 +120,0 @@ * `message` (String) |
120
src/index.js
@@ -1,6 +0,13 @@ | ||
const request = require('request') | ||
const fetch = require('node-fetch') | ||
const RateLimiter = require('limiter').RateLimiter | ||
const { version, description } = require('../package.json') | ||
const DATA_TYPE_STREAM = 'stream' | ||
const DATA_TYPE_BUFFER = 'buffer' | ||
const DATA_TYPE_JSON = 'json' | ||
const defaultOptions = { | ||
baseURL: 'https://app.useanvil.com', | ||
userAgent: `${description}/${version}`, | ||
} | ||
@@ -15,8 +22,10 @@ | ||
// baseURL: 'https://app.useanvil.com' | ||
// userAgent: 'Anvil API Client/2.0.0' | ||
// } | ||
constructor (options) { | ||
if (!options) throw new Error('options are required') | ||
this.options = Object.assign({}, defaultOptions, options) | ||
if (!options.apiKey && !options.accessToken) throw new Error('apiKey or accessToken required') | ||
this.options = Object.assign({}, defaultOptions, options) | ||
const { apiKey, accessToken } = this.options | ||
@@ -33,9 +42,24 @@ this.authHeader = accessToken | ||
fillPDF (pdfTemplateID, payload) { | ||
return this.requestREST(`/api/v1/fill/${pdfTemplateID}.pdf`, { | ||
method: 'POST', | ||
json: payload, | ||
encoding: null, | ||
headers: { Authorization: this.authHeader }, | ||
}) | ||
fillPDF (pdfTemplateID, payload, clientOptions = {}) { | ||
const supportedDataTypes = [DATA_TYPE_STREAM, DATA_TYPE_BUFFER] | ||
const { dataType = DATA_TYPE_BUFFER } = clientOptions | ||
if (dataType && !supportedDataTypes.includes(dataType)) { | ||
throw new Error(`dataType must be one of: ${supportedDataTypes.join('|')}`) | ||
} | ||
return this.requestREST( | ||
`/api/v1/fill/${pdfTemplateID}.pdf`, | ||
{ | ||
method: 'POST', | ||
body: JSON.stringify(payload), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: this.authHeader, | ||
}, | ||
}, | ||
{ | ||
...clientOptions, | ||
dataType, | ||
}, | ||
) | ||
} | ||
@@ -45,19 +69,35 @@ | ||
async requestREST (url, options) { | ||
const optionsWithURL = { | ||
...options, | ||
url: this.url(url), | ||
} | ||
async requestREST (url, options, clientOptions = {}) { | ||
return this.throttle(async (retry) => { | ||
const response = await this.request(url, options) | ||
const statusCode = response.status | ||
return this.throttle(async (retry) => { | ||
const { response, data } = await this.requestPromise(optionsWithURL) | ||
const statusCode = response.statusCode | ||
if (statusCode === 429) { | ||
return retry(getRetryMS(response.headers['retry-after'])) | ||
return retry(getRetryMS(response.headers.get('retry-after'))) | ||
} | ||
if (statusCode >= 300) { | ||
const isObject = data && data.constructor.name === 'Object' | ||
if (isObject && data.errors) return { statusCode, ...data } | ||
else if (isObject && data.message) return { statusCode, errors: [data] } | ||
const json = await response.json() | ||
const errors = json.errors || (json.message && [json]) | ||
return errors ? { statusCode, errors } : { statusCode, ...json } | ||
} | ||
const { dataType } = clientOptions | ||
let data | ||
switch (dataType) { | ||
case DATA_TYPE_JSON: | ||
data = await response.json() | ||
break | ||
case DATA_TYPE_STREAM: | ||
data = response.body | ||
break | ||
case DATA_TYPE_BUFFER: | ||
data = await response.buffer() | ||
break | ||
default: | ||
data = await response.buffer() | ||
break | ||
} | ||
return { statusCode, data } | ||
@@ -87,18 +127,34 @@ }) | ||
requestPromise (options) { | ||
return new Promise((resolve, reject) => { | ||
this.request(options, function (error, response, data) { | ||
if (error) return reject(error) | ||
resolve({ response, data }) | ||
}) | ||
}) | ||
request (url, options) { | ||
if (!url.startsWith(this.options.baseURL)) { | ||
url = this.url(url) | ||
} | ||
const opts = this.addDefaultHeaders(options) | ||
return fetch(url, opts) | ||
} | ||
request (options, cb) { | ||
return request(options, cb) | ||
} | ||
url (path) { | ||
return this.options.baseURL + path | ||
} | ||
addHeaders ({ options: existingOptions, headers: newHeaders }) { | ||
const { headers: existingHeaders = {} } = existingOptions | ||
return { | ||
...existingOptions, | ||
headers: { | ||
...existingHeaders, | ||
...newHeaders, | ||
}, | ||
} | ||
} | ||
addDefaultHeaders (options) { | ||
const { userAgent } = this.options | ||
return this.addHeaders({ | ||
options, | ||
headers: { | ||
'User-Agent': userAgent, | ||
}, | ||
}) | ||
} | ||
} | ||
@@ -105,0 +161,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
47898
10
372
155
1
3
+ Addednode-fetch@^2.6.0
+ Addednode-fetch@2.7.0(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-url@5.0.0(transitive)
- Removedrequest@^2.88.0
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.15.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)