@twin.org/node-core 0.0.1-next.3 → 0.0.1-next.6

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
@@ -1,32 +1,20 @@
1
1
  # TWIN Node Core
2
2
 
3
- Core of a TWIN node for running the TWIN engine as a REST server.
3
+ TWIN Node Core for serving APIs using the specified configuration
4
4
 
5
- ## Building and running the application
6
-
7
- To install the dependencies, perform a full build and start the server.
8
-
9
- ```shell
10
- npm install
11
- npm run dist
12
- npm start
13
- ```
14
-
15
- ## Development mode
16
-
17
- Once you have performed a full build you can run the server in development mode, this will watch the TypeScript code, rebuild if there are any changes, and relaunch the server.
5
+ ## Installation
18
6
 
19
7
  ```shell
20
- npm run dev
8
+ npm install @twin.org/node-core
21
9
  ```
22
10
 
23
- ## Configuration
11
+ ## Examples
24
12
 
25
- There are various options you can set through configuration, these can be found in [docs/configuration.md](docs/configuration.md)
13
+ Usage of the APIs is shown in the examples [docs/examples.md](docs/examples.md)
26
14
 
27
- ## Deployment
15
+ ## Reference
28
16
 
29
- Examples of how to deploy the app can be found in [docs/deployment.md](docs/deployment.md)
17
+ Detailed reference documentation for the API can be found in [docs/reference/index.md](docs/reference/index.md)
30
18
 
31
19
  ## Changelog
32
20
 
@@ -491,25 +491,30 @@ async function bootstrapAuth(engineCore, context, envVars, features) {
491
491
  /**
492
492
  * Start the engine server.
493
493
  * @param serverInfo The server information.
494
+ * @param envVarsPrefix The prefix for the environment variables.
494
495
  * @param envVars The environment variables.
495
496
  * @param openApiSpecFile Path to the OpenAPI spec file.
496
497
  * @param stateStorage The state storage.
497
- * @param customConfig Extends the engine configuration with any additional custom configuration.
498
+ * @param extendConfig Extends the engine configuration with any additional custom configuration.
499
+ * @param extendEngine Extends the engine with any additional options.
500
+ * @param extendEngineServer Extends the engine server with any additional options.
498
501
  * @returns The engine server.
499
502
  */
500
- async function start(serverInfo, envVars, openApiSpecFile, stateStorage, customConfig) {
503
+ async function start(serverInfo, envVarsPrefix, envVars, openApiSpecFile, stateStorage, extendConfig, extendEngine, extendEngineServer) {
501
504
  envVars.storageFileRoot ??= "";
502
505
  if ((envVars.entityStorageConnectorType === engineTypes.EntityStorageConnectorType.File ||
503
506
  envVars.blobStorageConnectorType === engineTypes.BlobStorageConnectorType.File ||
504
507
  core.Is.empty(stateStorage)) &&
505
508
  !core.Is.stringValue(envVars.storageFileRoot)) {
506
- throw new core.GeneralError("node", "storageFileRootNotSet");
509
+ throw new core.GeneralError("node", "storageFileRootNotSet", {
510
+ storageFileRoot: `${envVarsPrefix}_STORAGE_FILE_ROOT`
511
+ });
507
512
  }
508
513
  // Build the engine configuration from the environment variables.
509
514
  const engineConfig = engine.buildEngineConfiguration(envVars);
510
515
  // Extend the engine configuration with a custom type.
511
- if (core.Is.function(customConfig)) {
512
- await customConfig(engineConfig);
516
+ if (core.Is.function(extendConfig)) {
517
+ await extendConfig(engineConfig);
513
518
  }
514
519
  // Build the server configuration from the environment variables.
515
520
  const serverConfig = engineServer.buildEngineServerConfiguration(envVars, engineConfig, serverInfo, openApiSpecFile);
@@ -519,11 +524,19 @@ async function start(serverInfo, envVars, openApiSpecFile, stateStorage, customC
519
524
  stateStorage: stateStorage ?? new engineCore.FileStateStorage(envVars.stateFilename ?? ""),
520
525
  customBootstrap: async (core, engineContext) => bootstrap(core, engineContext, envVars)
521
526
  });
527
+ // Extend the engine.
528
+ if (core.Is.function(extendEngine)) {
529
+ await extendEngine(engine$1);
530
+ }
531
+ // Construct the server with the engine.
532
+ const server = new engineServer.EngineServer({ engineCore: engine$1 });
533
+ // Extend the engine server.
534
+ if (core.Is.function(extendEngineServer)) {
535
+ await extendEngineServer(server);
536
+ }
522
537
  // Need to register the engine with the factory so that background tasks
523
538
  // can clone it to spawn new instances.
524
539
  engineModels.EngineCoreFactory.register("engine", () => engine$1);
525
- // Construct the server with the engine.
526
- const server = new engineServer.EngineServer({ engineCore: engine$1 });
527
540
  // Start the server, which also starts the engine.
528
541
  const canContinue = await server.start();
529
542
  if (canContinue) {
@@ -546,7 +559,7 @@ async function run(options) {
546
559
  try {
547
560
  const serverInfo = {
548
561
  name: options?.serverName ?? "TWIN Node Server",
549
- version: options?.serverVersion ?? "0.0.1-next.3" // x-release-please-version
562
+ version: options?.serverVersion ?? "0.0.1-next.6" // x-release-please-version
550
563
  };
551
564
  console.log(`\u001B[4m🌩️ ${serverInfo.name} v${serverInfo.version}\u001B[24m\n`);
552
565
  const executionDirectory = options?.executionDirectory ?? getExecutionDirectory();
@@ -572,8 +585,11 @@ async function run(options) {
572
585
  const envPrefix = options?.envPrefix ?? "TWIN_NODE_";
573
586
  console.info("Environment Prefix:", envPrefix);
574
587
  const envVars = core.EnvHelper.envToJson(process.env, envPrefix);
588
+ if (Object.keys(envVars).length === 0) {
589
+ throw new core.GeneralError("node", "noEnvVars", { prefix: envPrefix });
590
+ }
575
591
  console.info();
576
- const startResult = await start(serverInfo, envVars, options?.openApiSpecFile, options.stateStorage, options.customConfig);
592
+ const startResult = await start(serverInfo, envPrefix, envVars, options?.openApiSpecFile, options.stateStorage, options.extendConfig, options.extendEngine, options.extendEngineServer);
577
593
  if (!core.Is.empty(startResult)) {
578
594
  for (const signal of ["SIGHUP", "SIGINT", "SIGTERM"]) {
579
595
  process.on(signal, async () => {
@@ -470,25 +470,30 @@ async function bootstrapAuth(engineCore, context, envVars, features) {
470
470
  /**
471
471
  * Start the engine server.
472
472
  * @param serverInfo The server information.
473
+ * @param envVarsPrefix The prefix for the environment variables.
473
474
  * @param envVars The environment variables.
474
475
  * @param openApiSpecFile Path to the OpenAPI spec file.
475
476
  * @param stateStorage The state storage.
476
- * @param customConfig Extends the engine configuration with any additional custom configuration.
477
+ * @param extendConfig Extends the engine configuration with any additional custom configuration.
478
+ * @param extendEngine Extends the engine with any additional options.
479
+ * @param extendEngineServer Extends the engine server with any additional options.
477
480
  * @returns The engine server.
478
481
  */
479
- async function start(serverInfo, envVars, openApiSpecFile, stateStorage, customConfig) {
482
+ async function start(serverInfo, envVarsPrefix, envVars, openApiSpecFile, stateStorage, extendConfig, extendEngine, extendEngineServer) {
480
483
  envVars.storageFileRoot ??= "";
481
484
  if ((envVars.entityStorageConnectorType === EntityStorageConnectorType.File ||
482
485
  envVars.blobStorageConnectorType === BlobStorageConnectorType.File ||
483
486
  Is.empty(stateStorage)) &&
484
487
  !Is.stringValue(envVars.storageFileRoot)) {
485
- throw new GeneralError("node", "storageFileRootNotSet");
488
+ throw new GeneralError("node", "storageFileRootNotSet", {
489
+ storageFileRoot: `${envVarsPrefix}_STORAGE_FILE_ROOT`
490
+ });
486
491
  }
487
492
  // Build the engine configuration from the environment variables.
488
493
  const engineConfig = buildEngineConfiguration(envVars);
489
494
  // Extend the engine configuration with a custom type.
490
- if (Is.function(customConfig)) {
491
- await customConfig(engineConfig);
495
+ if (Is.function(extendConfig)) {
496
+ await extendConfig(engineConfig);
492
497
  }
493
498
  // Build the server configuration from the environment variables.
494
499
  const serverConfig = buildEngineServerConfiguration(envVars, engineConfig, serverInfo, openApiSpecFile);
@@ -498,11 +503,19 @@ async function start(serverInfo, envVars, openApiSpecFile, stateStorage, customC
498
503
  stateStorage: stateStorage ?? new FileStateStorage(envVars.stateFilename ?? ""),
499
504
  customBootstrap: async (core, engineContext) => bootstrap(core, engineContext, envVars)
500
505
  });
506
+ // Extend the engine.
507
+ if (Is.function(extendEngine)) {
508
+ await extendEngine(engine);
509
+ }
510
+ // Construct the server with the engine.
511
+ const server = new EngineServer({ engineCore: engine });
512
+ // Extend the engine server.
513
+ if (Is.function(extendEngineServer)) {
514
+ await extendEngineServer(server);
515
+ }
501
516
  // Need to register the engine with the factory so that background tasks
502
517
  // can clone it to spawn new instances.
503
518
  EngineCoreFactory.register("engine", () => engine);
504
- // Construct the server with the engine.
505
- const server = new EngineServer({ engineCore: engine });
506
519
  // Start the server, which also starts the engine.
507
520
  const canContinue = await server.start();
508
521
  if (canContinue) {
@@ -525,7 +538,7 @@ async function run(options) {
525
538
  try {
526
539
  const serverInfo = {
527
540
  name: options?.serverName ?? "TWIN Node Server",
528
- version: options?.serverVersion ?? "0.0.1-next.3" // x-release-please-version
541
+ version: options?.serverVersion ?? "0.0.1-next.6" // x-release-please-version
529
542
  };
530
543
  console.log(`\u001B[4m🌩️ ${serverInfo.name} v${serverInfo.version}\u001B[24m\n`);
531
544
  const executionDirectory = options?.executionDirectory ?? getExecutionDirectory();
@@ -551,8 +564,11 @@ async function run(options) {
551
564
  const envPrefix = options?.envPrefix ?? "TWIN_NODE_";
552
565
  console.info("Environment Prefix:", envPrefix);
553
566
  const envVars = EnvHelper.envToJson(process.env, envPrefix);
567
+ if (Object.keys(envVars).length === 0) {
568
+ throw new GeneralError("node", "noEnvVars", { prefix: envPrefix });
569
+ }
554
570
  console.info();
555
- const startResult = await start(serverInfo, envVars, options?.openApiSpecFile, options.stateStorage, options.customConfig);
571
+ const startResult = await start(serverInfo, envPrefix, envVars, options?.openApiSpecFile, options.stateStorage, options.extendConfig, options.extendEngine, options.extendEngineServer);
556
572
  if (!Is.empty(startResult)) {
557
573
  for (const signal of ["SIGHUP", "SIGINT", "SIGTERM"]) {
558
574
  process.on(signal, async () => {
@@ -1,4 +1,4 @@
1
- import type { IEngineStateStorage } from "@twin.org/engine-models";
1
+ import type { IEngineCore, IEngineServer, IEngineStateStorage } from "@twin.org/engine-models";
2
2
  import type { IEngineConfig } from "@twin.org/engine-types";
3
3
  /**
4
4
  * The options when running the node.
@@ -36,7 +36,15 @@ export interface IRunOptions {
36
36
  /**
37
37
  * Method to extend the engine configuration with any additional custom configuration.
38
38
  */
39
- customConfig?: (config: IEngineConfig) => Promise<void>;
39
+ extendConfig?: (config: IEngineConfig) => Promise<void>;
40
+ /**
41
+ * Method to extend the engine with any additional options.
42
+ */
43
+ extendEngine?: (engine: IEngineCore) => Promise<void>;
44
+ /**
45
+ * Method to extend the engine server with any additional options.
46
+ */
47
+ extendEngineServer?: (engineServer: IEngineServer) => Promise<void>;
40
48
  /**
41
49
  * The state storage to use for the engine.
42
50
  * If not provided, a default file-based state storage will be used.
@@ -1,6 +1,6 @@
1
1
  import type { IServerInfo } from "@twin.org/api-models";
2
2
  import { Engine } from "@twin.org/engine";
3
- import { type IEngineStateStorage } from "@twin.org/engine-models";
3
+ import { type IEngineCore, type IEngineServer, type IEngineStateStorage } from "@twin.org/engine-models";
4
4
  import { EngineServer } from "@twin.org/engine-server";
5
5
  import type { IEngineServerConfig } from "@twin.org/engine-server-types";
6
6
  import { type IEngineConfig } from "@twin.org/engine-types";
@@ -9,13 +9,16 @@ import type { INodeVariables } from "./models/INodeVariables";
9
9
  /**
10
10
  * Start the engine server.
11
11
  * @param serverInfo The server information.
12
+ * @param envVarsPrefix The prefix for the environment variables.
12
13
  * @param envVars The environment variables.
13
14
  * @param openApiSpecFile Path to the OpenAPI spec file.
14
15
  * @param stateStorage The state storage.
15
- * @param customConfig Extends the engine configuration with any additional custom configuration.
16
+ * @param extendConfig Extends the engine configuration with any additional custom configuration.
17
+ * @param extendEngine Extends the engine with any additional options.
18
+ * @param extendEngineServer Extends the engine server with any additional options.
16
19
  * @returns The engine server.
17
20
  */
18
- export declare function start(serverInfo: IServerInfo, envVars: INodeVariables, openApiSpecFile?: string, stateStorage?: IEngineStateStorage, customConfig?: (config: IEngineConfig) => Promise<void>): Promise<{
21
+ export declare function start(serverInfo: IServerInfo, envVarsPrefix: string, envVars: INodeVariables, openApiSpecFile?: string, stateStorage?: IEngineStateStorage, extendConfig?: (config: IEngineConfig) => Promise<void>, extendEngine?: (engine: IEngineCore) => Promise<void>, extendEngineServer?: (engineServer: IEngineServer) => Promise<void>): Promise<{
19
22
  engine: Engine<IEngineServerConfig, INodeState>;
20
23
  server: EngineServer;
21
24
  } | undefined>;
package/docs/changelog.md CHANGED
@@ -1,12 +1,45 @@
1
- # Changelog
1
+ # @twin.org/node-core - Changelog
2
2
 
3
- ## [0.0.1-next.3](https://github.com/twinfoundation/node/compare/node-core-v0.0.1-next.2...node-core-v0.0.1-next.3) (2025-05-27)
3
+ ## [0.0.1-next.6](https://github.com/twinfoundation/node/compare/node-core-v0.0.1-next.5...node-core-v0.0.1-next.6) (2025-06-12)
4
4
 
5
5
 
6
6
  ### Features
7
7
 
8
+ * add extend engine and server methods ([ec09c7e](https://github.com/twinfoundation/node/commit/ec09c7eb882d9f5797f2fd372e96cad1a3716f59))
9
+ * add extend engine and server methods ([0136a6f](https://github.com/twinfoundation/node/commit/0136a6f3f4e1a82b1427ee9618b8a17c79bc7fda))
8
10
  * additional run options ([c35e5bb](https://github.com/twinfoundation/node/commit/c35e5bbb8a80fe6a36628d41f64585b3723d9ad7))
11
+ * improve error reporting ([fcd39a1](https://github.com/twinfoundation/node/commit/fcd39a18da2a6ce33965a99ca5f2f36f4aba712f))
12
+ * initial commit ([522f1e5](https://github.com/twinfoundation/node/commit/522f1e515348f9b1dd1eeb3170b1249e2b0b5371))
9
13
  * node app use JavaScript ([14fe08c](https://github.com/twinfoundation/node/commit/14fe08cb760dd885a5dac9056a4d5dbc3d61df64))
14
+ * update dependencies ([9d25f16](https://github.com/twinfoundation/node/commit/9d25f16f1d554cd38f3bec28fdf7f8fff892ceaf))
15
+
16
+
17
+ ### Bug Fixes
18
+
19
+ * broken docs ([61479fd](https://github.com/twinfoundation/node/commit/61479fd618f766d22c5aafec5277e1a89e22b453))
20
+
21
+ ## [0.0.1-next.5](https://github.com/twinfoundation/node/compare/node-core-v0.0.1-next.4...node-core-v0.0.1-next.5) (2025-06-12)
22
+
23
+ ### Features
24
+
25
+ - add extend engine and server methods ([0136a6f](https://github.com/twinfoundation/node/commit/0136a6f3f4e1a82b1427ee9618b8a17c79bc7fda))
26
+
27
+ ## [0.0.1-next.4](https://github.com/twinfoundation/node/compare/node-core-v0.0.1-next.3...node-core-v0.0.1-next.4) (2025-05-27)
28
+
29
+ ### Features
30
+
31
+ - improve error reporting ([fcd39a1](https://github.com/twinfoundation/node/commit/fcd39a18da2a6ce33965a99ca5f2f36f4aba712f))
32
+
33
+ ### Bug Fixes
34
+
35
+ - broken docs ([61479fd](https://github.com/twinfoundation/node/commit/61479fd618f766d22c5aafec5277e1a89e22b453))
36
+
37
+ ## [0.0.1-next.3](https://github.com/twinfoundation/node/compare/node-core-v0.0.1-next.2...node-core-v0.0.1-next.3) (2025-05-27)
38
+
39
+ ### Features
40
+
41
+ - additional run options ([c35e5bb](https://github.com/twinfoundation/node/commit/c35e5bbb8a80fe6a36628d41f64585b3723d9ad7))
42
+ - node app use JavaScript ([14fe08c](https://github.com/twinfoundation/node/commit/14fe08cb760dd885a5dac9056a4d5dbc3d61df64))
10
43
 
11
44
  ## [0.0.1-next.2](https://github.com/twinfoundation/node/compare/node-core-v0.0.1-next.1...node-core-v0.0.1-next.2) (2025-05-27)
12
45
 
@@ -0,0 +1 @@
1
+ # @twin.org/node-core - Examples
@@ -1,6 +1,6 @@
1
1
  # Function: start()
2
2
 
3
- > **start**(`serverInfo`, `envVars`, `openApiSpecFile?`, `stateStorage?`, `customConfig?`): `Promise`\<`undefined` \| \{ `engine`: `Engine`\<`IEngineServerConfig`, [`INodeState`](../interfaces/INodeState.md)\>; `server`: `EngineServer`; \}\>
3
+ > **start**(`serverInfo`, `envVarsPrefix`, `envVars`, `openApiSpecFile?`, `stateStorage?`, `extendConfig?`, `extendEngine?`, `extendEngineServer?`): `Promise`\<`undefined` \| \{ `engine`: `Engine`\<`IEngineServerConfig`, [`INodeState`](../interfaces/INodeState.md)\>; `server`: `EngineServer`; \}\>
4
4
 
5
5
  Start the engine server.
6
6
 
@@ -12,6 +12,12 @@ Start the engine server.
12
12
 
13
13
  The server information.
14
14
 
15
+ ### envVarsPrefix
16
+
17
+ `string`
18
+
19
+ The prefix for the environment variables.
20
+
15
21
  ### envVars
16
22
 
17
23
  [`INodeVariables`](../interfaces/INodeVariables.md)
@@ -30,12 +36,24 @@ Path to the OpenAPI spec file.
30
36
 
31
37
  The state storage.
32
38
 
33
- ### customConfig?
39
+ ### extendConfig?
34
40
 
35
41
  (`config`) => `Promise`\<`void`\>
36
42
 
37
43
  Extends the engine configuration with any additional custom configuration.
38
44
 
45
+ ### extendEngine?
46
+
47
+ (`engine`) => `Promise`\<`void`\>
48
+
49
+ Extends the engine with any additional options.
50
+
51
+ ### extendEngineServer?
52
+
53
+ (`engineServer`) => `Promise`\<`void`\>
54
+
55
+ Extends the engine server with any additional options.
56
+
39
57
  ## Returns
40
58
 
41
59
  `Promise`\<`undefined` \| \{ `engine`: `Engine`\<`IEngineServerConfig`, [`INodeState`](../interfaces/INodeState.md)\>; `server`: `EngineServer`; \}\>
@@ -66,9 +66,9 @@ The path to the OpenAPI spec file, defaults to docs/open-api/spec.json.
66
66
 
67
67
  ***
68
68
 
69
- ### customConfig()?
69
+ ### extendConfig()?
70
70
 
71
- > `optional` **customConfig**: (`config`) => `Promise`\<`void`\>
71
+ > `optional` **extendConfig**: (`config`) => `Promise`\<`void`\>
72
72
 
73
73
  Method to extend the engine configuration with any additional custom configuration.
74
74
 
@@ -84,6 +84,42 @@ Method to extend the engine configuration with any additional custom configurati
84
84
 
85
85
  ***
86
86
 
87
+ ### extendEngine()?
88
+
89
+ > `optional` **extendEngine**: (`engine`) => `Promise`\<`void`\>
90
+
91
+ Method to extend the engine with any additional options.
92
+
93
+ #### Parameters
94
+
95
+ ##### engine
96
+
97
+ `IEngineCore`
98
+
99
+ #### Returns
100
+
101
+ `Promise`\<`void`\>
102
+
103
+ ***
104
+
105
+ ### extendEngineServer()?
106
+
107
+ > `optional` **extendEngineServer**: (`engineServer`) => `Promise`\<`void`\>
108
+
109
+ Method to extend the engine server with any additional options.
110
+
111
+ #### Parameters
112
+
113
+ ##### engineServer
114
+
115
+ `IEngineServer`
116
+
117
+ #### Returns
118
+
119
+ `Promise`\<`void`\>
120
+
121
+ ***
122
+
87
123
  ### stateStorage?
88
124
 
89
125
  > `optional` **stateStorage**: `IEngineStateStorage`\<`IEngineState`\>
package/locales/en.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "error": {
3
3
  "node": {
4
- "noEnvVars": "There are no environment variables starting TWIN_NODE_ the server will not start without them, please set them in the shell or create a .env file",
5
- "storageFileRootNotSet": "TWIN_NODE_STORAGE_FILE_ROOT is not set, the server will not start without it, please set it in the shell or create a .env file"
4
+ "noEnvVars": "There are no environment variables starting \"{prefix}\" the server will not start without them, please set them in the shell or create a .env file",
5
+ "storageFileRootNotSet": "{storageFileRoot} is not set, the server will not start without it, please set it in the shell or create a .env file"
6
6
  }
7
7
  },
8
8
  "node": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/node-core",
3
- "version": "0.0.1-next.3",
3
+ "version": "0.0.1-next.6",
4
4
  "description": "TWIN Node Core for serving APIs using the specified configuration",
5
5
  "repository": {
6
6
  "type": "git",