@jsnw/srv-utils 1.0.0 → 1.0.3
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 +70 -0
- package/dist/types/useTsconfigPaths.d.ts +7 -3
- package/dist/useTsconfigPaths.js +9 -5
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# @jsnw/srv-utils
|
|
2
|
+
|
|
3
|
+
Small server-side utility bundle for Node.js/TypeScript projects. It includes helpers for tsconfig path aliases, NestJS app bootstrapping, and YAML config loading with Zod validation plus a few predefined schemas.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @jsnw/srv-utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Optional dependencies:
|
|
12
|
+
- `module-alias` for `useTsconfigPaths`
|
|
13
|
+
- `@nestjs/core` and `@nestjs/common` for `withNest`
|
|
14
|
+
|
|
15
|
+
## Exports
|
|
16
|
+
|
|
17
|
+
### useTsconfigPaths(fromPath, tsconfigPath)
|
|
18
|
+
Loads `compilerOptions.paths` from a tsconfig file and registers them via `module-alias`.
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import {useTsconfigPaths} from '@jsnw/srv-utils';
|
|
22
|
+
|
|
23
|
+
useTsconfigPaths(__dirname, './tsconfig.json');
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### getRootPackageDirnameSync()
|
|
27
|
+
Finds the highest directory (from the current module) that still has a readable `package.json`.
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import {getRootPackageDirnameSync} from '@jsnw/srv-utils';
|
|
31
|
+
|
|
32
|
+
const root = getRootPackageDirnameSync();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### ConfigLoader.loadAndValidate(path, schema, addProps?)
|
|
36
|
+
Loads a YAML file and validates it with a Zod schema. It also injects `isDev` based on `APP_CONTEXT`, `APPLICATION_CONTEXT`, or `NODE_ENV`. You can use `%pkgroot/` prefix in the path for automatic project-root resolution.
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import {ConfigLoader, configSchemas} from '@jsnw/srv-utils';
|
|
40
|
+
|
|
41
|
+
const config = ConfigLoader.loadAndValidate(
|
|
42
|
+
'%pkgroot/config.yml',
|
|
43
|
+
configSchemas.mongodbConnection
|
|
44
|
+
);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### configSchemas
|
|
48
|
+
Predefined Zod schemas:
|
|
49
|
+
- `timeString` (parses strings like `5s`, `2 min` into milliseconds)
|
|
50
|
+
- `connection` (host/port/user/pass)
|
|
51
|
+
- `mongodbConnection` (extends connection, adds db/authDb/connectTimeout + dsn)
|
|
52
|
+
- `mysqlConnection` (extends connection, adds dbname)
|
|
53
|
+
- `redisConnection` (extends connection, adds db/keyPrefix)
|
|
54
|
+
|
|
55
|
+
### withNest(moduleCls, fn)
|
|
56
|
+
Creates a NestJS application context, runs your function, then closes the app unless you call `preventClosing`.
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import {withNest} from '@jsnw/srv-utils';
|
|
60
|
+
import {AppModule} from './app.module';
|
|
61
|
+
|
|
62
|
+
await withNest(AppModule, async (app, preventClosing) => {
|
|
63
|
+
// use app.get(...) etc.
|
|
64
|
+
// preventClosing(); // if you want to keep it open
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Types
|
|
69
|
+
|
|
70
|
+
`ResolvedConfig` is exported from `@jsnw/srv-utils` for typed config results.
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
type UseTsconfigPathsOptions = {
|
|
2
|
+
basePath?: string;
|
|
3
|
+
tsconfigPath: string;
|
|
4
|
+
};
|
|
1
5
|
/**
|
|
2
|
-
* @param {
|
|
3
|
-
* @param {string} tsconfigPath
|
|
6
|
+
* @param {UseTsconfigPathsOptions} options
|
|
4
7
|
*/
|
|
5
|
-
export declare function useTsconfigPaths(
|
|
8
|
+
export declare function useTsconfigPaths({ basePath, tsconfigPath }: UseTsconfigPathsOptions): void;
|
|
9
|
+
export {};
|
package/dist/useTsconfigPaths.js
CHANGED
|
@@ -3,10 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.useTsconfigPaths = useTsconfigPaths;
|
|
4
4
|
const node_path_1 = require("node:path");
|
|
5
5
|
/**
|
|
6
|
-
* @param {
|
|
7
|
-
* @param {string} tsconfigPath
|
|
6
|
+
* @param {UseTsconfigPathsOptions} options
|
|
8
7
|
*/
|
|
9
|
-
function useTsconfigPaths(
|
|
8
|
+
function useTsconfigPaths({ basePath, tsconfigPath }) {
|
|
10
9
|
let moduleAlias = null;
|
|
11
10
|
try {
|
|
12
11
|
require('module-alias/register');
|
|
@@ -15,12 +14,17 @@ function useTsconfigPaths(fromPath, tsconfigPath) {
|
|
|
15
14
|
catch (e) {
|
|
16
15
|
throw new Error('module-alias package should be installed to use the function');
|
|
17
16
|
}
|
|
18
|
-
const tsconfig = require(tsconfigPath), tsconfigDirPathname = (0, node_path_1.dirname)(tsconfigPath), paths = {};
|
|
17
|
+
const tsconfig = require((0, node_path_1.resolve)(tsconfigPath)), tsconfigDirPathname = (0, node_path_1.dirname)(tsconfigPath), paths = {};
|
|
18
|
+
if (!basePath && !tsconfig?.compilerOptions?.baseUrl)
|
|
19
|
+
throw new TypeError('Either basePath option should be provided or tsconfig.json should contain compilerOptions.baseUrl property');
|
|
20
|
+
const baseUrl = tsconfig?.compilerOptions?.baseUrl
|
|
21
|
+
? (0, node_path_1.resolve)(tsconfig.compilerOptions.baseUrl)
|
|
22
|
+
: (0, node_path_1.resolve)(basePath);
|
|
19
23
|
Object.entries((tsconfig?.compilerOptions?.paths ?? {}))
|
|
20
24
|
.forEach(([moduleName, pathArray]) => {
|
|
21
25
|
if (pathArray.length === 0)
|
|
22
26
|
return;
|
|
23
|
-
paths[moduleName] = (0, node_path_1.resolve)(tsconfigDirPathname,
|
|
27
|
+
paths[moduleName] = (0, node_path_1.resolve)(tsconfigDirPathname, baseUrl, pathArray[0]);
|
|
24
28
|
});
|
|
25
29
|
moduleAlias.addAliases(paths);
|
|
26
30
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsnw/srv-utils",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "Server-side utilities for Node.js/TypeScript: tsconfig paths, Nest helpers, and config loading.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/types/index.d.ts",
|
|
7
7
|
"exports": {
|