Skip to content

Commit ccd26c8

Browse files
Merge pull request #18 from apivideo/dont-retry-on-401
Dont retry on 401
2 parents ea07ae6 + 482c814 commit ccd26c8

File tree

8 files changed

+69
-59
lines changed

8 files changed

+69
-59
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
All changes to this project will be documented in this file.
33

4+
## [1.0.7] - 2022-04-26
5+
- Don't retry on 401 errors
6+
- Mutualize some code
7+
48
## [1.0.6] - 2022-04-22
59
- Improve errors management
610

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/common.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,14 @@ export declare type VideoUploadError = {
3838
};
3939
export declare const apiResponseToVideoUploadResponse: (response: any) => VideoUploadResponse;
4040
export declare const parseErrorResponse: (xhr: XMLHttpRequest) => VideoUploadError;
41+
export declare const parseUserConfig: (options: {
42+
apiHost?: string | undefined;
43+
uploadToken?: string | undefined;
44+
accessToken?: string | undefined;
45+
apiKey?: string | undefined;
46+
videoId?: string | undefined;
47+
}) => {
48+
uploadEndpoint: string;
49+
authHeader?: string;
50+
videoId?: string;
51+
};

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@api.video/video-uploader",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"description": "api.video video uploader",
55
"repository": {
66
"type": "git",

src/common.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const parseErrorResponse = (xhr: XMLHttpRequest): VideoUploadError => {
6060
raw: xhr.response,
6161
...parsedResponse
6262
}
63-
} catch(e) {
63+
} catch (e) {
6464
// empty
6565
}
6666

@@ -69,4 +69,37 @@ export const parseErrorResponse = (xhr: XMLHttpRequest): VideoUploadError => {
6969
raw: xhr.response,
7070
reason: "UNKWOWN",
7171
}
72+
}
73+
74+
export const parseUserConfig = (options: { apiHost?: string, uploadToken?: string, accessToken?: string, apiKey?: string, videoId?: string }): {uploadEndpoint: string, authHeader?: string, videoId?: string} => {
75+
const apiHost = options.apiHost || DEFAULT_API_HOST;
76+
77+
if (options.uploadToken) {
78+
return {
79+
...(options.videoId ? { videoId: options.videoId } : {}),
80+
uploadEndpoint: `https://${apiHost}/upload?token=${options.uploadToken}`
81+
}
82+
}
83+
84+
if (options.accessToken) {
85+
if (!options.videoId) {
86+
throw new Error("'videoId' is missing");
87+
}
88+
return {
89+
uploadEndpoint: `https://${apiHost}/videos/${options.videoId}/source`,
90+
authHeader: `Bearer ${options.accessToken}`
91+
}
92+
}
93+
94+
if (options.apiKey) {
95+
if (!options.videoId) {
96+
throw new Error("'videoId' is missing");
97+
}
98+
return {
99+
uploadEndpoint: `https://${apiHost}/videos/${options.videoId}/source`,
100+
authHeader: `Basic ${btoa(options.apiKey + ":")}`
101+
}
102+
}
103+
104+
throw new Error(`You must provide either an accessToken, an uploadToken or an API key`);
72105
}

src/progressive-video-uploader.ts

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { apiResponseToVideoUploadResponse, DEFAULT_API_HOST, DEFAULT_RETRIES, MIN_CHUNK_SIZE, parseErrorResponse, VideoUploadResponse } from "./common";
1+
import { apiResponseToVideoUploadResponse, DEFAULT_API_HOST, DEFAULT_RETRIES, MIN_CHUNK_SIZE, parseErrorResponse, parseUserConfig, VideoUploadResponse } from "./common";
22
import { PromiseQueue } from "./promise-queue";
33

44
export interface ProgressiveUploaderOptionsWithUploadToken extends Options {
@@ -42,31 +42,12 @@ export class ProgressiveUploader {
4242
private queue = new PromiseQueue();
4343

4444
constructor(options: ProgressiveUploaderOptionsWithAccessToken | ProgressiveUploaderOptionsWithUploadToken | ProgressiveUploaderOptionsWithApiKey) {
45-
const apiHost = options.apiHost || DEFAULT_API_HOST;
45+
const parsedCondig = parseUserConfig(options);
4646

47-
if (options.hasOwnProperty("uploadToken")) {
48-
const optionsWithUploadToken = options as ProgressiveUploaderOptionsWithUploadToken;
49-
if (optionsWithUploadToken.videoId) {
50-
this.videoId = optionsWithUploadToken.videoId;
51-
}
52-
this.uploadEndpoint = `https://${apiHost}/upload?token=${optionsWithUploadToken.uploadToken}`;
53-
54-
} else if (options.hasOwnProperty("accessToken")) {
55-
const optionsWithAccessToken = options as ProgressiveUploaderOptionsWithAccessToken;
56-
if (!optionsWithAccessToken.videoId) {
57-
throw new Error("'videoId' is missing");
58-
}
59-
this.uploadEndpoint = `https://${apiHost}/videos/${optionsWithAccessToken.videoId}/source`;
60-
this.headers.Authorization = `Bearer ${optionsWithAccessToken.accessToken}`;
61-
} else if (options.hasOwnProperty("apiKey")) {
62-
const optionsWithApiKey = options as ProgressiveUploaderOptionsWithApiKey;
63-
if (!optionsWithApiKey.videoId) {
64-
throw new Error("'videoId' is missing");
65-
}
66-
this.uploadEndpoint = `https://${apiHost}/videos/${optionsWithApiKey.videoId}/source`;
67-
this.headers.Authorization = `Basic ${btoa(optionsWithApiKey.apiKey + ":")}`;
68-
}else {
69-
throw new Error(`You must provide either an accessToken, an uploadToken or an API key`);
47+
this.uploadEndpoint = parsedCondig.uploadEndpoint;
48+
this.videoId = parsedCondig.videoId;
49+
if(parsedCondig.authHeader) {
50+
this.headers.Authorization = parsedCondig.authHeader;
7051
}
7152

7253
this.retries = options.retries || DEFAULT_RETRIES;
@@ -151,8 +132,8 @@ export class ProgressiveUploader {
151132
response = await doUpload(thisPart);
152133
resolve(response);
153134
return;
154-
} catch (e) {
155-
if(retriesCount >= this.retries) {
135+
} catch (e: any) {
136+
if(e.status !== 401 && retriesCount >= this.retries) {
156137
reject(e);
157138
return;
158139
}

src/video-uploader.ts

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { apiResponseToVideoUploadResponse, DEFAULT_API_HOST, DEFAULT_CHUNK_SIZE, DEFAULT_RETRIES, MAX_CHUNK_SIZE, MIN_CHUNK_SIZE, parseErrorResponse, VideoUploadResponse } from "./common";
1+
import { apiResponseToVideoUploadResponse, DEFAULT_API_HOST, DEFAULT_CHUNK_SIZE, DEFAULT_RETRIES, MAX_CHUNK_SIZE, MIN_CHUNK_SIZE, parseErrorResponse, parseUserConfig, VideoUploadError, VideoUploadResponse } from "./common";
22
import { PromiseQueue } from "./promise-queue";
33

44
export interface VideoUploaderOptionsWithUploadToken extends Options {
@@ -44,35 +44,16 @@ export class VideoUploader {
4444
private queue = new PromiseQueue();
4545

4646
constructor(options: VideoUploaderOptionsWithAccessToken | VideoUploaderOptionsWithUploadToken | VideoUploaderOptionsWithApiKey) {
47-
const apiHost = options.apiHost || DEFAULT_API_HOST;
48-
4947
if (!options.file) {
5048
throw new Error("'file' is missing");
5149
}
5250

53-
if (options.hasOwnProperty("uploadToken")) {
54-
const optionsWithUploadToken = options as VideoUploaderOptionsWithUploadToken;
55-
if (optionsWithUploadToken.videoId) {
56-
this.videoId = optionsWithUploadToken.videoId;
57-
}
58-
this.uploadEndpoint = `https://${apiHost}/upload?token=${optionsWithUploadToken.uploadToken}`;
51+
const parsedCondig = parseUserConfig(options);
5952

60-
} else if (options.hasOwnProperty("accessToken")) {
61-
const optionsWithAccessToken = options as VideoUploaderOptionsWithAccessToken;
62-
if (!optionsWithAccessToken.videoId) {
63-
throw new Error("'videoId' is missing");
64-
}
65-
this.uploadEndpoint = `https://${apiHost}/videos/${optionsWithAccessToken.videoId}/source`;
66-
this.headers.Authorization = `Bearer ${optionsWithAccessToken.accessToken}`;
67-
} else if (options.hasOwnProperty("apiKey")) {
68-
const optionsWithApiKey = options as VideoUploaderOptionsWithApiKey;
69-
if (!optionsWithApiKey.videoId) {
70-
throw new Error("'videoId' is missing");
71-
}
72-
this.uploadEndpoint = `https://${apiHost}/videos/${optionsWithApiKey.videoId}/source`;
73-
this.headers.Authorization = `Basic ${btoa(optionsWithApiKey.apiKey + ":")}`;
74-
} else {
75-
throw new Error(`You must provide either an accessToken, an uploadToken or an API key`);
53+
this.uploadEndpoint = parsedCondig.uploadEndpoint;
54+
this.videoId = parsedCondig.videoId;
55+
if(parsedCondig.authHeader) {
56+
this.headers.Authorization = parsedCondig.authHeader;
7657
}
7758

7859
if(options.chunkSize && (options.chunkSize < MIN_CHUNK_SIZE || options.chunkSize > MAX_CHUNK_SIZE)) {
@@ -102,8 +83,8 @@ export class VideoUploader {
10283
this.videoId = response.videoId;
10384
this.currentChunk++;
10485

105-
} catch (e) {
106-
if (retriesCount >= this.retries) {
86+
} catch (e: any) {
87+
if (e.status === 401 || retriesCount >= this.retries) {
10788
reject(e);
10889
return;
10990
}

0 commit comments

Comments
 (0)