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 vm = require('vm')
|
||||||
import * as requireg from 'requireg'
|
|
||||||
import * as deepmerge from 'deepmerge'
|
import * as deepmerge from 'deepmerge'
|
||||||
import { toKebabCase, dynamicValueRegex, replaceInObject } from './shared'
|
import { toKebabCase, dynamicValueRegex, replaceInObject } from './shared'
|
||||||
import { isPlainObject } from 'is-plain-object'
|
import { isPlainObject } from 'is-plain-object'
|
||||||
import { RequestObject, UObjectString } from './config'
|
import { RequestObject } from './config'
|
||||||
|
|
||||||
type IPluginRegistry = {
|
const requireg = require('requireg')
|
||||||
[key in PluginType]: Array<(arg0: any, arg1: any) => any>
|
|
||||||
|
type PluginFn = (arg0: any, arg1: any) => any
|
||||||
|
|
||||||
|
interface IPluginRegistry {
|
||||||
|
preRequestModifiers: PluginFn[]
|
||||||
|
postRequestModifiers: PluginFn[]
|
||||||
|
dynamicValues: Array<{ name: string; fn: PluginFn }>
|
||||||
}
|
}
|
||||||
|
|
||||||
type PluginType =
|
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 })
|
let result = deepmerge<T>({}, obj, { isMergeableObject: isPlainObject })
|
||||||
|
this.registry[modifier].forEach((mod: any) => (result = mod(result, orig)))
|
||||||
this.registry[modifier].forEach((mod) => (result = mod(result, orig)))
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -116,15 +123,15 @@ export default class Plugins {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
addPreRequestModifier(modifier) {
|
addPreRequestModifier(modifier: any) {
|
||||||
this.registry.preRequestModifiers.push(modifier)
|
this.registry.preRequestModifiers.push(modifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
addPostRequestModifier(modifier) {
|
addPostRequestModifier(modifier: any) {
|
||||||
this.registry.postRequestModifiers.push(modifier)
|
this.registry.postRequestModifiers.push(modifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
defineDynamicValue(name, fn) {
|
defineDynamicValue(name: string, fn: (arg0: any, arg1: any) => string) {
|
||||||
this.registry.dynamicValues.push({ name, fn })
|
this.registry.dynamicValues.push({ name, fn })
|
||||||
this.context[name] = fn
|
this.context[name] = fn
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -138,12 +138,20 @@ export default class Request implements RequestObject {
|
||||||
|
|
||||||
settings = this.plugins.replaceDynamicValues(settings)
|
settings = this.plugins.replaceDynamicValues(settings)
|
||||||
|
|
||||||
|
if (typeof settings !== 'object' || settings === null) {
|
||||||
|
throw new Error()
|
||||||
|
}
|
||||||
|
|
||||||
settings = this.plugins.executeModifier(
|
settings = this.plugins.executeModifier(
|
||||||
'preRequestModifiers',
|
'preRequestModifiers',
|
||||||
settings,
|
settings,
|
||||||
this.originalRequest
|
this.originalRequest
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (typeof settings !== 'object' || settings === null) {
|
||||||
|
throw new Error()
|
||||||
|
}
|
||||||
|
|
||||||
const response = await request(settings)
|
const response = await request(settings)
|
||||||
|
|
||||||
let results: IBeauRequestResult = {
|
let results: IBeauRequestResult = {
|
||||||
|
|
|
||||||
|
|
@ -25,9 +25,7 @@ export default class RequestCache {
|
||||||
return crawler
|
return crawler
|
||||||
}
|
}
|
||||||
|
|
||||||
parse(
|
parse<T>(item: T): T | null {
|
||||||
item: { [key: string]: any } | null | undefined | string
|
|
||||||
): typeof item {
|
|
||||||
if (item === null) {
|
if (item === null) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,11 +19,11 @@ export const dynamicValueRegex = /\$\[(\w+\((?:.|[\n\r])*?\))\]/g
|
||||||
export const isEmptyObject = (obj: object) =>
|
export const isEmptyObject = (obj: object) =>
|
||||||
Object.keys(obj).length === 0 && obj.constructor === Object
|
Object.keys(obj).length === 0 && obj.constructor === Object
|
||||||
|
|
||||||
export const removeOptionalKeys = function (
|
export const removeOptionalKeys = function<T extends object>(
|
||||||
obj: object,
|
obj: T,
|
||||||
optionalValues: string[]
|
optionalValues: string[]
|
||||||
): Partial<typeof obj> {
|
): T {
|
||||||
let result: { [key: string]: any } = {}
|
let result: any = {}
|
||||||
|
|
||||||
Object.entries(obj).forEach(([key, value]) => {
|
Object.entries(obj).forEach(([key, value]) => {
|
||||||
if (optionalValues.includes(key) && isEmptyObject(value)) {
|
if (optionalValues.includes(key) && isEmptyObject(value)) {
|
||||||
|
|
@ -44,9 +44,9 @@ export const toKebabCase = function (str: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const replaceInObject = function (
|
export const replaceInObject = function (
|
||||||
obj: { [key: string]: any },
|
obj: any,
|
||||||
fn: (arg0: string) => string
|
fn: (arg0: string) => string
|
||||||
): typeof obj | null | {} | string {
|
): typeof obj {
|
||||||
if (obj === null) {
|
if (obj === null) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue