2.0.0 • Published 5 months ago

simply-https v2.0.0

Weekly downloads
-
License
CC-BY-NC-ND-4.0
Repository
github
Last release
5 months ago

⚡ simply-https

A light weight yet an efficient HTTPS module to make API requests

!IMPORTANT Welcome to v2.0.0

What's new?

  • New syntax to make it easy to switch from node-fetch or vanilla fetch() to simply-https
  • Faster, Efficient, More Type strict.
  • Supports various types for response
  • Fully supports all HTTPS methods
  • Less bloat.

Functions

https()

Https function to replace your good ol' node-fetch and axios.

const { https } = require("simply-https");
https("url", {
  // options (optional)
});

This returns a Promise so you should await it and should be located inside an async function. Or your project should be configured to top-level await

Types:

https(
  url: string,
  options?: HttpsOptions
): Promise<object | Buffer | string | Resolver | ArrayBuffer | Blob>;
  • url: string
  • options: HttpsOptions

  • Resolves: Promise<object | Buffer | string | Resolver | ArrayBuffer | Blob>

Options

HttpsOptions

ParametersTypeDefaultDescription
bodyobject | string (passed as JSON.stringify())The body to send the request (cannot be used in 'GET' request)
urlstringThe URL to call the API (if you are not using the second argument and not using hostname and endpoint options)
hostnamestringThe hostname of the url (Not necessary if URL argument has endpoint with it)
endpointstringEndpoint to request to (Not necessary if URL argument has endpoint with it)
headersRecord<string, string>{ "Content-Type": "application/json" }The headers of the request
method"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "CONNECT" | "OPTIONS" | "TRACE"GETThe method of request to do with the URL
responseType"json" | "stream" | "text" | "blob" | "arrayBuffer" | "buffer"Returns Resolver class where you can do .toJSON, .toString, .toBuffer, .toStream, .toBlob or Promised functions like .json(), .stream(), .text(), .blob() to support vanilla fetch syntax
statusCodenumber200Expected status code
timeoutnumber5000The time limit untill it gets HTTP Timeout
interface HttpsOptions {
  body?: object | string;
  url?: string;
  hostname?: string;
  endpoint?: string;
  headers?: Record<string, string>;
  method?:
    | "GET"
    | "POST"
    | "PUT"
    | "PATCH"
    | "DELETE"
    | "HEAD"
    | "CONNECT"
    | "OPTIONS"
    | "TRACE";
  responseType?: "json" | "stream" | "text" | "blob" | "arrayBuffer" | "buffer";
  statusCode?: number;
  timeout?: number;
}

Response

It returns a Resolver class which contains

Promise based functions

  • arrayBuffer() | array()
  • blob()
  • buffer() | stream()
  • json()
  • text() | string()

Not Promise based

  • toArrayBuffer()
  • toBlob()
  • toBuffer() | toStream()
  • toJSON()
  • toText() | toString()

These functions are used to resolve your data stream into desired format. Can convert into ArrayBuffer Blob Buffer JSON String

Examples

With await

const { https } = require("simply-https");

// should be inside a async function or have top-level await
const data = await https("postman-echo.com/get");
const res = await res.json();
console.log(res);

With .then()

const { https } = require("simply-https");

https("postman-echo.com/get")
  .then((data) => data.json())
  .then((res) => console.log(res));
const { https } = require("simply-https");

https("https://httpbin.org/post", {
  method: "POST",
})
  .then((data) => data.json())
  .then((res) => console.log(res));

With post body

const { https } = require("simply-https");

https("https://httpbin.org/post", {
  method: "POST",
  body: { message: "hello world" },
})
  .then((data) => data.json())
  .then((res) => console.log(res));
const { fetch } = require("node-fetch");

fetch("https://httpbin.org/post", {
  method: "POST",
})
  .then((data) => data.json())
  .then((res) => console.log(res));
const { https } = require("simply-https");

https("https://httpbin.org/post", {
  method: "POST",
  body: { message: "hello world" },
})
  .then((data) => data.json())
  .then((res) => console.log(res));
- fetch("https://httpbin.org/post", {
+ https("https://httpbin.org/post", {

Its that simple. No bloat, no sloppy anymore.