Skip to content

Commit

Permalink
Merge branch 'master' into feat/addons-api
Browse files Browse the repository at this point in the history
# Conflicts:
#	packages/upload-client/src/index.ts
  • Loading branch information
nd0ut committed Sep 13, 2022
2 parents 6a11d99 + 95ecfcc commit 18a9c2b
Show file tree
Hide file tree
Showing 38 changed files with 470 additions and 219 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# [5.0.0](https://github.com/uploadcare/uploadcare-js-api-clients/compare/v4.3.1...v5.0.0) (2022-09-13)


* refactor(upload-client)!: drop `multipartMaxAttempts` option ([178436e](https://github.com/uploadcare/uploadcare-js-api-clients/commit/178436e83c956a2b428e11bc4d76af27b627bcdb))


### Features

* **api-client-utils:** add `UploadcareNetworkError` ([0e917d2](https://github.com/uploadcare/uploadcare-js-api-clients/commit/0e917d29cfeb151c2484d89d5b69ba9eafaf31e9))
* **upload-client:** add `retryNetworkErrorMaxTimes` option to specify retries count on network errors ([400fedd](https://github.com/uploadcare/uploadcare-js-api-clients/commit/400fedd0e4a143ca4a18cf79d7b1385e797396a8))
* **upload-client:** throw `UploadcareNetworkError` instead of `Error` ([f7e3f55](https://github.com/uploadcare/uploadcare-js-api-clients/commit/f7e3f55626e047e214bce0d18e4ff34fc7445509))


### BREAKING CHANGES

* option `multipartMaxAttempts` is dropped. Use `retryNetworkErrorMaxTimes` instead. It will affect all the requests, not only multipart uploads.



## [4.3.1](https://github.com/uploadcare/uploadcare-js-api-clients/compare/v4.3.0...v4.3.1) (2022-08-24)


Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@uploadcare/api-clients",
"private": true,
"type": "module",
"version": "4.3.1",
"version": "5.0.0",
"license": "MIT",
"workspaces": [
"packages/api-client-utils",
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@uploadcare/api-client-utils",
"version": "4.3.1",
"version": "5.0.0",
"type": "module",
"main": "dist/index.browser.js",
"module": "dist/index.browser.js",
Expand Down
12 changes: 12 additions & 0 deletions packages/api-client-utils/src/UploadcareNetworkError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { UploadcareNetworkError } from './UploadcareNetworkError'

describe('UploadcareNetworkError', () => {
it('should work', () => {
const progressEvent = new Event('ProgressEvent') as ProgressEvent
const error = new UploadcareNetworkError(progressEvent)
expect(error.name).toBe('UploadcareNetworkError')
expect(error.message).toBe('Network error')
expect(error instanceof UploadcareNetworkError).toBeTruthy()
expect(error.originalProgressEvent).toBe(progressEvent)
})
})
13 changes: 13 additions & 0 deletions packages/api-client-utils/src/UploadcareNetworkError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class UploadcareNetworkError extends Error {
originalProgressEvent: ProgressEvent

constructor(progressEvent: ProgressEvent) {
super()

this.name = 'UploadcareNetworkError'
this.message = 'Network error'
Object.setPrototypeOf(this, UploadcareNetworkError.prototype)

this.originalProgressEvent = progressEvent
}
}
1 change: 1 addition & 0 deletions packages/api-client-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {
export { isNode } from './isNode'
export { isObject } from './isObject'
export { retrier } from './retrier'
export { UploadcareNetworkError } from './UploadcareNetworkError'
export { ContentInfo } from './types/ContentInfo'
export { ImageInfo } from './types/ImageInfo'
export { Metadata } from './types/Metadata'
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client-utils/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '4.3.1'
export default '5.0.0'
4 changes: 2 additions & 2 deletions packages/rest-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@uploadcare/rest-client",
"version": "4.3.1",
"version": "5.0.0",
"description": "Library for work with Uploadcare Rest API",
"type": "module",
"module": "./dist/index.node.js",
Expand Down Expand Up @@ -37,7 +37,7 @@
"keywords": ["uploadcare", "file", "rest", "api"],
"devDependencies": {
"@types/blueimp-md5": "^2.18.0",
"@uploadcare/upload-client": "^4.3.1",
"@uploadcare/upload-client": "^5.0.0",
"ts-node": "^10.8.1"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/rest-client/src/tools/retryIfThrottled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function getTimeoutFromThrottledRequest(response: Response): number {
return retryAfterSeconds * 1000
}

// TODO: implement NetworkError retry
export function retryIfThrottled(
fn: () => Promise<Response>,
retryThrottledMaxTimes: number
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-client/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '4.3.1'
export default '5.0.0'
16 changes: 15 additions & 1 deletion packages/upload-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,19 @@ Sets the maximum number of attempts to retry throttled requests.
Defaults to `1`.
#### `retryNetworkErrorMaxTimes: number`
Sets the maximum number of attempts to retry requests that failed with a network error.
Defaults to `3`.
The delay between attempts equals attempt number, i.e.
- first attempt - 1 second delay
- second attempt - 2 seconds delay
- third attempt - 3 seconds delay
- ...
#### `multipartChunkSize: number`
This option is only applicable when handling local files.
Expand Down Expand Up @@ -411,7 +424,7 @@ Metadata is additional, arbitrary data, associated with uploaded file.
Non-string values will be converted to `string`. `undefined` values will be ignored.
See [REST API reference][uc-docs-metadata] for details.
See [docs][uc-file-metadata] and [REST API][uc-docs-metadata] for details.
## Testing
Expand Down Expand Up @@ -476,3 +489,4 @@ request at [[email protected]][uc-email-hello].
[build-url]: https://github.com/uploadcare/uploadcare-js-api-clients/actions/workflows/checks.yml
[uc-docs-upload-api]: https://uploadcare.com/docs/api_reference/upload/?utm_source=github&utm_campaign=uploadcare-js-api-clients
[uc-docs-metadata]: https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File-Metadata
[uc-file-metadata]: https://uploadcare.com/docs/file-metadata/
4 changes: 2 additions & 2 deletions packages/upload-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@uploadcare/upload-client",
"version": "4.3.1",
"version": "5.0.0",
"description": "Library for work with Uploadcare Upload API",
"type": "module",
"module": "./dist/index.node.js",
Expand Down Expand Up @@ -65,7 +65,7 @@
"koa-body": "5.0.0",
"mock-socket": "9.0.3",
"start-server-and-test": "1.14.0",
"@uploadcare/api-client-utils": "^4.3.1",
"@uploadcare/api-client-utils": "^5.0.0",
"chalk": "^4.1.2"
},
"dependencies": {
Expand Down
8 changes: 5 additions & 3 deletions packages/upload-client/src/api/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { defaultSettings, defaultFilename } from '../defaultSettings'
import { getUserAgent } from '../tools/getUserAgent'
import { camelizeKeys, CustomUserAgent } from '@uploadcare/api-client-utils'
import { UploadClientError } from '../tools/errors'
import retryIfThrottled from '../tools/retryIfThrottled'
import { retryIfFailed } from '../tools/retryIfFailed'

/* Types */
import { Uuid, ProgressCallback, Metadata } from './types'
Expand Down Expand Up @@ -36,6 +36,7 @@ export type BaseOptions = {
userAgent?: CustomUserAgent

retryThrottledRequestMaxTimes?: number
retryNetworkErrorMaxTimes?: number
metadata?: Metadata
}

Expand All @@ -59,10 +60,11 @@ export default function base(
integration,
userAgent,
retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes,
retryNetworkErrorMaxTimes = defaultSettings.retryNetworkErrorMaxTimes,
metadata
}: BaseOptions
): Promise<BaseResponse> {
return retryIfThrottled(
return retryIfFailed(
() =>
request({
method: 'POST',
Expand Down Expand Up @@ -101,6 +103,6 @@ export default function base(
return response
}
}),
retryThrottledRequestMaxTimes
{ retryNetworkErrorMaxTimes, retryThrottledRequestMaxTimes }
)
}
8 changes: 5 additions & 3 deletions packages/upload-client/src/api/fromUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import getUrl from '../tools/getUrl'
import defaultSettings from '../defaultSettings'
import { getUserAgent } from '../tools/getUserAgent'
import { UploadClientError } from '../tools/errors'
import retryIfThrottled from '../tools/retryIfThrottled'
import { retryIfFailed } from '../tools/retryIfFailed'
import { getStoreValue } from '../tools/getStoreValue'

export enum TypeEnum {
Expand Down Expand Up @@ -67,6 +67,7 @@ export type FromUrlOptions = {
userAgent?: CustomUserAgent

retryThrottledRequestMaxTimes?: number
retryNetworkErrorMaxTimes?: number
metadata?: Metadata
}

Expand All @@ -89,10 +90,11 @@ export default function fromUrl(
integration,
userAgent,
retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes,
retryNetworkErrorMaxTimes = defaultSettings.retryNetworkErrorMaxTimes,
metadata
}: FromUrlOptions
): Promise<FromUrlSuccessResponse> {
return retryIfThrottled(
return retryIfFailed(
() =>
request({
method: 'POST',
Expand Down Expand Up @@ -128,6 +130,6 @@ export default function fromUrl(
return response
}
}),
retryThrottledRequestMaxTimes
{ retryNetworkErrorMaxTimes, retryThrottledRequestMaxTimes }
)
}
10 changes: 6 additions & 4 deletions packages/upload-client/src/api/fromUrlStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import getUrl from '../tools/getUrl'
import defaultSettings from '../defaultSettings'
import { getUserAgent } from '../tools/getUserAgent'
import { UploadClientError } from '../tools/errors'
import retryIfThrottled from '../tools/retryIfThrottled'
import { retryIfFailed } from '../tools/retryIfFailed'

export enum Status {
Unknown = 'unknown',
Expand Down Expand Up @@ -69,6 +69,7 @@ export type FromUrlStatusOptions = {
userAgent?: CustomUserAgent

retryThrottledRequestMaxTimes?: number
retryNetworkErrorMaxTimes?: number
}

/**
Expand All @@ -82,10 +83,11 @@ export default function fromUrlStatus(
signal,
integration,
userAgent,
retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes
retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes,
retryNetworkErrorMaxTimes = defaultSettings.retryNetworkErrorMaxTimes
}: FromUrlStatusOptions = {}
): Promise<FromUrlStatusResponse> {
return retryIfThrottled(
return retryIfFailed(
() =>
request({
method: 'GET',
Expand Down Expand Up @@ -118,6 +120,6 @@ export default function fromUrlStatus(
return response
}
}),
retryThrottledRequestMaxTimes
{ retryNetworkErrorMaxTimes, retryThrottledRequestMaxTimes }
)
}
10 changes: 6 additions & 4 deletions packages/upload-client/src/api/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import getUrl from '../tools/getUrl'
import defaultSettings from '../defaultSettings'
import { getUserAgent } from '../tools/getUserAgent'
import { UploadClientError } from '../tools/errors'
import retryIfThrottled from '../tools/retryIfThrottled'
import { retryIfFailed } from '../tools/retryIfFailed'

export type GroupOptions = {
publicKey: string
Expand All @@ -25,6 +25,7 @@ export type GroupOptions = {
userAgent?: CustomUserAgent

retryThrottledRequestMaxTimes?: number
retryNetworkErrorMaxTimes?: number
}

type Response = GroupInfo | FailedResponse
Expand All @@ -44,10 +45,11 @@ export default function group(
source,
integration,
userAgent,
retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes
retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes,
retryNetworkErrorMaxTimes = defaultSettings.retryNetworkErrorMaxTimes
}: GroupOptions
): Promise<GroupInfo> {
return retryIfThrottled(
return retryIfFailed(
() =>
request({
method: 'POST',
Expand Down Expand Up @@ -79,6 +81,6 @@ export default function group(
return response
}
}),
retryThrottledRequestMaxTimes
{ retryNetworkErrorMaxTimes, retryThrottledRequestMaxTimes }
)
}
Loading

0 comments on commit 18a9c2b

Please sign in to comment.