diff --git a/src/plugins.ts b/src/plugins.ts index 11ff220..bf6b2b8 100644 --- a/src/plugins.ts +++ b/src/plugins.ts @@ -1,12 +1,17 @@ import vm = require('vm') -import * as requireg from 'requireg' import * as deepmerge from 'deepmerge' import { toKebabCase, dynamicValueRegex, replaceInObject } from './shared' import { isPlainObject } from 'is-plain-object' -import { RequestObject, UObjectString } from './config' +import { RequestObject } from './config' -type IPluginRegistry = { - [key in PluginType]: Array<(arg0: any, arg1: any) => any> +const requireg = require('requireg') + +type PluginFn = (arg0: any, arg1: any) => any + +interface IPluginRegistry { + preRequestModifiers: PluginFn[] + postRequestModifiers: PluginFn[] + dynamicValues: Array<{ name: string; fn: PluginFn }> } type PluginType = @@ -76,11 +81,13 @@ export default class Plugins { }) } - executeModifier(modifier: PluginType, obj: T, orig: RequestObject): T { + executeModifier( + modifier: PluginType, + obj: T, + orig: RequestObject + ): T { let result = deepmerge({}, obj, { isMergeableObject: isPlainObject }) - - this.registry[modifier].forEach((mod) => (result = mod(result, orig))) - + this.registry[modifier].forEach((mod: any) => (result = mod(result, orig))) return result } @@ -116,15 +123,15 @@ export default class Plugins { }) } - addPreRequestModifier(modifier) { + addPreRequestModifier(modifier: any) { this.registry.preRequestModifiers.push(modifier) } - addPostRequestModifier(modifier) { + addPostRequestModifier(modifier: any) { this.registry.postRequestModifiers.push(modifier) } - defineDynamicValue(name, fn) { + defineDynamicValue(name: string, fn: (arg0: any, arg1: any) => string) { this.registry.dynamicValues.push({ name, fn }) this.context[name] = fn } diff --git a/src/request.ts b/src/request.ts index 635a2da..152e71a 100644 --- a/src/request.ts +++ b/src/request.ts @@ -138,12 +138,20 @@ export default class Request implements RequestObject { settings = this.plugins.replaceDynamicValues(settings) + if (typeof settings !== 'object' || settings === null) { + throw new Error() + } + settings = this.plugins.executeModifier( 'preRequestModifiers', settings, this.originalRequest ) + if (typeof settings !== 'object' || settings === null) { + throw new Error() + } + const response = await request(settings) let results: IBeauRequestResult = { diff --git a/src/requestCache.ts b/src/requestCache.ts index a73ed60..243bdf6 100644 --- a/src/requestCache.ts +++ b/src/requestCache.ts @@ -25,9 +25,7 @@ export default class RequestCache { return crawler } - parse( - item: { [key: string]: any } | null | undefined | string - ): typeof item { + parse(item: T): T | null { if (item === null) { return null } diff --git a/src/shared.ts b/src/shared.ts index 8d6dc4b..2fcb79e 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -19,11 +19,11 @@ export const dynamicValueRegex = /\$\[(\w+\((?:.|[\n\r])*?\))\]/g export const isEmptyObject = (obj: object) => Object.keys(obj).length === 0 && obj.constructor === Object -export const removeOptionalKeys = function ( - obj: object, +export const removeOptionalKeys = function( + obj: T, optionalValues: string[] -): Partial { - let result: { [key: string]: any } = {} +): T { + let result: any = {} Object.entries(obj).forEach(([key, value]) => { if (optionalValues.includes(key) && isEmptyObject(value)) { @@ -44,9 +44,9 @@ export const toKebabCase = function (str: string) { } export const replaceInObject = function ( - obj: { [key: string]: any }, + obj: any, fn: (arg0: string) => string -): typeof obj | null | {} | string { +): typeof obj { if (obj === null) { return null }