diff --git a/lib/index.test-d.ts b/lib/index.test-d.ts deleted file mode 100644 index 100a273..0000000 --- a/lib/index.test-d.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { asValue, AwilixContainer } from 'awilix' -import fastify, { FastifyInstance } from 'fastify' -import { diContainer, diContainerClassic, FastifyAwilixOptions, fastifyAwilixPlugin, Cradle, RequestCradle } from './index' - -import { expectAssignable, expectNotType, expectType } from 'tsd' - -expectAssignable({}) -expectAssignable({ disposeOnClose: false }) -expectAssignable({ container: diContainer }) -expectAssignable({ container: diContainerClassic }) -expectAssignable({ injectionMode: 'CLASSIC' }) -expectAssignable({ injectionMode: 'PROXY' }) - -expectAssignable({ disposeOnResponse: false }) -expectAssignable({ asyncInit: false, asyncDispose: false }) -expectAssignable({ asyncInit: true, asyncDispose: true }) -expectAssignable({ eagerInject: true }) - -expectAssignable({ strictBooleanEnforced: true }) -expectAssignable({ strictBooleanEnforced: false }) - -interface MailService { - greet(name: string): void -} -interface User { - name: string -} - -declare module './' { - interface Cradle { - mailService: MailService - } - interface RequestCradle { - user: User - } -} - -expectType>(diContainer) -expectType>(diContainerClassic) - -expectNotType>(diContainer) -expectNotType>(diContainer) -expectNotType>(diContainerClassic) -expectNotType>(diContainerClassic) - -expectType(diContainer.cradle.mailService) -expectType(diContainer.resolve('mailService')) - -const app: FastifyInstance = fastify() - -app.register(fastifyAwilixPlugin, {}) - -app.addHook('onRequest', (request, _reply, done) => { - request.diScope.register({ - user: asValue({ - name: 'John Doe', - }), - }) - done() -}) - -app.get('/user', (request) => { - expectType>(request.diScope) - - const mailService = request.diScope.cradle.mailService - const user = request.diScope.cradle.user - - expectType(mailService) - expectType(user) - - mailService.greet(user.name) -}) diff --git a/lib/index.tst.ts b/lib/index.tst.ts new file mode 100644 index 0000000..431feab --- /dev/null +++ b/lib/index.tst.ts @@ -0,0 +1,72 @@ +import { asValue, AwilixContainer } from 'awilix' +import fastify, { FastifyInstance } from 'fastify' +import { diContainer, diContainerClassic, FastifyAwilixOptions, fastifyAwilixPlugin, Cradle, RequestCradle } from './index' +import { expect } from 'tstyche' + +expect().type.toBeAssignableFrom({}) +expect().type.toBeAssignableFrom({ disposeOnClose: false }) +expect().type.toBeAssignableFrom({ container: diContainer }) +expect().type.toBeAssignableFrom({ container: diContainerClassic }) +expect().type.toBeAssignableFrom({ injectionMode: 'CLASSIC' } as const) +expect().type.toBeAssignableFrom({ injectionMode: 'PROXY' } as const) +expect().type.toBeAssignableFrom({ disposeOnResponse: false }) +expect().type.toBeAssignableFrom({ asyncInit: false, asyncDispose: false }) +expect().type.toBeAssignableFrom({ asyncInit: true, asyncDispose: true }) +expect().type.toBeAssignableFrom({ eagerInject: true }) + +expect().type.toBeAssignableFrom({ strictBooleanEnforced: true }) +expect().type.toBeAssignableFrom({ strictBooleanEnforced: false }) + +interface MailService { + greet(name: string): void +} +interface User { + name: string +} + +declare module './' { + interface Cradle { + mailService: MailService + } + interface RequestCradle { + user: User + } +} + +expect(diContainer).type.toBe>() +expect(diContainerClassic).type.toBe>() + +expect(diContainer).type.not.toBe>() +expect(diContainer).type.not.toBe>() +expect(diContainerClassic).type.not.toBe< + AwilixContainer +>() +expect(diContainerClassic).type.not.toBe>() + +expect(diContainer.cradle.mailService).type.toBe() +expect(diContainer.resolve('mailService')).type.toBe() + +const app: FastifyInstance = fastify() + +app.register(fastifyAwilixPlugin, {}) + +app.addHook('onRequest', (request, _reply, done) => { + request.diScope.register({ + user: asValue({ + name: 'John Doe', + }), + }) + done() +}) + +app.get('/user', (request) => { + expect(request.diScope).type.toBe>() + + const mailService = request.diScope.cradle.mailService + const user = request.diScope.cradle.user + + expect(mailService).type.toBe() + expect(user).type.toBe() + + mailService.greet(user.name) +}) diff --git a/package.json b/package.json index d1c6701..959b253 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "lint": "eslint", "lint:fix": "eslint --fix", "test": "npm run test:unit && npm run test:typescript", - "test:typescript": "tsd", + "test:typescript": "tstyche", "test:unit": "c8 --100 node --test" }, "dependencies": { @@ -54,7 +54,7 @@ "eslint": "^9.17.0", "fastify": "^5.6.2", "neostandard": "^0.13.0", - "tsd": "^0.33.0" + "tstyche": "^7.0.0" }, "homepage": "http://github.com/fastify/fastify-awilix", "funding": [ @@ -94,4 +94,4 @@ "publishConfig": { "access": "public" } -} +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index eec9314..a925e4c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,14 +1,16 @@ { "compilerOptions": { "outDir": "dist", - "module": "commonjs", + "rootDir": "lib", + "module": "node16", "target": "es2015", "sourceMap": false, "declaration": true, "declarationMap": false, "types": ["node"], "strict": true, - "moduleResolution": "node", + "noEmit": true, + "moduleResolution": "node16", "noUnusedLocals": false, "noUnusedParameters": false, "noFallthroughCasesInSwitch": true, @@ -17,7 +19,6 @@ "noImplicitThis": true, "strictNullChecks": true, "importHelpers": true, - "baseUrl": ".", "allowSyntheticDefaultImports": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true