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
;
Example 2: (build on top of example 1) getUsers of a certain organization
private organizationUsersURL = 'https://fromsomewhere.com/organizations/:organizationId/users/:userId'; getUsersorganizationId: number, userId: number
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'; getUsersorganizationId: number, role: string, name: string
Licence
MIT