0.8.75 • Published 2 years ago

@rafterjs/api v0.8.75

Weekly downloads
-
License
GPL-3.0-or-later
Repository
github
Last release
2 years ago

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: 'dan@mclaren.com' },
        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": "dan@mclaren.com"
  },
  "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": "dan@mclaren.com"
  }
}

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

0.8.74

2 years ago

0.8.73

2 years ago

0.8.75

2 years ago

0.8.70

2 years ago

0.8.72

2 years ago

0.8.71

2 years ago

0.8.69

2 years ago

0.8.68

2 years ago

0.8.67

2 years ago

0.8.66

2 years ago

0.8.63

2 years ago

0.8.65

2 years ago

0.8.64

2 years ago

0.8.62

2 years ago

0.8.61

2 years ago

0.8.60

2 years ago

0.8.56

2 years ago

0.8.55

2 years ago

0.8.58

2 years ago

0.8.57

2 years ago

0.8.54

2 years ago

0.8.59

2 years ago

0.8.52

3 years ago

0.8.51

3 years ago

0.8.53

3 years ago

0.8.50

3 years ago

0.8.49

3 years ago

0.8.45

3 years ago

0.8.47

3 years ago

0.8.46

3 years ago

0.8.48

3 years ago

0.8.44

3 years ago

0.8.43

3 years ago

0.8.41

3 years ago

0.8.40

3 years ago

0.8.42

3 years ago

0.8.39

3 years ago

0.8.37

3 years ago

0.8.34

3 years ago

0.8.36

3 years ago

0.8.35

3 years ago

0.8.33

3 years ago

0.8.32

3 years ago

0.8.32-alpha.0

3 years ago

0.8.30

3 years ago

0.8.31

3 years ago

0.8.23

3 years ago

0.8.22

3 years ago

0.8.25

3 years ago

0.8.24

3 years ago

0.8.21

3 years ago

0.8.20

3 years ago

0.8.27

3 years ago

0.8.26

3 years ago

0.8.29

3 years ago

0.8.28

3 years ago

0.8.19

3 years ago

0.8.12

3 years ago

0.8.9

3 years ago

0.8.11

3 years ago

0.8.8

3 years ago

0.8.14

3 years ago

0.8.13

3 years ago

0.8.10

3 years ago

0.8.16

3 years ago

0.8.15

3 years ago

0.8.18

3 years ago

0.8.17

3 years ago

0.8.7

3 years ago

0.8.6

3 years ago

0.8.5

3 years ago

0.8.4

3 years ago

0.8.3

3 years ago

0.8.2

3 years ago

0.8.1

3 years ago