# resting-squirrel-controller

> Controller for defining endpoints in resting-squirrel.

Latest version **2.6.1** (published 2026-06-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install resting-squirrel-controller
pnpm add resting-squirrel-controller
yarn add resting-squirrel-controller
bun add resting-squirrel-controller
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.6.1 |
| Published | 2026-06-26 |
| First published | 2020-02-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 0 |
| Unpacked size | 66.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Lukas Macuda |
| Maintainers | zabkwak |

## Links

- npm: https://www.npmjs.com/package/resting-squirrel-controller
- Repository: https://github.com/zabkwak/resting-squirrel-controller
- Homepage: https://github.com/zabkwak/resting-squirrel-controller#readme
- Issues: https://github.com/zabkwak/resting-squirrel-controller/issues
- npm.io page: https://npm.io/package/resting-squirrel-controller

## Recent versions

- 2.6.1 (latest) — 2026-06-26
- 2.6.0 — 2022-04-17
- 2.5.0 — 2022-02-16
- 2.4.1 — 2021-09-15
- 2.4.0 — 2021-09-15
- 2.2.2 — 2020-10-15
- 2.2.1 — 2020-09-17
- 2.2.0 — 2020-09-16
- 2.1.3 — 2020-07-28
- 2.1.2 — 2020-07-28
- 2.1.1 — 2020-07-06
- 2.1.0 — 2020-07-03
- 2.0.1 — 2020-06-26
- 2.0.0 — 2020-06-24
- 1.4.0 — 2020-04-29
- … 8 more at https://npm.io/package/resting-squirrel-controller/versions

## README

# resting-squirrel-controller
Controller for defining endpoints in [resting-squirrel](https://www.npmjs.com/package/resting-squirrel).  
Definitions of endpoints can be done with extending the `Controller` class and using decorators.

## Installation
```bash
npm install resting-squirrel-controller --save
```

## Usage
### Javascript
TBD
### Typescript
#### DTO
```typescript

import rs, { Field, IRequest, RouteAuth, Type } from 'resting-squirrel'; // peer dependency
import Controller from 'resting-squirrel-controller';
import RSDto, { IRSDto } from 'resting-squirrel-dto'; // peer dependency

class TestRequestDto implements IRSDto {

	@RequestDto.integer
	@RequestDto.required
	public id: number;
}

class TestResponseDto implements IRSDto {

	@ResponseDto.integer
	public id: number;

	@ResponseDto.string
	public status: string;
}

class TestDto implements IRSDto {

	@RSDto.integer
	@RSDto.required
	public id: number;

	@RSDto.string
	@RSDto.response
	public status: string;
}

@Controller.v(0)
class TestController extends Controller {

	@Controller.get('/test')
	@Controller.dto(TestDto)
	@Controller.auth(RouteAuth.REQUIRED)
	public async getTest(req: IRequest<{}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
		return { status: 'get', id: req.query.id };
	}

	@Controller.put('/test')
	@Controller.params(TestRequestDto)
	@Controller.auth(RouteAuth.REQUIRED)
	@Controller.response(TestResponseDto)
	public async createTest(req: IRequest<{}, {}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
		return { status: 'put', id: req.body.id };
	}

	@Controller.post('/test/:id')
	@Controller.params(TestRequestDto)
	@Controller.auth(RouteAuth.REQUIRED)
	@Controller.response(TestResponseDto)
	@Controller.args([new Field('id', Type.integer)])
	public async updateTest(req: IRequest<{}, {}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
		return { status: 'post', id: req.body.id };
	}

	@Controller.delete('/test/:id')
	@Controller.params(TestRequestDto)
	@Controller.auth(RouteAuth.REQUIRED)
	@Controller.emptyResponse
	@Controller.args([new Field('id', Type.integer)])
	public async deleteTest(req: IRequest<{}, {}, TestRequestDto>): Promise<null> {
		return null;
	}
}

const app = rs();

new TestController(app).register();

app.start();

```

#### DTO Legacy
```typescript

import rs, { Field, IRequest, RouteAuth, Type } from 'resting-squirrel'; // peer dependency
import Controller from 'resting-squirrel-controller';
import RSDto, { RequestDto, ResponseDto } from 'resting-squirrel-dto'; // peer dependency

class TestRequestDto extends RequestDto {

	@RequestDto.integer
	@RequestDto.required
	public id: number;
}

class TestResponseDto extends RequestDto {

	@ResponseDto.integer
	public id: number;

	@ResponseDto.string
	public status: string;
}

class TestDto extends RSDto {

	@RSDto.integer
	@RSDto.required
	public id: number;

	@RSDto.string
	@RSDto.response
	public status: string;
}

@Controller.v(0)
class TestController extends Controller {

	@Controller.get('/test')
	@Controller.dto(TestDto)
	@Controller.auth(RouteAuth.REQUIRED)
	public async getTest(req: IRequest<{}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
		return { status: 'get', id: req.query.id };
	}

	@Controller.put('/test')
	@Controller.params(TestRequestDto)
	@Controller.auth(RouteAuth.REQUIRED)
	@Controller.response(TestResponseDto)
	public async createTest(req: IRequest<{}, {}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
		return { status: 'put', id: req.body.id };
	}

	@Controller.post('/test/:id')
	@Controller.params(TestRequestDto)
	@Controller.auth(RouteAuth.REQUIRED)
	@Controller.response(TestResponseDto)
	@Controller.args([new Field('id', Type.integer)])
	public async updateTest(req: IRequest<{}, {}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
		return { status: 'post', id: req.body.id };
	}

	@Controller.delete('/test/:id')
	@Controller.params(TestRequestDto)
	@Controller.auth(RouteAuth.REQUIRED)
	@Controller.emptyResponse
	@Controller.args([new Field('id', Type.integer)])
	public async deleteTest(req: IRequest<{}, {}, TestRequestDto>): Promise<null> {
		return null;
	}
}

const app = rs();

new TestController(app).register();

app.start();

```

## Classes
### Connector
#### Methods
##### `static registerDirectory(app: Application, directory: string): Promise<void>`
Registers the directory with controllers to the `resting-squirrel` application.
##### `register(): void`
Registers the controller to the `resting-squirrel` application.
#### Decorators
##### Class
Decorators for the `Controller` class.
###### `version(version: number)`
Sets the version of all endpoint in the `Controller`.
###### `v(version: number)`
Alias for `version` decorator.
###### `controllerOptions(options: IRouteOptions)`
Class decorator to set some of route options to all endpoints.
##### Method
Decorators for the `Controller` methods defining endpoint.
###### `put(route: string)`
The endpoint is executed with `PUT` method.
###### `get(route: string)`
The endpoint is executed with `GET` method.
###### `post(route: string)`
The endpoint is executed with `POST` method.
###### `delete(route: string)`
The endpoint is executed with `DELETE` method.
###### `deprecated`
Marks the endpoint as deprecated.
###### `options(options: IRouteOptions)`
Sets the options to the endpoint.
###### `option<K extends keyof IRouteOptions>(option: K, value: IRouteOptions[K])`
Sets specific option to the endpoint.
###### `auth(auth: RouteAuth)`
###### `dto(dto: typeof BaseDto)`
###### `params(params: (new (...args: any[]) => IRSDto) | typeof BaseDto | typeof RequestDto)`
###### `response(response: (new (...args: any[]) => IRSDto) | typeof BaseDto | typeof ResponseDto)`
###### `errors(errors: Array<ErrorField>)`
###### `description(description: string)`
###### `hideDocs`
Sets the `hideDocs` option to `true`.
###### `args(args: Array<Field> | typeof ArgsDto)`
###### `requireApiKey(requireApiKey: boolean)`
###### `excludeApiKeys(excludeApiKeys: (() => Promise<Array<string>>) | Array<string>))`
###### `timeout(timeout: number)`
###### `<IProps = {[key: string]: any}>props(props: IProps)`
###### `emptyResponse`
Sets the endpoint as empty. It returns 204 status code.

## Migration to v2
There are no breaking changes in the v2 except the peer dependency on the `resting-squirrel-dto` module.

## TODO
### v3
- Replace deprecated static endpoint decorators with controller decorators.

---
_Source: https://npm.io/package/resting-squirrel-controller · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
