Skip to content

Commit

Permalink
feat(filelink): add set soruce method (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcholuj authored and Andrzej Sala committed Aug 7, 2019
1 parent 813de22 commit 4480d5e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/lib/filelink.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ describe('filelink', () => {
expect(result).toEqual(TransformSchema);
});

it('should not require apikay on filestack external url', () => {
const filelink = new Filelink('https://cdn.filestackcontent.com/5aYkEQJSQCmYShsoCnZN');
filelink.shadow(false).upscale();
expect(filelink.toString()).toBe('https://cdn.filestackcontent.com/upscale/\"https://cdn.filestackcontent.com/5aYkEQJSQCmYShsoCnZN\"');
});

it('should be able to disable selected task', () => {
const filelink = new Filelink(defaultSource);
filelink.shadow(false).upscale();
Expand Down
32 changes: 18 additions & 14 deletions src/lib/filelink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,20 +542,8 @@ export class Filelink {
* @memberof Filelink
*/
constructor(source: string | string[], apikey?: string) {
this.source = source;
const isExternal = this.isSourceExternal();

debug(`Source ${source} - isExternal? ${isExternal}`);

if (isExternal && !apikey) {
throw new FilestackError('External sources requires apikey to handle transforms');
}

if (!isExternal && typeof this.source === 'string' && !handleRegexp.test(this.source)) {
throw new FilestackError('Invalid filestack source provided');
}

this.apikey = apikey;
this.setSource(source);
}

/**
Expand Down Expand Up @@ -606,6 +594,22 @@ export class Filelink {
return this;
}

setSource(source: string | string[]) {
this.source = source;

const isExternal = this.isSourceExternal();

debug(`Source ${source} - isExternal? ${isExternal}`);

if (isExternal && !this.apikey) {
throw new FilestackError('External sources requires apikey to handle transforms');
}

if (!isExternal && typeof this.source === 'string' && (!handleRegexp.test(this.source) && this.source.indexOf('filestackcontent') === -1)) {
throw new FilestackError('Invalid filestack source provided');
}
}

/**
* Returns JSONSchema form transformations params
*
Expand Down Expand Up @@ -1289,7 +1293,7 @@ export class Filelink {
continue;
}

if (toTest[i].indexOf('src:') === 0 || toTest[i].indexOf('http') === 0) {
if (toTest[i].indexOf('src:') === 0 || (toTest[i].indexOf('http') === 0 && toTest[i].indexOf('filestackcontent') === -1)) {
return true;
}
}
Expand Down

0 comments on commit 4480d5e

Please sign in to comment.