Coronncet is a http client library that provides low-level access to TCP connection. Its made with TypeScript and built with Rollup so it supports both commonjs and es.
- Its directly made on top of Node.js TCP API.
- It supports both HTTP and HTTPS. (TLSv1.2 and TLSv1.3)
- Doesn't close the connection after each request.
- It has types by TypeScript.
- It supports HTTP/1.1.
import { HTTPAgent } from 'coronncet';
const conn = new HTTPAgent('https://www.example.com');
conn.connect(); // Async function
const resp = await conn.get('/'); // Async function
conn.disconnect(); // Async function
- We are creating a connection object
- We are calling
connect()
on the connection object. - We are calling
get()
on the connection object and returns the response object. - We are calling
disconnect()
on the connection object.
Is the class that responsible for creating a http agent and maintaining it.
It is the child class of HTTPConnection
.
Difrence beetwen them is that HTTPAgent
has built in http methods.
Option timeout is used for responses that has not Content-Length
header.
connection(Url: string, Options?: { timeout?: number}): Connection;
Connects to the server. It returns a promise that resolves when the connection is established. if the connection is already established, it returns a promise that resolves immediately.
if it returns with true, it means the connection is established.
connection.connect(): Promise<boolean>;
Sets the header for the requests that will be sent to the server.
connection.setHeader(key: string, value: string | number): void;
Sends a GET request to the server for the given path. Its returns a promise that resolves when the response is received.
connection.get(path: string): Promise<Response>;
Sends a POST request to the server for the given path. Its returns a promise that resolves when the response is received.
connection.post(path: string, body: any): Promise<Response>;
Most of the HTTP methods same as
.get()
and.post()
.Supported HTTP methods: GET, POST, PUT, DELETE, HEAD, PATCH
Disconnects from the server. Its same as the connect()
function.
connection.disconnect(): Promise<boolean>;
Type of boolean, default is false
. It indicates whether the connection is established or not. Protected veriable
Type of object, default is {}
. It contains the headers that will be sent to the server.
Is the class that responsible for handling the response.
Returns the raw body of the response. if the response is not fully received, it throw error.
response.getRaw(): Buffer;
Returns the body of the response as a string. With encoded as the parameter of the function. Default is ascıı
.
response.getText(encoding: string): string;
If sended request is a HEAD request
.getText()
and.getRaw()
will throw error.
Type of number. It contains the status code of the response.
Type of object. It contains the headers of the response.