@anvilco/anvil
Advanced tools
Comparing version 2.14.1 to 2.15.0
@@ -8,2 +8,6 @@ # Changelog | ||
## [v2.15.0] | ||
- Assert that the `filename` property is (likely) there [`312`](https://github.com/anvilco/node-anvil/pull/312) | ||
- Dependency updates. | ||
## [v2.14.1] | ||
@@ -10,0 +14,0 @@ - Dependency updates. |
{ | ||
"name": "@anvilco/anvil", | ||
"version": "2.14.1", | ||
"version": "2.15.0", | ||
"description": "Anvil API Client", | ||
@@ -60,3 +60,3 @@ "author": "Anvil Foundry Inc.", | ||
"@babel/eslint-parser": "^7.18.2", | ||
"@types/node": "^18.11.18", | ||
"@types/node": "^20.1.1", | ||
"auto-changelog": "^2.4.0", | ||
@@ -63,0 +63,0 @@ "bdd-lazy-var": "^2.5.4", |
@@ -364,3 +364,3 @@ ![Horizontal Lockupblack](https://user-images.githubusercontent.com/293079/169453889-ae211c6c-7634-4ccd-8ca9-8970c2621b6f.png#gh-light-mode-only) | ||
A nice helper to prepare a Stream-backed or Buffer-backed file upload for use with our GraphQL API. By default, this will upload your files as multipart uploads over the [jaydenseric / GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec). | ||
A nice helper to prepare a Stream-backed or Buffer-backed file upload for use with our GraphQL API. By default, this will upload your files as multipart uploads over the [jaydenseric / GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec). NOTE: Please see below about certain scenarios where you may need to manually provide a `filename`. | ||
@@ -367,0 +367,0 @@ * `pathOrStreamLikeThing` (String | Stream | Buffer) - An existing `Stream`, `Buffer` or other Stream-like thing supported by [FormData.append](https://github.com/form-data/form-data#void-append-string-field-mixed-value--mixed-options-) OR a string representing a fully resolved path to a file to be read into a new `Stream`. |
@@ -8,8 +8,10 @@ const path = require('path') | ||
// Local Dev | ||
const apiKey = 'AXZj09jvHRPNBFRHMnAF2jpwOx9J5Rb6' | ||
// const apiKey = 'AXZj09jvHRPNBFRHMnAF2jpwOx9J5Rb6' | ||
// Prod Mango Dev | ||
// const apiKey = 'oBU3hec1pdBRGB5NmSJaj7QQzhiME1u8' | ||
const apiKey = 'oBU3hec1pdBRGB5NmSJaj7QQzhiME1u8' | ||
// | ||
// const [eid, apiKey, jsonPath] = argv._ | ||
const baseURL = 'http://localhost:3000' | ||
let baseURL | ||
// baseURL = 'http://localhost:3000' | ||
// baseURL = 'https://outlook.office.com/webhook/' | ||
@@ -19,3 +21,3 @@ const constructorOptions = { | ||
// cookie: 'koa:sess=c2a23c802c7c9e09016378f74ef1ee1d; koa:sess.sig=PO0AfdOUgUcsLFhFLbaz7QgGncU', | ||
baseURL, | ||
// baseURL, | ||
// userAgent, | ||
@@ -22,0 +24,0 @@ debug: true, |
@@ -7,2 +7,4 @@ const { client, resolvePath, writeStream } = require('./base') | ||
console.log(client) | ||
const castEid = 'Opt5S8PjwKwnVSHdJDoj' | ||
@@ -9,0 +11,0 @@ |
@@ -112,2 +112,4 @@ const fs = require('fs') | ||
const FILENAME_IGNORE_MESSAGE = 'If you think you can ignore this, please pass `options.ignoreFilenameValidation` as `true`.' | ||
const failBufferMS = 50 | ||
@@ -207,6 +209,33 @@ | ||
*/ | ||
static prepareGraphQLFile (pathOrStreamLikeThing, formDataAppendOptions) { | ||
static prepareGraphQLFile (pathOrStreamLikeThing, { ignoreFilenameValidation, ...formDataAppendOptions } = {}) { | ||
if (typeof pathOrStreamLikeThing === 'string') { | ||
// @ts-ignore | ||
pathOrStreamLikeThing = fs.createReadStream(pathOrStreamLikeThing) | ||
} else if ( | ||
!formDataAppendOptions || | ||
( | ||
formDataAppendOptions && !( | ||
// Require the filename or the ignoreFilenameValidation option. | ||
formDataAppendOptions.filename || ignoreFilenameValidation | ||
) | ||
) | ||
) { | ||
// OK, there's a chance here that a `filename` needs to be provided via formDataAppendOptions | ||
if ( | ||
// Buffer has no way to get the filename | ||
pathOrStreamLikeThing instanceof Buffer || | ||
// Some stream things have the path in them | ||
!pathOrStreamLikeThing.path | ||
) { | ||
let message = 'For this type of input, `options.filename` must be provided to prepareGraphQLFile.' + ' ' + FILENAME_IGNORE_MESSAGE | ||
try { | ||
if (pathOrStreamLikeThing && pathOrStreamLikeThing.constructor && pathOrStreamLikeThing.constructor.name) { | ||
message = `When passing a ${pathOrStreamLikeThing.constructor.name} to prepareGraphQLFile, \`options.filename\` must be provided. ${FILENAME_IGNORE_MESSAGE}` | ||
} | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
throw new Error(message) | ||
} | ||
} | ||
@@ -213,0 +242,0 @@ |
@@ -896,2 +896,22 @@ const fs = require('fs') | ||
}) | ||
describe('prepareGraphQLFile', function () { | ||
it('works', function () { | ||
expect(() => | ||
Anvil.prepareGraphQLFile(Buffer.from('test')), | ||
).to.throw('When passing a Buffer to prepareGraphQLFile, `options.filename` must be provided. If you think you can ignore this, please pass `options.ignoreFilenameValidation` as `true`.') | ||
let uploadWithOptions = Anvil.prepareGraphQLFile(Buffer.from('test'), { ignoreFilenameValidation: true }) | ||
expect(uploadWithOptions).to.be.ok | ||
expect(uploadWithOptions.streamLikeThing).to.be.ok | ||
expect(uploadWithOptions.formDataAppendOptions).to.eql({}) | ||
uploadWithOptions = Anvil.prepareGraphQLFile(Buffer.from('test'), { filename: 'test.pdf' }) | ||
expect(uploadWithOptions).to.be.ok | ||
expect(uploadWithOptions.streamLikeThing).to.be.ok | ||
expect(uploadWithOptions.formDataAppendOptions).to.include({ | ||
filename: 'test.pdf', | ||
}) | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3379590
3834