@jsnw/srv-utils
v1.1.4
Published
Server-side utilities for Node.js/TypeScript: tsconfig paths, Nest helpers, and config loading.
Downloads
586
Readme
@jsnw/srv-utils
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.
Install
npm install @jsnw/srv-utilsOptional dependencies:
module-aliasforuseTsconfigPaths@nestjs/coreand@nestjs/commonforwithNest
Exports
useTsconfigPaths(fromPath, tsconfigPath)
Loads compilerOptions.paths from a tsconfig file and registers them via module-alias.
import {useTsconfigPaths} from '@jsnw/srv-utils';
useTsconfigPaths(__dirname, './tsconfig.json');getRootPackageDirnameSync()
Finds the highest directory (from the current module) that still has a readable package.json.
import {getRootPackageDirnameSync} from '@jsnw/srv-utils';
const root = getRootPackageDirnameSync();ConfigLoader.loadConfig(path, schema, addProps?)
Loads a YAML file and validates it with a Zod schema. It also injects isDev based on APP_CONTEXT, APPLICATION_CONTEXT, or NODE_ENV.
Path resolution prefixes:
%pkgroot/- Resolves to the project root directory%env:VAR_NAME/- Resolves based on environment variable value
import {ConfigLoader, configSchemas} from '@jsnw/srv-utils';
// Using project root
const config = ConfigLoader.loadConfig(
'%pkgroot/config.yml',
configSchemas.mongodbConnection
);
// Using environment variable
const config = ConfigLoader.loadConfig(
'%env:CONFIG_PATH/config.yml',
configSchemas.mongodbConnection
);configSchemas
Predefined Zod schemas:
timeString(parses strings like5s,2 mininto milliseconds)connection(host/port/user/pass)mongodbConnection(extends connection, adds db/authDb/connectTimeout + dsn)mysqlConnection(extends connection, adds dbname)redisConnection(extends connection, adds db/keyPrefix)
withNest(moduleCls, fn)
Creates a NestJS application context, runs your function, then closes the app unless you call preventClosing.
import {withNest} from '@jsnw/srv-utils';
import {AppModule} from './app.module';
await withNest(AppModule, async (app, preventClosing) => {
// use app.get(...) etc.
// preventClosing(); // if you want to keep it open
});Types
ResolvedConfig is exported from @jsnw/srv-utils for typed config results.
