Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@jaris/router

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jaris/router

> TODO: description

  • 0.0.7
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@jaris/router

jaris is still in very early alpha, production usage is not recommended

Router for @jaris/core

Installation

$ npm install -S @jaris/core @jaris/router

Usage

Hello, world

import server, { text } from '@jaris/core';
import router, { get } from '@jaris/router';

server([router([get('/', conn => text('Hello, world!', conn))])]);

Multi file structure

// user.controller.ts
import { json } from '@jaris/core';

const userController = {
  // method can be async!
  index: async conn => {
    // fetch users, etc
    return json({ users: [] }, conn);
  },
};

export default userController;
// api.routes.ts
import { get } from '@jaris/router';
import userController from './user.controller';

const apiRoutes = [get('/users', userController.index)];

export default apiRoutes;
// index.ts
import server  from '@jaris/core';
import router from '@jaris/router';
import apiRoutes from './api.routes.ts';

server([
  (conn) => {
    console.log('Since the router is also just a middleware itself, we can have as many middleware before or after that we want!');
    return conn;
  }
  router(apiRoutes)
])

Route Parameters

Route parameters are defined using a colon in the route definition and are set as object values on conn.params.

import server, { text } from '@jaris/core';
import router, { get } from '@jaris/router';

server([
  router([
    get('/users/:userId', conn =>
      text(`Hello, user ${conn.params.userId}!`, conn),
    ),
  ]),
]);

More complex routing

Prefixes & Middleware

import server, { json, status, halt } from '@jaris/core';
import router, { get, post, group } from '@jaris/router';

// Middleware are the same as @jaris/core
// so they need to follow the same rule
// of returning a new connection
const companyMiddleware = conn => {
  const token = conn.headers['Authorization'];

  // ... parse token
  // fetch user it belongs to
  // check if user has access to company

  // if you want to continue, return the connection
  if (userHasAccess) {
    return conn;
  }

  // otherwise we set errors and tell jaris
  // to stop by using the "halt" helper
  return pipe(
    status(403),
    json({ error: 'You do not have permission' }),
    halt,
  )(conn);
};

server([
  router([
    // groups need to be spread since
    // they return an array of routes
    ...group({ prefix: 'v1' }, () => [
      // will evaluate to /v1/users
      get('/users', userController.index),

      // leading / trailing slashes are optional
      post('users', userController.store),

      // groups can be nested
      ...group(
        {
          prefix: '/companies/:companyId',
          middleware: [companyMiddleware],
        },
        () => [
          // /v1/companies/:companyUid
          get('/', companyController.show),
        ],
      ),
    ]),
  ]),
]);

FAQs

Package last updated on 19 Feb 2019

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc