mirror of https://github.com/Seich/Beau.git
Beau is green.
This commit is contained in:
parent
164c6665bb
commit
008b401b8e
|
|
@ -3,16 +3,17 @@
|
|||
exports[`Beau's config Loader. should load the config 1`] = `
|
||||
Beau {
|
||||
"config": Config {
|
||||
"COOKIEJAR": false,
|
||||
"DEFAULTS": Object {
|
||||
"cookiejar": false,
|
||||
"defaults": Object {
|
||||
"headers": Object {
|
||||
"authentication": "hello",
|
||||
},
|
||||
},
|
||||
"ENDPOINT": "http://example.com",
|
||||
"ENVIRONMENT": Object {},
|
||||
"HOSTS": Array [],
|
||||
"PLUGINS": Plugins {
|
||||
"endpoint": "http://example.com",
|
||||
"environment": Object {},
|
||||
"host": undefined,
|
||||
"hosts": Array [],
|
||||
"plugins": Plugins {
|
||||
"autoload": Array [
|
||||
"std",
|
||||
],
|
||||
|
|
@ -23,17 +24,8 @@ Beau {
|
|||
"preRequestModifiers": Array [],
|
||||
},
|
||||
},
|
||||
"REQUESTS": Array [],
|
||||
"VERSION": 1,
|
||||
"configKeys": Array [
|
||||
"VERSION",
|
||||
"ENDPOINT",
|
||||
"PLUGINS",
|
||||
"DEFAULTS",
|
||||
"ENVIRONMENT",
|
||||
"HOSTS",
|
||||
"COOKIEJAR",
|
||||
],
|
||||
"requests": Array [],
|
||||
"version": 1,
|
||||
},
|
||||
"requests": RequestList {
|
||||
"cache": RequestCache {
|
||||
|
|
@ -55,19 +47,22 @@ RequestList {
|
|||
},
|
||||
"list": Array [
|
||||
Request {
|
||||
"ALIAS": "get-post",
|
||||
"COOKIEJAR": false,
|
||||
"DEPENDENCIES": Set {},
|
||||
"ENDPOINT": "http://example.com",
|
||||
"PATH": "/posts/1",
|
||||
"REQUEST": "GET /posts/1",
|
||||
"VERB": "GET",
|
||||
"alias": "get-post",
|
||||
"cookiejar": false,
|
||||
"dependencies": Set {},
|
||||
"endpoint": "http://example.com",
|
||||
"form": Object {},
|
||||
"formdata": Object {},
|
||||
"headers": Object {},
|
||||
"originalRequest": Object {
|
||||
"ALIAS": "get-post",
|
||||
"COOKIEJAR": false,
|
||||
"ENDPOINT": "http://example.com",
|
||||
"REQUEST": "GET /posts/1",
|
||||
"alias": "get-post",
|
||||
"cookiejar": false,
|
||||
"endpoint": "http://example.com",
|
||||
"request": "GET /posts/1",
|
||||
},
|
||||
"params": Object {},
|
||||
"path": "/posts/1",
|
||||
"payload": Object {},
|
||||
"plugins": Plugins {
|
||||
"autoload": Array [
|
||||
"std",
|
||||
|
|
@ -79,27 +74,31 @@ RequestList {
|
|||
"preRequestModifiers": Array [],
|
||||
},
|
||||
},
|
||||
"request": "GET /posts/1",
|
||||
"verb": "GET",
|
||||
},
|
||||
Request {
|
||||
"ALIAS": "user",
|
||||
"COOKIEJAR": false,
|
||||
"DEPENDENCIES": Set {},
|
||||
"ENDPOINT": "http://example.com",
|
||||
"HEADERS": Object {
|
||||
"alias": "user",
|
||||
"cookiejar": false,
|
||||
"dependencies": Set {},
|
||||
"endpoint": "http://example.com",
|
||||
"form": Object {},
|
||||
"formdata": Object {},
|
||||
"headers": Object {
|
||||
"hello": "world",
|
||||
},
|
||||
"PATH": "/user",
|
||||
"REQUEST": "GET /user",
|
||||
"VERB": "GET",
|
||||
"originalRequest": Object {
|
||||
"ALIAS": "user",
|
||||
"COOKIEJAR": false,
|
||||
"ENDPOINT": "http://example.com",
|
||||
"HEADERS": Object {
|
||||
"alias": "user",
|
||||
"cookiejar": false,
|
||||
"endpoint": "http://example.com",
|
||||
"headers": Object {
|
||||
"hello": "world",
|
||||
},
|
||||
"REQUEST": "GET /user",
|
||||
"request": "GET /user",
|
||||
},
|
||||
"params": Object {},
|
||||
"path": "/user",
|
||||
"payload": Object {},
|
||||
"plugins": Plugins {
|
||||
"autoload": Array [
|
||||
"std",
|
||||
|
|
@ -111,6 +110,8 @@ RequestList {
|
|||
"preRequestModifiers": Array [],
|
||||
},
|
||||
},
|
||||
"request": "GET /user",
|
||||
"verb": "GET",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@ Object {
|
|||
"request": Object {
|
||||
"body": Object {
|
||||
"lastname": "Diaz",
|
||||
"name": "Sergio",
|
||||
"name": "David",
|
||||
},
|
||||
"endpoint": "http://martianwabbit.com/user",
|
||||
"headers": undefined,
|
||||
|
|
@ -1,17 +1,15 @@
|
|||
const yaml = require('js-yaml')
|
||||
const Beau = require('../beau')
|
||||
const { moduleVersion } = require('../shared')
|
||||
|
||||
jest.mock('../shared')
|
||||
import Beau from '../beau'
|
||||
import { parseBeauConfig } from '../config'
|
||||
import * as shared from '../shared'
|
||||
|
||||
const requireg = require('requireg')
|
||||
requireg.resolving = false
|
||||
|
||||
shared.moduleVersion = jest.fn().mockReturnValue(1)
|
||||
|
||||
describe(`Beau's config Loader.`, () => {
|
||||
it('should load the config', () => {
|
||||
moduleVersion.mockReturnValue(1)
|
||||
|
||||
const doc = yaml.safeLoad(`
|
||||
const doc = parseBeauConfig(`
|
||||
version: 1
|
||||
endpoint: 'http://example.com'
|
||||
|
||||
|
|
@ -25,9 +23,7 @@ describe(`Beau's config Loader.`, () => {
|
|||
})
|
||||
|
||||
it(`should load the request list using the configuration`, () => {
|
||||
moduleVersion.mockReturnValue(1)
|
||||
|
||||
const doc = yaml.safeLoad(`
|
||||
const doc = parseBeauConfig(`
|
||||
version: 1
|
||||
endpoint: 'http://example.com'
|
||||
|
||||
|
|
@ -48,9 +44,9 @@ describe(`Beau's config Loader.`, () => {
|
|||
.spyOn(console, 'warn')
|
||||
.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.')
|
||||
|
||||
spy.mockReset()
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
const Config = require('../config')
|
||||
const RequestList = require('../requestList')
|
||||
import Config from '../config'
|
||||
import RequestList from '../requestList'
|
||||
const requestPromiseNativeMock = require('request-promise-native')
|
||||
|
||||
describe('RequestList', () => {
|
||||
|
|
@ -10,19 +10,19 @@ describe('RequestList', () => {
|
|||
}
|
||||
|
||||
const doc = {
|
||||
ENDPOINT: endpoint,
|
||||
ENVIRONMENT: env,
|
||||
endpoint: endpoint,
|
||||
environment: env,
|
||||
'GET /post': { alias: 'get-posts' },
|
||||
'POST /user': {
|
||||
alias: 'user',
|
||||
payload: {
|
||||
name: 'Sergio',
|
||||
name: 'David',
|
||||
lastname: 'Diaz'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let requests
|
||||
let requests: RequestList
|
||||
beforeEach(() => {
|
||||
requestPromiseNativeMock.fail = false
|
||||
|
||||
16
src/beau.ts
16
src/beau.ts
|
|
@ -1,25 +1,25 @@
|
|||
import deepmerge from "deepmerge"
|
||||
import {BeauConfig, Config} from "./config"
|
||||
import {moduleVersion} from './shared'
|
||||
import { moduleVersion } from './shared'
|
||||
import RequestList from './requestList'
|
||||
import Config, { BeauConfig } from './config'
|
||||
|
||||
const RequestList = require('./requestList')
|
||||
const deepmerge = require('deepmerge')
|
||||
|
||||
class Beau {
|
||||
export default class Beau {
|
||||
config: Config
|
||||
requests: RequestList
|
||||
|
||||
constructor(doc: BeauConfig, env = {}) {
|
||||
doc.environment = deepmerge(doc.environment, env)
|
||||
this.config = new Config(doc)
|
||||
this.requests = new RequestList(this.config)
|
||||
|
||||
if (this.config.VERSION !== moduleVersion()) {
|
||||
if (this.config.version !== moduleVersion()) {
|
||||
console.warn(
|
||||
`This Beau file expects v${
|
||||
this.config.VERSION
|
||||
this.config.version
|
||||
}. You are using v${moduleVersion()}.`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Beau
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ export default class Request implements RequestObject {
|
|||
request: string
|
||||
verb: string
|
||||
path: string
|
||||
dependencies: Set<string>
|
||||
|
||||
constructor(req: RequestObject, plugins = new Plugins()) {
|
||||
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) {
|
||||
Object.entries(request)
|
||||
.filter(([key]) => key !== 'alias')
|
||||
|
|
|
|||
|
|
@ -8,12 +8,11 @@ export default class RequestCache {
|
|||
return typeof this.$cache[key] !== 'undefined'
|
||||
}
|
||||
|
||||
add(key: string, value: UObjectString) {
|
||||
add(key: string, value: { [key: string]: any }) {
|
||||
this.$cache[key] = value
|
||||
}
|
||||
|
||||
get(path: string): string {
|
||||
let result: string = ''
|
||||
get(path: string): UObjectString {
|
||||
let crawler: UObjectString = this.$cache
|
||||
path.split('.').forEach((part) => {
|
||||
if (typeof crawler === 'string' || crawler[part] === undefined) {
|
||||
|
|
@ -23,11 +22,7 @@ export default class RequestCache {
|
|||
crawler = crawler[part]
|
||||
})
|
||||
|
||||
if (typeof crawler === 'string') {
|
||||
result = crawler
|
||||
}
|
||||
|
||||
return result
|
||||
return crawler
|
||||
}
|
||||
|
||||
parse(
|
||||
|
|
@ -43,7 +38,7 @@ export default class RequestCache {
|
|||
return match.replace('\\$', '$')
|
||||
}
|
||||
|
||||
return this.get(key)
|
||||
return this.get(key) as string
|
||||
})
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@ import Config from './config'
|
|||
import Request from './request'
|
||||
import RequestCache from './requestCache'
|
||||
|
||||
class RequestList {
|
||||
export default class RequestList {
|
||||
list: Request[] = []
|
||||
cache: RequestCache
|
||||
|
||||
constructor(config: Config) {
|
||||
constructor(config: Config = new Config({})) {
|
||||
this.list = config.requests.map(
|
||||
(req) => new Request(req, config.plugins)
|
||||
)
|
||||
|
|
@ -20,28 +20,27 @@ class RequestList {
|
|||
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') {
|
||||
throw new Error(`${alias} not found among the requests.`)
|
||||
}
|
||||
|
||||
try {
|
||||
await this.fetchDependencies(Array.from(request.DEPENDENCIES))
|
||||
await this.fetchDependencies(Array.from(request.dependencies))
|
||||
return await request.exec(this.cache)
|
||||
} catch (reason) {
|
||||
throw new Error(
|
||||
`Request ${request.VERB} ${request.ENDPOINT} FAILED. \n${reason}`
|
||||
`Request ${request.verb} ${request.endpoint} FAILED. \n${reason}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
async fetchDependencies(dependencies) {
|
||||
dependencies = dependencies.map((d) => this.execByAlias(d))
|
||||
await Promise.all(dependencies)
|
||||
async fetchDependencies(dependencies: string[]) {
|
||||
const requests = dependencies.map((d) => this.execByAlias(d))
|
||||
await Promise.all(requests)
|
||||
|
||||
return this.cache
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RequestList
|
||||
|
|
|
|||
Loading…
Reference in New Issue