forked from fastify/fastify-awilix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test-d.ts
62 lines (48 loc) · 1.77 KB
/
index.test-d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { asValue, AwilixContainer } from 'awilix'
import fastify, { FastifyInstance } from 'fastify'
import { diContainer, FastifyAwilixOptions, fastifyAwilixPlugin, Cradle, RequestCradle } from './index'
import { diContainer as diContainerClassic, fastifyAwilixPlugin as fastifyAwilixPluginClassic } from './classic'
import { expectAssignable, expectNotType, expectType } from 'tsd'
expectAssignable<FastifyAwilixOptions>({})
expectAssignable<FastifyAwilixOptions>({ disposeOnClose: false })
expectAssignable<FastifyAwilixOptions>({ disposeOnResponse: false })
interface MailService {
greet(name: string): void
}
interface User {
name: string
}
declare module './' {
interface Cradle {
mailService: MailService
}
interface RequestCradle {
user: User
}
}
expectType<AwilixContainer<Cradle>>(diContainer)
expectType<AwilixContainer<Cradle>>(diContainerClassic)
expectNotType<AwilixContainer<Cradle & RequestCradle>>(diContainer)
expectNotType<AwilixContainer<RequestCradle>>(diContainer)
expectNotType<AwilixContainer<Cradle & RequestCradle>>(diContainerClassic)
expectNotType<AwilixContainer<RequestCradle>>(diContainerClassic)
expectType<MailService>(diContainer.cradle.mailService)
expectType<MailService>(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<AwilixContainer<Cradle & RequestCradle>>(request.diScope)
const mailService = request.diScope.cradle.mailService
const user = request.diScope.cradle.user
expectType<MailService>(mailService)
expectType<User>(user)
mailService.greet(user.name)
})