New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

rest-url-builder

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rest-url-builder

A rest url builder with easy parameter substitution

latest
Source
npmnpm
Version
1.0.6
Version published
Weekly downloads
408
-37.99%
Maintainers
1
Weekly downloads
 
Created
Source

rest-url-builder

Build rest urls by substituting the named and query parameters.

Installation

yarn add rest-url-builder (recommended) || npm install --save rest-url-builder.

Description

Build better rest url's by substituting the parameters using the provided context. Few advantages are

  • It's easier to understand when you have many parameters.
  • Maintainable, All url's can be maintained at one place.
  • logic free No need to check if a query parameter exists or not. If it exists then it will be on the url else it is removed from the final url

Usage

import { RestURLBuilder } from 'rest-url-builder
// const RestURLBuilder = require('rest-url-builder);

let urlBuilder = new RestURLBuilder();
urlBuilder.buildRestURL('urlString');
urlBuilder.setNamedParameter('parameterName', parameterValue);
urlBuilder.setQueryParameter('parameterName', queryParameterValue);
urlBuilder.get();

Examples

All examples are Typescript based.

Example 1: Get users

// user-rest-api.service.ts

import { RestURLBuilder } from 'rest-url-builder';
class UserRestAPI {

 private usersURL = 'https://fromsomewhere.com/users/:userId';
 
 private urlBuilder = new RestURLBuilder();
 
  getUsers(userId: number) { // userId = 4
    let builder = this.urlBuilder.buildRestURL(this.userURL);
    builder.setNamedParameter('userId', ''+userId );
    let finalURL =  builder.get(); // produces https://fromsomewhere.com/users/4
    http.get(finalURL); // psuedo 
    
  }
} 

Example 2: (build on top of example 1) getUsers of a certain organization

private organizationUsersURL = 'https://fromsomewhere.com/organizations/:organizationId/users/:userId';

 getUsers(organizationId: number, userId: number) {
    let builder = this.urlBuilder.buildRestURL(this.organizationUsersURL);
    builder.setNamedParameter('userId', ''+userId); 
    builder.setNamedParameter('organizationId', ''+organizationId);
    let finalURL =  builder.get(); // produces https://fromsomewhere.com/organizations/3/users/110
    http.get(finalURL); // psuedo 
 }

Example 3: query parameter example - The advantage here is the unused query parameters are removed from the final url. No need to check with if else or run it with for (queryParam in queryParams). Produces nice urls.

private filterUserByRoleAndName = 'https://fromsomewhere.com/organizations/:organizationId/users?role=:role&name=:name';

 getUsers(organizationId: number, role: string, name: string) { // organizationId = 3, role ="manager", name=null
    let builder = this.urlBuilder.buildRestURL(this.filterUserByRoleAndName);
    builder.setNamedParameter('organizationId', organizationId);
    builder.setQueryParameter('role', role);
    builder.setQueryParameter('name', name);
    let finalURL =  builder.get(); // produces https://fromsomewhere.com/organizations/3/users?role=manager (since name is null, it will not get appeneded to the url)
    http.get(finalURL); // psuedo 
 }

Licence

MIT

Keywords

rest-url-builder

FAQs

Package last updated on 06 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts