Skip to content

Commit

Permalink
refactor: better decorators interop
Browse files Browse the repository at this point in the history
  • Loading branch information
L2jLiga committed Oct 2, 2024
1 parent 20f3655 commit 63f0e51
Show file tree
Hide file tree
Showing 142 changed files with 1,557 additions and 3,537 deletions.
28 changes: 27 additions & 1 deletion docs/Migration to v4.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,36 @@ Update dependencies:

| Dependency | Minimal supported version |
| ---------- | ------------------------- |
| Node.js | 18.18.0 |
| Node.js | 20.12.0 |
| TypeScript | 5.0.0 |
| Fastify | 4.0.0 |

### ControllerType was renamed

Replace usages of `ControllerType` with `Scope`:

_before_:

```typescript
import { Controller, ControllerType } from 'fastify-decorators';

@Controller({
route: '/',
type: ControllerType.SINGLETON,
})
```

_after_:

```typescript
import { Controller, Scope } from 'fastify-decorators';

@Controller({
route: '/',
scope: Scope.SINGLETON,
})
```

### Built-in DI was moved to plugin

[`@fastify-decorators/simple-di`]: https://www.npmjs.com/package/@fastify-decorators/simple-di
Expand Down
8 changes: 4 additions & 4 deletions examples/async-initializer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
},
"dependencies": {
"@fastify-decorators/simple-di": "^4.0.0-next.5",
"fastify": "^4.27.0",
"fastify": "^5.0.0",
"fastify-decorators": "^4.0.0-next.5"
},
"devDependencies": {
"@types/jest": "^29.5.7",
"@types/node": "~18.18.6",
"@types/jest": "^29.5.13",
"@types/node": "~20.12.14",
"jest": "^29.7.0",
"jest-resolve": "^29.7.0",
"jest-ts-webcompat-resolver": "^1.0.0",
"ts-jest": "^29.1.2",
"ts-jest": "^29.2.5",
"typescript": "^5.6.2"
},
"jest-junit": {
Expand Down
3 changes: 2 additions & 1 deletion examples/async-initializer/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
"target": "ES2022",
"target": "ES2023",
"module": "ESNext",
"outDir": "./dist",
"lib": ["ES2023"],

/* Strict Type-Checking Options */
"strict": true,
Expand Down
12 changes: 6 additions & 6 deletions examples/aws-lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
},
"dependencies": {
"@fastify-decorators/simple-di": "^4.0.0-next.5",
"@fastify/aws-lambda": "^4.1.0",
"fastify": "^4.27.0",
"@fastify/aws-lambda": "^5.0.0",
"fastify": "^5.0.0",
"fastify-decorators": "^4.0.0-next.5"
},
"devDependencies": {
"@types/jest": "^29.5.7",
"@types/node": "~18.18.6",
"@vercel/ncc": "^0.38.0",
"@types/jest": "^29.5.13",
"@types/node": "~20.12.14",
"@vercel/ncc": "^0.38.2",
"cross-env": "^7.0.3",
"jest": "^29.7.0",
"jest-resolve": "^29.7.0",
"jest-ts-webcompat-resolver": "^1.0.0",
"ts-jest": "^29.1.2",
"ts-jest": "^29.2.5",
"tslib": "^2.7.0",
"typescript": "^5.6.2"
},
Expand Down
3 changes: 2 additions & 1 deletion examples/aws-lambda/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
"target": "ES2022",
"target": "ES2023",
"module": "ESNext",
"outDir": "./dist",
"lib": ["ES2023"],

/* Strict Type-Checking Options */
"strict": true,
Expand Down
4 changes: 2 additions & 2 deletions examples/controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"dependencies": {
"@fastify-decorators/simple-di": "^4.0.0-next.5",
"fastify": "^4.27.0",
"fastify": "^5.0.0",
"fastify-decorators": "^4.0.0-next.5"
},
"devDependencies": {
Expand All @@ -22,7 +22,7 @@
"jest": "^29.7.0",
"jest-resolve": "^29.7.0",
"jest-ts-webcompat-resolver": "^1.0.0",
"ts-jest": "^29.1.2",
"ts-jest": "^29.2.5",
"typescript": "^5.6.2",
"undici": "^6.19.8"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { FastifyReply, FastifyRequest } from 'fastify';
import { Controller, ControllerType, ErrorHandler, GET } from 'fastify-decorators';
import { Controller, ErrorHandler, GET, Scope } from 'fastify-decorators';
import { ErrorType } from './error-type.js';

@Controller({
route: '/stateful/error-handling',
type: ControllerType.REQUEST,
scope: Scope.SINGLETON,
})
export default class StatelessController {
@GET({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { FastifyReply, FastifyRequest } from 'fastify';
import { Controller, ControllerType, ErrorHandler, GET } from 'fastify-decorators';
import { Controller, ErrorHandler, GET, Scope } from 'fastify-decorators';
import { ErrorType } from './error-type.js';

@Controller({
route: '/stateless/error-handling',
type: ControllerType.REQUEST,
scope: Scope.PER_REQUEST,
})
export default class StatelessController {
@GET({
Expand Down
4 changes: 2 additions & 2 deletions examples/controllers/src/hooks/stateful.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FastifyReply, FastifyRequest } from 'fastify';
import { Controller, ControllerType, GET, Hook } from 'fastify-decorators';
import { Controller, GET, Hook, Scope } from 'fastify-decorators';

@Controller({
route: '/stateful/hooks',
type: ControllerType.SINGLETON,
scope: Scope.SINGLETON,
})
export default class StatefulController {
@GET()
Expand Down
4 changes: 2 additions & 2 deletions examples/controllers/src/hooks/stateless.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FastifyReply, FastifyRequest } from 'fastify';
import { Controller, ControllerType, GET, Hook } from 'fastify-decorators';
import { Controller, GET, Hook, Scope } from 'fastify-decorators';

@Controller({
route: '/stateless/hooks',
type: ControllerType.REQUEST,
scope: Scope.PER_REQUEST,
})
export default class StatelessController {
@GET()
Expand Down
4 changes: 2 additions & 2 deletions examples/controllers/src/start.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { app } from './index.js';

const hostname = '127.0.0.1';
const host = '127.0.0.1';
const port = 3003;

app.listen(port, hostname, (error, address) => {
app.listen({ port, host }, (error, address) => {
if (error != null) {
console.error(error);
process.exit(-1);
Expand Down
4 changes: 2 additions & 2 deletions examples/controllers/src/states/stateful.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { FastifyReply, FastifyRequest } from 'fastify';
import { Controller, ControllerType, GET, POST } from 'fastify-decorators';
import { Controller, GET, POST, Scope } from 'fastify-decorators';

type State = Record<string, unknown>;

@Controller({
route: '/stateful',
// This is default type, just here for consistency with stateless controller
type: ControllerType.SINGLETON,
scope: Scope.SINGLETON,
})
export default class StatefulController {
private state: State = {};
Expand Down
4 changes: 2 additions & 2 deletions examples/controllers/src/states/stateless.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { FastifyReply, FastifyRequest } from 'fastify';
import { Controller, ControllerType, GET, POST } from 'fastify-decorators';
import { Controller, GET, POST, Scope } from 'fastify-decorators';

type State = Record<string, unknown>;

@Controller({
route: '/stateless',
type: ControllerType.REQUEST,
scope: Scope.PER_REQUEST,
})
export default class StatelessController {
private state: State = {};
Expand Down
3 changes: 2 additions & 1 deletion examples/controllers/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
"target": "ES2022",
"target": "ES2023",
"module": "ESNext",
"outDir": "./dist",
"lib": ["ES2023"],

/* Strict Type-Checking Options */
"strict": true,
Expand Down
8 changes: 4 additions & 4 deletions examples/ecmascript-decorators/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
"test": "cross-env NODE_OPTIONS=\"--experimental-vm-modules --no-warnings\" jest"
},
"dependencies": {
"fastify": "^4.27.0",
"fastify": "^5.0.0",
"fastify-decorators": "^4.0.0-next.5"
},
"devDependencies": {
"@types/jest": "^29.5.7",
"@types/node": "~18.18.6",
"@types/jest": "^29.5.13",
"@types/node": "~20.12.14",
"jest": "^29.7.0",
"jest-resolve": "^29.7.0",
"jest-ts-webcompat-resolver": "^1.0.0",
"ts-jest": "^29.1.2",
"ts-jest": "^29.2.5",
"typescript": "^5.6.2",
"undici": "^6.19.8"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { FastifyReply, FastifyRequest } from 'fastify';
import { Controller, ControllerType, ErrorHandler, GET } from 'fastify-decorators';
import { Controller, ErrorHandler, GET, Scope } from 'fastify-decorators';
import { ErrorType } from './error-type.js';

@Controller({
route: '/stateful/error-handling',
type: ControllerType.REQUEST,
scope: Scope.SINGLETON,
})
export default class StatelessController {
@GET({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { FastifyReply, FastifyRequest } from 'fastify';
import { Controller, ControllerType, ErrorHandler, GET } from 'fastify-decorators';
import { Controller, ErrorHandler, GET, Scope } from 'fastify-decorators';
import { ErrorType } from './error-type.js';

@Controller({
route: '/stateless/error-handling',
type: ControllerType.REQUEST,
scope: Scope.PER_REQUEST,
})
export default class StatelessController {
@GET({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FastifyReply, FastifyRequest } from 'fastify';
import { Controller, ControllerType, GET, Hook } from 'fastify-decorators';
import { Controller, GET, Hook, Scope } from 'fastify-decorators';

@Controller({
route: '/stateful/hooks',
type: ControllerType.SINGLETON,
scope: Scope.SINGLETON,
})
export default class StatefulController {
@GET()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FastifyReply, FastifyRequest } from 'fastify';
import { Controller, ControllerType, GET, Hook } from 'fastify-decorators';
import { Controller, GET, Hook, Scope } from 'fastify-decorators';

@Controller({
route: '/stateless/hooks',
type: ControllerType.REQUEST,
scope: Scope.PER_REQUEST,
})
export default class StatelessController {
@GET()
Expand Down
4 changes: 2 additions & 2 deletions examples/ecmascript-decorators/src/start.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { app } from './index.js';

const hostname = '127.0.0.1';
const host = '127.0.0.1';
const port = 3003;

app.listen(port, hostname, (error, address) => {
app.listen({ port, host }, (error, address) => {
if (error != null) {
console.error(error);
process.exit(-1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { FastifyReply, FastifyRequest } from 'fastify';
import { Controller, ControllerType, GET, POST } from 'fastify-decorators';
import { Controller, GET, POST, Scope } from 'fastify-decorators';

type State = Record<string, unknown>;

@Controller({
route: '/stateful',
// This is default type, just here for consistency with stateless controller
type: ControllerType.SINGLETON,
scope: Scope.SINGLETON,
})
export default class StatefulController {
private state: State = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { FastifyReply, FastifyRequest } from 'fastify';
import { Controller, ControllerType, GET, POST } from 'fastify-decorators';
import { Controller, GET, POST, Scope } from 'fastify-decorators';

type State = Record<string, unknown>;

@Controller({
route: '/stateless',
type: ControllerType.REQUEST,
scope: Scope.PER_REQUEST,
})
export default class StatelessController {
private state: State = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { FastifyInstance } from 'fastify';
import { configureControllerTest } from '@fastify-decorators/simple-di/testing';
import fastify, { FastifyInstance } from 'fastify';
import { users } from '../../src/user/user.js';
import UserController from '../../src/user/user.controller.js';
import { bootstrap } from 'fastify-decorators';

describe('Controller: User', () => {
let app: FastifyInstance;
beforeEach(async () => {
app = await configureControllerTest({
controller: UserController,
app = await fastify();
await app.register(bootstrap, {
controllers: [UserController],
});
});
afterEach(() => users.clear());
Expand Down
8 changes: 3 additions & 5 deletions examples/ecmascript-decorators/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
"target": "ES2022",
"target": "ES2023",
"module": "ESNext",
"outDir": "./dist",
"lib": ["ES2023"],

/* Strict Type-Checking Options */
"strict": true,
Expand All @@ -14,10 +15,7 @@
"moduleResolution": "Node",
"esModuleInterop": true,

/* FIXME: TAKE A LOOK AT ME, I AM OMITED */
// "experimentalDecorators": false,
/* FIXME: TAKE A LOOK AT ME, I AM OMITED AS WELL */
// "emitDecoratorMetadata": true,
"experimentalDecorators": false,

/* Advanced Options */
"skipLibCheck": true,
Expand Down
Loading

0 comments on commit 63f0e51

Please sign in to comment.