A exposes universal functionality to all othet @open-stock libraries on the server side. This library requires @open-stock/stock-universal to be initialised first.
Sample code:
import {
runStockUniversalServer,
createDirectories,
} from "@open-stock/stock-universal-server";
const databaseConfigUrl = "";
await runStockUniversalServer(databaseConfigUrl);
// with npm
npm install @open-stock/stock-universal-server
// with yarn
yarn add @open-stock/stock-universal-server
import {
runStockUniversalServer,
createDirectories,
} from "@open-stock/stock-universal-server";
const databaseConfigUrl = ""; // must be a mongodb connection string
await runStockUniversalServer(databaseConfigUrl);
The following are the features of the stock-universal library
Most of the features below are automatically implememnted by the library. But If you want to use the classes and functions provided by the library, you can do so by importing them from the library. Some of them are elaborated below.
getEnvVar is a function that is used to get the environment variables from the process.env object. It is used by the EhttpController to get the environment variables
import { getEnvVar } from "@open-stock/stock-universal-server";
const name = 'name'
const envVar = getEnvVar(name);
getExpressLocals is a function that is used to get the express locals from the express request object. It is used by the EhttpController to get the express locals
import { getExpressLocals } from "@open-stock/stock-universal-server";
import express from 'express';
const app = epress();
const localVar = getExpressLocals(app, 'name');
apiRouter is a function that is used to get the express router for the api. It is used by the EhttpController to get the express router for the api
import { apiRouter } from "@open-stock/stock-universal-server";
import express from 'express';
const app = epress();
app.use('/api', apiRouter());
requireAuth is a function that is used to get the express middleware for the authentication api. It is used by the EhttpController to get the express middleware for the api
import { requireAuth } from "@open-stock/stock-universal-server";
import express from 'express';
import { authRouter } from './authRouter';
const app = epress();
app.use('/api', requireAuth(), authRouter());
makeUrId is a function that is used to mkake an id by incrementing the supplie value by 1.
import { makeUrId } from "@open-stock/stock-universal-server";
const lastPosition = 11;
const id = makeUrId(lastPosition);
getHostname is a function that is used to get the running hostname;
import { getHostname } from "@open-stock/stock-universal-server";
import { apiRouter } from './apiRouter';
import express from 'express';
const app = epress();
app.use('/api', apiRouter());
apiRouter.get('/hostname', (req, res) => {
const hostname = getHostname(req);
});
// or without request object
const hostname = getHostname();
stringifyMongooseErr is a function that is used to stringify the mongoose error object
import { stringifyMongooseErr } from "@open-stock/stock-universal-server";
const errString = stringifyMongooseErr(err);
offsetLimitRelegator is a function that is used to get the offset and limit from the request query.
import { offsetLimitRelegator } from "@open-stock/stock-universal-server";
const initialOffset = 0;
const initialLimit = 10;
const { initialOffset, initialLimit } = offsetLimitRelegator(req);
verifyObjectId is a function that is used to verify if a string is a valid mongoose object id
import { verifyObjectId } from "@open-stock/stock-universal-server";
const valid = verifyObjectId(id);
verifyObjectIds is a function that is used to verify if an array of strings are valid mongoose object ids
import { verifyObjectIds } from "@open-stock/stock-universal-server";
const valid = verifyObjectIds(ids);
createDirectories is a function that is used to create directories
import { createDirectories } from "@open-stock/stock-universal-server";
const appName = 'app';
const absolutepath = '/'; // you might want to get this from process.cwd()
const directories = ['dir1', 'dir2', 'dir3'];
const created = await createDirectories(appName, absolutepath, directories);
checkDirectoryExists is a function that is used to check if a directory exists
import { checkDirectoryExists } from "@open-stock/stock-universal-server";
const absolutepath = '/'; // you might want to get this from process.cwd()
const useAbsolutePath = 'first';
const exists = await checkDirectoryExists(absolutepath, useAbsolutePath, useAbsolutePath);
uploadFiles is a middleware function that is used to upload files to a server
import { uploadFiles } from "@open-stock/stock-universal-server";
appendBody is a middleware function that is used to append the body to the request object after uploadFiles middleware
import { appendBody } from "@open-stock/stock-universal-server";
saveMetaToDb is a middleware function that is used to save the meta data to the database after appendBody middleware
import { saveMetaToDb } from "@open-stock/stock-universal-server";
updateFiles is a middleware function that is used to update files
import { updateFiles } from "@open-stock/stock-universal-server";
deleteFiles is a middleware function that is used to delete files
import { deleteFiles } from "@open-stock/stock-universal-server";
getOneFile is a middleware function that is used to get one file
import { getOneFile } from "@open-stock/stock-universal-server";
returnLazyFn is a function that is used to return response status 200 okay after all middleware functions have been executed successfully
import { returnLazyFn } from "@open-stock/stock-universal-server";
The source code of the website can be found here. Contributions are welcome!
Become a sponsor and get your logo on our README on Github with a link to your site.