diff --git a/src/__tests__/__snapshots__/beau.spec.js.snap b/src/__tests__/__snapshots__/beau.spec.ts.snap similarity index 57% rename from src/__tests__/__snapshots__/beau.spec.js.snap rename to src/__tests__/__snapshots__/beau.spec.ts.snap index e61e1c8..83c1e36 100644 --- a/src/__tests__/__snapshots__/beau.spec.js.snap +++ b/src/__tests__/__snapshots__/beau.spec.ts.snap @@ -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", }, ], } diff --git a/src/__tests__/__snapshots__/requestList.spec.js.snap b/src/__tests__/__snapshots__/requestList.spec.ts.snap similarity index 97% rename from src/__tests__/__snapshots__/requestList.spec.js.snap rename to src/__tests__/__snapshots__/requestList.spec.ts.snap index 04206c4..a82504e 100644 --- a/src/__tests__/__snapshots__/requestList.spec.js.snap +++ b/src/__tests__/__snapshots__/requestList.spec.ts.snap @@ -6,7 +6,7 @@ Object { "request": Object { "body": Object { "lastname": "Diaz", - "name": "Sergio", + "name": "David", }, "endpoint": "http://martianwabbit.com/user", "headers": undefined, diff --git a/src/__tests__/beau.spec.js b/src/__tests__/beau.spec.ts similarity index 75% rename from src/__tests__/beau.spec.js rename to src/__tests__/beau.spec.ts index 1bd2791..89b5ef4 100644 --- a/src/__tests__/beau.spec.js +++ b/src/__tests__/beau.spec.ts @@ -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() diff --git a/src/__tests__/requestList.spec.js b/src/__tests__/requestList.spec.ts similarity index 91% rename from src/__tests__/requestList.spec.js rename to src/__tests__/requestList.spec.ts index c2d2dd0..1db7f5d 100644 --- a/src/__tests__/requestList.spec.js +++ b/src/__tests__/requestList.spec.ts @@ -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 diff --git a/src/beau.ts b/src/beau.ts index 4978b28..273525c 100644 --- a/src/beau.ts +++ b/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 diff --git a/src/request.ts b/src/request.ts index 0eacf43..10daf91 100644 --- a/src/request.ts +++ b/src/request.ts @@ -25,6 +25,7 @@ export default class Request implements RequestObject { request: string verb: string path: string + dependencies: Set 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()) { if (typeof request === 'object' && request !== null) { Object.entries(request) .filter(([key]) => key !== 'alias') diff --git a/src/requestCache.ts b/src/requestCache.ts index 3fe04b0..0d513ea 100644 --- a/src/requestCache.ts +++ b/src/requestCache.ts @@ -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 }) ) } diff --git a/src/requestList.ts b/src/requestList.ts index 680cbb4..c143374 100644 --- a/src/requestList.ts +++ b/src/requestList.ts @@ -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