Giter Site home page Giter Site logo

tsemerad / ruty Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rmariuzzo/ruty

0.0 0.0 0.0 2.67 MB

๐Ÿ“ฆ Ruty is a simple URL route builder, that support typing route params and queries string with TypeScript.

License: MIT License

JavaScript 2.02% TypeScript 97.98%

ruty's Introduction

Ruty

Ruty is a simple URL route builder, that supports typing route params and queries string with TypeScript.

Motivation

Whether I am working in the frontend or backend, I always define a set of URLs for API endpoints or page URLs. Therefore, instead of having all these URLs routes spread out within multiple files, I prefer to have them defined in one file then reference them where I need them. Also, while using TypeScript I prefer to have my URLs routes params and queries string typed. That's the reason I created ruty , a tiny library that allows me to define routes with their params and queries strings.

Features

  • TypeScript support.
  • Typed route params /:param and typed query strings ?query&string.
  • No dependencies.
  • 100% test coverage.

Installation

npm i ruty

Usage

import { Ruty } from 'ruty'

const { route } = Ruty.configure()

const routes = {
  home: route('/').build(),
  users: route('/users').build(),
  userById: route('/user/:id?created&sort').build(),
}

routes.home()
// '/'
routes.users()
// '/users'
routes.userById({ id: 123 })
// '/user/123'
routes.userById({ id: 123, created: true, sort: 'desc' })
// '/user/123?created&sort=desc'

TypeScript

You can type your route params and queries string with TypeScript by adding generics to the build method as shown below:

import { route } from 'ruty'

Ruty.configure()

const routes = {
  userById: route('/user/:id?created&sort').build<{
    id: number
    created: boolean
    sort: 'asc' | 'desc'
  }>(),
}

routes.userById({ id: 123, created: true, sort: 'desc' })
// '/user/123?created&sort=desc'

Then you will have autocomplete suggestions and type assertion:

TypeScript example

Configuration

Any configuration is passed to Ruty.configure(...).

const ruty = Ruty.configure({
  // ๐Ÿ‘‡ Add a prefix to all genrated routes.
  prefix: '/:language',
  // ๐Ÿ‘‡ A function that transform the value for route params.
  paramTransformer: (param, value) => value,
  // ๐Ÿ‘‡ A function that transform the value for query strings.
  queryTransformer: (query, value) => value,
  // ๐Ÿ‘‡ Global route params.
  params: {
    language: () => detectLanguage(),
  },
  // ๐Ÿ‘‡ Global query string.
  query: {
    theme: 'dark',
  },
})

Development

If you want to contribute to this project please follow these instructions:

  1. Clone this repo.
  2. Install dependencies with npm i.
  3. Add your changes, make sure to include tests.

Tests can be run with npm t. If you want to run tests in watch mode use: npm t -- --watch. Also, if you want to run tests coverage use: npm t -- --coverage.

Release

Releases are done with: npm run release.


Made with love by @rmariuzzo and contributors.

ruty's People

Contributors

david-mateogit avatar rmariuzzo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.