3.4.1 • Published 2 months ago

@routup/decorators v3.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

@routup/decorators

npm version main codecov Known Vulnerabilities Conventional Commits

This is a plugin to create request handlers with class-, method- & parameter-decorators. Those, can than be bound/mounted to an arbitrary router instance.

Table of Contents

Installation

npm install @routup/decorators --save

Configuration

The following TypeScript options must be present in the project tsconfig.json file:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Documentation

To read the docs, visit https://routup.net

Usage

Controller

The first step is to define a Controller.

controller.ts

import {
    DBody,
    DController,
    DDelete,
    DGet,
    DNext,
    DParam,
    DPost,
    DRequest,
    DResponse,
} from '@routup/decorators';

import {
    Next,
    Request,
    Response,
    send,
} from 'routup';

@DController('/users')
export class UserController {
    @DGet('')
    async getMany(
        @DRequest() req: Request,
        @DResponse() res: Response,
        @DNext() next: Next
    ) {
        return 'Hello, World!';
    }

    @DGet('/:id')
    async getOne(
        @DRequest() req: Request,
        @DResponse() res: Response,
        @DParam('id') id: string,
    ) {
        return 'Hello, World!';
    }

    @DPost('')
    async create(
        @DRequest() req: Request,
        @DResponse() res: Response,
        @DBody() body: any,
    ) {
        return 'Hello, World!';
    }

    @DDelete('/:id', [])
    async delete(
        @DRequest() req: Request,
        @DResponse() res: Response,
        @DParam('id') id: string,
    ) {
        return 'Hello, World!';
    }
}

Installation

The last step is to install the plugin and mount the controllers to a router instance.

Parameters like body, cookie and query cannot be automatically injected into the controller methods. Therefore, so-called parameter getters must be defined, with the help of which the parameters are extracted from the request object. If you do not use the corresponding decorator, they do not need to be provided.

app.ts

import { createServer } from 'node:http';

import { decorators } from '@routup/decorators';
import {
    basic,
    useRequestBody,
    useRequestCookie,
    useRequestCookies,
    useRequestQuery,
} from '@routup/basic';
import { createNodeDispatcher, Router } from 'routup';

import { UserController } from './controller';

const router = new Router();

router.use(basic());
router.use(decorators({
    controllers: [
        UserController
    ],
    parameter: {
        body: (context, name) => {
            if (name) {
                return useRequestBody(context.request, name);
            }

            return useRequestBody(context.request);
        },
        cookie: (context, name) => {
            if (name) {
                return useRequestCookie(context.request, name);
            }

            return useRequestCookies(context.request);
        },
        query: (context, name) => {
            if (name) {
                return useRequestQuery(context.request, name);
            }

            return useRequestQuery(context.request);
        },
    },
}))

const server = createServer(createNodeDispatcher(router));
server.listen(3000);

License

Made with 💚

Published under MIT License.

3.4.1

2 months ago

3.4.0

5 months ago

3.3.2

8 months ago

3.3.1

11 months ago

3.0.0-alpha.0

1 year ago

3.0.0-alpha.3

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

3.3.0

1 year ago

3.2.0

1 year ago

3.1.0

1 year ago

3.0.0

1 year ago

1.0.1

1 year ago

1.0.0

2 years ago

1.0.0-alpha.0

2 years ago

0.6.3

2 years ago

0.6.2

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.1.10

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.9

2 years ago

0.5.0

2 years ago

0.1.4

2 years ago

0.4.0

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago