tsdkarc 1.1.0 → 1.1.1

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/README.md CHANGED
@@ -8,6 +8,8 @@
8
8
 
9
9
  ---
10
10
 
11
+ [![npm version](https://img.shields.io/npm/v/tsdkarc.svg?style=flat)](https://www.npmjs.com/package/tsdkarc) [![Size](https://deno.bundlejs.com/badge?q=tsdkarc&config={%22esbuild%22:{%22external%22:[%22react%22,%22react-dom%22,%22react/jsx-runtime%22]}})](https://bundlejs.com/?q=tsdkarc&treeshake=%5B%7Blittkk%7D%5D&config=%7B%22esbuild%22:%7B%22external%22:%5B%22react%22,%22react-dom%22,%22react/jsx-runtime%22%5D%7D%7D) ![0 dependencies](https://img.shields.io/badge/0-dependencies!-brightgreen) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/suhaotian/tsdkarc/pulls) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/suhaotian/tsdkarc/blob/main/LICENSE) [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/tsdkarc) ![typescript](https://badgen.net/badge/icon/typescript?icon=typescript&label&color=blue)
12
+
11
13
  ## Why `TsdkArc`
12
14
 
13
15
  Your application codebases grow, the code become coupled and messy — hard to reuse, hard to share.
@@ -215,7 +217,7 @@ beforeBoot → boot → afterBoot → [running] → beforeShutdown → shutdown
215
217
  | Hook | Purpose |
216
218
  | -------------------- | ------------------------------------------------------------------------------ |
217
219
  | `beforeBoot` | Pre-setup, Called once before the first module begins booting. |
218
- | `boot` | Register values on `ctx` via `ctx.set()` |
220
+ | `boot` | Register values on `ctx` via return ctx or call `ctx.set()` |
219
221
  | `afterBoot` | Called once after the last module has finished booting. |
220
222
  | `beforeShutdown` | Called once before the first module begins shutting down. |
221
223
  | `shutdown` | Release resources |
package/esm/index.d.ts CHANGED
@@ -90,7 +90,7 @@ export interface Module<S extends object, Sl extends object = S> {
90
90
  name: string;
91
91
  description?: string;
92
92
  modules?: AnyModule[];
93
- boot?(ctx: ContextWriter<S, Sl>): Promise<void> | void;
93
+ boot?(ctx: ContextWriter<S, Sl>): Promise<void> | void | Sl | Promise<Sl>;
94
94
  shutdown?(ctx: ContextWriter<S, Sl>): Promise<void> | void;
95
95
  /** Called immediately before this module's own boot(). */
96
96
  beforeBoot?(ctx: ContextWriter<S, Sl>): Promise<void> | void;
@@ -117,17 +117,37 @@ export interface Module<S extends object, Sl extends object = S> {
117
117
  *
118
118
  * @param def module definition
119
119
  */
120
- export declare function defineModule<OwnSlice extends object = Record<never, never>>(): <const Deps extends readonly AnyModule[] = []>(def: {
120
+ export declare function defineModule<OwnSlice extends object = Record<never, never>>(): <const Deps extends readonly AnyModule[] = [], R extends OwnSlice = OwnSlice>(def: {
121
121
  name: string;
122
122
  description?: string;
123
123
  modules?: Deps;
124
- boot?(ctx: ContextWriter<FullContext<Deps, OwnSlice>, OwnSlice>): Promise<void> | void | OwnSlice | Promise<OwnSlice>;
124
+ boot?(ctx: ContextWriter<FullContext<Deps, OwnSlice>, NoOverlap<MergeSlices<Deps>, OwnSlice>>): Promise<void> | void | (R & Exact<NoOverlap<MergeSlices<Deps>, OwnSlice>, R>) | Promise<R & Exact<NoOverlap<MergeSlices<Deps>, OwnSlice>, R>>;
125
125
  shutdown?(ctx: ContextWriter<FullContext<Deps, OwnSlice>, OwnSlice>): Promise<void> | void;
126
126
  beforeBoot?(ctx: ContextWriter<FullContext<Deps, OwnSlice>, OwnSlice>): Promise<void> | void;
127
127
  afterBoot?(ctx: ContextWriter<FullContext<Deps, OwnSlice>, OwnSlice>): Promise<void> | void;
128
128
  beforeShutdown?(ctx: ContextWriter<FullContext<Deps, OwnSlice>, OwnSlice>): Promise<void> | void;
129
129
  afterShutdown?(ctx: ContextWriter<FullContext<Deps, OwnSlice>, OwnSlice>): Promise<void> | void;
130
130
  }) => Module<FullContext<Deps, OwnSlice>, OwnSlice>;
131
+ /**
132
+ * Errors if U has any key not in T by mapping extra keys to never.
133
+ */
134
+ type Exact<T, U> = {
135
+ [K in keyof U]: K extends keyof T ? T[K] : never;
136
+ };
137
+ /**
138
+ * Produces a readable type error message instead of `never`.
139
+ * Msg appears directly in the IDE error output.
140
+ */
141
+ type TypeError<Msg extends string> = {
142
+ readonly __error__: Msg;
143
+ };
144
+ /**
145
+ * Errors if OwnSlice declares a key that already exists in DepCtx.
146
+ * Overlapping keys show a readable message instead of `never`.
147
+ */
148
+ type NoOverlap<DepCtx, OwnSlice> = {
149
+ [K in keyof OwnSlice]: K extends keyof DepCtx ? TypeError<`Key "${K & string}" is already owned by a dependency module`> : OwnSlice[K];
150
+ };
131
151
  /**
132
152
  * Options for start(). Flat intersection — no nesting required:
133
153
  * start([db], { log, afterBoot: async (ctx) => ... })
@@ -1 +1 @@
1
- {"fileNames":["../node_modules/typescript/lib/lib.es5.d.ts","../node_modules/typescript/lib/lib.es2015.d.ts","../node_modules/typescript/lib/lib.dom.d.ts","../node_modules/typescript/lib/lib.es2015.core.d.ts","../node_modules/typescript/lib/lib.es2015.collection.d.ts","../node_modules/typescript/lib/lib.es2015.generator.d.ts","../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../node_modules/typescript/lib/lib.es2015.promise.d.ts","../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../node_modules/typescript/lib/lib.decorators.d.ts","../node_modules/typescript/lib/lib.decorators.legacy.d.ts","../src/index.ts","../node_modules/@types/deep-eql/index.d.ts","../node_modules/assertion-error/index.d.ts","../node_modules/@types/chai/index.d.ts","../node_modules/@types/estree/index.d.ts"],"fileIdsList":[[16,17]],"fileInfos":[{"version":"c430d44666289dae81f30fa7b2edebf186ecc91a2d4c71266ea6ae76388792e1","affectsGlobalScope":true,"impliedFormat":1},{"version":"45b7ab580deca34ae9729e97c13cfd999df04416a79116c3bfb483804f85ded4","impliedFormat":1},{"version":"080941d9f9ff9307f7e27a83bcd888b7c8270716c39af943532438932ec1d0b9","affectsGlobalScope":true,"impliedFormat":1},{"version":"c57796738e7f83dbc4b8e65132f11a377649c00dd3eee333f672b8f0a6bea671","affectsGlobalScope":true,"impliedFormat":1},{"version":"dc2df20b1bcdc8c2d34af4926e2c3ab15ffe1160a63e58b7e09833f616efff44","affectsGlobalScope":true,"impliedFormat":1},{"version":"515d0b7b9bea2e31ea4ec968e9edd2c39d3eebf4a2d5cbd04e88639819ae3b71","affectsGlobalScope":true,"impliedFormat":1},{"version":"0559b1f683ac7505ae451f9a96ce4c3c92bdc71411651ca6ddb0e88baaaad6a3","affectsGlobalScope":true,"impliedFormat":1},{"version":"0dc1e7ceda9b8b9b455c3a2d67b0412feab00bd2f66656cd8850e8831b08b537","affectsGlobalScope":true,"impliedFormat":1},{"version":"ce691fb9e5c64efb9547083e4a34091bcbe5bdb41027e310ebba8f7d96a98671","affectsGlobalScope":true,"impliedFormat":1},{"version":"8d697a2a929a5fcb38b7a65594020fcef05ec1630804a33748829c5ff53640d0","affectsGlobalScope":true,"impliedFormat":1},{"version":"4ff2a353abf8a80ee399af572debb8faab2d33ad38c4b4474cff7f26e7653b8d","affectsGlobalScope":true,"impliedFormat":1},{"version":"fb0f136d372979348d59b3f5020b4cdb81b5504192b1cacff5d1fbba29378aa1","affectsGlobalScope":true,"impliedFormat":1},{"version":"8e7f8264d0fb4c5339605a15daadb037bf238c10b654bb3eee14208f860a32ea","affectsGlobalScope":true,"impliedFormat":1},{"version":"782dec38049b92d4e85c1585fbea5474a219c6984a35b004963b00beb1aab538","affectsGlobalScope":true,"impliedFormat":1},{"version":"3d2cddd1ad972ba2b9c02c9027c7724a50ef0a1d83210807e2f0dcdb1317e270","signature":"aadedff0c250acaa6d24078a4cd8ad38abdd873d20e3475a09d9835523243086"},{"version":"427fe2004642504828c1476d0af4270e6ad4db6de78c0b5da3e4c5ca95052a99","impliedFormat":1},{"version":"2eeffcee5c1661ddca53353929558037b8cf305ffb86a803512982f99bcab50d","impliedFormat":99},{"version":"9afb4cb864d297e4092a79ee2871b5d3143ea14153f62ef0bb04ede25f432030","affectsGlobalScope":true,"impliedFormat":99},{"version":"151ff381ef9ff8da2da9b9663ebf657eac35c4c9a19183420c05728f31a6761d","impliedFormat":1}],"root":[15],"options":{"declaration":true,"downlevelIteration":true,"emitDecoratorMetadata":true,"esModuleInterop":true,"experimentalDecorators":true,"module":5,"noImplicitAny":true,"outDir":"./","skipLibCheck":true,"strict":true,"strictPropertyInitialization":false,"target":2},"referencedMap":[[18,1]],"version":"5.9.3"}
1
+ {"fileNames":["../node_modules/typescript/lib/lib.es5.d.ts","../node_modules/typescript/lib/lib.es2015.d.ts","../node_modules/typescript/lib/lib.dom.d.ts","../node_modules/typescript/lib/lib.es2015.core.d.ts","../node_modules/typescript/lib/lib.es2015.collection.d.ts","../node_modules/typescript/lib/lib.es2015.generator.d.ts","../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../node_modules/typescript/lib/lib.es2015.promise.d.ts","../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../node_modules/typescript/lib/lib.decorators.d.ts","../node_modules/typescript/lib/lib.decorators.legacy.d.ts","../src/index.ts","../node_modules/@types/deep-eql/index.d.ts","../node_modules/assertion-error/index.d.ts","../node_modules/@types/chai/index.d.ts","../node_modules/@types/estree/index.d.ts"],"fileIdsList":[[16,17]],"fileInfos":[{"version":"c430d44666289dae81f30fa7b2edebf186ecc91a2d4c71266ea6ae76388792e1","affectsGlobalScope":true,"impliedFormat":1},{"version":"45b7ab580deca34ae9729e97c13cfd999df04416a79116c3bfb483804f85ded4","impliedFormat":1},{"version":"080941d9f9ff9307f7e27a83bcd888b7c8270716c39af943532438932ec1d0b9","affectsGlobalScope":true,"impliedFormat":1},{"version":"c57796738e7f83dbc4b8e65132f11a377649c00dd3eee333f672b8f0a6bea671","affectsGlobalScope":true,"impliedFormat":1},{"version":"dc2df20b1bcdc8c2d34af4926e2c3ab15ffe1160a63e58b7e09833f616efff44","affectsGlobalScope":true,"impliedFormat":1},{"version":"515d0b7b9bea2e31ea4ec968e9edd2c39d3eebf4a2d5cbd04e88639819ae3b71","affectsGlobalScope":true,"impliedFormat":1},{"version":"0559b1f683ac7505ae451f9a96ce4c3c92bdc71411651ca6ddb0e88baaaad6a3","affectsGlobalScope":true,"impliedFormat":1},{"version":"0dc1e7ceda9b8b9b455c3a2d67b0412feab00bd2f66656cd8850e8831b08b537","affectsGlobalScope":true,"impliedFormat":1},{"version":"ce691fb9e5c64efb9547083e4a34091bcbe5bdb41027e310ebba8f7d96a98671","affectsGlobalScope":true,"impliedFormat":1},{"version":"8d697a2a929a5fcb38b7a65594020fcef05ec1630804a33748829c5ff53640d0","affectsGlobalScope":true,"impliedFormat":1},{"version":"4ff2a353abf8a80ee399af572debb8faab2d33ad38c4b4474cff7f26e7653b8d","affectsGlobalScope":true,"impliedFormat":1},{"version":"fb0f136d372979348d59b3f5020b4cdb81b5504192b1cacff5d1fbba29378aa1","affectsGlobalScope":true,"impliedFormat":1},{"version":"8e7f8264d0fb4c5339605a15daadb037bf238c10b654bb3eee14208f860a32ea","affectsGlobalScope":true,"impliedFormat":1},{"version":"782dec38049b92d4e85c1585fbea5474a219c6984a35b004963b00beb1aab538","affectsGlobalScope":true,"impliedFormat":1},{"version":"3bd478e1d8330528504d334d98a052dd592414da970e9090aa7d1d949c01a61a","signature":"564bb0ef8a8688b87eda9682a18fdd1224e753852226a5827f822a6066804ac1"},{"version":"427fe2004642504828c1476d0af4270e6ad4db6de78c0b5da3e4c5ca95052a99","impliedFormat":1},{"version":"2eeffcee5c1661ddca53353929558037b8cf305ffb86a803512982f99bcab50d","impliedFormat":99},{"version":"9afb4cb864d297e4092a79ee2871b5d3143ea14153f62ef0bb04ede25f432030","affectsGlobalScope":true,"impliedFormat":99},{"version":"151ff381ef9ff8da2da9b9663ebf657eac35c4c9a19183420c05728f31a6761d","impliedFormat":1}],"root":[15],"options":{"declaration":true,"downlevelIteration":true,"emitDecoratorMetadata":true,"esModuleInterop":true,"experimentalDecorators":true,"module":5,"noImplicitAny":true,"outDir":"./","skipLibCheck":true,"strict":true,"strictPropertyInitialization":false,"target":2},"referencedMap":[[18,1]],"version":"5.9.3"}
package/lib/index.d.ts CHANGED
@@ -90,7 +90,7 @@ export interface Module<S extends object, Sl extends object = S> {
90
90
  name: string;
91
91
  description?: string;
92
92
  modules?: AnyModule[];
93
- boot?(ctx: ContextWriter<S, Sl>): Promise<void> | void;
93
+ boot?(ctx: ContextWriter<S, Sl>): Promise<void> | void | Sl | Promise<Sl>;
94
94
  shutdown?(ctx: ContextWriter<S, Sl>): Promise<void> | void;
95
95
  /** Called immediately before this module's own boot(). */
96
96
  beforeBoot?(ctx: ContextWriter<S, Sl>): Promise<void> | void;
@@ -117,17 +117,37 @@ export interface Module<S extends object, Sl extends object = S> {
117
117
  *
118
118
  * @param def module definition
119
119
  */
120
- export declare function defineModule<OwnSlice extends object = Record<never, never>>(): <const Deps extends readonly AnyModule[] = []>(def: {
120
+ export declare function defineModule<OwnSlice extends object = Record<never, never>>(): <const Deps extends readonly AnyModule[] = [], R extends OwnSlice = OwnSlice>(def: {
121
121
  name: string;
122
122
  description?: string;
123
123
  modules?: Deps;
124
- boot?(ctx: ContextWriter<FullContext<Deps, OwnSlice>, OwnSlice>): Promise<void> | void | OwnSlice | Promise<OwnSlice>;
124
+ boot?(ctx: ContextWriter<FullContext<Deps, OwnSlice>, NoOverlap<MergeSlices<Deps>, OwnSlice>>): Promise<void> | void | (R & Exact<NoOverlap<MergeSlices<Deps>, OwnSlice>, R>) | Promise<R & Exact<NoOverlap<MergeSlices<Deps>, OwnSlice>, R>>;
125
125
  shutdown?(ctx: ContextWriter<FullContext<Deps, OwnSlice>, OwnSlice>): Promise<void> | void;
126
126
  beforeBoot?(ctx: ContextWriter<FullContext<Deps, OwnSlice>, OwnSlice>): Promise<void> | void;
127
127
  afterBoot?(ctx: ContextWriter<FullContext<Deps, OwnSlice>, OwnSlice>): Promise<void> | void;
128
128
  beforeShutdown?(ctx: ContextWriter<FullContext<Deps, OwnSlice>, OwnSlice>): Promise<void> | void;
129
129
  afterShutdown?(ctx: ContextWriter<FullContext<Deps, OwnSlice>, OwnSlice>): Promise<void> | void;
130
130
  }) => Module<FullContext<Deps, OwnSlice>, OwnSlice>;
131
+ /**
132
+ * Errors if U has any key not in T by mapping extra keys to never.
133
+ */
134
+ type Exact<T, U> = {
135
+ [K in keyof U]: K extends keyof T ? T[K] : never;
136
+ };
137
+ /**
138
+ * Produces a readable type error message instead of `never`.
139
+ * Msg appears directly in the IDE error output.
140
+ */
141
+ type TypeError<Msg extends string> = {
142
+ readonly __error__: Msg;
143
+ };
144
+ /**
145
+ * Errors if OwnSlice declares a key that already exists in DepCtx.
146
+ * Overlapping keys show a readable message instead of `never`.
147
+ */
148
+ type NoOverlap<DepCtx, OwnSlice> = {
149
+ [K in keyof OwnSlice]: K extends keyof DepCtx ? TypeError<`Key "${K & string}" is already owned by a dependency module`> : OwnSlice[K];
150
+ };
131
151
  /**
132
152
  * Options for start(). Flat intersection — no nesting required:
133
153
  * start([db], { log, afterBoot: async (ctx) => ... })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsdkarc",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "TsdkArc is an elegant and fully type-safe module composable library",
5
5
  "repository": "tsdk-monorepo/tsdkarc",
6
6
  "bugs": "https://github.com/tsdk-monorepo/tsdkarc/issues",