From 164c6665bb99be0cf8b000d38a8d6463f2e8d243 Mon Sep 17 00:00:00 2001 From: David Diaz Date: Sun, 22 Nov 2020 16:09:25 -0600 Subject: [PATCH] Request is green-ish. --- ...uest.spec.js.snap => request.spec.ts.snap} | 0 .../{request.spec.js => request.spec.ts} | 20 ++-- src/config.ts | 8 +- src/plugins.ts | 2 +- src/request.ts | 112 +++++++++++------- src/requestCache.ts | 4 +- src/requestList.ts | 3 +- 7 files changed, 90 insertions(+), 59 deletions(-) rename src/__tests__/__snapshots__/{request.spec.js.snap => request.spec.ts.snap} (100%) rename src/__tests__/{request.spec.js => request.spec.ts} (84%) diff --git a/src/__tests__/__snapshots__/request.spec.js.snap b/src/__tests__/__snapshots__/request.spec.ts.snap similarity index 100% rename from src/__tests__/__snapshots__/request.spec.js.snap rename to src/__tests__/__snapshots__/request.spec.ts.snap diff --git a/src/__tests__/request.spec.js b/src/__tests__/request.spec.ts similarity index 84% rename from src/__tests__/request.spec.js rename to src/__tests__/request.spec.ts index 77272f0..febe73c 100644 --- a/src/__tests__/request.spec.js +++ b/src/__tests__/request.spec.ts @@ -1,5 +1,5 @@ -const Request = require('../request') -const RequestCache = require('../requestCache') +import Request from '../request' +import RequestCache from '../requestCache' const requestPromiseNativeMock = require('request-promise-native') describe('Request', () => { @@ -45,11 +45,11 @@ describe('Request', () => { }) it('should load up the given request', () => { - expect(request.VERB).toBe('POST') - expect(request.ENDPOINT).toBe(validRequestConfig.endpoint) - expect(request.HEADERS).toBeDefined() - expect(request.PAYLOAD).toBeDefined() - expect(request.PARAMS).toBeDefined() + expect(request.verb).toBe('POST') + expect(request.endpoint).toBe(validRequestConfig.endpoint) + expect(request.headers).toBeDefined() + expect(request.payload).toBeDefined() + expect(request.params).toBeDefined() }) it('should throw if a given request is invalid', () => { @@ -57,9 +57,9 @@ describe('Request', () => { }) it('should list all of its dependencies', () => { - expect(request.DEPENDENCIES.size).toBe(2) - expect(request.DEPENDENCIES).toContain('session') - expect(request.DEPENDENCIES).toContain('profile') + expect(request.dependencies.size).toBe(2) + expect(request.dependencies).toContain('session') + expect(request.dependencies).toContain('profile') }) it('should execute a request', async () => { diff --git a/src/config.ts b/src/config.ts index b6352c8..5fb6364 100644 --- a/src/config.ts +++ b/src/config.ts @@ -34,11 +34,11 @@ export type RequestConfig = RequestObject | RequestObject[] | string export type UObjectString = { [key: string]: UObjectString } | string export default class Config implements BeauConfig { - version = moduleVersion() - cookiejar = false - endpoint = '' + version: number + cookiejar: boolean + endpoint: string - defaults: RequestObject = {} + defaults: RequestObject plugins: Plugins environment = {} host?: string = undefined diff --git a/src/plugins.ts b/src/plugins.ts index 1c58876..6b7690d 100644 --- a/src/plugins.ts +++ b/src/plugins.ts @@ -62,7 +62,7 @@ export default class Plugins { }) } - executeModifier(modifier, obj, orig) { + executeModifier(modifier, obj, orig): typeof obj { let result = deepmerge({}, obj, { isMergeableObject: isPlainObject }) this.registry[modifier].forEach( diff --git a/src/request.ts b/src/request.ts index 87d6d37..0eacf43 100644 --- a/src/request.ts +++ b/src/request.ts @@ -1,59 +1,85 @@ -const request = require('request-promise-native') -const RequestCache = require('./requestCache') -const Plugins = require('./plugins') +import request = require('request-promise-native') +import RequestCache from './requestCache' +import Plugins from './plugins' -const { +import { requestRegex, replacementRegex, UpperCaseKeys, removeOptionalKeys, isUrl -} = require('./shared') +} from './shared' +import { RequestObject, UObjectString } from './config' -export default class Request { - constructor(req, plugins = new Plugins()) { +export default class Request implements RequestObject { + originalRequest: RequestObject + plugins: Plugins + endpoint: string + cookiejar: boolean + alias?: string + form: { [key: string]: any } + formdata: { [key: string]: any } + headers: { [key: string]: any } + params: { [key: string]: any } + payload: UObjectString + request: string + verb: string + path: string + + constructor(req: RequestObject, plugins = new Plugins()) { this.originalRequest = req this.plugins = plugins + this.endpoint = req.endpoint ?? '' + this.cookiejar = req.cookiejar ?? false + this.alias = req.alias ?? undefined + this.form = req.form ?? {} + this.formdata = req.formdata ?? {} + this.payload = req.payload ?? {} + this.headers = req.headers ?? {} + this.params = req.params ?? {} + this.request = req.request ?? '' - req = UpperCaseKeys(req) - Object.assign(this, req) - - if (!this.ALIAS) { - throw new Error(`${this.REQUEST} is missing an alias.`) + if (!this.alias) { + throw new Error(`${this.request} is missing an alias.`) } - const { VERB, PATH } = this.parseRequest(this.REQUEST) + const { verb, path } = this.parseRequest(this.request) - this.VERB = VERB - this.PATH = PATH + this.verb = verb + this.path = path - this.DEPENDENCIES = this.findDependencies(req) + this.dependencies = this.findDependencies(req) } - parseRequest(request) { + parseRequest(request: string) { const parts = request.match(requestRegex) + if (parts === null) { + throw new Error('Request path misformed.') + } + return { - VERB: parts[1], - PATH: parts[2] + verb: parts[1], + path: parts[2] } } - findDependencies(request, set = new Set()) { - let type = typeof request - - if (type === 'object' && request !== null) { + findDependencies(request: RequestObject | string, set = new Set()) { + if (typeof request === 'object' && request !== null) { Object.entries(request) - .filter(([key]) => key !== 'ALIAS') - .forEach(([key, value]) => { + .filter(([key]) => key !== 'alias') + .forEach(([_, value]) => { set = this.findDependencies(value, set) }) - } else if (type === 'string') { - const matches = [] - request.replace( - replacementRegex, - (match, g1) => !match.startsWith('\\') && matches.push(g1) - ) + } else if (typeof request === 'string') { + const matches: string[] = [] + request.replace(replacementRegex, (match, g1: any) => { + if (!match.startsWith('\\')) { + matches.push(g1) + } + + return '' + }) const deps = matches.map((m) => m.split('.')[0]) @@ -66,23 +92,27 @@ export default class Request { async exec(cache = new RequestCache()) { let settings = cache.parse({ baseUrl: '', - uri: this.PATH, - method: this.VERB, - jar: this.COOKIEJAR, + uri: this.path, + method: this.verb, + jar: this.cookiejar, - headers: this.HEADERS, - qs: this.PARAMS, - body: this.PAYLOAD, - form: this.FORM, - formData: this.FORMDATA, + headers: this.headers, + qs: this.params, + body: this.payload, + form: this.form, + formData: this.formdata, json: true, simple: false, resolveWithFullResponse: true }) + if (typeof settings !== 'object' || settings === null) { + throw new Error('Error parsing request.') + } + const isPathFullUrl = isUrl(settings.uri) - settings.baseUrl = isPathFullUrl ? '' : this.ENDPOINT + settings.baseUrl = isPathFullUrl ? '' : this.endpoint settings = removeOptionalKeys(settings, [ 'headers', @@ -122,7 +152,7 @@ export default class Request { this.originalRequest ) - cache.add(this.ALIAS, results) + cache.add(this.alias!, results) return results } diff --git a/src/requestCache.ts b/src/requestCache.ts index 12a8758..3fe04b0 100644 --- a/src/requestCache.ts +++ b/src/requestCache.ts @@ -30,7 +30,9 @@ export default class RequestCache { return result } - parse(item: UObjectString | null | undefined) { + parse( + item: { [key: string]: any } | null | undefined + ): string | null | { [key: string]: any } { if (item === null) { return null } diff --git a/src/requestList.ts b/src/requestList.ts index aba81bf..680cbb4 100644 --- a/src/requestList.ts +++ b/src/requestList.ts @@ -1,5 +1,4 @@ -import Config, { RequestObject } from './config' -import Plugins from './plugins' +import Config from './config' import Request from './request' import RequestCache from './requestCache'