Skip to content

Commit 2d3eb7f

Browse files
authored
Feature/use validator as static (#213)
* feat(filelink): Add able to switch of validation on task params
1 parent f898768 commit 2d3eb7f

File tree

1 file changed

+60
-26
lines changed

1 file changed

+60
-26
lines changed

src/lib/filelink.ts

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,18 @@ export enum CropfacesType {
110110
* Convert to format
111111
*/
112112
export enum VideoTypes {
113-
h264 = 'h264',
114-
h264_hi = 'h264.hi',
115-
webm = 'webm',
116-
'webm-hi' = 'webm.hi',
117-
ogg = 'ogg',
118-
'ogg-hi' = 'ogg.hi',
119-
'hls-variant' = 'hls.variant',
120-
mp3 = 'mp3',
121-
oga = 'oga',
122-
m4a = 'm4a',
123-
aac = 'aac',
124-
hls = 'hls.variant.audio',
113+
h264 = 'h264',
114+
h264_hi = 'h264.hi',
115+
webm = 'webm',
116+
'webm-hi' = 'webm.hi',
117+
ogg = 'ogg',
118+
'ogg-hi' = 'ogg.hi',
119+
'hls-variant' = 'hls.variant',
120+
mp3 = 'mp3',
121+
oga = 'oga',
122+
m4a = 'm4a',
123+
aac = 'aac',
124+
hls = 'hls.variant.audio',
125125
}
126126

127127
export enum URLScreenshotAgent {
@@ -450,7 +450,7 @@ export class Filelink {
450450
* @private
451451
* @memberof Filelink
452452
*/
453-
private validator = getValidator(TransformSchema);
453+
private static validator = getValidator(TransformSchema);
454454

455455
/**
456456
* Applied transforms array
@@ -486,6 +486,14 @@ export class Filelink {
486486
*/
487487
private b64: boolean = false;
488488

489+
/**
490+
* should use a validator to check params of every task
491+
* @private
492+
* @type {boolean}
493+
* @memberof Filelink
494+
*/
495+
private useValidator: boolean = true;
496+
489497
/**
490498
* Custom CNAME
491499
*
@@ -538,6 +546,18 @@ export class Filelink {
538546
return this;
539547
}
540548

549+
/**
550+
* Switch the useValidator flag
551+
*
552+
* @param {boolean} flag
553+
* @returns
554+
* @memberof Filelink
555+
*/
556+
setUseValidator(flag: boolean) {
557+
this.useValidator = flag;
558+
return this;
559+
}
560+
541561
/**
542562
* Set cname for transformation link
543563
*
@@ -579,6 +599,9 @@ export class Filelink {
579599
* @memberof Filelink
580600
*/
581601
getTransformations() {
602+
if (this.useValidator) {
603+
this.validateTasks(this.transforms);
604+
}
582605
return this.transforms;
583606
}
584607

@@ -592,6 +615,10 @@ export class Filelink {
592615
const returnUrl = [];
593616
returnUrl.push(this.getCdnHost());
594617

618+
if (this.useValidator) {
619+
this.validateTasks(this.transforms);
620+
}
621+
595622
if (this.apikey) {
596623
returnUrl.push(this.apikey);
597624
}
@@ -634,8 +661,6 @@ export class Filelink {
634661
* @memberof Filelink
635662
*/
636663
addTask(name: string, params?) {
637-
this.validateTask(name, params);
638-
639664
if (name !== 'cache' && typeof params === 'boolean') {
640665
if (!params) {
641666
return this;
@@ -1212,23 +1237,19 @@ export class Filelink {
12121237
}
12131238

12141239
/**
1215-
* Validate single task against schema
1240+
* Validate every task against schema
12161241
*
12171242
* @private
1218-
* @param {*} name
1219-
* @param {*} options
1243+
* @param {object[]} transformations - object which contain all transformations
12201244
* @returns {void}
12211245
* @memberof Filelink
12221246
*/
1223-
private validateTask(name, options): void {
1224-
const toValidate = {};
1225-
toValidate[name] = options;
1226-
1227-
const res = this.validator(toValidate);
1247+
private validateTasks(transformations: object[]): void {
1248+
const transformationsObj = this.arrayToObject(transformations, 'name', 'params');
1249+
const res = Filelink.validator(transformationsObj);
12281250
if (res.errors.length) {
1229-
throw new FilestackError(`Task "${name}" validation error, Params: ${JSON.stringify(options)}`, res.errors);
1251+
throw new FilestackError(`Params validation error: ${JSON.stringify(transformations)}`, res.errors);
12301252
}
1231-
12321253
return;
12331254
}
12341255

@@ -1318,7 +1339,7 @@ export class Filelink {
13181339
* @returns {string}
13191340
* @memberof Filelink
13201341
*/
1321-
private escapeValue (value: string): string {
1342+
private escapeValue(value: string): string {
13221343
if (typeof value !== 'string') {
13231344
return value;
13241345
}
@@ -1349,4 +1370,17 @@ export class Filelink {
13491370
return `[${toReturn}]`;
13501371
}
13511372

1373+
/**
1374+
* Converts array of objects to object
1375+
*
1376+
* @private
1377+
* @example [{name: 'resize', params: {height: 125}}] => {resize: {height: 125}}
1378+
* @param arr - any array
1379+
*/
1380+
private arrayToObject = (array: object[], nameKey: string, dataKey: string) => {
1381+
return array.reduce((obj, item) => {
1382+
obj[item[nameKey]] = item[dataKey];
1383+
return obj;
1384+
}, {});
1385+
}
13521386
}

0 commit comments

Comments
 (0)