@taicode/common-server 4.0.0-beta.2 → 4.0.0-beta.4
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.
- package/output/app/index.d.ts +13 -15
- package/output/app/index.d.ts.map +1 -1
- package/output/app/index.js +256 -18
- package/output/auth/index.d.ts +72 -0
- package/output/auth/index.d.ts.map +1 -0
- package/output/auth/index.js +176 -0
- package/output/core/assemble.d.ts +64 -0
- package/output/core/assemble.d.ts.map +1 -0
- package/output/core/assemble.js +88 -0
- package/output/core/boot/context.d.ts +89 -0
- package/output/core/boot/context.d.ts.map +1 -0
- package/output/core/boot/context.js +106 -0
- package/output/core/boot/lifecycle.d.ts +94 -0
- package/output/core/boot/lifecycle.d.ts.map +1 -0
- package/output/{app → core/boot}/lifecycle.js +45 -14
- package/output/core/boot/shutdown.d.ts +30 -0
- package/output/core/boot/shutdown.d.ts.map +1 -0
- package/output/core/boot/shutdown.js +45 -0
- package/output/core/boot/signal.d.ts +13 -0
- package/output/core/boot/signal.d.ts.map +1 -0
- package/output/{app → core/boot}/signal.js +3 -2
- package/output/core/boot/steps.d.ts +107 -0
- package/output/core/boot/steps.d.ts.map +1 -0
- package/output/core/boot/steps.js +200 -0
- package/output/core/entries.d.ts +123 -0
- package/output/core/entries.d.ts.map +1 -0
- package/output/core/entries.js +193 -0
- package/output/core/entries.test.d.ts +2 -0
- package/output/core/entries.test.d.ts.map +1 -0
- package/output/core/entries.test.js +217 -0
- package/output/core/env.d.ts +62 -0
- package/output/core/env.d.ts.map +1 -0
- package/output/core/env.js +96 -0
- package/output/{app → core}/env.test.d.ts.map +1 -1
- package/output/core/env.test.js +105 -0
- package/output/core/modules/auth-provider/index.d.ts +10 -0
- package/output/core/modules/auth-provider/index.d.ts.map +1 -0
- package/output/core/modules/auth-provider/index.js +9 -0
- package/output/core/modules/auth-provider/types.d.ts +71 -0
- package/output/core/modules/auth-provider/types.d.ts.map +1 -0
- package/output/core/modules/context/index.d.ts +18 -0
- package/output/core/modules/context/index.d.ts.map +1 -0
- package/output/core/modules/context/index.js +23 -0
- package/output/core/modules/context/index.test.d.ts.map +1 -0
- package/output/core/modules/cookie/index.d.ts +21 -0
- package/output/core/modules/cookie/index.d.ts.map +1 -0
- package/output/{app/core/cookie.js → core/modules/cookie/index.js} +1 -6
- package/output/core/modules/identity/index.d.ts +4 -0
- package/output/core/modules/identity/index.d.ts.map +1 -0
- package/output/{app/core/identity.js → core/modules/identity/index.js} +1 -1
- package/output/core/modules/logging/index.d.ts +5 -0
- package/output/core/modules/logging/index.d.ts.map +1 -0
- package/output/{app/core/logging.js → core/modules/logging/index.js} +2 -2
- package/output/core/modules/openapi/index.d.ts +7 -0
- package/output/core/modules/openapi/index.d.ts.map +1 -0
- package/output/{app/core/openapi.js → core/modules/openapi/index.js} +11 -12
- package/output/core/modules/permission-operations/index.d.ts +26 -0
- package/output/core/modules/permission-operations/index.d.ts.map +1 -0
- package/output/core/modules/permission-operations/index.js +36 -0
- package/output/core/modules/permission-operations/index.test.d.ts +2 -0
- package/output/core/modules/permission-operations/index.test.d.ts.map +1 -0
- package/output/core/modules/permission-operations/index.test.js +45 -0
- package/output/{app/core/request-id.d.ts → core/modules/request-id/index.d.ts} +1 -4
- package/output/core/modules/request-id/index.d.ts.map +1 -0
- package/output/{app/core/request-id.js → core/modules/request-id/index.js} +2 -2
- package/output/core/modules/response/index.d.ts +22 -0
- package/output/core/modules/response/index.d.ts.map +1 -0
- package/output/{app/core/response.js → core/modules/response/index.js} +22 -11
- package/output/{schema/index.d.ts → core/modules/schema/builders.d.ts} +8 -1
- package/output/core/modules/schema/builders.d.ts.map +1 -0
- package/output/{schema/index.js → core/modules/schema/builders.js} +7 -4
- package/output/core/modules/schema/index.d.ts +34 -0
- package/output/core/modules/schema/index.d.ts.map +1 -0
- package/output/core/modules/schema/index.js +62 -0
- package/output/core/modules/schema/index.test.d.ts +2 -0
- package/output/core/modules/schema/index.test.d.ts.map +1 -0
- package/output/core/modules/schema/index.test.js +55 -0
- package/output/core/modules/system-routes/index.d.ts +15 -0
- package/output/core/modules/system-routes/index.d.ts.map +1 -0
- package/output/core/modules/system-routes/index.js +171 -0
- package/output/core/test-support/database.d.ts +96 -0
- package/output/core/test-support/database.d.ts.map +1 -0
- package/output/core/test-support/database.js +144 -0
- package/output/core/test-support/database.test.d.ts +2 -0
- package/output/core/test-support/database.test.d.ts.map +1 -0
- package/output/core/test-support/database.test.js +110 -0
- package/output/core/test-support/environment.d.ts +33 -0
- package/output/core/test-support/environment.d.ts.map +1 -0
- package/output/core/test-support/environment.js +42 -0
- package/output/core/test-support/test-server.d.ts +94 -0
- package/output/core/test-support/test-server.d.ts.map +1 -0
- package/output/core/test-support/test-server.js +60 -0
- package/output/core/test-support/test-server.test.d.ts +2 -0
- package/output/core/test-support/test-server.test.d.ts.map +1 -0
- package/output/core/test-support/test-server.test.js +162 -0
- package/output/{app → core}/type-augment.d.ts +2 -2
- package/output/core/type-augment.d.ts.map +1 -0
- package/output/core/types.d.ts +338 -0
- package/output/core/types.d.ts.map +1 -0
- package/output/cors/index.d.ts +3 -0
- package/output/cors/index.d.ts.map +1 -0
- package/output/cors/index.js +27 -0
- package/output/index.d.ts +8 -4
- package/output/index.d.ts.map +1 -1
- package/output/index.js +24 -5
- package/output/rate-limit/index.d.ts +23 -0
- package/output/rate-limit/index.d.ts.map +1 -0
- package/output/rate-limit/index.js +42 -0
- package/output/{rate-limiter/rate-limiter.d.ts → rate-limit/limiter.d.ts} +1 -1
- package/output/rate-limit/limiter.d.ts.map +1 -0
- package/output/rate-limit/memory-store.d.ts.map +1 -0
- package/output/rate-limit/redis-store.d.ts.map +1 -0
- package/output/rate-limit/redis-store.test.d.ts.map +1 -0
- package/output/{rate-limiter → rate-limit}/redis-store.test.js +2 -2
- package/output/{rate-limiter → rate-limit}/types.d.ts +0 -1
- package/output/rate-limit/types.d.ts.map +1 -0
- package/output/rate-limit/types.js +1 -0
- package/output/{redis-cache/redis-cache.d.ts → redis/cache.d.ts} +1 -1
- package/output/redis/cache.d.ts.map +1 -0
- package/output/redis/cache.test.d.ts +2 -0
- package/output/redis/cache.test.d.ts.map +1 -0
- package/output/{redis-cache/redis-cache.test.js → redis/cache.test.js} +1 -1
- package/output/{app/redis.d.ts → redis/index.d.ts} +12 -7
- package/output/redis/index.d.ts.map +1 -0
- package/output/{app/redis.js → redis/index.js} +15 -5
- package/output/redis/index.test.d.ts +2 -0
- package/output/{context → redis}/index.test.d.ts.map +1 -1
- package/output/redis/index.test.js +55 -0
- package/output/redis-queue/index.js +1 -1
- package/output/redis-queue/test-helpers.d.ts +1 -1
- package/output/redis-queue/test-helpers.js +1 -1
- package/output/redis-queue/types.d.ts +3 -12
- package/output/redis-queue/types.d.ts.map +1 -1
- package/package.json +16 -15
- package/LICENSE +0 -21
- package/output/app/auth.test.d.ts +0 -2
- package/output/app/auth.test.d.ts.map +0 -1
- package/output/app/auth.test.js +0 -148
- package/output/app/boot.d.ts +0 -39
- package/output/app/boot.d.ts.map +0 -1
- package/output/app/boot.js +0 -140
- package/output/app/config.test.d.ts +0 -2
- package/output/app/config.test.d.ts.map +0 -1
- package/output/app/config.test.js +0 -132
- package/output/app/context.d.ts +0 -70
- package/output/app/context.d.ts.map +0 -1
- package/output/app/context.js +0 -73
- package/output/app/core/cookie.d.ts +0 -25
- package/output/app/core/cookie.d.ts.map +0 -1
- package/output/app/core/identity.d.ts +0 -12
- package/output/app/core/identity.d.ts.map +0 -1
- package/output/app/core/index.d.ts +0 -44
- package/output/app/core/index.d.ts.map +0 -1
- package/output/app/core/index.js +0 -57
- package/output/app/core/logging.d.ts +0 -13
- package/output/app/core/logging.d.ts.map +0 -1
- package/output/app/core/openapi.d.ts +0 -17
- package/output/app/core/openapi.d.ts.map +0 -1
- package/output/app/core/request-id.d.ts.map +0 -1
- package/output/app/core/response.d.ts +0 -41
- package/output/app/core/response.d.ts.map +0 -1
- package/output/app/define-server.negative-cases.d.ts +0 -40
- package/output/app/define-server.negative-cases.d.ts.map +0 -1
- package/output/app/define-server.negative-cases.js +0 -268
- package/output/app/define-server.test.d.ts +0 -2
- package/output/app/define-server.test.d.ts.map +0 -1
- package/output/app/define-server.test.js +0 -97
- package/output/app/entries.d.ts +0 -29
- package/output/app/entries.d.ts.map +0 -1
- package/output/app/entries.js +0 -64
- package/output/app/env.d.ts +0 -59
- package/output/app/env.d.ts.map +0 -1
- package/output/app/env.js +0 -66
- package/output/app/env.test.js +0 -124
- package/output/app/error-contract.test.d.ts +0 -2
- package/output/app/error-contract.test.d.ts.map +0 -1
- package/output/app/error-contract.test.js +0 -227
- package/output/app/errors.d.ts +0 -20
- package/output/app/errors.d.ts.map +0 -1
- package/output/app/errors.js +0 -51
- package/output/app/generate-openapi.test.d.ts +0 -2
- package/output/app/generate-openapi.test.d.ts.map +0 -1
- package/output/app/generate-openapi.test.js +0 -123
- package/output/app/lifecycle-logging.test.d.ts +0 -2
- package/output/app/lifecycle-logging.test.d.ts.map +0 -1
- package/output/app/lifecycle-logging.test.js +0 -155
- package/output/app/lifecycle.d.ts +0 -70
- package/output/app/lifecycle.d.ts.map +0 -1
- package/output/app/lifecycle.test.d.ts +0 -2
- package/output/app/lifecycle.test.d.ts.map +0 -1
- package/output/app/lifecycle.test.js +0 -152
- package/output/app/middleware/auth.d.ts +0 -71
- package/output/app/middleware/auth.d.ts.map +0 -1
- package/output/app/middleware/auth.js +0 -190
- package/output/app/middleware/context.d.ts +0 -13
- package/output/app/middleware/context.d.ts.map +0 -1
- package/output/app/middleware/context.js +0 -15
- package/output/app/middleware/cors.d.ts +0 -14
- package/output/app/middleware/cors.d.ts.map +0 -1
- package/output/app/middleware/cors.js +0 -30
- package/output/app/middleware/index.d.ts +0 -17
- package/output/app/middleware/index.d.ts.map +0 -1
- package/output/app/middleware/index.js +0 -16
- package/output/app/middleware/rate-limit.d.ts +0 -24
- package/output/app/middleware/rate-limit.d.ts.map +0 -1
- package/output/app/middleware/rate-limit.js +0 -46
- package/output/app/middleware-order.test.d.ts +0 -2
- package/output/app/middleware-order.test.d.ts.map +0 -1
- package/output/app/middleware-order.test.js +0 -148
- package/output/app/permission-operations.d.ts +0 -48
- package/output/app/permission-operations.d.ts.map +0 -1
- package/output/app/permission-operations.js +0 -77
- package/output/app/permission-operations.test.d.ts +0 -2
- package/output/app/permission-operations.test.d.ts.map +0 -1
- package/output/app/permission-operations.test.js +0 -72
- package/output/app/permission-registration.test.d.ts +0 -2
- package/output/app/permission-registration.test.d.ts.map +0 -1
- package/output/app/permission-registration.test.js +0 -138
- package/output/app/redis.d.ts.map +0 -1
- package/output/app/registered-operations.test.d.ts +0 -2
- package/output/app/registered-operations.test.d.ts.map +0 -1
- package/output/app/registered-operations.test.js +0 -57
- package/output/app/request-id.test.d.ts +0 -2
- package/output/app/request-id.test.d.ts.map +0 -1
- package/output/app/request-id.test.js +0 -130
- package/output/app/resources.test.d.ts +0 -2
- package/output/app/resources.test.d.ts.map +0 -1
- package/output/app/resources.test.js +0 -199
- package/output/app/shutdown.d.ts +0 -22
- package/output/app/shutdown.d.ts.map +0 -1
- package/output/app/shutdown.js +0 -30
- package/output/app/shutdown.test.d.ts +0 -2
- package/output/app/shutdown.test.d.ts.map +0 -1
- package/output/app/shutdown.test.js +0 -103
- package/output/app/signal.d.ts +0 -12
- package/output/app/signal.d.ts.map +0 -1
- package/output/app/smoke.test.d.ts +0 -2
- package/output/app/smoke.test.d.ts.map +0 -1
- package/output/app/smoke.test.js +0 -210
- package/output/app/test-support.d.ts +0 -21
- package/output/app/test-support.d.ts.map +0 -1
- package/output/app/test-support.js +0 -24
- package/output/app/type-augment.d.ts.map +0 -1
- package/output/app/types.d.ts +0 -228
- package/output/app/types.d.ts.map +0 -1
- package/output/context/index.d.ts +0 -6
- package/output/context/index.d.ts.map +0 -1
- package/output/context/index.js +0 -14
- package/output/rate-limiter/index.d.ts +0 -5
- package/output/rate-limiter/index.d.ts.map +0 -1
- package/output/rate-limiter/index.js +0 -4
- package/output/rate-limiter/memory-store.d.ts.map +0 -1
- package/output/rate-limiter/rate-limiter.d.ts.map +0 -1
- package/output/rate-limiter/redis-store.d.ts.map +0 -1
- package/output/rate-limiter/redis-store.test.d.ts.map +0 -1
- package/output/rate-limiter/types.d.ts.map +0 -1
- package/output/redis-cache/index.d.ts +0 -4
- package/output/redis-cache/index.d.ts.map +0 -1
- package/output/redis-cache/index.js +0 -1
- package/output/redis-cache/redis-cache.d.ts.map +0 -1
- package/output/redis-cache/redis-cache.test.d.ts +0 -2
- package/output/redis-cache/redis-cache.test.d.ts.map +0 -1
- package/output/schema/index.d.ts.map +0 -1
- /package/output/{app → core}/env.test.d.ts +0 -0
- /package/output/{rate-limiter → core/modules/auth-provider}/types.js +0 -0
- /package/output/{context → core/modules/context}/index.test.d.ts +0 -0
- /package/output/{context → core/modules/context}/index.test.js +0 -0
- /package/output/{app → core}/type-augment.js +0 -0
- /package/output/{app → core}/types.js +0 -0
- /package/output/{rate-limiter/rate-limiter.js → rate-limit/limiter.js} +0 -0
- /package/output/{rate-limiter → rate-limit}/memory-store.d.ts +0 -0
- /package/output/{rate-limiter → rate-limit}/memory-store.js +0 -0
- /package/output/{rate-limiter → rate-limit}/redis-store.d.ts +0 -0
- /package/output/{rate-limiter → rate-limit}/redis-store.js +0 -0
- /package/output/{rate-limiter → rate-limit}/redis-store.test.d.ts +0 -0
- /package/output/{redis-cache/redis-cache.js → redis/cache.js} +0 -0
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { defineServer, startServer } from './entries.js';
|
|
3
|
-
import { runShutdown, SHUTDOWN_ORDER } from './shutdown.js';
|
|
4
|
-
function createShutdownLog() {
|
|
5
|
-
const entries = [];
|
|
6
|
-
return { entries, log: { error: (obj, msg) => void entries.push({ obj, msg }) } };
|
|
7
|
-
}
|
|
8
|
-
function createRecordingHandles(record, throwOn) {
|
|
9
|
-
const step = (name) => () => {
|
|
10
|
-
record.push(name);
|
|
11
|
-
if (name === throwOn) {
|
|
12
|
-
throw new Error(`boom:${name}`);
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
return {
|
|
16
|
-
stopProducers: step('stopProducers'),
|
|
17
|
-
disposeServices: step('disposeServices'),
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
describe('SHUTDOWN_ORDER', () => {
|
|
21
|
-
it('固定两步且顺序不可变', () => {
|
|
22
|
-
expect(SHUTDOWN_ORDER).toEqual(['stopProducers', 'disposeServices']);
|
|
23
|
-
});
|
|
24
|
-
});
|
|
25
|
-
describe('runShutdown', () => {
|
|
26
|
-
it('按固定序执行全部步骤', async () => {
|
|
27
|
-
const record = [];
|
|
28
|
-
const { log } = createShutdownLog();
|
|
29
|
-
await runShutdown(createRecordingHandles(record), log);
|
|
30
|
-
expect(record).toEqual([...SHUTDOWN_ORDER]);
|
|
31
|
-
});
|
|
32
|
-
it('单步抛错不阻断后续,并聚合上报', async () => {
|
|
33
|
-
const record = [];
|
|
34
|
-
const { entries, log } = createShutdownLog();
|
|
35
|
-
await runShutdown(createRecordingHandles(record, 'stopProducers'), log);
|
|
36
|
-
expect(record).toEqual([...SHUTDOWN_ORDER]);
|
|
37
|
-
const aggregate = entries.find((entry) => entry.msg === '[shutdown] completed with errors');
|
|
38
|
-
expect(aggregate).toBeDefined();
|
|
39
|
-
const payload = aggregate?.obj;
|
|
40
|
-
expect(payload.count).toBe(1);
|
|
41
|
-
expect(payload.errors).toHaveLength(1);
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
describe('关闭流程集成', () => {
|
|
45
|
-
it('默认两步按序执行:disposer → services 逆序', async () => {
|
|
46
|
-
const calls = [];
|
|
47
|
-
const server = defineServer({
|
|
48
|
-
app: { service: 'shutdown-integration', title: 'Shutdown Integration', version: '1.0.0' },
|
|
49
|
-
resources: () => ({ database: { dispose: () => calls.push('disposeResource') } }),
|
|
50
|
-
config: () => ({}),
|
|
51
|
-
services: (ctx) => {
|
|
52
|
-
ctx.disposer.add(() => {
|
|
53
|
-
calls.push('stopProducers');
|
|
54
|
-
});
|
|
55
|
-
return {
|
|
56
|
-
a: {
|
|
57
|
-
dispose: () => {
|
|
58
|
-
calls.push('disposeServiceA');
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
b: {
|
|
62
|
-
dispose: () => {
|
|
63
|
-
calls.push('disposeServiceB');
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
};
|
|
67
|
-
},
|
|
68
|
-
middlewares: () => [],
|
|
69
|
-
routers: () => undefined,
|
|
70
|
-
});
|
|
71
|
-
const app = await startServer(server, { listen: false });
|
|
72
|
-
await app.close();
|
|
73
|
-
// 资源不在生命周期内(框架只管 services)
|
|
74
|
-
expect(calls).toEqual(['stopProducers', 'disposeServiceB', 'disposeServiceA']);
|
|
75
|
-
});
|
|
76
|
-
it('StartServerOptions.shutdown 整体覆盖默认关闭流程', async () => {
|
|
77
|
-
const calls = [];
|
|
78
|
-
const server = defineServer({
|
|
79
|
-
app: { service: 'shutdown-override', title: 'Shutdown Override', version: '1.0.0' },
|
|
80
|
-
resources: () => ({}),
|
|
81
|
-
config: () => ({}),
|
|
82
|
-
services: () => ({
|
|
83
|
-
a: {
|
|
84
|
-
dispose: () => {
|
|
85
|
-
calls.push('default-dispose');
|
|
86
|
-
},
|
|
87
|
-
},
|
|
88
|
-
}),
|
|
89
|
-
middlewares: () => [],
|
|
90
|
-
routers: () => undefined,
|
|
91
|
-
});
|
|
92
|
-
const app = await startServer(server, {
|
|
93
|
-
listen: false,
|
|
94
|
-
shutdown: async (ctx) => {
|
|
95
|
-
expect(ctx.app).toBeDefined();
|
|
96
|
-
expect(ctx.services).toBeDefined();
|
|
97
|
-
calls.push('custom');
|
|
98
|
-
},
|
|
99
|
-
});
|
|
100
|
-
await app.close();
|
|
101
|
-
expect(calls).toEqual(['custom']);
|
|
102
|
-
});
|
|
103
|
-
});
|
package/output/app/signal.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { LoggerLike } from './types.js';
|
|
2
|
-
import type { FastifyInstance } from 'fastify';
|
|
3
|
-
/**
|
|
4
|
-
* 内置信号处理(设计 §7.1 `activate` 尾部 / §7.2)。
|
|
5
|
-
*
|
|
6
|
-
* `SIGTERM` / `SIGINT` / `uncaughtException` / `unhandledRejection` ⇒ `app.close()`(触发 `onClose`
|
|
7
|
-
* 的关闭 6 步)⇒ `process.exit(0/1)`。**仅 `listen: true` 时装信号处理**。
|
|
8
|
-
*
|
|
9
|
-
* @returns detach 函数(移除本次注册的监听器)
|
|
10
|
-
*/
|
|
11
|
-
export declare function attachSignalHandlers(app: FastifyInstance, log: LoggerLike): () => void;
|
|
12
|
-
//# sourceMappingURL=signal.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"signal.d.ts","sourceRoot":"","sources":["../../source/app/signal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAC5C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAE9C;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,UAAU,GAAG,MAAM,IAAI,CA2CtF"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"smoke.test.d.ts","sourceRoot":"","sources":["../../source/app/smoke.test.ts"],"names":[],"mappings":""}
|
package/output/app/smoke.test.js
DELETED
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { defineServer, generateOpenApi, startServer } from './entries.js';
|
|
4
|
-
import { SystemError, UserError } from './errors.js';
|
|
5
|
-
import { defaultMiddlewares } from './middleware/index.js';
|
|
6
|
-
import { createCache, createRedis } from './redis.js';
|
|
7
|
-
const summarySchema = {
|
|
8
|
-
querystring: z.object({ currency: z.string().optional() }),
|
|
9
|
-
response: {
|
|
10
|
-
200: z.object({
|
|
11
|
-
status: z.string(),
|
|
12
|
-
message: z.string(),
|
|
13
|
-
data: z.object({ siteName: z.string(), currency: z.string() }),
|
|
14
|
-
}),
|
|
15
|
-
},
|
|
16
|
-
};
|
|
17
|
-
describe('端到端冒烟:defineServer → startServer → inject → generateOpenApi', () => {
|
|
18
|
-
it('真实装配路径跑通最小服务器(无 Postgres / 无 Redis / 无网络)', async () => {
|
|
19
|
-
const previousDatabaseUrl = process.env.DATABASE_URL;
|
|
20
|
-
const previousRedisUrl = process.env.REDIS_URL;
|
|
21
|
-
delete process.env.DATABASE_URL;
|
|
22
|
-
delete process.env.REDIS_URL;
|
|
23
|
-
const lifecycleEvents = [];
|
|
24
|
-
const counters = { readConfiguration: 0, disposeDatabase: 0 };
|
|
25
|
-
const database = {
|
|
26
|
-
readSystemConfiguration: async () => {
|
|
27
|
-
counters.readConfiguration += 1;
|
|
28
|
-
return { siteName: 'taicode-smoke', currency: 'CNY' };
|
|
29
|
-
},
|
|
30
|
-
};
|
|
31
|
-
try {
|
|
32
|
-
const server = defineServer({
|
|
33
|
-
app: { service: 'smoke', title: 'Smoke Service', version: '1.4.0' },
|
|
34
|
-
resources: (ctx) => {
|
|
35
|
-
const redis = createRedis(ctx.env, ctx.log);
|
|
36
|
-
return {
|
|
37
|
-
// 构造但不建连;出 spec 路径也会构造
|
|
38
|
-
database,
|
|
39
|
-
redis,
|
|
40
|
-
cache: createCache({ env: ctx.env, prefix: 'smoke', redis, log: ctx.log }),
|
|
41
|
-
};
|
|
42
|
-
},
|
|
43
|
-
config: async ({ resources }) => {
|
|
44
|
-
const configuration = await resources.database.readSystemConfiguration();
|
|
45
|
-
return { siteName: configuration.siteName, currency: configuration.currency };
|
|
46
|
-
},
|
|
47
|
-
// 声明序即启动序(services 之间不得相互依赖)
|
|
48
|
-
services: (ctx) => {
|
|
49
|
-
return {
|
|
50
|
-
// routers 拿不到 resources / config,只能穿过 service
|
|
51
|
-
// 配置一律在**调用期**现读:构造期不得读配置(出 spec 路径不跑 config 槽位)
|
|
52
|
-
configuration: {
|
|
53
|
-
readSiteName: () => ctx.config.siteName,
|
|
54
|
-
readCurrency: () => ctx.config.currency,
|
|
55
|
-
},
|
|
56
|
-
ledger: {
|
|
57
|
-
init: () => {
|
|
58
|
-
lifecycleEvents.push('init:ledger');
|
|
59
|
-
},
|
|
60
|
-
dispose: () => {
|
|
61
|
-
lifecycleEvents.push('dispose:ledger');
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
repository: {
|
|
65
|
-
init: () => {
|
|
66
|
-
lifecycleEvents.push('init:repository');
|
|
67
|
-
},
|
|
68
|
-
dispose: () => {
|
|
69
|
-
lifecycleEvents.push('dispose:repository');
|
|
70
|
-
},
|
|
71
|
-
},
|
|
72
|
-
mailer: {
|
|
73
|
-
init: () => {
|
|
74
|
-
lifecycleEvents.push('init:mailer');
|
|
75
|
-
},
|
|
76
|
-
dispose: () => {
|
|
77
|
-
lifecycleEvents.push('dispose:mailer');
|
|
78
|
-
},
|
|
79
|
-
},
|
|
80
|
-
databaseLifecycle: {
|
|
81
|
-
dispose: () => {
|
|
82
|
-
counters.disposeDatabase += 1;
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
};
|
|
86
|
-
},
|
|
87
|
-
// 默认预设 [cors, context, auth]
|
|
88
|
-
middlewares: (ctx) => defaultMiddlewares(ctx),
|
|
89
|
-
routers: (ctx) => {
|
|
90
|
-
ctx.app.get('/ledger/summary', { schema: summarySchema }, () => ({
|
|
91
|
-
siteName: ctx.services.configuration.readSiteName(),
|
|
92
|
-
currency: ctx.services.configuration.readCurrency(),
|
|
93
|
-
}));
|
|
94
|
-
ctx.app.get('/ledger/invalid-token', () => {
|
|
95
|
-
throw new UserError('INVALID_TOKEN', undefined, '令牌无效或已过期');
|
|
96
|
-
});
|
|
97
|
-
ctx.app.get('/ledger/database-down', () => {
|
|
98
|
-
throw new SystemError('DATABASE_UNAVAILABLE', undefined, '数据库暂不可用');
|
|
99
|
-
});
|
|
100
|
-
},
|
|
101
|
-
});
|
|
102
|
-
const app = await startServer(server, { listen: false });
|
|
103
|
-
// ── 装配期:声明序 init ────────────────────────────────────────────
|
|
104
|
-
expect(lifecycleEvents).toEqual(['init:ledger', 'init:repository', 'init:mailer']);
|
|
105
|
-
expect(counters.readConfiguration).toBe(1);
|
|
106
|
-
try {
|
|
107
|
-
// ── 业务路由:成功信封 ────────────────────────────────────────────
|
|
108
|
-
const summaryResponse = await app.inject({
|
|
109
|
-
method: 'GET',
|
|
110
|
-
url: '/ledger/summary?currency=CNY',
|
|
111
|
-
headers: { 'x-request-id': 'smoke-summary-1' },
|
|
112
|
-
});
|
|
113
|
-
expect(summaryResponse.statusCode).toBe(200);
|
|
114
|
-
expect(summaryResponse.json()).toEqual({
|
|
115
|
-
status: 'SUCCESS',
|
|
116
|
-
message: 'ok',
|
|
117
|
-
data: { siteName: 'taicode-smoke', currency: 'CNY' },
|
|
118
|
-
});
|
|
119
|
-
expect(summaryResponse.headers['x-request-id']).toBe('smoke-summary-1');
|
|
120
|
-
// ── UserError → 400 ───────────────────────────────────────────────
|
|
121
|
-
const userErrorResponse = await app.inject({ method: 'GET', url: '/ledger/invalid-token' });
|
|
122
|
-
expect(userErrorResponse.statusCode).toBe(400);
|
|
123
|
-
expect(userErrorResponse.json()).toEqual({
|
|
124
|
-
status: 'INVALID_TOKEN',
|
|
125
|
-
message: '令牌无效或已过期',
|
|
126
|
-
data: null,
|
|
127
|
-
});
|
|
128
|
-
// ── SystemError → 500 ─────────────────────────────────────────────
|
|
129
|
-
const systemErrorResponse = await app.inject({ method: 'GET', url: '/ledger/database-down' });
|
|
130
|
-
expect(systemErrorResponse.statusCode).toBe(500);
|
|
131
|
-
expect(systemErrorResponse.json()).toEqual({
|
|
132
|
-
status: 'DATABASE_UNAVAILABLE',
|
|
133
|
-
message: '数据库暂不可用',
|
|
134
|
-
data: null,
|
|
135
|
-
});
|
|
136
|
-
// ── 未匹配路由 → 404 ──────────────────────────────────────────────
|
|
137
|
-
const notFoundResponse = await app.inject({ method: 'GET', url: '/no-such-route' });
|
|
138
|
-
expect(notFoundResponse.statusCode).toBe(404);
|
|
139
|
-
expect(notFoundResponse.json()).toMatchObject({ status: 'NOT_IMPLEMENTED', data: null });
|
|
140
|
-
expect(notFoundResponse.headers['x-request-id']).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/);
|
|
141
|
-
expect(notFoundResponse.json()).not.toHaveProperty('requestId');
|
|
142
|
-
}
|
|
143
|
-
finally {
|
|
144
|
-
await app.close();
|
|
145
|
-
}
|
|
146
|
-
// ── 关闭:services 逆序 dispose ─────────────────────────────────────
|
|
147
|
-
expect(lifecycleEvents).toEqual([
|
|
148
|
-
'init:ledger',
|
|
149
|
-
'init:repository',
|
|
150
|
-
'init:mailer',
|
|
151
|
-
'dispose:mailer',
|
|
152
|
-
'dispose:repository',
|
|
153
|
-
'dispose:ledger',
|
|
154
|
-
]);
|
|
155
|
-
expect(counters.disposeDatabase).toBe(1);
|
|
156
|
-
// ── 同一份定义 → generateOpenApi:不跑 config、不重跑 init ──────────
|
|
157
|
-
const spec = (await generateOpenApi(server));
|
|
158
|
-
expect(counters.readConfiguration).toBe(1);
|
|
159
|
-
expect(lifecycleEvents).toHaveLength(6);
|
|
160
|
-
expect(spec.openapi).toBe('3.0.3');
|
|
161
|
-
expect(spec.info).toMatchObject({ title: 'Smoke Service', version: '1.4.0' });
|
|
162
|
-
expect(Object.keys(spec.paths ?? {})).toEqual(expect.arrayContaining(['/ledger/summary', '/ledger/invalid-token', '/ledger/database-down']));
|
|
163
|
-
}
|
|
164
|
-
finally {
|
|
165
|
-
if (previousDatabaseUrl === undefined) {
|
|
166
|
-
delete process.env.DATABASE_URL;
|
|
167
|
-
}
|
|
168
|
-
else {
|
|
169
|
-
process.env.DATABASE_URL = previousDatabaseUrl;
|
|
170
|
-
}
|
|
171
|
-
if (previousRedisUrl === undefined) {
|
|
172
|
-
delete process.env.REDIS_URL;
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
process.env.REDIS_URL = previousRedisUrl;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
it('非预置原生插件可在 routers(ctx) 里经 ctx.app.register() 注册并生效', async () => {
|
|
180
|
-
const smokeDecoratorPlugin = async (app) => {
|
|
181
|
-
app.decorate('smokeDecorator', () => 'smoke-decorated');
|
|
182
|
-
app.get('/plugin/decorated', () => ({
|
|
183
|
-
value: app.smokeDecorator(),
|
|
184
|
-
}));
|
|
185
|
-
};
|
|
186
|
-
const server = defineServer({
|
|
187
|
-
app: { service: 'smoke-plugin', title: 'Smoke Plugin', version: '1.0.0' },
|
|
188
|
-
resources: () => ({}),
|
|
189
|
-
config: () => ({}),
|
|
190
|
-
services: () => ({}),
|
|
191
|
-
middlewares: () => [],
|
|
192
|
-
routers: (ctx) => {
|
|
193
|
-
void ctx.app.register(smokeDecoratorPlugin);
|
|
194
|
-
},
|
|
195
|
-
});
|
|
196
|
-
const app = await startServer(server, { listen: false });
|
|
197
|
-
try {
|
|
198
|
-
const response = await app.inject({ method: 'GET', url: '/plugin/decorated' });
|
|
199
|
-
expect(response.statusCode).toBe(200);
|
|
200
|
-
expect(response.json()).toEqual({
|
|
201
|
-
status: 'SUCCESS',
|
|
202
|
-
message: 'ok',
|
|
203
|
-
data: { value: 'smoke-decorated' },
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
finally {
|
|
207
|
-
await app.close();
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
});
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { ConfigEnv } from './env.js';
|
|
2
|
-
import type { LoggerLike } from './types.js';
|
|
3
|
-
/**
|
|
4
|
-
* 测试公共助手(仅测试使用)。
|
|
5
|
-
*
|
|
6
|
-
* 只放「多个测试文件都要用」的小工具:记录型 logger、测试用 env。
|
|
7
|
-
* 断言与业务语义一律写在各自 `*.test.ts` 内,避免助手文件成为第二真相源。
|
|
8
|
-
*/
|
|
9
|
-
export interface LogEntry {
|
|
10
|
-
level: 'debug' | 'info' | 'warn' | 'error';
|
|
11
|
-
obj: unknown;
|
|
12
|
-
msg?: string;
|
|
13
|
-
}
|
|
14
|
-
/** 记录型 logger:满足 `LoggerLike`,便于断言「被调用了 / 在什么级别」 */
|
|
15
|
-
export interface RecordingLogger extends LoggerLike {
|
|
16
|
-
readonly entries: LogEntry[];
|
|
17
|
-
}
|
|
18
|
-
export declare function createRecordingLogger(): RecordingLogger;
|
|
19
|
-
/** 测试用 `ConfigEnv`(固定读取约定:`ENV` / `API_PORT` / `LOG_LEVEL` / `DATABASE_URL` / `REDIS_URL`) */
|
|
20
|
-
export declare function createTestConfigEnv(overrides?: Partial<ConfigEnv>): ConfigEnv;
|
|
21
|
-
//# sourceMappingURL=test-support.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"test-support.d.ts","sourceRoot":"","sources":["../../source/app/test-support.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AACzC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAE5C;;;;;GAKG;AAEH,MAAM,WAAW,QAAQ;IACvB,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAA;IAC1C,GAAG,EAAE,OAAO,CAAA;IACZ,GAAG,CAAC,EAAE,MAAM,CAAA;CACb;AAED,oDAAoD;AACpD,MAAM,WAAW,eAAgB,SAAQ,UAAU;IACjD,QAAQ,CAAC,OAAO,EAAE,QAAQ,EAAE,CAAA;CAC7B;AAED,wBAAgB,qBAAqB,IAAI,eAAe,CAcvD;AAED,8FAA8F;AAC9F,wBAAgB,mBAAmB,CAAC,SAAS,GAAE,OAAO,CAAC,SAAS,CAAM,GAAG,SAAS,CASjF"}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export function createRecordingLogger() {
|
|
2
|
-
const entries = [];
|
|
3
|
-
const record = (level) => (obj, msg) => {
|
|
4
|
-
entries.push({ level, obj, msg });
|
|
5
|
-
};
|
|
6
|
-
return {
|
|
7
|
-
entries,
|
|
8
|
-
debug: record('debug'),
|
|
9
|
-
info: record('info'),
|
|
10
|
-
warn: record('warn'),
|
|
11
|
-
error: record('error'),
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
/** 测试用 `ConfigEnv`(固定读取约定:`ENV` / `API_PORT` / `LOG_LEVEL` / `DATABASE_URL` / `REDIS_URL`) */
|
|
15
|
-
export function createTestConfigEnv(overrides = {}) {
|
|
16
|
-
return {
|
|
17
|
-
ENV: 'test',
|
|
18
|
-
API_PORT: '',
|
|
19
|
-
LOG_LEVEL: 'info',
|
|
20
|
-
DATABASE_URL: '',
|
|
21
|
-
REDIS_URL: undefined,
|
|
22
|
-
...overrides,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"type-augment.d.ts","sourceRoot":"","sources":["../../source/app/type-augment.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAEzD,8DAA8D;AAC9D,MAAM,MAAM,aAAa,GAAG,iBAAiB,GAAG,kBAAkB,GAAG,sBAAsB,CAAA;AAE3F,uEAAuE;AACvE,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,MAAM,CAAA;IACb,cAAc;IACd,MAAM,EAAE,MAAM,CAAA;IACd,wBAAwB;IACxB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;CAChB;AAED,OAAO,QAAQ,SAAS,CAAC;IACvB,UAAU,cAAc;QACtB,wBAAwB;QACxB,IAAI,CAAC,EAAE,OAAO,CAAA;QACd,wBAAwB;QACxB,KAAK,CAAC,EAAE,OAAO,CAAA;QACf,4CAA4C;QAC5C,GAAG,CAAC,EAAE,cAAc,CAAA;KACrB;IAED,UAAU,oBAAoB;QAC5B,+EAA+E;QAC/E,cAAc,CAAC,EAAE,aAAa,EAAE,CAAA;QAChC,cAAc;QACd,SAAS,CAAC,EAAE,aAAa,CAAA;KAC1B;CACF"}
|
package/output/app/types.d.ts
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
import type { Disposer } from '@taicode/common-base';
|
|
2
|
-
import type { FastifyBaseLogger, FastifyInstance, RawServerDefault } from 'fastify';
|
|
3
|
-
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
4
|
-
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
5
|
-
import type { ConfigEnv } from './env.js';
|
|
6
|
-
import type { SystemError } from './errors.js';
|
|
7
|
-
/**
|
|
8
|
-
* `defineServer` 槽位口径与接口类型(N1 / N2,**唯一规格来源**)。
|
|
9
|
-
*
|
|
10
|
-
* 顶层是「一个身份块 + 五个工厂槽位」,**键顺序即分层**:
|
|
11
|
-
*
|
|
12
|
-
* ```
|
|
13
|
-
* app (身份块,无依赖)
|
|
14
|
-
* resources ← env 引导期:唯一读 env 的地方
|
|
15
|
-
* config ← env + resources 引导期:❗不给 services(结构性解耦)
|
|
16
|
-
* services ← resources + config 运行期开始:❗不给 env、不给 services
|
|
17
|
-
* middlewares ← app + services + config (不给 resources / env)
|
|
18
|
-
* routers ← app + services (不给 resources / env / config)
|
|
19
|
-
* ```
|
|
20
|
-
*
|
|
21
|
-
* 槽位只能引用**排在它前面**的产物(向下引用是编译错误);`env` 只属引导期
|
|
22
|
-
* (`resources` / `config` 可见,运行期三层一律看不到)。
|
|
23
|
-
*
|
|
24
|
-
* 类型安全机制(N2 §1):
|
|
25
|
-
* - **M1 分阶段 ctx**:每个工厂只拿到「该阶段该看的字段」;
|
|
26
|
-
* - **M2 `Awaited<>` 归一**:工厂签名写 `(ctx) => R`(不是 `R | Promise<R>`),下游一律
|
|
27
|
-
* `Awaited<R>` / `Awaited<C>` / `Awaited<S>`;
|
|
28
|
-
* - **M3 资源嵌套**:资源包挂在 `ctx.resources` 上(不再摊平到 ctx,键冲突从根上消失);
|
|
29
|
-
* - **M4 `Live<T>` 静态开关**:资源包里值为 `false` 的键在类型上剔除;
|
|
30
|
-
* - **M5 强类型 `services`**:`middlewares` / `routers` 的 `ctx.services` 是 `S` 实体;
|
|
31
|
-
* - **M6 品牌 + 类型守卫**:`Server` 带品牌,`isServer()` 收窄;
|
|
32
|
-
* - **M7 负向类型测试**:`define-server.negative-cases.ts`,由 `tsc --noEmit` 覆盖。
|
|
33
|
-
*
|
|
34
|
-
* 命名全拼:`requestId` / `database` / `configuration` / `service`(不用 `db` / `cfg`)。
|
|
35
|
-
*/
|
|
36
|
-
/** 骨架只按 `(obj, msg)` 形态打日志;`app.log` 与 `@taicode/common-base` 的 `Logger` 都满足 */
|
|
37
|
-
export interface LoggerLike {
|
|
38
|
-
debug: (obj: unknown, msg?: string) => void;
|
|
39
|
-
info: (obj: unknown, msg?: string) => void;
|
|
40
|
-
warn: (obj: unknown, msg?: string) => void;
|
|
41
|
-
error: (obj: unknown, msg?: string) => void;
|
|
42
|
-
}
|
|
43
|
-
/** 同步或异步(`Awaited<>` 归一的原料) */
|
|
44
|
-
export type MaybePromise<T> = T | Promise<T>;
|
|
45
|
-
/**
|
|
46
|
-
* 路由级类型安全的载体:带 `ZodTypeProvider` 的 `FastifyInstance`。
|
|
47
|
-
*
|
|
48
|
-
* 裸 `FastifyInstance` 会让路由的 `params` / `body` / `query` 全是 `unknown`;带上 provider 后
|
|
49
|
-
* `request.params.id` 有类型。
|
|
50
|
-
*/
|
|
51
|
-
export type AppInstance = FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse, FastifyBaseLogger, ZodTypeProvider>;
|
|
52
|
-
/**
|
|
53
|
-
* 身份块:非 env 派生,出 spec 路径也要用。
|
|
54
|
-
*
|
|
55
|
-
* `version` **必填、无默认值**(缺它就是编译错误,杜绝「悄悄兜底成 1.0.0」)。
|
|
56
|
-
*/
|
|
57
|
-
export interface AppIdentity {
|
|
58
|
-
/** 权限注册键 / cache 前缀 / 失效通道前缀 / 日志标签 */
|
|
59
|
-
readonly service: string;
|
|
60
|
-
readonly title: string;
|
|
61
|
-
/** openapi `info.version` + 权限操作自注册;建议从 package.json 读入 */
|
|
62
|
-
readonly version: string;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Redis 句柄(惰性包装)。
|
|
66
|
-
*
|
|
67
|
-
* 框架**不操作** Redis 命令,只持有并在关闭时透传 dispose;构造时不建连、不 ping。
|
|
68
|
-
* 因此保持开放类型,各仓注入自己的客户端实现不受约束。
|
|
69
|
-
*/
|
|
70
|
-
export interface RedisHandle {
|
|
71
|
-
client: unknown;
|
|
72
|
-
dispose: () => void | Promise<void>;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* 剔掉值为 `false` 的键(编译期静态开关)。
|
|
76
|
-
*
|
|
77
|
-
* 资源包里写 `cache: false` 表示**静态关闭**:`Live<T>` 里该键**不存在**,访问直接编译失败;
|
|
78
|
-
* 而 `cache: Cache | undefined`(显式可选)表示**运行时缺省**:键存在,类型为 `Cache | undefined`。
|
|
79
|
-
*/
|
|
80
|
-
export type Live<T> = {
|
|
81
|
-
[K in keyof T as T[K] extends false ? never : K]: T[K];
|
|
82
|
-
};
|
|
83
|
-
/** 框架在 ctx 上暴露的资源视图(`Awaited<>` 归一 + `false` 键剔除) */
|
|
84
|
-
export type ResourcesView<R> = Live<Awaited<R>>;
|
|
85
|
-
/**
|
|
86
|
-
* 资源错误分类器(响应层用)。
|
|
87
|
-
*
|
|
88
|
-
* **每个资源注册自己的分类器**(一个资源一个):只认领自己错误域内的错误,认领不了返回 `null` 交给
|
|
89
|
-
* 下一个。由服务仓在 `resources(ctx)` 里通过 `ctx.registerClassifyError(...)` 注册;可注册多个,
|
|
90
|
-
* 按注册顺序求值取第一个非 `null` 的结果;全部未命中 / 一个都没注册 → 响应层落 `UNKNOWN_ERROR` 兜底。
|
|
91
|
-
*/
|
|
92
|
-
export type ErrorClassifier = (error: unknown) => SystemError | null;
|
|
93
|
-
/**
|
|
94
|
-
* `resources(ctx)` 的入参:**只有 `env` + `log`**(外加槽位专属的注册能力)。
|
|
95
|
-
*
|
|
96
|
-
* 结构上排除 `config` / `services` / `app` → 「资源依赖配置」这类循环**编译期写不出来**。
|
|
97
|
-
*/
|
|
98
|
-
export interface ResourcesContext {
|
|
99
|
-
readonly env: ConfigEnv;
|
|
100
|
-
readonly log: LoggerLike;
|
|
101
|
-
/**
|
|
102
|
-
* **每个资源注册自己的错误分类器**(响应层用)。
|
|
103
|
-
*
|
|
104
|
-
* 一个资源一个分类器,各管各的错误域:只认领自己域内的错误,认领不了返回 `null` 交给下一个。
|
|
105
|
-
* 可注册多个:按注册顺序求值,取第一个非 `null` 的结果;全部未命中 → 响应层落 `UNKNOWN_ERROR` 兜底。
|
|
106
|
-
* 本方法是 **resources 槽位专属能力**(其他槽位的 ctx 不提供)。
|
|
107
|
-
*/
|
|
108
|
-
registerClassifyError(classifier: ErrorClassifier): void;
|
|
109
|
-
}
|
|
110
|
-
export type ResourcesFactory<R> = (ctx: ResourcesContext) => R;
|
|
111
|
-
/**
|
|
112
|
-
* `config(ctx)` 的入参:`env` + `log` + 资源视图。
|
|
113
|
-
*
|
|
114
|
-
* **没有** `services`(此时尚未构造)/ `app`(此时尚未装配)。
|
|
115
|
-
*/
|
|
116
|
-
export interface ConfigContext<R> {
|
|
117
|
-
readonly env: ConfigEnv;
|
|
118
|
-
readonly log: LoggerLike;
|
|
119
|
-
readonly resources: ResourcesView<R>;
|
|
120
|
-
}
|
|
121
|
-
export type ConfigFactory<R, C> = (ctx: ConfigContext<R>) => C;
|
|
122
|
-
/**
|
|
123
|
-
* 一个 service 值:可选实现 `init` / `dispose`。
|
|
124
|
-
*
|
|
125
|
-
* **没有 `name` / `dependsOn`**:名字用对象键,依赖不存在(`services` ctx 没有 `services` 字段,
|
|
126
|
-
* 编译期禁止互依)。顺序语义只有一条:按**对象声明序** `init` / **逆序** `dispose`。
|
|
127
|
-
*/
|
|
128
|
-
export interface ServiceUnit {
|
|
129
|
-
init?(): void | Promise<void>;
|
|
130
|
-
dispose?(): void | Promise<void>;
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* `services(ctx)` 的入参:`log` + `disposer` + `resources` + `config`。
|
|
134
|
-
*
|
|
135
|
-
* ❗**故意没有 `env`**(服务层属运行期,不得读 env);❗**故意没有 `services`**(禁 service 互依)。
|
|
136
|
-
*/
|
|
137
|
-
export interface ServiceContext<R, C> {
|
|
138
|
-
readonly log: LoggerLike;
|
|
139
|
-
readonly disposer: Disposer;
|
|
140
|
-
readonly resources: ResourcesView<R>;
|
|
141
|
-
readonly config: Awaited<C>;
|
|
142
|
-
}
|
|
143
|
-
export type ServicesFactory<R, C, S> = (ctx: ServiceContext<R, C>) => S;
|
|
144
|
-
/**
|
|
145
|
-
* 中间件单元:一个注册函数(`register(app)`),按**数组序**注册。
|
|
146
|
-
*
|
|
147
|
-
* `name` 仅用于报错定位 / 日志跟踪;**不参与生命周期**(要常驻资源就放 `services`)。
|
|
148
|
-
*/
|
|
149
|
-
export interface MiddlewareUnit {
|
|
150
|
-
readonly name?: string;
|
|
151
|
-
register(app: AppInstance): void | Promise<void>;
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* `middlewares(ctx)` 的入参:`app` + `log` + `services` + `config`。
|
|
155
|
-
*
|
|
156
|
-
* ❗**故意没有 `resources` / `env`** —— 中间件要访问数据层必须穿过 service。
|
|
157
|
-
*/
|
|
158
|
-
export interface MiddlewareContext<C, S> {
|
|
159
|
-
readonly app: AppInstance;
|
|
160
|
-
readonly log: LoggerLike;
|
|
161
|
-
readonly services: Awaited<S>;
|
|
162
|
-
readonly config: Awaited<C>;
|
|
163
|
-
}
|
|
164
|
-
export type MiddlewaresFactory<C, S> = (ctx: MiddlewareContext<C, S>) => MiddlewareUnit[];
|
|
165
|
-
/**
|
|
166
|
-
* `routers(ctx)` 的入参:`app` + `log` + `services`。
|
|
167
|
-
*
|
|
168
|
-
* ❗**故意没有 `resources` / `env` / `config`** —— 路由**只能穿过 service**,绕不过层直连数据层。
|
|
169
|
-
*/
|
|
170
|
-
export interface RouterContext<S> {
|
|
171
|
-
readonly app: AppInstance;
|
|
172
|
-
readonly log: LoggerLike;
|
|
173
|
-
readonly services: Awaited<S>;
|
|
174
|
-
}
|
|
175
|
-
export type RoutersFactory<S> = (ctx: RouterContext<S>) => void | Promise<void>;
|
|
176
|
-
/**
|
|
177
|
-
* 若某个 service 值声明了 `dependsOn`,该值类型塌成 `never` → 赋值报错。
|
|
178
|
-
*/
|
|
179
|
-
export type AssertNoDependencies<S> = {
|
|
180
|
-
[K in keyof S]: S[K] extends {
|
|
181
|
-
dependsOn: unknown;
|
|
182
|
-
} ? never : S[K];
|
|
183
|
-
};
|
|
184
|
-
/**
|
|
185
|
-
* `services` 返回值形状闸门:
|
|
186
|
-
* - 原始类型值(`string` / `number` / `boolean`)**拒绝**(防把配置塞进 services);
|
|
187
|
-
* - 声明了 `dependsOn` 的值**拒绝**(禁 service 互依)。
|
|
188
|
-
*/
|
|
189
|
-
export type ServicesShape<S> = {
|
|
190
|
-
[K in keyof S]: S[K] extends string | number | boolean ? never : S[K] extends {
|
|
191
|
-
dependsOn: unknown;
|
|
192
|
-
} ? never : S[K];
|
|
193
|
-
};
|
|
194
|
-
/**
|
|
195
|
-
* `defineServer()` 入参:一个身份块 + 五个工厂槽位(键顺序=分层)。
|
|
196
|
-
*
|
|
197
|
-
* `S` 上的两道闸门(`ServicesShape<S>`,N2 §2.5)写在 `services` 的返回类型上:
|
|
198
|
-
* `S & ServicesShape<S>` —— 形状合法时恒等,非法时该值塌成 `never` → 编译失败。
|
|
199
|
-
*/
|
|
200
|
-
export interface ServerConfig<R, C, S> {
|
|
201
|
-
app: AppIdentity;
|
|
202
|
-
resources: ResourcesFactory<R>;
|
|
203
|
-
config: ConfigFactory<NoInfer<R>, C>;
|
|
204
|
-
services: (ctx: ServiceContext<NoInfer<R>, NoInfer<C>>) => S & ServicesShape<S>;
|
|
205
|
-
middlewares: MiddlewaresFactory<NoInfer<C>, NoInfer<S>>;
|
|
206
|
-
routers: RoutersFactory<NoInfer<S>>;
|
|
207
|
-
}
|
|
208
|
-
/** 品牌字段值(`Server` 的 `__brand`) */
|
|
209
|
-
export declare const SERVER_BRAND = "taicode.server";
|
|
210
|
-
/** 纯定义产物:同一份定义可同时喂给「启动」与「出 spec」 */
|
|
211
|
-
export type Server<R, C, S> = ServerConfig<R, C, S> & {
|
|
212
|
-
readonly __brand: typeof SERVER_BRAND;
|
|
213
|
-
};
|
|
214
|
-
/** `startServer()` 的逃生选项 */
|
|
215
|
-
export interface StartServerOptions<R = unknown, C = unknown, S = unknown> {
|
|
216
|
-
/** 是否 listen + 装信号;默认 `NODE_ENV !== 'test'` */
|
|
217
|
-
listen?: boolean;
|
|
218
|
-
/** 整体覆盖关闭流程 */
|
|
219
|
-
shutdown?: (ctx: ShutdownContext<R, C, S>) => Promise<void>;
|
|
220
|
-
}
|
|
221
|
-
/** 关闭阶段视图:`services` ctx + `app` + `services` 实体 */
|
|
222
|
-
export interface ShutdownContext<R, C, S> extends ServiceContext<R, C> {
|
|
223
|
-
readonly app: AppInstance;
|
|
224
|
-
readonly services: Awaited<S>;
|
|
225
|
-
}
|
|
226
|
-
/** `generateOpenApi()` 的返回(OpenAPI 文档对象) */
|
|
227
|
-
export type OpenApiDocument = Record<string, unknown>;
|
|
228
|
-
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../source/app/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAA;AACpD,OAAO,KAAK,EAAE,iBAAiB,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AACnF,OAAO,KAAK,EAAE,e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oC;AACpC,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GAAG;IACpD,QAAQ,CAAC,OAAO,EAAE,OAAO,YAAY,CAAA;CACtC,CAAA;AAED,4BAA4B;AAC5B,MAAM,WAAW,kBAAkB,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO;IACvE,+CAA+C;IAC/C,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,eAAe;IACf,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAC5D;AAED,oDAAoD;AACpD,MAAM,WAAW,eAAe,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAE,SAAQ,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC;IACpE,QAAQ,CAAC,GAAG,EAAE,WAAW,CAAA;IACzB,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC,CAAA;CAC9B;AAED,4CAA4C;AAC5C,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/context/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAEhE,MAAM,WAAW,cAAc;IAC7B,GAAG,EAAE,iBAAiB,CAAA;CACvB;AAcD,wBAAgB,aAAa,CAAC,OAAO,EAAE,cAAc,GAAG,cAAc,CAErE"}
|
package/output/context/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
const fallbackLogger = {
|
|
2
|
-
trace: () => { },
|
|
3
|
-
debug: () => { },
|
|
4
|
-
info: () => { },
|
|
5
|
-
warn: () => { },
|
|
6
|
-
error: () => { },
|
|
7
|
-
fatal: () => { },
|
|
8
|
-
child: () => fallbackLogger,
|
|
9
|
-
level: 'info',
|
|
10
|
-
silent: () => { },
|
|
11
|
-
};
|
|
12
|
-
export function createContext(request) {
|
|
13
|
-
return { log: request.log || fallbackLogger };
|
|
14
|
-
}
|