nest-shared
TypeScript icon, indicating that this package has built-in type declarations

5.1.0 • Public • Published

codecov

Open in Gitpod

Node.js Package

Running Code Coverage

📝 Update Lock

Shared helpers, contracts and configuration utilities for NestJS/TypeScript projects.

Installation

Install with yarn or npm: yarn or npm:

# yarn
yarn add nest-shared
# npm
npm i nest-shared --save

Requirements

  • Node.js >= 20.18.1
  • TypeScript >= 5.9

Import the lib with ES Modules or CommonJS:

// es6
import shared from 'nest-shared';
// cjs
const shared = require('nest-shared');

Usage examples

Express usage

#!/usr/bin/env node
import { configService } from 'nest-shared';
import express, { Router } from 'express';

const app = express();
const router = Router();

router.get('/', (req, res) => {
  return res.send('Test');
});

app.use(router);
const port = configService.getPort();

app.listen(port, () => {
  process.stdout.write(`Server is running on port ${port}\n`);
});

Constants

#!/usr/bin/env node
import {
  NODE_PORT,
  CACHE_TTL,
  CACHE_TTL_50_SEC,
  API_HEADER_OPTIONS,
  WEBSOCKET_PORT,
  VALID_UUID_REGEX,
} from 'nest-shared';

console.log('NODE_PORT', NODE_PORT); // 4000
console.log('CACHE_TTL', CACHE_TTL); // 3600
console.log('CACHE_TTL_50_SEC', CACHE_TTL_50_SEC); // 50
console.log('API_HEADER_OPTIONS', API_HEADER_OPTIONS); // []
console.log('WEBSOCKET_PORT', WEBSOCKET_PORT); // 4001
console.log('VALID_UUID_REGEX', VALID_UUID_REGEX.test('28aebbd6-173b-4375-99eb-56dc04ec2bcb')); // true

Helpers

generateAPIKey
#!/usr/bin/env node
import { generateAPIKey } from 'nest-shared';

const api_key = generateAPIKey({
  str: 'Hello World',
  prefix: 'apk',
  digest: 'hex',
  size: 32,
});
console.log('api_key', api_key); // apk_f9cfa3c29500449828aebc910ce1d328
YourClass implements BufferBase
#!/usr/bin/env node
import { BufferBase } from 'nest-shared';
import { Buffer } from 'buffer';
// Types are available via a deep import (barrels exclude this to avoid bloat)
import type {
  EncodeDataType,
  DecodeStrType,
} from 'nest-shared/lib/shared/contract/types/buffer.type';

console.log(BufferBase.name); // BufferBase

class BufferBaseImpl implements BufferBase {
  encode(data: EncodeDataType): string {
    return Buffer.from(data).toString('base64');
  }
  decode(str: DecodeStrType): string {
    // example: decoding from base64 into utf-8
    return Buffer.from(str, 'base64').toString('utf-8');
  }
}

const bufferBaseImpl = new BufferBaseImpl();

const content = 'Hello World!';
const encoded = bufferBaseImpl.encode(content);
console.log(encoded); // SGVsbG8gV29ybGQh
console.log(bufferBaseImpl.decode(encoded)); // Hello World!
Base64 helper
import { encode, decode, validateUUID, randomUUID } from 'nest-shared';

const b64 = encode({ text: 'hello' });
console.log(b64); // aGVsbG8=
console.log(decode({ text: b64 })); // hello

console.log(validateUUID(randomUUID)); // true

File structure

├── src
│   ├── config
│   │   └── application.config.ts
│   ├── modules
│   │   └── file
│   │       ├── interfaces
│   │       ├── services
│   │       └── types
│   └── shared
│       ├── constants
│       ├── contract
│       │   ├── base
│       │   ├── entity
│       │   ├── interfaces
│       │   └── types
│       └── helpers
│           ├── class
│           ├── crypto
│           ├── fs
│           ├── http
│           ├── math
│           └── time

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check the issues page.

Show your support

Give a ⭐️ if this project helped you!

Or buy me a coffee 🙌🏾

📝 License

Copyright © 2023–2025 Hebert F Barros.
This project is MIT licensed.

Readme

Keywords

Package Sidebar

Install

npm i nest-shared

Weekly Downloads

164

Version

5.1.0

License

MIT

Unpacked Size

55.9 kB

Total Files

99

Last publish

Collaborators

  • tecnobert