diff --git a/CHANGELOG.md b/CHANGELOG.md index ade24d4e7..319e21bb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,7 +100,7 @@ The first public alpha release. * Low-level request wrappers for `/base/` and `/info/` paths of Upload API. * `UploadClient` class with settings and `fileFrom` method that supports only direct uploads now. -* Support of following Uploadcare Settings: `publicKey`, `baseUrl`, +* Support of following Uploadcare Settings: `publicKey`, `baseUrl`, `doNotStore`, `integration`, `secureExpire`, `secureSignature`. * Test environment for both Node.js and browsers diff --git a/src/api/request.ts b/src/api/request.ts index 8af2a439e..db293b5d1 100644 --- a/src/api/request.ts +++ b/src/api/request.ts @@ -9,6 +9,7 @@ import {FileData, Settings} from '../types' import {BaseProgress} from './base' import {Thenable} from '../tools/Thenable' import {CancelableInterface} from './types' +import {isNode} from '../tools/isNode' export type Query = { [key: string]: string | string[] | boolean | number | void, @@ -52,6 +53,35 @@ const MAX_CONTENT_LENGTH = 50 * 1000 * 1000 const DEFAULT_FILE_NAME = 'original' const DEFAULT_RETRY_AFTER_TIMEOUT = 15000 +if (isNode()) { + axios.interceptors.request.use( + config => { + const {data, onUploadProgress} = config + if (!onUploadProgress) { + return config + } + + const total = data.getLengthSync() + + let loaded = 0 + + data.on('data', chunk => { + loaded += chunk.length + + onUploadProgress({ + total, + loaded, + } as ProgressEvent) + }) + + return config + }, + error => { + return Promise.reject(error) + } + ) +} + /** * Updates options with Uploadcare Settings * diff --git a/src/tools/isNode.ts b/src/tools/isNode.ts new file mode 100644 index 000000000..529cc27d8 --- /dev/null +++ b/src/tools/isNode.ts @@ -0,0 +1,8 @@ +export const isNode = (): boolean => { + try { + return Object.prototype.toString.call(global.process) === '[object process]' + } + catch (e) { + return false + } +} diff --git a/test/api/base.spec.ts b/test/api/base.spec.ts index 088e78763..de9f10c1c 100644 --- a/test/api/base.spec.ts +++ b/test/api/base.spec.ts @@ -78,7 +78,7 @@ describe('API - base', () => { }) }) - xit('should be able to handle progress', (done) => { + it('should be able to handle progress', (done) => { let progressValue = 0 const settings = getSettingsForTesting({ publicKey: factory.publicKey('demo')