Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(request): HTTP/s requests with get and post helpers
Closes #13
- Loading branch information
1 parent
1a5bfdb
commit 7ba1702
Showing
6 changed files
with
212 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters