mirror of https://github.com/Seich/Beau.git
parent
faceb87197
commit
0b3e16b120
|
|
@ -1,6 +1,6 @@
|
||||||
import Beau from '../beau'
|
import Beau from '../beau'
|
||||||
import { parseBeauConfig } from '../config'
|
import { parseBeauConfig } from '../config'
|
||||||
import * as shared from '../shared'
|
const shared = require('../shared')
|
||||||
|
|
||||||
const requireg = require('requireg')
|
const requireg = require('requireg')
|
||||||
requireg.resolving = false
|
requireg.resolving = false
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
const Plugins = require('../plugins')
|
import Plugins from '../plugins'
|
||||||
const Request = require('../request')
|
import Request from '../request'
|
||||||
const RequestCache = require('../requestCache')
|
import RequestCache from '../requestCache'
|
||||||
const requireg = require('requireg')
|
const requireg = require('requireg')
|
||||||
|
|
||||||
describe(`Beau's plugin system`, () => {
|
describe(`Beau's plugin system`, () => {
|
||||||
|
|
@ -1,13 +1,12 @@
|
||||||
const {
|
import {
|
||||||
requestRegex,
|
requestRegex,
|
||||||
replacementRegex,
|
replacementRegex,
|
||||||
dynamicValueRegex,
|
dynamicValueRegex,
|
||||||
UpperCaseKeys,
|
|
||||||
removeOptionalKeys,
|
removeOptionalKeys,
|
||||||
toKebabCase,
|
toKebabCase,
|
||||||
replaceInObject,
|
replaceInObject,
|
||||||
expandPath
|
expandPath
|
||||||
} = require('../shared')
|
} from '../shared'
|
||||||
|
|
||||||
describe('Shared Utilities', () => {
|
describe('Shared Utilities', () => {
|
||||||
describe('requestRegex', () => {
|
describe('requestRegex', () => {
|
||||||
|
|
@ -47,13 +46,6 @@ describe('Shared Utilities', () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('UpperCaseKeys', () => {
|
|
||||||
it('should uppercase all first-level keys in an object', () => {
|
|
||||||
let a = { test: 1, Test2: 2 }
|
|
||||||
expect(UpperCaseKeys(a)).toEqual({ TEST: 1, TEST2: 2 })
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('removeOptionalKeys', () => {
|
describe('removeOptionalKeys', () => {
|
||||||
it('should remove empty objects from an object', () => {
|
it('should remove empty objects from an object', () => {
|
||||||
let a = { b: {}, c: 2, d: {} }
|
let a = { b: {}, c: 2, d: {} }
|
||||||
|
|
@ -3,9 +3,22 @@ import requireg = require('requireg')
|
||||||
import deepmerge = require('deepmerge')
|
import deepmerge = require('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 {UObjectString} from './config'
|
import { RequestObject, UObjectString } from './config'
|
||||||
|
|
||||||
|
type IPluginRegistry = {
|
||||||
|
[key in PluginType]: Array<(arg0: any, arg1: any) => any>
|
||||||
|
}
|
||||||
|
|
||||||
|
type PluginType =
|
||||||
|
| 'preRequestModifiers'
|
||||||
|
| 'postRequestModifiers'
|
||||||
|
| 'dynamicValues'
|
||||||
|
|
||||||
export default class Plugins {
|
export default class Plugins {
|
||||||
|
registry: IPluginRegistry
|
||||||
|
context: vm.Context
|
||||||
|
autoload: string[] = []
|
||||||
|
|
||||||
constructor(plugins: UObjectString[] = [], autoload = ['std']) {
|
constructor(plugins: UObjectString[] = [], autoload = ['std']) {
|
||||||
this.registry = {
|
this.registry = {
|
||||||
preRequestModifiers: [],
|
preRequestModifiers: [],
|
||||||
|
|
@ -14,14 +27,12 @@ export default class Plugins {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.context = {}
|
this.context = {}
|
||||||
|
|
||||||
this.autoload = autoload
|
this.autoload = autoload
|
||||||
|
|
||||||
this.loadPlugins(plugins.concat(this.autoload))
|
this.loadPlugins(plugins.concat(this.autoload))
|
||||||
}
|
}
|
||||||
|
|
||||||
normalizePlugins(plugins) {
|
normalizePlugins(plugins: Array<string | { [key: string]: any }>) {
|
||||||
let results = {}
|
let results: { [key: string]: any } = {}
|
||||||
|
|
||||||
plugins.forEach((plugin) => {
|
plugins.forEach((plugin) => {
|
||||||
let name = plugin
|
let name = plugin
|
||||||
|
|
@ -38,14 +49,14 @@ export default class Plugins {
|
||||||
settings = plugin[name]
|
settings = plugin[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
results[name] = settings
|
results[name as string] = settings
|
||||||
})
|
})
|
||||||
|
|
||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
loadPlugins(plugins) {
|
loadPlugins(pluginsToLoad: Array<string | { [key: string]: any }>) {
|
||||||
plugins = this.normalizePlugins(plugins)
|
const plugins = this.normalizePlugins(pluginsToLoad)
|
||||||
Object.keys(plugins).forEach((name) => {
|
Object.keys(plugins).forEach((name) => {
|
||||||
const module = `beau-${toKebabCase(name)}`
|
const module = `beau-${toKebabCase(name)}`
|
||||||
|
|
||||||
|
|
@ -62,17 +73,15 @@ export default class Plugins {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
executeModifier(modifier, obj, orig): typeof obj {
|
executeModifier<T>(modifier: PluginType, obj: T, orig: RequestObject): T {
|
||||||
let result = deepmerge({}, obj, { isMergeableObject: isPlainObject })
|
let result = deepmerge<T>({}, obj, { isMergeableObject: isPlainObject })
|
||||||
|
|
||||||
this.registry[modifier].forEach(
|
this.registry[modifier].forEach((mod) => (result = mod(result, orig)))
|
||||||
(modifier) => (result = modifier(result, orig))
|
|
||||||
)
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
replaceDynamicValues(obj) {
|
replaceDynamicValues(obj: { [key: string]: any }) {
|
||||||
vm.createContext(this.context)
|
vm.createContext(this.context)
|
||||||
return replaceInObject(obj, (val) => {
|
return replaceInObject(obj, (val) => {
|
||||||
let valIsEmpty = val.trim().length === 0
|
let valIsEmpty = val.trim().length === 0
|
||||||
|
|
@ -86,15 +95,16 @@ export default class Plugins {
|
||||||
val.replace(dynamicValueRegex, '').trim() === ''
|
val.replace(dynamicValueRegex, '').trim() === ''
|
||||||
|
|
||||||
if (onlyHasDynamic) {
|
if (onlyHasDynamic) {
|
||||||
let call
|
let call = ''
|
||||||
val.replace(dynamicValueRegex, (match, c) => {
|
val.replace(dynamicValueRegex, (_, c) => {
|
||||||
call = c
|
call = c
|
||||||
|
return ''
|
||||||
})
|
})
|
||||||
|
|
||||||
return vm.runInContext(call, this.context)
|
return vm.runInContext(call, this.context)
|
||||||
}
|
}
|
||||||
|
|
||||||
return val.replace(dynamicValueRegex, (match, call) => {
|
return val.replace(dynamicValueRegex, (_, call) => {
|
||||||
return vm.runInContext(call, this.context)
|
return vm.runInContext(call, this.context)
|
||||||
})
|
})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,25 @@ import Plugins from './plugins'
|
||||||
import {
|
import {
|
||||||
requestRegex,
|
requestRegex,
|
||||||
replacementRegex,
|
replacementRegex,
|
||||||
UpperCaseKeys,
|
|
||||||
removeOptionalKeys,
|
removeOptionalKeys,
|
||||||
isUrl
|
isUrl
|
||||||
} from './shared'
|
} from './shared'
|
||||||
import { RequestObject, UObjectString } from './config'
|
import { RequestObject, UObjectString } from './config'
|
||||||
|
|
||||||
|
export interface IBeauRequestResult {
|
||||||
|
request: {
|
||||||
|
headers: { [key: string]: any }
|
||||||
|
body: { [key: string]: any } | string
|
||||||
|
endpoint: string
|
||||||
|
}
|
||||||
|
response: {
|
||||||
|
status: number
|
||||||
|
headers: { [key: string]: any }
|
||||||
|
body: { [key: string]: any } | string
|
||||||
|
}
|
||||||
|
body: { [key: string]: any } | string
|
||||||
|
}
|
||||||
|
|
||||||
export default class Request implements RequestObject {
|
export default class Request implements RequestObject {
|
||||||
originalRequest: RequestObject
|
originalRequest: RequestObject
|
||||||
plugins: Plugins
|
plugins: Plugins
|
||||||
|
|
@ -133,7 +146,7 @@ export default class Request implements RequestObject {
|
||||||
|
|
||||||
const response = await request(settings)
|
const response = await request(settings)
|
||||||
|
|
||||||
let results = {
|
let results: IBeauRequestResult = {
|
||||||
request: {
|
request: {
|
||||||
headers: response.request.headers,
|
headers: response.request.headers,
|
||||||
body: response.request.body,
|
body: response.request.body,
|
||||||
|
|
|
||||||
|
|
@ -26,8 +26,8 @@ export default class RequestCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
parse(
|
parse(
|
||||||
item: { [key: string]: any } | null | undefined
|
item: { [key: string]: any } | null | undefined | string
|
||||||
): string | null | { [key: string]: any } {
|
): typeof item {
|
||||||
if (item === null) {
|
if (item === null) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,19 +16,13 @@ export const requestRegex = new RegExp(`(${httpVerbs.join('|')})\\s(.*)`, 'i')
|
||||||
export const replacementRegex = /(?:\\?)\$([a-zA-Z\.\d\-\_\:]+)/g
|
export const replacementRegex = /(?:\\?)\$([a-zA-Z\.\d\-\_\:]+)/g
|
||||||
export const dynamicValueRegex = /\$\[(\w+\((?:.|[\n\r])*?\))\]/g
|
export const dynamicValueRegex = /\$\[(\w+\((?:.|[\n\r])*?\))\]/g
|
||||||
|
|
||||||
export const UpperCaseKeys = function (obj: object) {
|
|
||||||
let result = {}
|
|
||||||
Object.entries(obj).forEach(([k, v]) => (result[k.toUpperCase()] = v))
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
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 (
|
||||||
obj: object,
|
obj: object,
|
||||||
optionalValues: string[]
|
optionalValues: string[]
|
||||||
) {
|
): typeof obj {
|
||||||
let result = {}
|
let result = {}
|
||||||
|
|
||||||
Object.entries(obj).forEach(([key, value]) => {
|
Object.entries(obj).forEach(([key, value]) => {
|
||||||
|
|
@ -49,10 +43,10 @@ export const toKebabCase = function (str: string) {
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
}
|
}
|
||||||
|
|
||||||
export const replaceInObject = function (
|
export const replaceInObject = function<T> (
|
||||||
obj: object | string | undefined | null,
|
obj: T,
|
||||||
fn: (arg0: string) => string
|
fn: (arg0: string) => string
|
||||||
) {
|
): T | null | {} | string {
|
||||||
if (obj === null) {
|
if (obj === null) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue