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'
44type Request = http . RequestOptions & {
5- body ?: any ;
6- params ?: object ;
7- } ;
5+ body ?: any
6+ params ?: object
7+ }
88
99type 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 */
5050export 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 */
5757export 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 */
6464export 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 */
7171export 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 */
7878function _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 }
0 commit comments