Skip to content

hebertcisco/nest-shared

Repository files navigation

codecov

Open in Gitpod

Node.js Package

Running Code Coverage

πŸ“ Update Lock

Installation

Install with yarn or npm: yarn or npm:

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

Import the lib with es6 or cjs:

// 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';
import type { EncodeDataType, DecodeStrType } from 'nest-shared/lib/shared/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 {
    return Buffer.from(str, 'utf-8').toString('utf-8');
  }
}

const bufferBaseImpl = new BufferBaseImpl();

const content = 'Hello World!';

console.log(bufferBaseImpl.encode(content)); // SGVsbG8gV29ybGQh
console.log(bufferBaseImpl.decode(content)); // Hello World!

File structure

β”œβ”€β”€ src
β”‚   β”œβ”€β”€ common
β”‚   β”‚   β”œβ”€β”€ base
β”‚   β”‚   β”‚   β”œβ”€β”€ buffer-base.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ class-base.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ crud-base.ts
β”‚   β”‚   β”œβ”€β”€ constants
β”‚   β”‚   β”‚   β”œβ”€β”€ global.constants.ts
β”‚   β”‚   β”‚   └── regex.constants.ts
β”‚   β”‚   β”œβ”€β”€ entity
β”‚   β”‚   β”‚   β”œβ”€β”€ global-common.entity.ts
β”‚   β”‚   β”‚   └── user.common.entity.ts
β”‚   β”‚   └── interfaces
β”‚   β”‚       β”œβ”€β”€ app-service.interface.ts
β”‚   β”‚       β”œβ”€β”€ http.responses.interface.ts
β”‚   β”‚       └── type-orm.interface.ts
β”‚   β”œβ”€β”€ config
β”‚   β”‚   β”œβ”€β”€ application.config.ts
β”‚   β”œβ”€β”€ modules
β”‚   β”‚   β”œβ”€β”€ file
β”‚   β”‚   β”‚   β”œβ”€β”€ interfaces
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ file.interface.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ services
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ file.service.ts
β”‚   β”‚   β”‚   └── types
β”‚   β”‚   β”‚       └── file.type.ts
β”‚   β”œβ”€β”€ shared
β”‚   β”‚   β”œβ”€β”€ helpers
β”‚   β”‚   β”‚   β”œβ”€β”€ class
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ getKeyFromClass.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ crypto
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ Base64.ts
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ generateAPIKey.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ fs
β”‚   β”‚   β”‚   β”‚   └── parseFile.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ http
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ axiosErrorHandler.ts
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ handleWithAxiosResponse.ts
β”‚   β”‚   β”‚   β”‚   └── parseQueryParams.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ math
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ RandomNumber.ts
β”‚   β”‚   β”‚   β”‚   └── sum.ts
β”‚   β”‚   β”‚   └── time
β”‚   β”‚   β”‚       β”œβ”€β”€ date-handle.ts
β”‚   β”‚   └── types
β”‚   β”‚       β”œβ”€β”€ buffer.type.ts
β”‚   β”‚       β”œβ”€β”€ class.type.ts
β”‚   β”‚       β”œβ”€β”€ crud-base.type.ts
β”‚   └── @types
β”‚       └── unique-slug

🀝 Contributing

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

Show your support

Give a ⭐️ if this project helped you!

Or buy me a coffee πŸ™ŒπŸΎ

πŸ“ License

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