mirror of https://github.com/Seich/Beau.git
RequestCache is green.
This commit is contained in:
parent
9a6daf34f9
commit
c82ee555c2
|
|
@ -1,4 +1,4 @@
|
|||
const RequestCache = require('../requestCache')
|
||||
import RequestCache from '../requestCache'
|
||||
|
||||
describe('Request Cache', () => {
|
||||
let cache
|
||||
|
|
@ -31,7 +31,7 @@ export interface RequestObject {
|
|||
|
||||
export type RequestConfig = RequestObject | RequestObject[] | string
|
||||
|
||||
export type UObjectString = { [key: string]: any } | string
|
||||
export type UObjectString = { [key: string]: UObjectString } | string
|
||||
|
||||
export default class Config implements BeauConfig {
|
||||
version = moduleVersion()
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
const vm = require('vm')
|
||||
const requireg = require('requireg')
|
||||
const deepmerge = require('deepmerge')
|
||||
const { toKebabCase, dynamicValueRegex, replaceInObject } = require('./shared')
|
||||
const { isPlainObject } = require('is-plain-object')
|
||||
import vm = require('vm')
|
||||
import requireg = require('requireg')
|
||||
import deepmerge = require('deepmerge')
|
||||
import { toKebabCase, dynamicValueRegex, replaceInObject } from './shared'
|
||||
import { isPlainObject } from 'is-plain-object'
|
||||
import {UObjectString} from './config'
|
||||
|
||||
export default class Plugins {
|
||||
constructor(plugins = [], autoload = ['std']) {
|
||||
constructor(plugins: UObjectString[] = [], autoload = ['std']) {
|
||||
this.registry = {
|
||||
preRequestModifiers: [],
|
||||
postRequestModifiers: [],
|
||||
|
|
@ -115,4 +116,3 @@ export default class Plugins {
|
|||
this.context[name] = fn
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ const {
|
|||
isUrl
|
||||
} = require('./shared')
|
||||
|
||||
class Request {
|
||||
export default class Request {
|
||||
constructor(req, plugins = new Plugins()) {
|
||||
this.originalRequest = req
|
||||
this.plugins = plugins
|
||||
|
|
@ -127,5 +127,3 @@ class Request {
|
|||
return results
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Request
|
||||
|
|
@ -1,32 +1,36 @@
|
|||
const { replacementRegex, replaceInObject } = require('./shared')
|
||||
import { UObjectString } from './config'
|
||||
import { replacementRegex, replaceInObject } from './shared'
|
||||
|
||||
class RequestCache {
|
||||
constructor() {
|
||||
this.$cache = {}
|
||||
}
|
||||
export default class RequestCache {
|
||||
$cache: { [key: string]: UObjectString } = {}
|
||||
|
||||
exists(key) {
|
||||
exists(key: string) {
|
||||
return typeof this.$cache[key] !== 'undefined'
|
||||
}
|
||||
|
||||
add(key, value) {
|
||||
add(key: string, value: UObjectString) {
|
||||
this.$cache[key] = value
|
||||
}
|
||||
|
||||
get(path) {
|
||||
let result = this.$cache
|
||||
get(path: string): string {
|
||||
let result: string = ''
|
||||
let crawler: UObjectString = this.$cache
|
||||
path.split('.').forEach((part) => {
|
||||
if (result[part] === undefined) {
|
||||
if (typeof crawler === 'string' || crawler[part] === undefined) {
|
||||
throw new Error(`${path} not found in cache.`)
|
||||
}
|
||||
|
||||
result = result[part]
|
||||
crawler = crawler[part]
|
||||
})
|
||||
|
||||
if (typeof crawler === 'string') {
|
||||
result = crawler
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
parse(item) {
|
||||
parse(item: UObjectString | null | undefined) {
|
||||
if (item === null) {
|
||||
return null
|
||||
}
|
||||
|
|
@ -42,5 +46,3 @@ class RequestCache {
|
|||
)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RequestCache
|
||||
|
|
@ -1,15 +1,22 @@
|
|||
const Request = require('./request')
|
||||
const RequestCache = require('./requestCache')
|
||||
import Config, { RequestObject } from './config'
|
||||
import Plugins from './plugins'
|
||||
import Request from './request'
|
||||
import RequestCache from './requestCache'
|
||||
|
||||
class RequestList {
|
||||
constructor(config = { REQUESTS: [] }) {
|
||||
this.list = this.loadRequests(config.REQUESTS, config.PLUGINS)
|
||||
this.cache = new RequestCache()
|
||||
list: Request[] = []
|
||||
cache: RequestCache
|
||||
|
||||
this.cache.add(`env`, config.ENVIRONMENT)
|
||||
constructor(config: Config) {
|
||||
this.list = config.requests.map(
|
||||
(req) => new Request(req, config.plugins)
|
||||
)
|
||||
|
||||
this.cache = new RequestCache()
|
||||
this.cache.add(`env`, config.environment)
|
||||
}
|
||||
|
||||
async execByAlias(alias) {
|
||||
async execByAlias(alias: string) {
|
||||
if (this.cache.exists(alias)) {
|
||||
return this.cache.get(alias)
|
||||
}
|
||||
|
|
@ -36,19 +43,6 @@ class RequestList {
|
|||
|
||||
return this.cache
|
||||
}
|
||||
|
||||
loadRequests(REQUESTS, PLUGINS) {
|
||||
let requests = []
|
||||
REQUESTS.forEach((request) => {
|
||||
try {
|
||||
requests.push(new Request(request, PLUGINS))
|
||||
} catch (e) {
|
||||
throw new Error(`${request.request} was ignored: ${e}`)
|
||||
}
|
||||
})
|
||||
|
||||
return requests
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RequestList
|
||||
Loading…
Reference in New Issue