Comparing version 1.6.0 to 1.7.0
@@ -15,2 +15,5 @@ ## Description | ||
date: new Date(), | ||
nested: { | ||
name: 'new object', | ||
}, | ||
}; | ||
@@ -21,3 +24,7 @@ | ||
console.log(clone === some); // false | ||
console.log(clone.nested === some.nested); // false | ||
console.log(clone.nested.name === some.nested.name); // true | ||
console.log(clone.numb === some.numb); // true | ||
console.log(clone.date === some.date); // false | ||
console.log(clone.date.toString() === some.date.toString()); // true | ||
console.log(clone.date instanceof Date); // true | ||
@@ -32,2 +39,5 @@ ``` | ||
date = new Date(); | ||
nested = { | ||
name: 'new object', | ||
}; | ||
@@ -43,5 +53,9 @@ sayHi() { | ||
console.log(clone === classInstance); // false | ||
console.log(clone.nested === classInstance.nested); // false | ||
console.log(clone.nested.name === classInstance.nested.name); // true | ||
console.log(clone.name === classInstance.name); // true | ||
console.log(clone.date === classInstance.date); // false | ||
console.log(clone.date.toString() === classInstance.date.toString()); // true | ||
console.log(clone.date instanceof Date); //true | ||
console.log(clone.sayHi); // undefined | ||
``` |
@@ -9,2 +9,5 @@ ## Description | ||
Implemented for convenience with types, but **does not support response | ||
stream features**. | ||
## Usage | ||
@@ -201,5 +204,3 @@ | ||
```ts | ||
const response = await nuti.req.get('http://localhost:3000/foo', { | ||
'content-type': 'text/html', | ||
}); | ||
const response = await nuti.req.get('http://localhost:3000/not-found'); | ||
@@ -206,0 +207,0 @@ console.log(response); |
@@ -34,4 +34,14 @@ /*! | ||
delete<T extends object>(url: string, body: unknown, headers?: http.IncomingHttpHeaders): Promise<Response<T>>; | ||
/** | ||
* Main request's handler. | ||
* @returns Promise which will be resolved **after** response stream is finished. | ||
*/ | ||
private request; | ||
/** | ||
* Function for validation request input. | ||
* @param payload options for request and url | ||
* @returns valid request options, raw request body and valid protocol (http(s)) | ||
*/ | ||
private validateReqInput; | ||
} | ||
export declare const req: HttpClient; |
@@ -63,29 +63,14 @@ "use strict"; | ||
} | ||
/** | ||
* Main request's handler. | ||
* @returns Promise which will be resolved **after** response stream is finished. | ||
*/ | ||
async request(url, options) { | ||
return new Promise((resolve, reject) => { | ||
const { protocol, hostname, port, pathname, host } = new URL(url); | ||
const validProtocol = protocols[protocol]; | ||
if (validProtocol == null) { | ||
throw new Error(`Unsupported protocol: ${protocol.replace(':', '')}`); | ||
} | ||
const { headers = {}, body = {}, method } = options; | ||
const stringReqBody = JSON.stringify(body); | ||
if (headers['content-type'] == null) { | ||
Object.assign(headers, { | ||
'content-type': 'application/json', | ||
'content-length': stringReqBody.length, | ||
}); | ||
} | ||
const opts = { | ||
protocol, | ||
hostname, | ||
host, | ||
port, | ||
path: pathname, | ||
headers, | ||
method, | ||
}; | ||
const { opts, validProtocol, rawReqBody } = this.validateReqInput({ | ||
...options, | ||
url, | ||
}); | ||
const req = validProtocol.request(opts, (res) => { | ||
let rawData = ''; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const status = res.statusCode; | ||
@@ -113,8 +98,41 @@ const isJSON = res.headers['content-type']?.includes('application/json') === true; | ||
req.on('error', reject); | ||
req.write(stringReqBody); | ||
req.write(rawReqBody); | ||
req.end(); | ||
}); | ||
} | ||
/** | ||
* Function for validation request input. | ||
* @param payload options for request and url | ||
* @returns valid request options, raw request body and valid protocol (http(s)) | ||
*/ | ||
validateReqInput(payload) { | ||
const { headers = {}, body = {}, method, url } = payload; | ||
const { protocol, hostname, port, pathname, host } = new URL(url); | ||
const validProtocol = protocols[protocol]; | ||
if (validProtocol == null) { | ||
throw new Error(`Unsupported protocol: ${protocol.replace(':', '')}`); | ||
} | ||
const rawReqBody = JSON.stringify(body); | ||
if (headers['content-type'] == null) { | ||
Object.assign(headers, { | ||
'content-type': 'application/json', | ||
'content-length': rawReqBody.length, | ||
}); | ||
} | ||
return { | ||
opts: { | ||
protocol, | ||
hostname, | ||
host, | ||
port, | ||
path: pathname, | ||
headers, | ||
method, | ||
}, | ||
validProtocol, | ||
rawReqBody, | ||
}; | ||
} | ||
} | ||
exports.HttpClient = HttpClient; | ||
exports.req = new HttpClient(); |
@@ -9,3 +9,3 @@ /*! | ||
export type Method = 'POST' | 'PUT' | 'GET' | 'DELETE'; | ||
export interface Response<T extends object> { | ||
export interface Response<T> { | ||
status: number; | ||
@@ -12,0 +12,0 @@ contentLength: number; |
@@ -1,2 +0,2 @@ | ||
/** | ||
/*! | ||
* @description The main file of `nuti` package. | ||
@@ -46,4 +46,4 @@ * @author Andrii Lytovchenko <andr.lyt.dev@gmail.com> | ||
/** | ||
* An object with methods to generate random `string`, | ||
* `number` and `boolean` types. | ||
* A helper for generating random `string`, `number` | ||
* and `boolean` values. | ||
* @see [docs](../docs/rand.md) | ||
@@ -63,3 +63,8 @@ */ | ||
clone: <T extends object | unknown[]>(entity: T) => import("./clone/types").OmitMethods<T>; | ||
/** | ||
* A helper for performing valid mathematical operations with floats. | ||
* @see [docs](../docs/floats.md) | ||
*/ | ||
floats: import("./floats").Floats; | ||
}; | ||
export default nuti; |
"use strict"; | ||
/** | ||
/*! | ||
* @description The main file of `nuti` package. | ||
@@ -16,2 +16,3 @@ * @author Andrii Lytovchenko <andr.lyt.dev@gmail.com> | ||
const clone_1 = require("./clone"); | ||
const floats_1 = require("./floats"); | ||
exports.nuti = { | ||
@@ -52,4 +53,4 @@ /** | ||
/** | ||
* An object with methods to generate random `string`, | ||
* `number` and `boolean` types. | ||
* A helper for generating random `string`, `number` | ||
* and `boolean` values. | ||
* @see [docs](../docs/rand.md) | ||
@@ -69,3 +70,8 @@ */ | ||
clone: clone_1.clone, | ||
/** | ||
* A helper for performing valid mathematical operations with floats. | ||
* @see [docs](../docs/floats.md) | ||
*/ | ||
floats: floats_1.floats, | ||
}; | ||
exports.default = exports.nuti; |
{ | ||
"name": "nuti", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"author": "Andrii Lytovchenko <andr.lyt.dev@gmail.com>", | ||
@@ -49,11 +49,11 @@ "license": "MIT", | ||
"@types/jest": "^29.4.0", | ||
"@types/node": "^18.13.0", | ||
"@typescript-eslint/eslint-plugin": "^5.51.0", | ||
"@typescript-eslint/parser": "^5.52.0", | ||
"cspell": "^6.24.0", | ||
"eslint": "^8.34.0", | ||
"eslint-config-prettier": "^8.6.0", | ||
"@types/node": "^18.15.3", | ||
"@typescript-eslint/eslint-plugin": "^5.55.0", | ||
"@typescript-eslint/parser": "^5.55.0", | ||
"cspell": "^6.29.3", | ||
"eslint": "^8.36.0", | ||
"eslint-config-prettier": "^8.7.0", | ||
"eslint-plugin-jest": "^27.2.1", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"jest": "^29.4.2", | ||
"jest": "^29.5.0", | ||
"npsh": "^2.0.0", | ||
@@ -60,0 +60,0 @@ "prettier": "^2.8.4", |
@@ -35,2 +35,3 @@ # nuti | ||
7. [clone](./docs/clone.md) | ||
8. [floats](./docs/floats.md) | ||
@@ -37,0 +38,0 @@ [npm-img]: https://img.shields.io/npm/v/nuti.svg?logo=npm |
48582
39
996
45