Git Product home page Git Product logo

simply-https's Introduction

⚡ 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.

simply-https's People

Contributors

rahuletto avatar zaid-maker avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

zaid-maker

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.