mirror of https://github.com/Seich/Beau.git
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:
parent
fcc36d57ed
commit
785173eab2
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 = {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue