I'll probably have to start rewriting pieces soon.

It's starting to look a bit gross. Ideally I wouldn't be shoehorning
these methods and would start trying to rewrite them thinking about the
correct types. But I feel like getting this to green is more important
first.
This commit is contained in:
David Diaz 2020-11-22 21:47:46 -06:00
parent fcc36d57ed
commit 785173eab2
4 changed files with 33 additions and 20 deletions

View File

@ -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<T>(modifier: PluginType, obj: T, orig: RequestObject): T {
executeModifier<T extends object>(
modifier: PluginType,
obj: T,
orig: RequestObject
): T {
let result = deepmerge<T>({}, 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
}

View File

@ -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 = {

View File

@ -25,9 +25,7 @@ export default class RequestCache {
return crawler
}
parse(
item: { [key: string]: any } | null | undefined | string
): typeof item {
parse<T>(item: T): T | null {
if (item === null) {
return null
}

View File

@ -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<T extends object>(
obj: T,
optionalValues: string[]
): Partial<typeof obj> {
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
}