Skip to content

Commit

Permalink
feat(request): HTTP/s requests with get and post helpers
Browse files Browse the repository at this point in the history
Closes #13
  • Loading branch information
timkinnane committed Aug 14, 2018
1 parent 1a5bfdb commit 7ba1702
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 7 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -84,11 +84,13 @@
"@types/inquirer": "^0.0.42",
"@types/mongoose": "^5.2.0",
"@types/node": "^10.0.0",
"@types/request": "^2.47.1",
"@types/yargs": "^11.0.0",
"chalk": "^2.4.0",
"dotenv": "^6.0.0",
"inquirer": "^6.0.0",
"mongoose": "^5.2.0",
"request": "^2.88.0",
"winston": "^3.0.0",
"yargs": "^12.0.0"
},
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -4,6 +4,7 @@ export * from './lib/logger'
export * from './lib/id'
export * from './lib/state'
export * from './lib/middleware'
export * from './lib/request'
export * from './lib/adapter'
export * from './lib/adapter-classes'
export * from './lib/user'
Expand Down
2 changes: 1 addition & 1 deletion src/lib/e2e-tests/e2e.spec.ts
Expand Up @@ -9,7 +9,7 @@ class MockMessenger extends bot.MessageAdapter {
async start () { return }
async shutdown () { return }
}
const sandbox = sinon.sandbox.create()
const sandbox = sinon.createSandbox()

describe('[E2E]', () => {
beforeEach(() => {
Expand Down
50 changes: 50 additions & 0 deletions src/lib/request.spec.ts
@@ -0,0 +1,50 @@
import 'mocha'
import { expect } from 'chai'
import * as bot from '..'

// @todo replace external requests with internal 'json-server'

describe('[request]', () => {
describe('.request', () => {
it('handles GET request without data', async () => {
const result = await bot.request({
method: 'GET',
uri: 'https://jsonplaceholder.typicode.com/users/1'
})
expect(result).to.include({ id: 1 })
})
it('handles GET request with data', async () => {
const result = await bot.request({
method: 'GET',
uri: 'https://jsonplaceholder.typicode.com/posts',
qs: { userId: 1 }
})
expect(result[result.length - 1]).to.include({ userId: 1 })
})
it('handles POST request with data', async () => {
const result = await bot.request({
method: 'POST',
uri: 'https://jsonplaceholder.typicode.com/posts',
json: true,
body: { userId: 1 }
})
expect(result).to.include({ userId: 1 })
})
})
describe('.getRequest', () => {
it('handles request without data', async () => {
const result = await bot.getRequest('https://jsonplaceholder.typicode.com/users/1')
expect(result).to.include({ id: 1 })
})
it('handles request with data', async () => {
const result = await bot.getRequest('https://jsonplaceholder.typicode.com/posts', { userId: 1 })
expect(result[result.length - 1]).to.include({ userId: 1 })
})
})
describe('.postRequest', () => {
it('handles request with data', async () => {
const result = await bot.postRequest('https://jsonplaceholder.typicode.com/posts', { userId: 1 })
expect(result).to.include({ userId: 1 })
})
})
})
78 changes: 78 additions & 0 deletions src/lib/request.ts
@@ -0,0 +1,78 @@
import * as client from 'request'
import * as bot from '..'

/** Standard arguments object for requests */
export interface IRequestMeta {
data?: any
json?: boolean
auth?: {
user: string,
pass: string,
sendImmediately?: boolean,
bearer?: string
}
headers?: { [name: string]: string }
}

/** HTTP/S request handler, promisifies request callbacks */
export function request (
opts: client.CoreOptions & client.OptionsWithUri
): Promise<any> {
bot.logger.info(`[request] ${opts.method} ${opts.uri} ${(opts.body || opts.qs)
? 'with data (' + Object.keys(opts.body || opts.qs).join(', ') + ')'
: 'without data'
}`)
return new Promise((resolve, reject) => {
opts.callback = (err: Error, res: client.Response, body: any) => {
const result = res && res.statusCode ? res.statusCode : 'unknown'
if (err) {
bot.logger.error(`[request] GET ${result} error ${err.message}`)
return reject(err)
}
if (Buffer.isBuffer(body)) {
return reject('[request] GET body was buffer (HTML, not JSON)')
}
const data = (opts.json) ? body : JSON.parse(body)
const keys = Object.keys(data).join(', ')
bot.logger.info(`[request] GET ${result} success (${keys})`)
resolve(data)
}
if (!opts.method || opts.method === 'GET') client.get(opts)
else if (opts.method === 'POST') client.post(opts)
else if (opts.method === 'PUT') client.put(opts)
else if (opts.method === 'PATCH') client.patch(opts)
else if (opts.method === 'DELETE') client.del(opts)
else if (opts.method === 'HEAD') client.head(opts)
})
}

/** GET request handler, adds data to query string with default options */
export function getRequest (
url: string,
data = {},
options?: client.CoreOptions & client.Options
) {
const opts: client.OptionsWithUri = {
method: 'GET',
uri: url,
qs: data
}
if (options) Object.assign(opts, options)
return request(opts)
}

/** POST request handler, adds data to body with default options */
export function postRequest (
url: string,
data = {},
options?: client.CoreOptions & client.Options
) {
const opts: client.OptionsWithUri = {
method: 'POST',
uri: url,
body: data,
json: true
}
if (options) Object.assign(opts, options)
return request(opts)
}
86 changes: 80 additions & 6 deletions yarn.lock
Expand Up @@ -108,6 +108,10 @@
dependencies:
"@types/node" "*"

"@types/caseless@*":
version "0.12.1"
resolved "https://registry.yarnpkg.com/@types/caseless/-/caseless-0.12.1.tgz#9794c69c8385d0192acc471a540d1f8e0d16218a"

"@types/chai@^4.1.0":
version "4.1.4"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.4.tgz#5ca073b330d90b4066d6ce18f60d57f2084ce8ca"
Expand All @@ -116,6 +120,12 @@
version "1.2.0"
resolved "https://registry.yarnpkg.com/@types/events/-/events-1.2.0.tgz#81a6731ce4df43619e5c8c945383b3e62a89ea86"

"@types/form-data@*":
version "2.2.1"
resolved "https://registry.yarnpkg.com/@types/form-data/-/form-data-2.2.1.tgz#ee2b3b8eaa11c0938289953606b745b738c54b1e"
dependencies:
"@types/node" "*"

"@types/fs-extra@5.0.1":
version "5.0.1"
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-5.0.1.tgz#cd856fbbdd6af2c11f26f8928fd8644c9e9616c9"
Expand Down Expand Up @@ -189,6 +199,15 @@
version "9.6.23"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.6.23.tgz#fc429962c1b75f32bd66214a3997f660e8434f0d"

"@types/request@^2.47.1":
version "2.47.1"
resolved "https://registry.yarnpkg.com/@types/request/-/request-2.47.1.tgz#25410d3afbdac04c91a94ad9efc9824100735824"
dependencies:
"@types/caseless" "*"
"@types/form-data" "*"
"@types/node" "*"
"@types/tough-cookie" "*"

"@types/rx-core-binding@*":
version "4.0.4"
resolved "https://registry.yarnpkg.com/@types/rx-core-binding/-/rx-core-binding-4.0.4.tgz#d969d32f15a62b89e2862c17b3ee78fe329818d3"
Expand Down Expand Up @@ -294,6 +313,10 @@
dependencies:
"@types/node" "*"

"@types/tough-cookie@*":
version "2.3.3"
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-2.3.3.tgz#7f226d67d654ec9070e755f46daebf014628e9d9"

"@types/yargs@^11.0.0":
version "11.1.1"
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-11.1.1.tgz#2e724257167fd6b615dbe4e54301e65fe597433f"
Expand All @@ -302,7 +325,7 @@ abbrev@1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"

ajv@^5.1.0:
ajv@^5.1.0, ajv@^5.3.0:
version "5.5.2"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-5.5.2.tgz#73b5eeca3fab653e3d3f9422b341ad42205dc965"
dependencies:
Expand Down Expand Up @@ -477,6 +500,10 @@ aws4@^1.6.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.7.0.tgz#d4d0e9b9dbfca77bf08eeb0a8a471550fe39e289"

aws4@^1.8.0:
version "1.8.0"
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f"

babel-code-frame@^6.22.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b"
Expand Down Expand Up @@ -820,7 +847,7 @@ colorspace@1.1.x:
color "3.0.x"
text-hex "1.0.x"

combined-stream@1.0.6, combined-stream@~1.0.5:
combined-stream@1.0.6, combined-stream@~1.0.5, combined-stream@~1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.6.tgz#723e7df6e801ac5613113a7e445a9b69cb632818"
dependencies:
Expand Down Expand Up @@ -1226,7 +1253,7 @@ extend@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.1.tgz#a755ea7bc1adfcc5a31ce7e762dbaadc5e636444"

extend@~3.0.1:
extend@~3.0.1, extend@~3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.2.tgz#f8b1136b4071fbd8eb140aff858b1019ec2915fa"

Expand Down Expand Up @@ -1406,7 +1433,7 @@ forever-agent@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"

form-data@~2.3.1:
form-data@~2.3.1, form-data@~2.3.2:
version "2.3.2"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.2.tgz#4970498be604c20c005d4f5c23aecd21d6b49099"
dependencies:
Expand Down Expand Up @@ -1609,6 +1636,13 @@ har-validator@~5.0.3:
ajv "^5.1.0"
har-schema "^2.0.0"

har-validator@~5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/har-validator/-/har-validator-5.1.0.tgz#44657f5688a22cfd4b72486e81b3a3fb11742c29"
dependencies:
ajv "^5.3.0"
har-schema "^2.0.0"

has-ansi@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-2.0.0.tgz#34f5049ce1ecdf2b0649af3ef24e45ed35416d91"
Expand Down Expand Up @@ -2367,7 +2401,7 @@ mime-db@~1.35.0:
version "1.35.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.35.0.tgz#0569d657466491283709663ad379a99b90d9ab47"

mime-types@^2.1.12, mime-types@~2.1.17:
mime-types@^2.1.12, mime-types@~2.1.17, mime-types@~2.1.19:
version "2.1.19"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.19.tgz#71e464537a7ef81c15f2db9d97e913fc0ff606f0"
dependencies:
Expand Down Expand Up @@ -2681,6 +2715,10 @@ oauth-sign@~0.8.2:
version "0.8.2"
resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.8.2.tgz#46a6ab7f0aead8deae9ec0565780b7d4efeb9d43"

oauth-sign@~0.9.0:
version "0.9.0"
resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455"

object-assign@^4.0.1, object-assign@^4.1.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
Expand Down Expand Up @@ -2974,6 +3012,10 @@ pseudomap@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"

psl@^1.1.24:
version "1.1.29"
resolved "https://registry.yarnpkg.com/psl/-/psl-1.1.29.tgz#60f580d360170bb722a797cc704411e6da850c67"

pstree.remy@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/pstree.remy/-/pstree.remy-1.1.0.tgz#f2af27265bd3e5b32bbfcc10e80bac55ba78688b"
Expand All @@ -2988,7 +3030,7 @@ q@^1.0.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7"

qs@~6.5.1:
qs@~6.5.1, qs@~6.5.2:
version "6.5.2"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"

Expand Down Expand Up @@ -3138,6 +3180,31 @@ request@^2.87.0:
tunnel-agent "^0.6.0"
uuid "^3.1.0"

request@^2.88.0:
version "2.88.0"
resolved "https://registry.yarnpkg.com/request/-/request-2.88.0.tgz#9c2fca4f7d35b592efe57c7f0a55e81052124fef"
dependencies:
aws-sign2 "~0.7.0"
aws4 "^1.8.0"
caseless "~0.12.0"
combined-stream "~1.0.6"
extend "~3.0.2"
forever-agent "~0.6.1"
form-data "~2.3.2"
har-validator "~5.1.0"
http-signature "~1.2.0"
is-typedarray "~1.0.0"
isstream "~0.1.2"
json-stringify-safe "~5.0.1"
mime-types "~2.1.19"
oauth-sign "~0.9.0"
performance-now "^2.1.0"
qs "~6.5.2"
safe-buffer "^5.1.2"
tough-cookie "~2.4.3"
tunnel-agent "^0.6.0"
uuid "^3.3.2"

require-directory@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"
Expand Down Expand Up @@ -3645,6 +3712,13 @@ tough-cookie@~2.3.3:
dependencies:
punycode "^1.4.1"

tough-cookie@~2.4.3:
version "2.4.3"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.4.3.tgz#53f36da3f47783b0925afa06ff9f3b165280f781"
dependencies:
psl "^1.1.24"
punycode "^1.4.1"

trim-right@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003"
Expand Down

0 comments on commit 7ba1702

Please sign in to comment.