npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jaris/router

v0.0.7

Published

> TODO: description

Downloads

2

Readme

@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),
        ],
      ),
    ]),
  ]),
]);