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

@rafterjs/api

v0.8.75

Published

An API server that simplifies creating json apis with rafter

Downloads

36

Readme

Rafter API Server

The @rafterjs/api server provides a simple wrapper around rafter and express to create JSON API's rapidly with all the benefits that rafter provides including autoloading dependency injection.

Getting started

yarn add @rafterjs/api

Add rafter api to your project

Example structure

  • config
    • config.ts
    • middleware.ts
    • plugins.ts
    • preStartHooks.ts
    • routes.ts
  • lib
    • HomeController.ts
  • index.ts

index.ts

This is the server entry point. Think of this like your express() server definition.

You pass in the paths you want to autoload our dependencies from. In the example below all the ./config files and ./lib/HomeController.ts will load into the rafter api server.

import { Server } from '@rafterjs/api';
import { join } from 'path';

const paths = [join(__dirname, `{lib,config}/**/`)];

const server = new Server({ paths });

async function run(): Promise<void> {
  try {
    console.info(`Starting the simple rafter api`);
    await server.start();
  } catch (error) {
    console.error(error);
    await server.stop();
    process.exit(1);
  }
}

run();

lib/HomeController.ts

One of the benefits of the @rafterjs/api is that you can extend the provided JsonController and it will handle rendering in a consistent format.

import {
  JsonController,
  JsonResponseDto,
  IController,
  IControllerAction,
  IRequest,
  IResponse,
  Status,
} from '@rafterjs/api';

interface IHomeController extends IController {
  index: IControllerAction;
}

export default class UsersController extends JsonController implements IHomeController {
  public index(request: IRequest, response: IResponse): void {
    this.render(
      request,
      response,
      new JsonResponseDto({
        message: 'This is the users endpoint',
        data: { name: 'Daniel Ricciardo', email: '[email protected]' },
        meta: { totalPages: 1, totalRecords: 1 },
        status: Status.SUCCESS,
      }),
    );
  }
}

By extending JsonController you call the this.render method and pass in the JsonResponseDto. This will output the response in the following format:

{
  "transactionId": "399f91a2-77f9-49c2-8df6-e7928f48429e",
  "message": "This is the users endpoint",
  "data": {
    "name": "Daniel Ricciardo",
    "email": "[email protected]"
  },
  "links": {
    "self": "http://localhost:4000/users"
  },
  "meta": {
    "totalPages": 1,
    "totalRecords": 1
  }
}

Overrides

You can override any of the default @rafterjs/api services.

JsonResponseTransformer override

By creating a file called JsonResponseTransformer, it allows you to change the output of the controller render function.

import { IJsonResponse, IJsonResponseData, IJsonResponseTransformer, IRequest, JsonResponseDto } from '@rafterjs/api';

export class JsonResponseTransformer implements IJsonResponseTransformer {
  public convert<T extends IJsonResponseData>(
    request: IRequest,
    jsonResponseDto: JsonResponseDto<T>,
  ): IJsonResponse<T> {
    return {
      data: jsonResponseDto.data,
    } as IJsonResponse<T>;
  }
}

export default JsonResponseTransformer;

This will change the json response to something much simpler eg.

{
  "data": {
    "name": "Daniel Ricciardo",
    "email": "[email protected]"
  }
}

By allowing you to override the default rafter services, it gives you complete control while still benefiting from the simplicity of rafter.

See the api example https://github.com/rafterjs/rafter/tree/master/examples/simple-api