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

fast-maker

v4.0.0

Published

create route file on specific directory

Downloads

280

Readme

fast-maker

ts Download Status Github Star Github Issues NPM version License ci codecov code style: prettier

fast-maker generate fastify.js route configuration using by directory structure.

Why fast-maker?

fastify.js already have excellent auto route mechanics using fastify-autoload. But why you have to use fast-maker?

  1. Zero cost in Run-Time.
  2. Static analysis: fast-maker generate TypeScript source code. Because it help to find error in compile-time, not runtime
  3. Flexable Routing: You can use like that: /person/[kind]-[id]/. It help to get id and kind of id, for example serial-number and id or db-pk and id, even if you can use regular expression.
  4. Unifying how route paths are built: fast-maker use the same mechanics as Next.js. Route paths using file-system cannot be developer-specific
  5. fast-maker support a beautiful cli-interface

Table of Contents

Getting started

npx fast-maker init
npx fast-maker route

You can create configuration file using init command. And you can run route command, fast-maker generate route.ts file on your output directory in configuration file.

You can see this mechanics!

fast-maker-showcase.gif

How it works?

fast-maker using TypeScript Compiler API. So fast-maker exactly know handler function and route option in each file.

graph LR

A[route file] --> fast-maker
subgraph fast-maker
direction TB
C[TypeScript Compiler API]-->|extract <br/>handler function,<br /> option variable|B[fast-maker]
end
fast-maker-->|extract <br />route path|D[route.ts]

The image below briefly shows how the directory is converted to route configurations.

| AS-IS (directory structure) | | TO-BE (route function) | | ---------------------------------------------------------- | --- | -------------------------------------------------- | | directory_structure.png | ➜ | route_config_ts.png |

Installation

npm i fast-maker --save-dev

Usage

You can see help from --help option.

# display help for each commands
npx fast-maker --help

# display help for route commands
npx fast-maker route --help

# display help for init commands
npx fast-maker init --help

Also you can see detail option here.

Routing

fast-maker has a file-system based route configuration. This concept borrowed from Next.js routing system. But one difference is that HTTP Method is separated by file-system.

HTTP Method

use file-system.

handlers/
├─ superheros/
│  ├─ [id]/
│  │  ├─ powers/
│  │  │  ├─ [id]/
│  │  │  │  ├─ delete.ts
│  │  │  │  ├─ get.ts
│  │  │  │  ├─ put.ts
│  │  │  ├─ post.ts
│  │  ├─ delete.ts
│  │  ├─ get.ts
│  │  ├─ put.ts
│  ├─ get.ts
│  ├─ post.ts

get, post, put, delete filename represent HTTP Method. Also you can use options, patch, head, all filename.

Route options

You can pass RouteShorthandOptions option like that,

// When not using a `fastify` instance, you can declare it as a variable like this
export const option: RouteShorthandOptions = {
  schema: {
    querystring: schema.properties?.Querystring,
    body: schema.properties?.Body,
  },
};
// When using the `fastify` instance, you can declare it as a function like this
export const option = (fastify: FastifyInstance): RouteShorthandOptions => {
  return {
    schema: {
      querystring: schema.properties?.Querystring,
      body: schema.properties?.Body,
    },
     preHandler: fastify.auth([
      fastify.allowAnonymous,
      fastify.verifyBearerAuth
    ]),
  };
};

You have to named export and variable name must be a option.

Route handler

You can pass route handler function like that,

import { FastifyRequest } from 'fastify';
import type { IReqSearchPokemonQuerystring, IReqSearchPokemonParams } from '../../interface/IReqSearchPokemon';

export async function handler(
  req: FastifyRequest<{ Querystring: IReqSearchPokemonQuerystring; Params: IReqSearchPokemonParams }>,
) {
  console.debug(req.query);
  console.debug(req.body);

  return 'hello';
}

You have to named export and variable name must be a handler. Also you can use arrow function and you can use any name under TypeScript function name rule, as well as type arguments perfectly applied on route configuration

Variable in Route Path

File or Directory name surrounded square bracket like that,

handlers/
├─ superheros/
│  ├─ [id]/
│  │  ├─ get.ts
│  │  ├─ put.ts

Multiple variable, No problem.

handlers/
├─ superheros/
│  ├─ [kind]-[id]/
│  │  ├─ get.ts

This route path access like that: curl http://localhost:8080/hero/marvel-ironman

That's it. fast-maker takes care of the rest.

Example using fastify.js

A complete example of using fast-maker can be found at Ma-eum.

Relate To

Roadmaps

  • [x] display each route path in cli-table
  • [ ] add new option silent
  • [ ] documentation site
  • [x] add more test

License

This software is licensed under the MIT.