Skip to content

Commit

Permalink
chore: token setter mod
Browse files Browse the repository at this point in the history
  • Loading branch information
kukhariev committed May 4, 2019
1 parent 6371502 commit 72c6315
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/uploadx/src/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export abstract class Uploader {
/**
* Custom headers
*/
headers: { [key: string]: string } | null;
headers: { [key: string]: string } | null = {};
/**
* Metadata Object
*/
Expand Down Expand Up @@ -271,7 +271,7 @@ export abstract class Uploader {
this.token = token || this.token;
if (this.token) {
const _token = await unfunc(this.token, this.responseStatus);
this.headers = { ...this.headers, ...{ Authorization: `Bearer ${_token}` } };
this.headers.Authorization = `Bearer ${_token}`;
}
}

Expand All @@ -281,7 +281,8 @@ export abstract class Uploader {
async start() {
while (this.status === 'uploading' || this.status === 'retry') {
try {
this.offset = isNaN(this.offset) ? await this.getOffset() : await this.sendFileContent();
this.offset =
typeof this.offset === 'number' ? await this.sendFileContent() : await this.getOffset();
this.retry.reset();
!this.options.chunkSize && this.setChunkSize();
if (this.offset >= this.size) {
Expand Down
5 changes: 4 additions & 1 deletion src/uploadx/src/uploaderx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ describe('Uploader', () => {
});
it('should upload', async function() {
const file = getFile();
const uploader: Uploader = new UploaderX(file, {} as UploadxOptions);
const uploader: Uploader = new UploaderX(file, {
token: () => Promise.resolve('_token_')
} as UploadxOptions);
await uploader.upload();
expect(uploader.responseStatus).toEqual(404);
expect(uploader.headers.Authorization).toBeDefined();
});
});

Expand Down

0 comments on commit 72c6315

Please sign in to comment.