Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(filelink): Add able to switch of validation on task params #213

Merged
merged 4 commits into from
Apr 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 60 additions & 26 deletions src/lib/filelink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,18 @@ export enum CropfacesType {
* Convert to format
*/
export enum VideoTypes {
h264 = 'h264',
h264_hi = 'h264.hi',
webm = 'webm',
'webm-hi' = 'webm.hi',
ogg = 'ogg',
'ogg-hi' = 'ogg.hi',
'hls-variant' = 'hls.variant',
mp3 = 'mp3',
oga = 'oga',
m4a = 'm4a',
aac = 'aac',
hls = 'hls.variant.audio',
h264 = 'h264',
h264_hi = 'h264.hi',
webm = 'webm',
'webm-hi' = 'webm.hi',
ogg = 'ogg',
'ogg-hi' = 'ogg.hi',
'hls-variant' = 'hls.variant',
mp3 = 'mp3',
oga = 'oga',
m4a = 'm4a',
aac = 'aac',
hls = 'hls.variant.audio',
}

export enum URLScreenshotAgent {
Expand Down Expand Up @@ -450,7 +450,7 @@ export class Filelink {
* @private
* @memberof Filelink
*/
private validator = getValidator(TransformSchema);
private static validator = getValidator(TransformSchema);

/**
* Applied transforms array
Expand Down Expand Up @@ -486,6 +486,14 @@ export class Filelink {
*/
private b64: boolean = false;

/**
* should use a validator to check params of every task
* @private
* @type {boolean}
* @memberof Filelink
*/
private useValidator: boolean = true;

/**
* Custom CNAME
*
Expand Down Expand Up @@ -538,6 +546,18 @@ export class Filelink {
return this;
}

/**
* Switch the useValidator flag
*
* @param {boolean} flag
* @returns
* @memberof Filelink
*/
setUseValidator(flag: boolean) {
this.useValidator = flag;
return this;
}

/**
* Set cname for transformation link
*
Expand Down Expand Up @@ -579,6 +599,9 @@ export class Filelink {
* @memberof Filelink
*/
getTransformations() {
if (this.useValidator) {
this.validateTasks(this.transforms);
}
return this.transforms;
}

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

if (this.useValidator) {
this.validateTasks(this.transforms);
}

if (this.apikey) {
returnUrl.push(this.apikey);
}
Expand Down Expand Up @@ -634,8 +661,6 @@ export class Filelink {
* @memberof Filelink
*/
addTask(name: string, params?) {
this.validateTask(name, params);

if (name !== 'cache' && typeof params === 'boolean') {
if (!params) {
return this;
Expand Down Expand Up @@ -1212,23 +1237,19 @@ export class Filelink {
}

/**
* Validate single task against schema
* Validate every task against schema
*
* @private
* @param {*} name
* @param {*} options
* @param {object[]} transformations - object which contain all transformations
* @returns {void}
* @memberof Filelink
*/
private validateTask(name, options): void {
const toValidate = {};
toValidate[name] = options;

const res = this.validator(toValidate);
private validateTasks(transformations: object[]): void {
const transformationsObj = this.arrayToObject(transformations, 'name', 'params');
const res = Filelink.validator(transformationsObj);
if (res.errors.length) {
throw new FilestackError(`Task "${name}" validation error, Params: ${JSON.stringify(options)}`, res.errors);
throw new FilestackError(`Params validation error: ${JSON.stringify(transformations)}`, res.errors);
}

return;
}

Expand Down Expand Up @@ -1318,7 +1339,7 @@ export class Filelink {
* @returns {string}
* @memberof Filelink
*/
private escapeValue (value: string): string {
private escapeValue(value: string): string {
if (typeof value !== 'string') {
return value;
}
Expand Down Expand Up @@ -1349,4 +1370,17 @@ export class Filelink {
return `[${toReturn}]`;
}

/**
* Converts array of objects to object
*
* @private
* @example [{name: 'resize', params: {height: 125}}] => {resize: {height: 125}}
* @param arr - any array
*/
private arrayToObject = (array: object[], nameKey: string, dataKey: string) => {
return array.reduce((obj, item) => {
obj[item[nameKey]] = item[dataKey];
return obj;
}, {});
}
}