simply-https
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

⚡ 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

Parameters Type Default Description
body object | string (passed as JSON.stringify()) The body to send the request (cannot be used in 'GET' request)
url string The URL to call the API (if you are not using the second argument and not using hostname and endpoint options)
hostname string The hostname of the url (Not necessary if URL argument has endpoint with it)
endpoint string Endpoint to request to (Not necessary if URL argument has endpoint with it)
headers Record<string, string> { "Content-Type": "application/json" } The headers of the request
method "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "CONNECT" | "OPTIONS" | "TRACE" GET The 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
statusCode number 200 Expected status code
timeout number 5000 The 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

Default mode

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));
POST Request
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));
Transition from node-fetch
From fetch To simply-https
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.

Package Sidebar

Install

npm i simply-https

Weekly Downloads

1

Version

2.0.0

License

CC-BY-NC-ND-4.0

Unpacked Size

43.3 kB

Total Files

6

Last publish

Collaborators

  • rahuletto