Socket
Book a DemoInstallSign in
Socket

@penname/routepath

Package Overview
Dependencies
Maintainers
2
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@penname/routepath

Convenient API route interface.

1.0.3
latest
Source
npmnpm
Version published
Maintainers
2
Created
Source

npm install --save @penname/routepath

routepath

Convenient API route interface.

const RoutePath = require('@penname/routepath')
const route = RoutePath.instance({ path: '/article/:id', baseUrl: 'example.com' })

const url = route.buildUrl({
	params: { id: 'abc123' },
	query: { position: 'middle', ref: 'twitter' },
})
console.log(url)
// prints: example.com/article/abc123?position=middle&ref=twitter

const path = route.buildPath({
	params: { id: 'abc123' },
	query: { position: 'middle', ref: 'twitter' },
})
console.log(path)
// prints: /article/abc123?position=middle&ref=twitter

Methods

#instance()

Create a route path instance with a minimum of baseUrl, path:

const route = RoutePath.instance({ path: '/article/:id', baseUrl: 'https://example.com' })

#buildUrl()

Builds the url from the route path with the provided params and query

const route = RoutePath.instance({ path: '/article/:id', baseUrl: 'localhost:3000/api/' })

const url = route.buildUrl({
	params: { id: 'abc123' },
	query: { position: 'middle', ref: 'twitter' },
})

console.log(url)
// prints: localhost:3000/api/article/abc123?position=middle&ref=twitter

#buildPath()

Builds the url path, no host, from the route path with the provided params and query

const route = RoutePath.instance({
	path: '/user/:userId/article/:articleId',
	baseUrl: 'http://www.example.com',
})

const path = route.buildPath({
	params: { userId: 'u1cba', articleId: 'abc123' },
	query: { position: 'middle', date: 1609036608349 },
})

console.log(path)
// prints: /user/u1cba/article/abc123?position=middle&date=1609036608349

#fromSerialized()

const routeFromJsonString = RoutePath.fromSerialized(
	`{path: "/user/:id/articles", "baseUrl": 'example.com/api/' }`,
)

const url = route.buildUrl({
	params: { id: 'u1cba' },
	query: { from: 'abc456', limit: 15 },
})

console.log(url)
// prints: example.com/api/user/u1cba/articles?from=abc456&limit=15

Keywords

routing

FAQs

Package last updated on 27 Dec 2020

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

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.