Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support uri and url options #10

Merged
merged 1 commit into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 47 additions & 49 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

import * as r from 'request'; // Only for type declarations
import fetch, * as f from 'node-fetch';
import {PassThrough} from 'stream';
Expand All @@ -9,7 +7,7 @@ import * as uuid from 'uuid';
const HttpsProxyAgent = require('https-proxy-agent');

interface RequestToFetchOptions {
(reqOpts: r.OptionsWithUri): [string, f.RequestInit];
(reqOpts: r.Options): [string, f.RequestInit];
}

interface Headers {
Expand All @@ -20,48 +18,48 @@ interface FetchToRequestResponse {
(res: f.Response): r.Response;
}

const requestToFetchOptions: RequestToFetchOptions =
(reqOpts: r.OptionsWithUri) => {
const options: f.RequestInit = {
method: reqOpts.method || 'GET',
...reqOpts.timeout && {timeout: reqOpts.timeout},
...reqOpts.gzip && {compress: reqOpts.gzip},

};

if (typeof reqOpts.json === 'object') {
// Add Content-type: application/json header
if (!reqOpts.headers) {
reqOpts.headers = {};
}
reqOpts.headers['Content-Type'] = 'application/json';

// Set body to JSON representation of value
options.body = JSON.stringify(reqOpts.json);
} else {
if (typeof reqOpts.body !== 'string') {
options.body = JSON.stringify(reqOpts.body);
} else {
options.body = reqOpts.body;
}
}
const requestToFetchOptions: RequestToFetchOptions = (reqOpts: r.Options) => {
const options: f.RequestInit = {
method: reqOpts.method || 'GET',
...reqOpts.timeout && {timeout: reqOpts.timeout},
...reqOpts.gzip && {compress: reqOpts.gzip},

options.headers = reqOpts.headers as Headers;

let uri: string = reqOpts.uri as string;
if (reqOpts.useQuerystring === true || typeof reqOpts.qs === 'object') {
const qs = require('querystring');
const params = qs.stringify(reqOpts.qs);
uri = uri + '?' + params;
}

if (reqOpts.proxy || process.env.HTTP_PROXY || process.env.HTTPS_PROXY) {
const proxy = (process.env.HTTP_PROXY || process.env.HTTPS_PROXY)!;
options.agent = new HttpsProxyAgent(proxy);
}
};

return [uri, options];
};
if (typeof reqOpts.json === 'object') {
// Add Content-type: application/json header
if (!reqOpts.headers) {
reqOpts.headers = {};
}
reqOpts.headers['Content-Type'] = 'application/json';

// Set body to JSON representation of value
options.body = JSON.stringify(reqOpts.json);
} else {
if (typeof reqOpts.body !== 'string') {
options.body = JSON.stringify(reqOpts.body);
} else {
options.body = reqOpts.body;
}
}

options.headers = reqOpts.headers as Headers;

let uri = ((reqOpts as r.OptionsWithUri).uri ||
(reqOpts as r.OptionsWithUrl).url) as string;
if (reqOpts.useQuerystring === true || typeof reqOpts.qs === 'object') {
const qs = require('querystring');
const params = qs.stringify(reqOpts.qs);
uri = uri + '?' + params;
}

if (reqOpts.proxy || process.env.HTTP_PROXY || process.env.HTTPS_PROXY) {
const proxy = (process.env.HTTP_PROXY || process.env.HTTPS_PROXY)!;
options.agent = new HttpsProxyAgent(proxy);
}

return [uri, options];
};

interface Callback {
(err: Error|null, response?: r.Response, body?: {}|string): void;
Expand All @@ -78,10 +76,10 @@ const fetchToRequestResponse: FetchToRequestResponse = (res: f.Response) => {
// Mimics `request`. Can be used as `request(options, callback)`
// or `request.defaults(opts)(options, callback)`
interface TeenyRequest {
(reqOpts: r.OptionsWithUri, callback: Callback): void;
(reqOpts: r.Options, callback: Callback): void;
defaults:
((options: r.OptionsWithUri) =>
((reqOpts: r.OptionsWithUri, callback: Callback) => void));
((options:
r.Options) => ((reqOpts: r.Options, callback: Callback) => void));
}

// create POST body from two parts as multipart/related content-type
Expand Down Expand Up @@ -110,7 +108,7 @@ const createMultipartStream =
};

const teenyRequest =
((reqOpts: r.OptionsWithUri, callback?: Callback) => {
((reqOpts: r.Options, callback?: Callback) => {
const [uri, options] = requestToFetchOptions(reqOpts);

const multipart: r.RequestPart[] = reqOpts.multipart as r.RequestPart[];
Expand Down Expand Up @@ -242,8 +240,8 @@ const teenyRequest =
return;
}) as TeenyRequest;

teenyRequest.defaults = (defaults: r.OptionsWithUri) => {
return (reqOpts: r.OptionsWithUri,
teenyRequest.defaults = (defaults: r.Options) => {
return (reqOpts: r.Options,
callback:
(err: Error|null, response?: r.Response, body?: {}|string) =>
void) => {
Expand Down
6 changes: 1 addition & 5 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@

'use strict';

import * as assert from 'assert';
import * as r from 'request';

Expand All @@ -19,8 +16,7 @@ describe('teeny', () => {
done();
});
}), it('should set defaults', (done) => {
const defaultRequest =
teenyRequest.defaults({timeout: 60000} as r.OptionsWithUri);
const defaultRequest = teenyRequest.defaults({timeout: 60000} as r.Options);
defaultRequest(
{uri: 'https://jsonplaceholder.typicode.com/todos/1'},
// tslint:disable-next-line:no-any
Expand Down