2.0.0-dev.1 • Published 1 year ago

cheap-di v2.0.0-dev.1

Weekly downloads
62
License
MIT
Repository
github
Last release
1 year ago

nice-web-routes

Easy way to create nice web routes for you application

license npm latest package codecov

Installation

npm install nice-web-routes

How to use

import { createNiceWebRoutes } from 'nice-web-routes';

const routes = createNiceWebRoutes({
  users: {
    statistic: {},
  },
  user: {
    userId: () => ({
      avatar: {},
      private_info: {},
    }),
    // typed parameter
    form: (form: 'create' | 'edit') => ({}),
  },
});

routes.url(); // '/'
routes.users.url(); // '/users'
routes.users.statistic.url(); // '/users/statistic'
routes.users.statistic.relativeUrl(); // 'statistic'

routes.users.statistic.url({ view: 'print', filter: 'no' }); // '/users/statistic?view=print&filter=no'
routes.users.statistic.url('/*'); // '/users/statistic/*'

routes.user.userId().relativeUrl(); // ':userId'
routes.user.userId('18').private_info.url(); // '/user/18/private-info'

// typed parameter
routes.user.form('create').url(); // '/user/create'
routes.user.form('edit').url(); // '/user/edit'
routes.user.form('something').url(); // error because it violates type constraint of 'create' | 'edit' | undefined

Base route customization

Generally you choose to use base route or don't on routes creation time:

const routes = createObjectNiceWebRoutes({
  home: {},
  welcome: {},
});

routes.home.url(); // "/home"
routes.welcome.url(); // "/welcome"

// with base route

const apiRoutes = createObjectNiceWebRoutes(
  {
    users: {
      userId: () => ({}),
    },
  },
  { parentRoute: '/api/v4' }
);

routes.users.url(); // "/api/v4/users"
routes.users.userId('123').url(); // "/api/v4/users/123"

But there are some cases when you need to change base route dynamically, or for example if your routes are started with locale and after user changes page language, routes should be changed as well:

const routes = createObjectNiceWebRoutes(
  {
    home: {},
    welcome: {},
  },
  { parentRoute: '/en' }
);

routes.home.url(); // "/en/home"
routes.welcome.url(); // "/en/welcome"

routes.setBaseRoute('/de');

routes.home.url(); // "/de/home"
routes.welcome.url(); // "/de/welcome"

Using with react-router

import { createNiceWebRoutes } from 'nice-web-routes';
import { Navigate, Route, Routes } from 'react-router-dom';

const appRoutes = createNiceWebRoutes({
  auth: {
    login: {},
    registration: {},
  },
  profile: {
    userId: () => ({}),
    settings: {},
    edit: {
      personal: {},
      career: {},
    },
  },
});

const App = () => (
  <Routes>
    <Route
      index
      element={<Navigate to={appRoutes.auth.relativeUrl()} replace />}
    />

    <Route path={appRoutes.auth.url('/*')}>
      {' '}
      {/* '/auth/*' */}
      <Route
        index
        element={<Navigate to={appRoutes.auth.login.relativeUrl()} replace />}
      />
      <Route
        path={appRoutes.auth.login.relativeUrl()}
        element={<LoginDisplay />}
      />
      <Route
        path={appRoutes.auth.registration.relativeUrl()}
        element={<RegistrationDisplay />}
      />
    </Route>

    <Route path={appRoutes.profile.url('/*')}>
      {' '}
      {/* '/profile/*' */}
      <Route index element={<MyProfileDisplay />} />
      <Route
        path={appRoutes.profile.userId().relativeUrl()}
        element={<UserProfile />}
      />
      <Route
        path={appRoutes.profile.settings.relativeUrl()}
        element={<SettingsDisplay />}
      />
      <Route path={appRoutes.profile.edit.relativeUrl('/*')}>
        {' '}
        {/* 'edit/*' */}
        <Route index element={<ProfileSettings />} />
        <Route
          path={appRoutes.profile.edit.career.relativeUrl()}
          element={<EditCareerDisplay />}
        />
        <Route
          path={appRoutes.profile.edit.personal.relativeUrl()}
          element={<EditPersonalInformationDisplay />}
        />
      </Route>
    </Route>
  </Routes>
);

Customization

You can customize routes creating by using configureNiceWebRoutesCreating and passing FactoryConfig:

import { configureNiceWebRoutesCreating } from 'nice-web-routes';

const routes = configureNiceWebRoutesCreating({
  getSegmentValue: (segmentName, segmentValue) => {
    if (typeof segmentValue === 'string') {
      return `argument_${segmentValue}`;
    }

    // it is how route parameters are created by default
    if (segmentName.toLowerCase().includes('id')) {
      return ':id';
    }
    return `:${segmentName}`;
  },
  snakeTransformation: {
    disableForSegmentValue: true,
  },
})({
  user: {
    group: () => ({}),
    userId: () => ({
      avatar: {},
    }),
  },
});

routes.user.group().url(); // '/user/:group'
routes.user.userId().url(); // '/user/:id'
routes.user.userId('18').url(); // '/user/argument_18'

FactoryConfig

PropertyTypeDescriptionDefault value
getSegmentValueGetSegmentValue => (segmentName: string, segmentValue: string or undefined) => stringIt is responsible for displaying parametrized route valuevalue is displayed as is, and when there is no value it shows as :segmentName
urlBuilderImplUrlBuilderConstructor => class that implements UrlBuilder interfaceYou can override how the target url is creatingDefaultUrlBuilder - internal implementation
creatingStrategyCreatingStrategyVariant => 'proxy' or 'object'it is about how your routes object is created (see Creating strategies section bellow)object
snakeTransformation{ disableForSegmentName?: boolean; disableForSegmentValue?: boolean; }You can disable transformation of user_list segment name or value to user-list url part{}

Creating strategies

Object strategy creates nested routes only when parametrized route is called.

It is good option, when you have no large trees under parametrized routes, because it traverses description tree for each parametrized node call . Objects nodes are traversed during routes creation time until it reaches parametrized route.

Proxy strategy creates proxy for each tree node when that node is accessed.

It is good option, if you have large route tree or many nested routes under parametrized routes, because it will not traverse entire tree on each node call. But performance in such case is lower than in Object strategy (caused Proxy implementation in js core).

4.1.2-dev.2

1 year ago

4.1.2-dev.1

1 year ago

2.0.0-dev.1

1 year ago

4.1.3

1 year ago

4.1.3-dev.1

1 year ago

4.1.2

1 year ago

4.1.1

1 year ago

4.1.0

1 year ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.0-rc-33

2 years ago

4.0.0-rc-31

2 years ago

4.0.0-rc-32

2 years ago

4.0.0-rc-30

2 years ago

4.0.0-rc-13

2 years ago

4.0.0-rc-12

2 years ago

4.0.0-rc-14

2 years ago

4.0.0-rc-11

2 years ago

4.0.0-rc-10

2 years ago

4.0.0-rc-8

2 years ago

4.0.0-rc-9

2 years ago

4.0.0-rc-7

2 years ago

4.0.0-rc-3

2 years ago

4.0.0-rc-6

2 years ago

4.0.0-rc-5

2 years ago

3.4.4

2 years ago

4.0.0-rc.1

2 years ago

4.0.0-rc.2

2 years ago

3.5.0

2 years ago

3.4.0-alpha-0.1

2 years ago

3.4.3

3 years ago

3.4.0

3 years ago

3.4.2

3 years ago

3.4.1

3 years ago

3.3.1

3 years ago

3.2.5

3 years ago

3.3.2

3 years ago

3.2.4

3 years ago

3.2.3

3 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.0

4 years ago

3.0.0

4 years ago

2.3.0

4 years ago

2.2.3

4 years ago

2.2.1

4 years ago

2.2.2

4 years ago

2.2.0

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.0

4 years ago

1.0.0

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.0

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.6

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago