@gpx/path-to-name
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

path-to-name

Converts a path into a name.

Let's start with an example:

import { pathToName } from "@gpx/path-to-name";

pathToName("/user"); // => 'getUser'
pathToName("/users"); // => 'getUsers'
pathToName("/users/:user_id"); // => 'getUserById'
pathToName("/users/:user_id/products"); // => 'getProductsForUserById'
pathToName("/users/:user_id/products/:product_id"); // => 'getProductByIdForUserById'

That's pretty much it. Pass a path and get back a name for it. The name is camel-cased and follows the RESTful naming conventions.

There's only a second optional argument method. It matches the HTTP method and can only have values of GET, POST, PUT, PATCH, and, DELETE. If the method is not provided, it defaults to GET.

pathToName("/users", "PUT"); // => 'createUser'
pathToName("/users/:id", "get"); // => 'getUserById'
pathToName("/users/:id", "post"); // => 'updateUserById'
pathToName("/users/:id", "patch"); // => 'updateUserById'
pathToName("/users/:id", "delete"); // => 'deleteUserById'

Readme

Keywords

none

Package Sidebar

Install

npm i @gpx/path-to-name

Weekly Downloads

1

Version

0.1.1

License

ISC

Unpacked Size

11.2 kB

Total Files

9

Last publish

Collaborators

  • gpx