exclusivejs
v1.0.26
Published
<div class="w-full border-b border-black/10 dark:border-gray-900/50 text-gray-800 dark:text-gray-100 group bg-gray-50 dark:bg-[#444654]"><div class="text-base gap-4 md:gap-6 m-auto md:max-w-2xl lg:max-w-2xl xl:max-w-3xl p-4 md:py-6 flex lg:px-0"><div clas
Downloads
6
Readme
const ExclusiveJs = require('exclusivejs').instance();
ExclusiveJs.setRoutePath("src/routes") // set the path to your routes folder .setApiPrefix("api") // set the prefix for your API routes .setDebugger(true) // enable debugging
.setRoutePath("src/app/routes") // set route path// default route path is src/routes
.connectDocumentation( "/api-docs",swaggerUi.serve,swaggerUi.setup(swaggerDocument) //swagger document ) // serve documentation
.injectPackages() // inject your installed packages
.setValidator({}) // set your validator
ExclusiveJs.init();
This will automatically generate routes for your Express.js app based on the file structure of your src/routes folder.OptionsExclusiveJs provides several options that you can use to configure how your routes are generated:setRoutePath(path)Sets the path to your routes folder. Default value is src/routes.setApiPrefix(prefix)Sets the prefix for your API routes. Default value is api.setDebugger(debug)Enables or disables debugging. Default value is true.injectPackages()Injects your installed packages into your routes. This allows you to use packages in your routes without having to require them in each file. Only compiles packages that are installed, not inbuilt packages.
const AuthController = require("../../controller/auth");
const UserSchema = require("../../model/user");
class AuthRoute {
#validator;
#packages;
#services;
#models;
constructor({ packages, models, services }) {
this.#packages = packages;
this.#services = services;
this.#models = models;
}
"post.login" = async (req, res) => {
const { walletAddress } = req.body;
let user = await this.#services.AuthController.login(walletAddress);
if (!user) return res.status(400).json({ message: "user does not exist" });
user = { email: user.email, userId: String(user._id) };
const token = this.#packages.jsonwebtoken.sign(
user,
process.env.SECRET_KEY
);
return res.status(200).json({ token, user });
};
"post.register" = async (req, res) => {
const { walletAddress, githubLink, linkedInLink, lastName, firstName } =
req.body;
let user = await this.#services.AuthController.login(walletAddress);
if (user) return res.status(400).json({ message: "user does exist" });
user = await this.#services.AuthController.createUser(
walletAddress,
githubLink,
linkedInLink,
lastName,
firstName
);
user = {
email: user.email,
userId: String(user._id),
walletAddress: user.walletAddress,
};
const token = this.#packages.jsonwebtoken.sign(
user,
process.env.SECRET_KEY
);
return res.status(200).json({ token, user });
};
}
class Validator {
#validator;
constructor({ validator }) {
this.#validator = validator;
}
all = () => [];
"post.login" = () => {
return [
this.#validator.ValidatorFactory.createUserValidator(),
this.#validator.validatorRule,
];
};
}
module.exports = {
route: AuthRoute,
middleware: Validator,
injectableClass: [AuthController],
injectableModel: [UserSchema],
};
In the above code we created two route called register and a login routes. this file is assume to be inside the auth folder in the routes folder. any model/database schema that will be used must be injected into the injectableModel same for any services or controller. After doing that, you will then get access to the injected in the constructor. check the bellow code snippet.
constructor({ packages, models, services }) {
}
model ---> the injected models/repository or database schema
services ---> the injectableClass
packages --> access to all installed packages that inside your package.json file
Also we have one that is called validator
constructor({ validator }) {
this.#validator = validator;
}
Let say your validation file looks like this
const { body, param, validationResult } = require("express-validator");
exports.validatorRule = function (req, res, next) {
const error = validationResult(req).formatWith(({ msg }) => msg);
const hasError = !error.isEmpty();
if (hasError) {
res.status(422).json({ error: error.array() });
} else {
next();
}
};
class ValidatorFactory {
static walletAddress = body("walletAddress")
.isString()
.withMessage("Provide a wallet address");
static createUserValidator() {
return [this.walletAddress];
}
}
exports.ValidatorFactory = ValidatorFactory;
All you just need to do is to export the whole file and the use the function(setValidator) to set it in the entry file .
.setValidator(validator)
Incase you have more that one validator file you can export the in a single file like this
const validator = require("../validator");
module.exports = {
validator,
};
then import into the entry file
let validator = require("../packages/index");
then set
.setValidator(validator)