Beau is green.

This commit is contained in:
David Diaz 2020-11-22 16:54:09 -06:00
parent 164c6665bb
commit 008b401b8e
8 changed files with 80 additions and 88 deletions

View File

@ -3,16 +3,17 @@
exports[`Beau's config Loader. should load the config 1`] = ` exports[`Beau's config Loader. should load the config 1`] = `
Beau { Beau {
"config": Config { "config": Config {
"COOKIEJAR": false, "cookiejar": false,
"DEFAULTS": Object { "defaults": Object {
"headers": Object { "headers": Object {
"authentication": "hello", "authentication": "hello",
}, },
}, },
"ENDPOINT": "http://example.com", "endpoint": "http://example.com",
"ENVIRONMENT": Object {}, "environment": Object {},
"HOSTS": Array [], "host": undefined,
"PLUGINS": Plugins { "hosts": Array [],
"plugins": Plugins {
"autoload": Array [ "autoload": Array [
"std", "std",
], ],
@ -23,17 +24,8 @@ Beau {
"preRequestModifiers": Array [], "preRequestModifiers": Array [],
}, },
}, },
"REQUESTS": Array [], "requests": Array [],
"VERSION": 1, "version": 1,
"configKeys": Array [
"VERSION",
"ENDPOINT",
"PLUGINS",
"DEFAULTS",
"ENVIRONMENT",
"HOSTS",
"COOKIEJAR",
],
}, },
"requests": RequestList { "requests": RequestList {
"cache": RequestCache { "cache": RequestCache {
@ -55,19 +47,22 @@ RequestList {
}, },
"list": Array [ "list": Array [
Request { Request {
"ALIAS": "get-post", "alias": "get-post",
"COOKIEJAR": false, "cookiejar": false,
"DEPENDENCIES": Set {}, "dependencies": Set {},
"ENDPOINT": "http://example.com", "endpoint": "http://example.com",
"PATH": "/posts/1", "form": Object {},
"REQUEST": "GET /posts/1", "formdata": Object {},
"VERB": "GET", "headers": Object {},
"originalRequest": Object { "originalRequest": Object {
"ALIAS": "get-post", "alias": "get-post",
"COOKIEJAR": false, "cookiejar": false,
"ENDPOINT": "http://example.com", "endpoint": "http://example.com",
"REQUEST": "GET /posts/1", "request": "GET /posts/1",
}, },
"params": Object {},
"path": "/posts/1",
"payload": Object {},
"plugins": Plugins { "plugins": Plugins {
"autoload": Array [ "autoload": Array [
"std", "std",
@ -79,27 +74,31 @@ RequestList {
"preRequestModifiers": Array [], "preRequestModifiers": Array [],
}, },
}, },
"request": "GET /posts/1",
"verb": "GET",
}, },
Request { Request {
"ALIAS": "user", "alias": "user",
"COOKIEJAR": false, "cookiejar": false,
"DEPENDENCIES": Set {}, "dependencies": Set {},
"ENDPOINT": "http://example.com", "endpoint": "http://example.com",
"HEADERS": Object { "form": Object {},
"formdata": Object {},
"headers": Object {
"hello": "world", "hello": "world",
}, },
"PATH": "/user",
"REQUEST": "GET /user",
"VERB": "GET",
"originalRequest": Object { "originalRequest": Object {
"ALIAS": "user", "alias": "user",
"COOKIEJAR": false, "cookiejar": false,
"ENDPOINT": "http://example.com", "endpoint": "http://example.com",
"HEADERS": Object { "headers": Object {
"hello": "world", "hello": "world",
}, },
"REQUEST": "GET /user", "request": "GET /user",
}, },
"params": Object {},
"path": "/user",
"payload": Object {},
"plugins": Plugins { "plugins": Plugins {
"autoload": Array [ "autoload": Array [
"std", "std",
@ -111,6 +110,8 @@ RequestList {
"preRequestModifiers": Array [], "preRequestModifiers": Array [],
}, },
}, },
"request": "GET /user",
"verb": "GET",
}, },
], ],
} }

View File

@ -6,7 +6,7 @@ Object {
"request": Object { "request": Object {
"body": Object { "body": Object {
"lastname": "Diaz", "lastname": "Diaz",
"name": "Sergio", "name": "David",
}, },
"endpoint": "http://martianwabbit.com/user", "endpoint": "http://martianwabbit.com/user",
"headers": undefined, "headers": undefined,

View File

@ -1,17 +1,15 @@
const yaml = require('js-yaml') import Beau from '../beau'
const Beau = require('../beau') import { parseBeauConfig } from '../config'
const { moduleVersion } = require('../shared') import * as shared from '../shared'
jest.mock('../shared')
const requireg = require('requireg') const requireg = require('requireg')
requireg.resolving = false requireg.resolving = false
shared.moduleVersion = jest.fn().mockReturnValue(1)
describe(`Beau's config Loader.`, () => { describe(`Beau's config Loader.`, () => {
it('should load the config', () => { it('should load the config', () => {
moduleVersion.mockReturnValue(1) const doc = parseBeauConfig(`
const doc = yaml.safeLoad(`
version: 1 version: 1
endpoint: 'http://example.com' endpoint: 'http://example.com'
@ -25,9 +23,7 @@ describe(`Beau's config Loader.`, () => {
}) })
it(`should load the request list using the configuration`, () => { it(`should load the request list using the configuration`, () => {
moduleVersion.mockReturnValue(1) const doc = parseBeauConfig(`
const doc = yaml.safeLoad(`
version: 1 version: 1
endpoint: 'http://example.com' endpoint: 'http://example.com'
@ -48,9 +44,9 @@ describe(`Beau's config Loader.`, () => {
.spyOn(console, 'warn') .spyOn(console, 'warn')
.mockImplementation((val) => (stdout = val)) .mockImplementation((val) => (stdout = val))
moduleVersion.mockReturnValue(2) shared.moduleVersion.mockReturnValue(2)
const beau = new Beau({ version: 1 }) new Beau({ version: 1 })
expect(stdout).toEqual('This Beau file expects v1. You are using v2.') expect(stdout).toEqual('This Beau file expects v1. You are using v2.')
spy.mockReset() spy.mockReset()

View File

@ -1,5 +1,5 @@
const Config = require('../config') import Config from '../config'
const RequestList = require('../requestList') import RequestList from '../requestList'
const requestPromiseNativeMock = require('request-promise-native') const requestPromiseNativeMock = require('request-promise-native')
describe('RequestList', () => { describe('RequestList', () => {
@ -10,19 +10,19 @@ describe('RequestList', () => {
} }
const doc = { const doc = {
ENDPOINT: endpoint, endpoint: endpoint,
ENVIRONMENT: env, environment: env,
'GET /post': { alias: 'get-posts' }, 'GET /post': { alias: 'get-posts' },
'POST /user': { 'POST /user': {
alias: 'user', alias: 'user',
payload: { payload: {
name: 'Sergio', name: 'David',
lastname: 'Diaz' lastname: 'Diaz'
} }
} }
} }
let requests let requests: RequestList
beforeEach(() => { beforeEach(() => {
requestPromiseNativeMock.fail = false requestPromiseNativeMock.fail = false

View File

@ -1,25 +1,25 @@
import deepmerge from "deepmerge" import { moduleVersion } from './shared'
import {BeauConfig, Config} from "./config" import RequestList from './requestList'
import {moduleVersion} from './shared' import Config, { BeauConfig } from './config'
const RequestList = require('./requestList') const deepmerge = require('deepmerge')
class Beau { export default class Beau {
config: Config config: Config
requests: RequestList
constructor(doc: BeauConfig, env = {}) { constructor(doc: BeauConfig, env = {}) {
doc.environment = deepmerge(doc.environment, env) doc.environment = deepmerge(doc.environment, env)
this.config = new Config(doc) this.config = new Config(doc)
this.requests = new RequestList(this.config) this.requests = new RequestList(this.config)
if (this.config.VERSION !== moduleVersion()) { if (this.config.version !== moduleVersion()) {
console.warn( console.warn(
`This Beau file expects v${ `This Beau file expects v${
this.config.VERSION this.config.version
}. You are using v${moduleVersion()}.` }. You are using v${moduleVersion()}.`
) )
} }
} }
} }
module.exports = Beau

View File

@ -25,6 +25,7 @@ export default class Request implements RequestObject {
request: string request: string
verb: string verb: string
path: string path: string
dependencies: Set<string>
constructor(req: RequestObject, plugins = new Plugins()) { constructor(req: RequestObject, plugins = new Plugins()) {
this.originalRequest = req this.originalRequest = req
@ -64,7 +65,7 @@ export default class Request implements RequestObject {
} }
} }
findDependencies(request: RequestObject | string, set = new Set()) { findDependencies(request: RequestObject | string, set = new Set<string>()) {
if (typeof request === 'object' && request !== null) { if (typeof request === 'object' && request !== null) {
Object.entries(request) Object.entries(request)
.filter(([key]) => key !== 'alias') .filter(([key]) => key !== 'alias')

View File

@ -8,12 +8,11 @@ export default class RequestCache {
return typeof this.$cache[key] !== 'undefined' return typeof this.$cache[key] !== 'undefined'
} }
add(key: string, value: UObjectString) { add(key: string, value: { [key: string]: any }) {
this.$cache[key] = value this.$cache[key] = value
} }
get(path: string): string { get(path: string): UObjectString {
let result: string = ''
let crawler: UObjectString = this.$cache let crawler: UObjectString = this.$cache
path.split('.').forEach((part) => { path.split('.').forEach((part) => {
if (typeof crawler === 'string' || crawler[part] === undefined) { if (typeof crawler === 'string' || crawler[part] === undefined) {
@ -23,11 +22,7 @@ export default class RequestCache {
crawler = crawler[part] crawler = crawler[part]
}) })
if (typeof crawler === 'string') { return crawler
result = crawler
}
return result
} }
parse( parse(
@ -43,7 +38,7 @@ export default class RequestCache {
return match.replace('\\$', '$') return match.replace('\\$', '$')
} }
return this.get(key) return this.get(key) as string
}) })
) )
} }

View File

@ -2,11 +2,11 @@ import Config from './config'
import Request from './request' import Request from './request'
import RequestCache from './requestCache' import RequestCache from './requestCache'
class RequestList { export default class RequestList {
list: Request[] = [] list: Request[] = []
cache: RequestCache cache: RequestCache
constructor(config: Config) { constructor(config: Config = new Config({})) {
this.list = config.requests.map( this.list = config.requests.map(
(req) => new Request(req, config.plugins) (req) => new Request(req, config.plugins)
) )
@ -20,28 +20,27 @@ class RequestList {
return this.cache.get(alias) return this.cache.get(alias)
} }
const request = this.list.find((r) => r.ALIAS === alias) const request = this.list.find((r) => r.alias === alias)
if (typeof request === 'undefined') { if (typeof request === 'undefined') {
throw new Error(`${alias} not found among the requests.`) throw new Error(`${alias} not found among the requests.`)
} }
try { try {
await this.fetchDependencies(Array.from(request.DEPENDENCIES)) await this.fetchDependencies(Array.from(request.dependencies))
return await request.exec(this.cache) return await request.exec(this.cache)
} catch (reason) { } catch (reason) {
throw new Error( throw new Error(
`Request ${request.VERB} ${request.ENDPOINT} FAILED. \n${reason}` `Request ${request.verb} ${request.endpoint} FAILED. \n${reason}`
) )
} }
} }
async fetchDependencies(dependencies) { async fetchDependencies(dependencies: string[]) {
dependencies = dependencies.map((d) => this.execByAlias(d)) const requests = dependencies.map((d) => this.execByAlias(d))
await Promise.all(dependencies) await Promise.all(requests)
return this.cache return this.cache
} }
} }
module.exports = RequestList