npm package: @spicadev/network
Description: Simplify networking in Node.js with support for TLS, UDP, WS, Socket, DNS, and TCP.
Author: @spicadev
Latest version: 0.3.0-tr3

# @spicadev/network ![NPM](https://img.shields.io/npm/v/%40spicadev%2Fnetwork?style=flat-square&logo=npm&logoColor=%2300000000&label=%40spicadev%2Fnetwork&labelColor=%23FF8F8F&color=%23999999&cacheSeconds=10800&link=https%3A%2F%2Fnpmjs.com%2Fpackage%2F%40spicadev%2Fnetwork&link=https%3A%2F%2Fnpmjs.com%2Fpackage%2F%40spicadev%2Fnetwork%2Fv%2F0.2.0-beta)


Simplify networking in Node.js with support for TLS, UDP, WS, Socket, DNS, and TCP.

# Installation
Via [npm](https://npmjs.com/package/@spicadev/network):
```sh
npm install @spicadev/network
```

Via [Node.js](https://nodejs.org/):
```js
const net = require('@spicadev/network')
const { http } = require('@spicadev/network')
```

Using `import`:
```js
import net from '@spicadev/network'
import * as net from '@spicadev/network'
import { http } from '@spicadev/network'
```

# Changes
> - Modified README.md
> - Bug Fix + More efficient
> - Added `redirect` property to HttpClientOptions
> - Removed `Herobrine`

# Next Update (Plan)
> - Socket APIs
> - WebSocket APIs

#

# API

#### net.http.Server(options?: HttpServerOptions)
Create a http server instance.

#### net.http.Client(...args?: HttpClientRequestArguments)
Create a http client instance.

#### net.http.ClientRequest(...HttpClientRequestArguments):
#### Promise<net.http.ClientResponse, Error>?
Create a http request

#### net.http.ClientResponse(
####    res: http.IncomingMessage | http2.ClientResponse,
####    data: Buffer)
Create a http response

#### net.http.http
Object containing `http: node:http`, `https: node:https`, `http2: node:http2 | null`, and `get(options?: HttpChooserOptions)`

# Static Methods

#### net.http.Server.createServer(options?: HttpServerOptions): http.Server
Alias for `http.Server` constructor

#### net.http.Client.createClient(...HttpClientRequestArguments):
#### Promise<net.http.ClientResponse, Error>?
Alias for `http.Client` constructor

# Prototypes

#### net.http.Server.route(httpMethod: string, route: string | regex,
####   callback: function(req, res, next)): net.http.Server
Open a route with specified http method, route/path, and callback

```ts
net.http.Server[HttpMethods](route: string | regex, callback: function(req, res, next)): net.http.Server
```
Open a route with specified http method, route/path, and callback (if http-method haven't implemented, use net.http.Server.route instead)

```ts
net.http.Server.hook(name: HttpHooks, callback: function): http.Server
```
Hook the available hooks

```ts
net.http.Server.decorate(name: string, value: any): http.Server
```
Decorate the server object, and req+res object per request

```ts
net.http.Server.on(event: string, callback: Function): void
```
Listen to event emitted by EventEmitter
> ### Available events:
> - error
> - clientError

```ts
net.http.Server.listen(): Promise<ServerAddress, void>
```
Listen to the server

```ts
net.http.Server.close(): Promise<void, void>
```
Close the server

```ts
net.http.Client.request(...HttpClientRequestArguments): Promise<net.http.ClientResponse, Error>
```
Create a request

```ts
net.http.Client[HttpMethods](...HttpClientRequestArguments): Promise<net.http.ClientResponse, Error>
```
Create a request with specific `http-method` (Use `http.Client.request` for more flexibility)

```ts
net.http.http.get(options?: HttpChooserOptions): node:http | node:https | node:http2
```
Retrieve nodejs http(s) module

# Types

## Header
- Object

## HttpHooks
> Hook 1st argument are always `req: net.http.ServerRequest`
>
> Hook 2nd argument are always `res: net.http.ServerResponse`
- requestIncoming(...HooksArguments)
- preProcess(...HooksArguments)
- error(...HooksArguments, error: Error)
- unhandledRoute(...HooksArguments)

## HttpMethods
- get
- post
- put
- patch
- delete

## HttpServerOptions
- `http` -> HTTP-related options
  - `...all node:http.createServer options except IncomingMessage and ServerResponse`
  - `...all node:http2.createServer options excopt IncomingMessage and ServerResponse`
- `secure` -> HTTP2 and HTTPS related options
  - `cert`: string -> Filepath targeting your ssl certificate.
  - `key`: string -> Filepath targeting your ssl key
- `...ServerAddress` -> Server address (excluding family)
- `...HttpChooserOptions` -> Chooser options

## HttpClientOptions
- `version`: number -> The http version to be use, Either 1 (http+https) or 2 (http2), default: 1
- `url`: ValidURL -> URL for the request to be send to
- `body`: ValidBody -> The request body, default: null
- `headers`: Headers -> The request header, default: null
- `method`: HTTPMethod -> HTTP method to use, default: GET
- `redirect`: string -> What to do when there are redirect, either `follow`, `manual`, and `error`

## HttpClientRequestArguments
- 2 Arguments:
  - `url`: ValidURL -> URL for the request to be send to, can be placed both as 1st or 2nd argument
  - `options`: HttpClientOptions -> Options used, can be placed both as 1st or 2nd argument
- 1 Argument: ValidURL | HttpClientOptions -> Either the url, or options that contain `url` property.

## HttpChooserOptions
- `useHTTP2`: boolean -> Wheter to create http2 (secure) server, throw error of http2 doesn't available, default: `false`
- `secure`: boolean -> Wheter to create a https (secure) server, default: `false`

## ServerAddress
- `host`: string -> The server host, default for server: `0.0.0.0`
- `port`: number -> The server port, default for server: `3000`
- `family`: boolean -> Either Ipv4 or Ipv6

## ValidURL
- Native URL
- net.utilty.Url
- string
- object with the same property as URL (for example: host, protocol, pathname)

## ValidBody
- Object (json-stringified)
- String
- Buffer
- ArrayBuffer
- All typed array including Uint8Array