jnext-library-ts
TypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

jnext-library-ts

An authentication library for Node.js simplifies the implementation of authentication-related functionalities in your Express applications. It provides tools for managing Sequelize models, creating Express routes, initializing email configurations, and Swagger documentation setup.

Installation

npm install jnext-library-ts

Usage

1. Setup server connection in your project

import dotenv from 'dotenv';
dotenv.config();
import express, { Express } from 'express';
import { Sequelize } from 'sequelize';
import * as myLib from 'jnext-library-ts';

// Create an Express application
const app: Express = express();

const sequelize = new Sequelize('dbName', 'root', '', {
    dialect: 'mysql',
    host: '127.0.0.1',
    logging: false
});

// Start the server on port 7000
app.listen(7000, () => {
    console.log(`Server is started on port:`, 7000);
});

2. Create Sequelize Models

Use the getModels function to retrieve predefined Sequelize models for users, user_meta, and user_roles. The user_roles model is automatically populated with data for the 'user' and 'admin' roles. Additionally, you can dynamically add fields to the user model using createModel().

import { Sequelize } from 'sequelize';
import * as myLib from 'jnext-library-ts';

const sequelize = new Sequelize('dbName', 'root', '', {
    dialect: 'mysql',
    host: '127.0.0.1',
    logging: false
});

myLib.getModels();
myLib.createModel('customUserModel', sequelize, {
    // Define additional fields here
    filedName: dataType // Datatype should be string. Ex. 'STRING' , 'INTEGER'
});

3. Create Express Routes

Use the createRoutes function to fetch API routes from the library for use in your Express application.

import express, { Express } from 'express';
import * as myLib from 'jnext-library-ts';

const router = myLib.createRoutes({ validator: false });

const app: Express = express();
app.use('/', router);

4. Set Environment variables in .env files

   PORT: 8000

   #JWT comfiguration details
   JWT_SECRET: jwtSecretkey
   JWT_EXPIRATION_TIME: Expiration time of jwt. Example - 1h

   #APIs prefix route to access swagger
   API_BASE_PREFIX: /

   #To manage delete APIs functionality. Hard delete or soft delete
   HARD_DELETE: false

   #To add/ manage custome templete for email Templetes in request data
   CUSTOM_TEMPLATE: true

   #To manage send mail using SMTP or sendmail()   
   SMTP=true

Example

import express, { Express } from 'express';
import Sequelize from 'sequelize';
import * as myLib from 'jnext-library-ts';

const router = myLib.createRoutes({ validator: false });
const sequelize = new Sequelize.Sequelize('authapis', 'root', '', {
    dialect: 'mysql',
    host: '127.0.0.1',
    logging: false
});

myLib.getModels();
myLib.createModel('customUserModel', sequelize, {
    // Define additional fields here
});

const app: Express = express();
app.use('/', router);

app.listen(8000, () => {
    console.log(`Server is started on`, 8000);
});

Additional Details

Types of validations

Type Description
string.base Specifies that the value must be a string.
number.base Specifies that the value must be a number.
boolean.base Specifies that the value must be a boolean.
object.base Specifies that the value must be an object.
array.base Specifies that the value must be an array.
date.base Specifies that the value must be a date.
alternatives Specifies multiple valid alternatives for the value.
any.required Specifies that the property is required.
any.optional Specifies that the property is optional.
any.forbidden Specifies that the property is forbidden.
any.allow Specifies the allowed values for the property.
any.valid Specifies the valid values for the property.
any.invalid Specifies the invalid values for the property.
any.default Specifies the default value for the property.
string.email Specifies that the string must be a valid email.
string.min Specifies the minimum length of the string.
string.max Specifies the maximum length of the string.
number.min Specifies the minimum value for the number.
number.max Specifies the maximum value for the number.
date.min Specifies the minimum date for the date.
date.max Specifies the maximum date for the date.
string.pattern Specifies a regular expression pattern for the string.
any.when Specifies conditional validation based on another property.
any.error Specifies custom error messages for the property.
any.label Specifies a custom label for the property in error messages.
any.messages Specifies custom validation error messages.

Acknowledgements

Readme

Keywords

none

Package Sidebar

Install

npm i jnext-library-ts

Weekly Downloads

1

Version

1.0.7

License

ISC

Unpacked Size

402 kB

Total Files

223

Last publish

Collaborators

  • hiralmakwana