Skip to content

Commit 272afd2

Browse files
params in request options will not stringify if already a string
1 parent e515311 commit 272afd2

5 files changed

Lines changed: 52 additions & 41 deletions

File tree

.prettierrc.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
semi: false
2+
singleQuote: true
3+
useTabs: true
4+
bracketSpacing: true
5+
arrowParens: always
6+
trailingComma: none

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ Another `README.md` update
1313
# 1.0.3
1414

1515
Fixed type exports in `package.json`
16+
17+
# 1.0.4
18+
19+
params in request options will not stringify if already a string

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "@splitscript.js/https",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Send HTTPS/HTTP requests",
5+
"types": "./dist/types/index.d.ts",
56
"exports": {
67
".": {
78
"import": "./dist/esm/index.js",

src/index.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import * as https from 'node:https';
2-
import * as http from 'node:http';
3-
import { isHttps, parseData, toQuery } from './utils.js';
1+
import * as https from 'node:https'
2+
import * as http from 'node:http'
3+
import { isHttps, parseData, toQuery } from './utils.js'
44
type Request = http.RequestOptions & {
5-
body?: any;
6-
params?: object;
7-
};
5+
body?: any
6+
params?: object
7+
}
88

99
type requestRes = {
10-
res: http.IncomingMessage;
11-
data: string | object;
12-
};
10+
res: http.IncomingMessage
11+
data: string | object
12+
}
1313
/**
1414
* Send a `http(s)` request
1515
*
@@ -20,66 +20,66 @@ export function request(
2020
options: Request = { method: 'GET' }
2121
): Promise<requestRes> {
2222
if (!url.startsWith('https://') && !url.startsWith('http://')) {
23-
url = (options.port === 80 ? 'http://' : 'https://') + url;
23+
url = (options.port === 80 ? 'http://' : 'https://') + url
2424
}
25-
const client = isHttps(url, options) ? https : http;
25+
const client = isHttps(url, options) ? https : http
2626
if (options.params) {
27-
url += toQuery(options.params);
27+
url += toQuery(options.params)
2828
}
2929
return new Promise((resolve, reject) => {
3030
const req = client.request(url, options, (res) => {
31-
let data = '';
31+
let data = ''
3232

33-
res.setEncoding('utf-8');
33+
res.setEncoding('utf-8')
3434
res.on('data', (chunk: string) => {
35-
data += chunk;
36-
});
35+
data += chunk
36+
})
3737

3838
res.on('end', () => {
39-
resolve({ res, data: parseData(res, data) });
40-
});
41-
});
42-
req.on('error', reject);
39+
resolve({ res, data: parseData(res, data) })
40+
})
41+
})
42+
req.on('error', reject)
4343
if (options.body) {
44-
req.write(Buffer.from(JSON.stringify(options.body)));
44+
req.write(Buffer.from(JSON.stringify(options.body)))
4545
}
46-
req.end();
47-
});
46+
req.end()
47+
})
4848
}
4949
/** Sends a `GET` request */
5050
export function get(
5151
url: string,
5252
options: Request = { method: 'GET' }
5353
): Promise<requestRes> {
54-
return request(url, { method: 'GET', ...options });
54+
return request(url, { method: 'GET', ...options })
5555
}
5656
/** Sends a `POST` request */
5757
export function post(
5858
url: string,
5959
options: Request = { method: 'POST' }
6060
): Promise<requestRes> {
61-
return request(url, { method: 'POST', ...options });
61+
return request(url, { method: 'POST', ...options })
6262
}
6363
/** Sends a `PUT` request */
6464
export function put(
6565
url: string,
6666
options: Request = { method: 'PUT' }
6767
): Promise<requestRes> {
68-
return request(url, { method: 'PUT', ...options });
68+
return request(url, { method: 'PUT', ...options })
6969
}
7070
/** Sends a `PATCH` request */
7171
export function patch(
7272
url: string,
7373
options: Request = { method: 'PATCH' }
7474
): Promise<requestRes> {
75-
return request(url, { method: 'PATCH', ...options });
75+
return request(url, { method: 'PATCH', ...options })
7676
}
7777
/** Sends a `DELETE` request */
7878
function _delete(
7979
url: string,
8080
options: Request = { method: 'DELETE' }
8181
): Promise<requestRes> {
82-
return request(url, { method: 'DELETE', ...options });
82+
return request(url, { method: 'DELETE', ...options })
8383
}
84-
export { _delete as delete };
85-
export default { request, get, post, put, patch, delete: _delete };
84+
export { _delete as delete }
85+
export default { request, get, post, put, patch, delete: _delete }

src/utils.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import { RequestOptions } from 'https';
2-
import { IncomingMessage } from 'http';
1+
import { RequestOptions } from 'https'
2+
import { IncomingMessage } from 'http'
33
export function isHttps(url: string, options: RequestOptions = {}): boolean {
4-
return url.startsWith('https://') || options.port === 443;
4+
return url.startsWith('https://') || options.port === 443
55
}
66
export function parseData(res: IncomingMessage, data: any): string | object {
7-
const type = res.headers['content-type']?.split(';')[0].trim();
7+
const type = res.headers['content-type']?.split(';')[0].trim()
88
if (type === 'application/json') {
9-
return JSON.parse(data);
10-
} else return data;
9+
return JSON.parse(data)
10+
} else return data
1111
}
1212
export function toQuery(obj: object) {
1313
const queryString = Object.entries(obj)
1414
.map(([key, val]) => {
15-
let stringified = JSON.stringify(val);
16-
return `${key}=${encodeURIComponent(stringified)}`;
15+
const stringified = typeof val === 'string' ? val : JSON.stringify(val)
16+
return `${key}=${encodeURIComponent(stringified)}`
1717
})
18-
.join('&');
19-
return '?' + queryString;
18+
.join('&')
19+
return '?' + queryString
2020
}

0 commit comments

Comments
 (0)