balena-request
Advanced tools
Comparing version 12.1.0-build-otaviojacobi-does-multipart-form-when-blob-is-present-c55aae989843200f9a73b9e5a84c42643c31767d-1 to 12.1.0-build-otaviojacobi-does-multipart-form-when-blob-is-present-d04064548b2eddb6dacc3589c3b5ac0c4dbf1127-1
@@ -98,24 +98,21 @@ "use strict"; | ||
const interceptRequestOrError = (initialPromise) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
return exports.interceptors.reduce(function (promise, { request, requestError }) { | ||
let promise = initialPromise; | ||
for (const { request, requestError } of exports.interceptors) { | ||
if (request != null || requestError != null) { | ||
return promise.then(request, requestError); | ||
promise = promise.then(request, requestError); | ||
} | ||
else { | ||
return promise; | ||
} | ||
}, initialPromise); | ||
} | ||
return promise; | ||
}); | ||
const interceptResponseOrError = function (initialPromise) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
return exports.interceptors | ||
let promise = initialPromise; | ||
for (const { response, responseError } of exports.interceptors | ||
.slice() | ||
.reverse() | ||
.reduce(function (promise, { response, responseError }) { | ||
.reverse()) { | ||
if (response != null || responseError != null) { | ||
return promise.then(response, responseError); | ||
promise = promise.then(response, responseError); | ||
} | ||
else { | ||
return promise; | ||
} | ||
}, initialPromise); | ||
} | ||
return promise; | ||
}); | ||
@@ -122,0 +119,0 @@ }; |
@@ -24,4 +24,5 @@ "use strict"; | ||
const errors = require("balena-errors"); | ||
const token_1 = require("balena-auth/build/token"); | ||
const balena_auth_1 = require("balena-auth"); | ||
const NodeFormData = require("form-data"); | ||
const buffer_1 = require("buffer"); | ||
const IS_BROWSER = typeof window !== 'undefined' && window !== null; | ||
@@ -57,3 +58,3 @@ /** | ||
const type = yield auth.getType(); | ||
if (type !== token_1.TokenType.JWT) { | ||
if (type !== balena_auth_1.TokenType.JWT) { | ||
return false; | ||
@@ -320,11 +321,8 @@ } | ||
exports.getBody = getBody; | ||
const isBlob = (value) => { | ||
return ((typeof Blob !== 'undefined' && value instanceof Blob) || | ||
(typeof buffer_1.Blob !== 'undefined' && value instanceof buffer_1.Blob)); | ||
}; | ||
const hasBlob = (requestBody) => { | ||
if (requestBody) { | ||
for (const value of Object.values(requestBody)) { | ||
if (typeof Blob !== 'undefined' && value instanceof Blob) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
return Object.values(requestBody !== null && requestBody !== void 0 ? requestBody : {}).some(isBlob); | ||
}; | ||
@@ -354,2 +352,3 @@ // This is the actual implementation that hides the internal `retriesRemaining` parameter | ||
} | ||
opts.headers.delete('Content-Type'); | ||
opts.body = form; | ||
@@ -360,3 +359,3 @@ } | ||
for (const [k, v] of Object.entries(options.body)) { | ||
if (v instanceof Blob) { | ||
if (v instanceof buffer_1.Blob) { | ||
form.append(k, Buffer.from(yield v.arrayBuffer()), { | ||
@@ -363,0 +362,0 @@ filename: 'upload', |
@@ -7,6 +7,15 @@ # Change Log | ||
## 12.1.0 - 2023-08-08 | ||
## 12.1.0 - 2023-09-28 | ||
* Add multi part request support when Blob is present [Otávio Jacobi] | ||
## 12.0.4 - 2023-08-23 | ||
* Refactor the interceptors to stop using .reduce() [Thodoris Greasidis] | ||
## 12.0.3 - 2023-08-09 | ||
* Avoid deep imports from balena-auth [Thodoris Greasidis] | ||
* Update balena-auth to 5.1.0 [Thodoris Greasidis] | ||
## 12.0.2 - 2023-07-25 | ||
@@ -13,0 +22,0 @@ |
@@ -192,10 +192,11 @@ /* | ||
const interceptRequestOrError = async (initialPromise: Promise<any>) => | ||
exports.interceptors.reduce(function (promise, { request, requestError }) { | ||
const interceptRequestOrError = async (initialPromise: Promise<any>) => { | ||
let promise = initialPromise; | ||
for (const { request, requestError } of exports.interceptors) { | ||
if (request != null || requestError != null) { | ||
return promise.then(request, requestError); | ||
} else { | ||
return promise; | ||
promise = promise.then(request, requestError); | ||
} | ||
}, initialPromise); | ||
} | ||
return promise; | ||
}; | ||
@@ -205,12 +206,11 @@ const interceptResponseOrError = async function ( | ||
) { | ||
return exports.interceptors | ||
let promise = initialPromise; | ||
for (const { response, responseError } of exports.interceptors | ||
.slice() | ||
.reverse() | ||
.reduce(function (promise, { response, responseError }) { | ||
if (response != null || responseError != null) { | ||
return promise.then(response, responseError); | ||
} else { | ||
return promise; | ||
} | ||
}, initialPromise); | ||
.reverse()) { | ||
if (response != null || responseError != null) { | ||
promise = promise.then(response, responseError); | ||
} | ||
} | ||
return promise; | ||
}; | ||
@@ -217,0 +217,0 @@ |
@@ -25,5 +25,6 @@ /* | ||
import type BalenaAuth from 'balena-auth'; | ||
import { TokenType } from 'balena-auth/build/token'; | ||
import { TokenType } from 'balena-auth'; | ||
import type { BalenaRequestOptions, BalenaRequestResponse } from './request'; | ||
import * as NodeFormData from 'form-data'; | ||
import { Blob as BufferBlob } from 'buffer'; | ||
@@ -377,11 +378,11 @@ const IS_BROWSER = typeof window !== 'undefined' && window !== null; | ||
const isBlob = (value: any) => { | ||
return ( | ||
(typeof Blob !== 'undefined' && value instanceof Blob) || | ||
(typeof BufferBlob !== 'undefined' && value instanceof BufferBlob) | ||
); | ||
}; | ||
const hasBlob = (requestBody?: any) => { | ||
if (requestBody) { | ||
for (const value of Object.values(requestBody)) { | ||
if (typeof Blob !== 'undefined' && value instanceof Blob) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
return Object.values(requestBody ?? {}).some(isBlob); | ||
}; | ||
@@ -418,2 +419,3 @@ | ||
} | ||
opts.headers.delete('Content-Type'); | ||
opts.body = form; | ||
@@ -423,3 +425,3 @@ } else { | ||
for (const [k, v] of Object.entries(options.body)) { | ||
if (v instanceof Blob) { | ||
if (v instanceof BufferBlob) { | ||
form.append(k, Buffer.from(await v.arrayBuffer()), { | ||
@@ -426,0 +428,0 @@ filename: 'upload', |
{ | ||
"name": "balena-request", | ||
"version": "12.1.0-build-otaviojacobi-does-multipart-form-when-blob-is-present-c55aae989843200f9a73b9e5a84c42643c31767d-1", | ||
"version": "12.1.0-build-otaviojacobi-does-multipart-form-when-blob-is-present-d04064548b2eddb6dacc3589c3b5ac0c4dbf1127-1", | ||
"description": "Balena HTTP client", | ||
@@ -43,2 +43,3 @@ "main": "build/request.js", | ||
"@types/mocha": "^9.1.1", | ||
"@types/node": "^16.0.0", | ||
"@types/progress-stream": "^2.0.0", | ||
@@ -48,3 +49,3 @@ "@types/qs": "^6.9.3", | ||
"assert": "^2.0.0", | ||
"balena-auth": "^5.0.0", | ||
"balena-auth": "^5.1.0", | ||
"balena-config-karma": "4.0.0", | ||
@@ -83,7 +84,7 @@ "bluebird": "^3.7.2", | ||
"peerDependencies": { | ||
"balena-auth": "^5.0.0" | ||
"balena-auth": "^5.1.0" | ||
}, | ||
"versionist": { | ||
"publishedAt": "2023-08-08T01:55:13.858Z" | ||
"publishedAt": "2023-09-28T17:17:49.005Z" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
215439
30
4112