npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rest-manager

v3.1.3

Published

A framework in order to facilitate/handle requests to a URL in an easy way.

Downloads

19

Readme

Table of contents

Installation

Note The package already has modules and commonjs support.

Using NPM:

npm install rest-manager

Using yarn:

yarn add rest-manager

Using Pnpm:

pnpm add rest-manager

Features

  • [x] New documentation
  • [x] Bugs and errors correction
  • [x] Written in TypeScript with dynamic typing
  • [x] Addition of endpoint to static URLs
  • [x] New Functions and Code refactored
  • [x] Standard requests using native fetch
  • [x] Modify the requests according to your taste
  • [x] Support for third party libraries
  • [ ] CDN support

Options and Functions

Class RestManager

Options

import RestManager from 'rest-manager'

// The URL is the basis of all Endpoints requests you create
const rest new RestManager(url: string, {
    // Use to define the request header
    headers?: object;

    // Use to manipulate the request, with it you can use third party libraries to make requests
    // By default we already use the native Nodejs fetch
    request?: (url: string, method: Methods, data: any) => Promise<any> | any;
})

RestManager.createRouter

Use this function to create "endpoints", to learn more look this example.

Note The URL of each route will be defined along with the initial URL of the class. For example, I have a URL https://google.com, if you create a route it will be added to the initial URL, it will be like this: https://google.com/ + search = https://google.com/search.

/**
 * @param name Identification name for your route
 * @param path The URL path to create Endpoint
 */
rest.createRouter(name: string, path: string, {
    // Use to define the request header
    headers?: object
});

RestManager.deleteRouter

Use this function to delete existing routes.

// Name of the route to be deleted, if there is no exist will return false
rest.deleteRouter(name: string);

RestManager.getRouter

Use this function to pull an existing route.

// Name of the route to be pulled, if there is no exist will return false
rest.getRouter(name: string);

Static functions

RestManager.create

Use this function to create an endpoint in a single URL.

Note This function does not perform the same functionality as the createRouter, since this function is static, it does not need to run a class. However, it will be limited in terms of creating multiple endpoints in a single url.

import RestManager from 'rest-manager';

// The url will be the main url of your request
const user = RestManager.create(url: string, {
    // Use to define the request header
    headers?: object;

    // Use it to handle the request, with it you can use third-party libraries to make requests
    // By default we already use NodeJs native Fetch
    request?: (url: string, method: Methods, data: any) => Promise<any> | any;
});

First steps

Routes with endpoints

import RestManager from 'rest-manager';

const api = new RestManager('http://localhost:8080', {
    headers: {
        'Content-Type': 'application/json',
        Authorization: '123'
    }
});

const user = api.createRouter('user', '/user');

(async() => {
    // http://localhost:8080?id=123
    const userResponse = await user({ id: '123' }).get(),
        userData = await userResponse.json();

    // http://localhost:8080/api/users
    api.router.api.users;
    return console.log(userData);
})();

Independent routes

import RestManager from 'rest-manager';

const userRouter = RestManager.create('http://localhost:8080/api/user'),
    usersRouter = RestManager.create('http://localhost:8080/api/users');

(async() => {
    // http://localhost:8080?id=123
    const userResponse = await userRouter({ id: '123' }).get(),
        usersRouter = await usersRouter.get();

    const userData = await userResponse.json(),
        usersData = await userResponse.json();

    return console.log(userData, usersData);
})();

Creating requisitions

To add paths to a url is the same thing as pull/access a value inside an object.

For example if you use it like this: router.api.users['123'].dashboard.example will return like this: http://localhost:8080/api/users/123/dashboard/example.

In any request method in the options you can add headers, if you are using the RestManager class it will assign the additional values in the predefined header.

Query String

Note Querys will always be located at the end of a url.

// http://localhost:8080/api/users/info?id=123
router.api.users.info({ id: '123' }).get();

// http://localhost:8080/api/search?query=example&max=15
router.api.search({ query: 'example', max: 15 }).get();

Simple Get

router.api.users.get();

Simple Post

router.api.user.post({
    body: JSON.stringify({
        name: 'isBucky',
        ...
    })
});

Handling requests

Request handling can be used to return a value of according to your taste, or use third-party libraries to make the requests.

Changing values

import RestManager from 'rest-manager';

const api = new RestManager('http://localhost:8080/api', {
    headers: {
        'Content-Type': 'application/json'
    },
    
    async request(url, method, bodyRequest) {
        const response = await fetch(url, { method, ...bodyRequest });
        
        if (response.headers.get('content-type') === 'application/json') return response.json();
        else return response.text();
    }
});

Using third-party libraries

In this example I will use the Undici library to make the requests.

import RestManager from 'rest-manager';
import Undici from 'undici';

const api = new RestManager('http://localhost:8080/api', {
    headers: {
        'Content-Type': 'application/json'
    },
        
    async request(url, method, bodyRequest) {
        const { body } = await Undici.request(url, {
                method,
                ...bodyRequest
            });

        return body.json();
    }
});

Back to top