New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@anvilco/anvil

Package Overview
Dependencies
Maintainers
5
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@anvilco/anvil - npm Package Compare versions

Comparing version 2.0.0 to 2.1.0

dummy.pdf

6

CHANGELOG.md

@@ -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)

@@ -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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc