@andremalveira/route
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

Route

License-MIT  Version

Helpers for creating route paths from the api to the client

Table of contents

Install

pnpm i @andremalveira/route

Route

param

Add dynamic parameters and query parameters in the path

  • param: path
  • return: function(ParamValue | Object(ParamValue), Object?(QueryValue))

    Recognized parameter key format: :key | {key} | [key]
// routes/api.routes.ts

import { param } from '@andremalveira/route';

const routes = {
    user: param('/user/:userId')
}

export default routes
// How to use

import routes from 'routes/api.routes';

const userId = 032;

routes.user(userId); // /user/032

Using typescript

// Type
param<ParamKeys, QueryKeys>('/path') => (paramKeys, queryKeys) => string

//Example 
param<'userId'>('/user/:userId')

using-typescript-for-params-type.png

query

Add query parameters in path

  • param: path
  • return: function(Object?(QueryValue))

// routes/api.routes.ts

import { query } from '@andremalveira/route';

const routes = {
    posts: query('/posts')
}

export default routes
// How to use

import routes from 'routes/api.routes';

routes.posts({ category: 'movies', type:'science-fiction' }); // /posts?category=movies&type=science-fiction

Using typescript

// Type
query<QueryKeys>('/path') => (queryKeys?) => string

//Example 
query<'category' | 'type'>('/posts')

using-typescript-for-query-type.png

group

Creates a group of routes based on the options

  • param_1: Object(options) | {} | null
    • prefix?: prefix for route path
    • namespace?: returns the routes inside a new object called 'namespace'
    • baseUrl?: url base from path
  • param_2: Object(routes)
  • return: Object(routes)

// routes/api.routes.ts

import { group } from '@andremalveira/route';

const routes = {
    post: group({ prefix: '/post' }, {
        add: '/add',
        update: param('/update/:postId'),
        delete: param('/delete/:postId'),
    })
}

export default routes
// How to use

import routes from 'routes/api.routes';

const postId = 12;

routes.post.add             //  /post/add
routes.post.update(postId)  //  /post/update/12
routes.post.delete(postId)  //  /post/delete/12

Type

group({ 
    prefix?: string, 
    baseUrl?: string
    namespace?: string,
}, routes ) => routes

path

Returns formatted path segment

  • param_1: string[]: url segment
  • param_2: object: query keys
  • return: string

import { path } from '@andremalveira/route';

const newPath = path(['api', 'user', 'posts'], { status: true }) // api/user/posts?status=true

Type

path(string[], object) => string

Licence

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @andremalveira/route

Weekly Downloads

0

Version

0.1.1

License

MIT

Unpacked Size

15.3 kB

Total Files

12

Last publish

Collaborators

  • andremalveira.dev