@nepaul/rest-url-parser

1.0.4 • Public • Published

Rest URL Parser

Rest URL Parser

license npm

Only a function called parseRequestURL().

Simplify the handling of URLs that contain parameters..

It's particularly useful for applications that deal with REST APIs where resource identifiers are embedded in the URL.

Table of Contents

Get Started

Install

This project uses node and npm. Please install them first.

npm install  @nepaul/rest-url-parser

Usage

import parseRequestURL from '@nepaul/rest-url-parser';

const sourceConfig = {
  url: '/user/:userId/post/:postId',
  params: {
    userId: '123',
    postId: '456',
  },
};

const result = parseRequestURL(sourceConfig);

console.log(result);

Use with Axios:

import parseRequestURL from '@nepaul/rest-url-parser';

const instance = axios.create(appConfig.httpClient);

const requestInterceptor = instance.interceptors.request.use(
  function (config) {
    const restConfig = parseRequestURL(config);
    return restConfig;
  },
  function (error) {
    return Promise.reject(error);
  }
);

instance.post('/user/:userId/post/:postId', {
  userId: '123',
  postId: '456',
});

API

parseRequestURL(sourceConfig)

This function takes a sourceConfig object and parses the url property for parameters. It returns the updated sourceConfig.

parseRequestURL(sourceConfig);

Contributing

Your contributions and suggestions are heartily welcome. Submit an issue or submit a pull request.

Thanks to all the people who already contributed!

License

MIT

Package Sidebar

Install

npm i @nepaul/rest-url-parser

Weekly Downloads

3

Version

1.0.4

License

MIT

Unpacked Size

356 kB

Total Files

24

Last publish

Collaborators

  • nepaul