3.1.3 • Published 6 months ago

rest-manager v3.1.3

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
6 months ago

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

  • New documentation
  • Bugs and errors correction
  • Written in TypeScript with dynamic typing
  • Addition of endpoint to static URLs
  • New Functions and Code refactored
  • Standard requests using native fetch
  • Modify the requests according to your taste
  • 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

3.0.9

6 months ago

3.1.3

6 months ago

3.1.2

6 months ago

3.1.1

6 months ago

3.1.0

6 months ago

3.0.8

7 months ago

1.1.5

11 months ago

1.1.4

11 months ago

1.1.3

11 months ago

1.1.2

12 months ago

2.0.0

11 months ago

3.0.4

11 months ago

3.0.3

11 months ago

3.0.2

11 months ago

3.0.1

11 months ago

3.0.7

11 months ago

3.0.6

11 months ago

3.0.5

11 months ago

3.0.0

11 months ago

1.1.1

1 year ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago