mirror of https://github.com/Seich/Beau.git
Request is green-ish.
This commit is contained in:
parent
c82ee555c2
commit
164c6665bb
|
|
@ -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 () => {
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
112
src/request.ts
112
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue