Utility functions for Node.js and React developers
Deprecated: This package has been deprecated. Kindly update to latest Node.js version as most of the functions are now native to Node.js.
npm install iyasunday
# or
yarn add iyasundayconst Iya = require('iyasunday');
// Generate a random string
const token = Iya.randomString(16);
// Slugify a title
const slug = Iya.slugify('Hello, World!');
// Make HTTP requests
const data = await Iya.getContent({ url: 'https://api.example.com/items' });This package ships with type definitions (index.d.ts) and works out of the box in TypeScript. You can use named imports or a namespace import.
// Named imports (recommended)
import { randomString, slugify, getContent, postContent } from 'iyasunday';
async function run() {
const id = randomString(12);
const title = slugify('Hello TS!');
const list = await getContent({ url: 'https://api.example.com/items' });
const created = await postContent({
url: 'https://api.example.com/items',
data: { name: title, id },
headers: { 'Content-Type': 'application/json' },
});
console.log(created);
}
run().catch(console.error);// Alternatively, namespace import
import * as Iya from 'iyasunday';
async function main() {
const token = Iya.randomString(16);
const result = await Iya.getContent({ url: 'https://api.example.com/items' });
console.log(token, result);
}
main();Below is a catalog of the available utilities. All functions are available from the package root:
const Iya = require('iyasunday');
// Iya.functionName(...)randomString(length = 10): string– Generate a random base36 string of a given length.uniqueString(capitalize = false): string– Time-seeded unique string; uppercase whencapitalizeis true.shuffelWord(word: string | number): string– Randomly shuffle characters.slugify(text: string, lowerCase = true): string– URL slug; removes special characters. Usesslugifyunder the hood.htmlEncode(value: string): string– Encode HTML special characters.htmlDecode(value: string): string– Decode HTML entities.md5(plainText?: string): string– Return MD5 hash of a string.
encrypt(plainText: string, secret: string, expireMinutes?: number): string– Symmetric encryption usingcryptr. IfexpireMinutesis set, the token carries an expiry.decrypt(cipherText: string, secret: string): string– Decrypt a token produced byencrypt; throwsTokenExpiredErrorwhen expired.encodeJwt({ data, secreteKey, duration = '24h' }): Promise<string>– Sign a JWT.EncodeJwt(data, secreteKey, duration = '24h'): Promise<string>– Same asencodeJwtbut positional args.decodeJwt(cipher: string, secreteKey): Promise<any>– Verify a JWT from anAuthorizationheader value (e.g.Bearer <token>).
getContent({ url, method = 'GET', headers = {}, token, data }): Promise<any>– Perform a request; attachesX-Requested-Withand optionalAuthorizationheader.postContent({ url, token, data, method = 'POST', headers = {} }): Promise<any>– POST/PUT/PATCH helper. Errors containhttpStatusCodewhen available.urlQueryToString(query: Record<string, any>): string– Serialize a plain object to?key=value&....
fileExists(path: string): Promise<boolean>– Check if a file exists.isFile(path: string): Promise<boolean>– Alias that resolves true/false if accessible.deleteFile(path: string): Promise<boolean>– Delete a file if it exists.createPath(path: string): Promise<boolean>– Recursively create a directory if missing.uploadFile(opts): multer.Instance– Configure a Multer disk storage:name?: string– Optional output filename root.limit = 5– Max size in MB.allowedFormat = ['jpg','jpeg','png','gif']– Restrict extensions.location = '/'– Output directory; auto-created.
removeUpload(files): Promise<void>– Remove uploaded file(s) by Multer result shape.base64ToFile(base64: string, path: string): Promise<string>– Persist a base64 data URL topathand return the created filepath.readJson(path: string): Promise<object>– Read and parse a JSON file.
dateFormat(date: Date | string, format = 'DD-MM-YYYY'): string– Format dates viamoment.rand(min = 0, max = 10000): number– Random integer in range inclusive.paginate(totalCount: number, currentPage: number, perPage: number): { pageCount, offset }– Page math helper.
validate(schema, object, option?): any– Run Joi schema validation and throwValidationErroron failure.joiValidator(constraint, isMiddleware = true)– Express middleware generator or direct validator:- As middleware: pass
{ body?, params?, query?, headers? }each with{ schema, options }. - As function: pass
{ schema, data, option }and setisMiddleware = false.
- As middleware: pass
successMessage(message: string): { success: true, message }– Convenience success shape.notFound(message: string): { success: false, message }– Convenience not-found shape.errorMessage(err, ERROR_TYPE = 'FATAL_ERROR')– Normalize errors. In non-production logs stack; maps Axios errors to{ message, httpStatusCode, error }.appRequestOnly()– Express middleware enforcing a signedapp_keyheader. Usesdecryptandprocess.env.APP_KEY.
Custom errors include the httpStatusCode property:
InvalidTokenError,TokenExpiredError,AuthenticationError,AuthorizationError,EntryExistError,EntryNotFoundError,NotFoundError,ExistsError,ValidationError,PaymentRequiredError.
const express = require('express');
const Iya = require('iyasunday');
const app = express();
const upload = Iya.uploadFile({ location: './uploads', allowedFormat: ['png','jpg'] });
app.post('/photos', upload.single('photo'), (req, res) => {
res.json(Iya.successMessage('Uploaded'));
});const Iya = require('iyasunday');
const Joi = require('joi');
app.post(
'/users',
Iya.joiValidator({
body: { schema: Joi.object({ email: Joi.string().email().required() }) },
}),
(req, res) => res.json({ body: req.body })
);const Iya = require('iyasunday');
const item = await Iya.postContent({
url: 'https://api.example.com/items',
token: `Bearer ${jwt}`,
data: { name: 'Sample' },
});Most of these utilities have native equivalents in modern Node.js and popular libraries:
- Use
crypto.randomUUID()/crypto.randomBytes()for IDs. - Use
fetch(Node 18+) oraxiosdirectly for HTTP. - Use
URLSearchParamsfor query strings. - Use
fs/promisesfor file utilities.
MIT © Moses Peter