@dangao/bun-server 1.8.0 → 1.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/docs/api.md +194 -81
  2. package/docs/extensions.md +53 -0
  3. package/docs/guide.md +243 -1
  4. package/docs/microservice-config-center.md +73 -74
  5. package/docs/microservice-nacos.md +89 -90
  6. package/docs/microservice-service-registry.md +85 -86
  7. package/docs/microservice.md +142 -137
  8. package/docs/request-lifecycle.md +45 -4
  9. package/docs/symbol-interface-pattern.md +106 -106
  10. package/docs/zh/api.md +458 -18
  11. package/docs/zh/extensions.md +53 -0
  12. package/docs/zh/guide.md +251 -4
  13. package/docs/zh/microservice-config-center.md +258 -0
  14. package/docs/zh/microservice-nacos.md +346 -0
  15. package/docs/zh/microservice-service-registry.md +306 -0
  16. package/docs/zh/microservice.md +680 -0
  17. package/docs/zh/request-lifecycle.md +43 -5
  18. package/package.json +1 -1
  19. package/tests/auth/auth-decorators.test.ts +241 -0
  20. package/tests/auth/oauth2-service.test.ts +318 -0
  21. package/tests/cache/cache-decorators-extended.test.ts +272 -0
  22. package/tests/cache/cache-interceptors.test.ts +534 -0
  23. package/tests/cache/cache-service-proxy.test.ts +246 -0
  24. package/tests/cache/memory-cache-store.test.ts +155 -0
  25. package/tests/cache/redis-cache-store.test.ts +199 -0
  26. package/tests/config/config-center-integration.test.ts +334 -0
  27. package/tests/config/config-module-extended.test.ts +165 -0
  28. package/tests/controller/param-binder.test.ts +333 -0
  29. package/tests/error/error-handler.test.ts +166 -57
  30. package/tests/error/i18n-extended.test.ts +105 -0
  31. package/tests/events/event-listener-scanner.test.ts +114 -0
  32. package/tests/events/event-module.test.ts +133 -302
  33. package/tests/extensions/logger-module.test.ts +158 -0
  34. package/tests/files/file-storage.test.ts +136 -0
  35. package/tests/interceptor/base-interceptor.test.ts +605 -0
  36. package/tests/interceptor/builtin/cache-interceptor.test.ts +233 -86
  37. package/tests/interceptor/builtin/log-interceptor.test.ts +469 -0
  38. package/tests/interceptor/builtin/permission-interceptor.test.ts +219 -120
  39. package/tests/interceptor/interceptor-chain.test.ts +241 -189
  40. package/tests/interceptor/interceptor-metadata.test.ts +221 -0
  41. package/tests/microservice/circuit-breaker.test.ts +221 -0
  42. package/tests/microservice/service-client-decorators.test.ts +86 -0
  43. package/tests/microservice/service-client-interceptors.test.ts +274 -0
  44. package/tests/microservice/service-registry-decorators.test.ts +147 -0
  45. package/tests/microservice/tracer.test.ts +213 -0
  46. package/tests/microservice/tracing-collectors.test.ts +168 -0
  47. package/tests/middleware/builtin/middleware-builtin-extended.test.ts +237 -0
  48. package/tests/middleware/builtin/rate-limit.test.ts +257 -0
  49. package/tests/middleware/middleware-decorators.test.ts +222 -0
  50. package/tests/middleware/middleware-pipeline.test.ts +160 -0
  51. package/tests/queue/queue-decorators.test.ts +139 -0
  52. package/tests/queue/queue-service.test.ts +191 -0
  53. package/tests/request/body-parser-extended.test.ts +291 -0
  54. package/tests/request/request-wrapper.test.ts +319 -0
  55. package/tests/router/router-decorators.test.ts +260 -0
  56. package/tests/router/router-extended.test.ts +298 -0
  57. package/tests/security/guards/reflector.test.ts +188 -0
  58. package/tests/security/security-filter.test.ts +182 -0
  59. package/tests/security/security-module-extended.test.ts +133 -0
  60. package/tests/session/memory-session-store.test.ts +172 -0
  61. package/tests/session/session-decorators.test.ts +163 -0
  62. package/tests/swagger/ui.test.ts +212 -0
@@ -0,0 +1,272 @@
1
+ import { describe, expect, test, beforeEach } from 'bun:test';
2
+ import 'reflect-metadata';
3
+
4
+ import {
5
+ Cacheable,
6
+ CacheEvict,
7
+ CachePut,
8
+ getCacheableMetadata,
9
+ getCacheEvictMetadata,
10
+ getCachePutMetadata,
11
+ type CacheableOptions,
12
+ type CacheEvictOptions,
13
+ type CachePutOptions,
14
+ } from '../../src/cache/decorators';
15
+
16
+ describe('Cache Decorators', () => {
17
+ describe('@Cacheable', () => {
18
+ test('should set cacheable metadata with default options', () => {
19
+ class TestService {
20
+ @Cacheable()
21
+ public findAll(): string[] {
22
+ return ['a', 'b'];
23
+ }
24
+ }
25
+
26
+ const metadata = getCacheableMetadata(TestService.prototype.findAll);
27
+ expect(metadata).toBeDefined();
28
+ expect(metadata?.key).toBeUndefined();
29
+ expect(metadata?.keyPrefix).toBeUndefined();
30
+ expect(metadata?.ttl).toBeUndefined();
31
+ expect(metadata?.condition).toBeUndefined();
32
+ });
33
+
34
+ test('should set cacheable metadata with custom key', () => {
35
+ class TestService {
36
+ @Cacheable({ key: 'user:{0}' })
37
+ public findById(id: string): string {
38
+ return id;
39
+ }
40
+ }
41
+
42
+ const metadata = getCacheableMetadata(TestService.prototype.findById);
43
+ expect(metadata?.key).toBe('user:{0}');
44
+ });
45
+
46
+ test('should set cacheable metadata with keyPrefix', () => {
47
+ class TestService {
48
+ @Cacheable({ keyPrefix: 'users' })
49
+ public findAll(): string[] {
50
+ return [];
51
+ }
52
+ }
53
+
54
+ const metadata = getCacheableMetadata(TestService.prototype.findAll);
55
+ expect(metadata?.keyPrefix).toBe('users');
56
+ });
57
+
58
+ test('should set cacheable metadata with ttl', () => {
59
+ class TestService {
60
+ @Cacheable({ ttl: 60000 })
61
+ public getData(): string {
62
+ return 'data';
63
+ }
64
+ }
65
+
66
+ const metadata = getCacheableMetadata(TestService.prototype.getData);
67
+ expect(metadata?.ttl).toBe(60000);
68
+ });
69
+
70
+ test('should set cacheable metadata with condition', () => {
71
+ class TestService {
72
+ @Cacheable({ condition: 'result.status === "success"' })
73
+ public fetchData(): { status: string } {
74
+ return { status: 'success' };
75
+ }
76
+ }
77
+
78
+ const metadata = getCacheableMetadata(TestService.prototype.fetchData);
79
+ expect(metadata?.condition).toBe('result.status === "success"');
80
+ });
81
+
82
+ test('should set all options together', () => {
83
+ const options: CacheableOptions = {
84
+ key: 'item:{id}',
85
+ keyPrefix: 'shop',
86
+ ttl: 30000,
87
+ condition: 'true',
88
+ };
89
+
90
+ class TestService {
91
+ @Cacheable(options)
92
+ public getItem(id: string): string {
93
+ return id;
94
+ }
95
+ }
96
+
97
+ const metadata = getCacheableMetadata(TestService.prototype.getItem);
98
+ expect(metadata?.key).toBe('item:{id}');
99
+ expect(metadata?.keyPrefix).toBe('shop');
100
+ expect(metadata?.ttl).toBe(30000);
101
+ expect(metadata?.condition).toBe('true');
102
+ });
103
+ });
104
+
105
+ describe('@CacheEvict', () => {
106
+ test('should set cache evict metadata with default options', () => {
107
+ class TestService {
108
+ @CacheEvict()
109
+ public deleteAll(): void {}
110
+ }
111
+
112
+ const metadata = getCacheEvictMetadata(TestService.prototype.deleteAll);
113
+ expect(metadata).toBeDefined();
114
+ expect(metadata?.beforeInvocation).toBe(false);
115
+ expect(metadata?.allEntries).toBe(false);
116
+ });
117
+
118
+ test('should set cache evict metadata with custom key', () => {
119
+ class TestService {
120
+ @CacheEvict({ key: 'user:{0}' })
121
+ public deleteUser(id: string): void {}
122
+ }
123
+
124
+ const metadata = getCacheEvictMetadata(TestService.prototype.deleteUser);
125
+ expect(metadata?.key).toBe('user:{0}');
126
+ });
127
+
128
+ test('should set beforeInvocation option', () => {
129
+ class TestService {
130
+ @CacheEvict({ beforeInvocation: true })
131
+ public clearBefore(): void {}
132
+ }
133
+
134
+ const metadata = getCacheEvictMetadata(TestService.prototype.clearBefore);
135
+ expect(metadata?.beforeInvocation).toBe(true);
136
+ });
137
+
138
+ test('should set allEntries option', () => {
139
+ class TestService {
140
+ @CacheEvict({ allEntries: true })
141
+ public clearAll(): void {}
142
+ }
143
+
144
+ const metadata = getCacheEvictMetadata(TestService.prototype.clearAll);
145
+ expect(metadata?.allEntries).toBe(true);
146
+ });
147
+
148
+ test('should set all options together', () => {
149
+ const options: CacheEvictOptions = {
150
+ key: 'item:{0}',
151
+ keyPrefix: 'shop',
152
+ beforeInvocation: true,
153
+ allEntries: true,
154
+ };
155
+
156
+ class TestService {
157
+ @CacheEvict(options)
158
+ public clearShop(id: string): void {}
159
+ }
160
+
161
+ const metadata = getCacheEvictMetadata(TestService.prototype.clearShop);
162
+ expect(metadata?.key).toBe('item:{0}');
163
+ expect(metadata?.keyPrefix).toBe('shop');
164
+ expect(metadata?.beforeInvocation).toBe(true);
165
+ expect(metadata?.allEntries).toBe(true);
166
+ });
167
+ });
168
+
169
+ describe('@CachePut', () => {
170
+ test('should set cache put metadata with default options', () => {
171
+ class TestService {
172
+ @CachePut()
173
+ public update(): string {
174
+ return 'updated';
175
+ }
176
+ }
177
+
178
+ const metadata = getCachePutMetadata(TestService.prototype.update);
179
+ expect(metadata).toBeDefined();
180
+ expect(metadata?.key).toBeUndefined();
181
+ expect(metadata?.ttl).toBeUndefined();
182
+ expect(metadata?.condition).toBeUndefined();
183
+ });
184
+
185
+ test('should set cache put metadata with custom key', () => {
186
+ class TestService {
187
+ @CachePut({ key: 'user:{0}' })
188
+ public updateUser(id: string): string {
189
+ return id;
190
+ }
191
+ }
192
+
193
+ const metadata = getCachePutMetadata(TestService.prototype.updateUser);
194
+ expect(metadata?.key).toBe('user:{0}');
195
+ });
196
+
197
+ test('should set cache put metadata with ttl', () => {
198
+ class TestService {
199
+ @CachePut({ ttl: 120000 })
200
+ public refreshData(): string {
201
+ return 'data';
202
+ }
203
+ }
204
+
205
+ const metadata = getCachePutMetadata(TestService.prototype.refreshData);
206
+ expect(metadata?.ttl).toBe(120000);
207
+ });
208
+
209
+ test('should set cache put metadata with condition', () => {
210
+ class TestService {
211
+ @CachePut({ condition: 'result !== null' })
212
+ public saveData(): string | null {
213
+ return 'data';
214
+ }
215
+ }
216
+
217
+ const metadata = getCachePutMetadata(TestService.prototype.saveData);
218
+ expect(metadata?.condition).toBe('result !== null');
219
+ });
220
+
221
+ test('should set all options together', () => {
222
+ const options: CachePutOptions = {
223
+ key: 'product:{0}',
224
+ keyPrefix: 'inventory',
225
+ ttl: 45000,
226
+ condition: 'true',
227
+ };
228
+
229
+ class TestService {
230
+ @CachePut(options)
231
+ public updateProduct(id: string): string {
232
+ return id;
233
+ }
234
+ }
235
+
236
+ const metadata = getCachePutMetadata(TestService.prototype.updateProduct);
237
+ expect(metadata?.key).toBe('product:{0}');
238
+ expect(metadata?.keyPrefix).toBe('inventory');
239
+ expect(metadata?.ttl).toBe(45000);
240
+ expect(metadata?.condition).toBe('true');
241
+ });
242
+ });
243
+
244
+ describe('Metadata getters', () => {
245
+ test('getCacheableMetadata should return undefined for non-decorated method', () => {
246
+ class TestService {
247
+ public normalMethod(): void {}
248
+ }
249
+
250
+ const metadata = getCacheableMetadata(TestService.prototype.normalMethod);
251
+ expect(metadata).toBeUndefined();
252
+ });
253
+
254
+ test('getCacheEvictMetadata should return undefined for non-decorated method', () => {
255
+ class TestService {
256
+ public normalMethod(): void {}
257
+ }
258
+
259
+ const metadata = getCacheEvictMetadata(TestService.prototype.normalMethod);
260
+ expect(metadata).toBeUndefined();
261
+ });
262
+
263
+ test('getCachePutMetadata should return undefined for non-decorated method', () => {
264
+ class TestService {
265
+ public normalMethod(): void {}
266
+ }
267
+
268
+ const metadata = getCachePutMetadata(TestService.prototype.normalMethod);
269
+ expect(metadata).toBeUndefined();
270
+ });
271
+ });
272
+ });