RequestCache is green.

This commit is contained in:
David Diaz 2020-11-22 15:38:21 -06:00
parent 9a6daf34f9
commit c82ee555c2
6 changed files with 40 additions and 46 deletions

View File

@ -1,4 +1,4 @@
const RequestCache = require('../requestCache') import RequestCache from '../requestCache'
describe('Request Cache', () => { describe('Request Cache', () => {
let cache let cache

View File

@ -31,7 +31,7 @@ export interface RequestObject {
export type RequestConfig = RequestObject | RequestObject[] | string 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 { export default class Config implements BeauConfig {
version = moduleVersion() version = moduleVersion()

View File

@ -1,11 +1,12 @@
const vm = require('vm') import vm = require('vm')
const requireg = require('requireg') import requireg = require('requireg')
const deepmerge = require('deepmerge') import deepmerge = require('deepmerge')
const { toKebabCase, dynamicValueRegex, replaceInObject } = require('./shared') import { toKebabCase, dynamicValueRegex, replaceInObject } from './shared'
const { isPlainObject } = require('is-plain-object') import { isPlainObject } from 'is-plain-object'
import {UObjectString} from './config'
export default class Plugins { export default class Plugins {
constructor(plugins = [], autoload = ['std']) { constructor(plugins: UObjectString[] = [], autoload = ['std']) {
this.registry = { this.registry = {
preRequestModifiers: [], preRequestModifiers: [],
postRequestModifiers: [], postRequestModifiers: [],
@ -115,4 +116,3 @@ export default class Plugins {
this.context[name] = fn this.context[name] = fn
} }
} }

View File

@ -10,7 +10,7 @@ const {
isUrl isUrl
} = require('./shared') } = require('./shared')
class Request { export default class Request {
constructor(req, plugins = new Plugins()) { constructor(req, plugins = new Plugins()) {
this.originalRequest = req this.originalRequest = req
this.plugins = plugins this.plugins = plugins
@ -127,5 +127,3 @@ class Request {
return results return results
} }
} }
module.exports = Request

View File

@ -1,32 +1,36 @@
const { replacementRegex, replaceInObject } = require('./shared') import { UObjectString } from './config'
import { replacementRegex, replaceInObject } from './shared'
class RequestCache { export default class RequestCache {
constructor() { $cache: { [key: string]: UObjectString } = {}
this.$cache = {}
}
exists(key) { exists(key: string) {
return typeof this.$cache[key] !== 'undefined' return typeof this.$cache[key] !== 'undefined'
} }
add(key, value) { add(key: string, value: UObjectString) {
this.$cache[key] = value this.$cache[key] = value
} }
get(path) { get(path: string): string {
let result = this.$cache let result: string = ''
let crawler: UObjectString = this.$cache
path.split('.').forEach((part) => { path.split('.').forEach((part) => {
if (result[part] === undefined) { if (typeof crawler === 'string' || crawler[part] === undefined) {
throw new Error(`${path} not found in cache.`) throw new Error(`${path} not found in cache.`)
} }
result = result[part] crawler = crawler[part]
}) })
if (typeof crawler === 'string') {
result = crawler
}
return result return result
} }
parse(item) { parse(item: UObjectString | null | undefined) {
if (item === null) { if (item === null) {
return null return null
} }
@ -42,5 +46,3 @@ class RequestCache {
) )
} }
} }
module.exports = RequestCache

View File

@ -1,15 +1,22 @@
const Request = require('./request') import Config, { RequestObject } from './config'
const RequestCache = require('./requestCache') import Plugins from './plugins'
import Request from './request'
import RequestCache from './requestCache'
class RequestList { class RequestList {
constructor(config = { REQUESTS: [] }) { list: Request[] = []
this.list = this.loadRequests(config.REQUESTS, config.PLUGINS) cache: RequestCache
this.cache = new 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)) { if (this.cache.exists(alias)) {
return this.cache.get(alias) return this.cache.get(alias)
} }
@ -36,19 +43,6 @@ class RequestList {
return this.cache 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 module.exports = RequestList