@codisolutions23/node-utils 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +143 -0
- package/dist/index.d.ts +143 -0
- package/dist/index.js +758 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +704 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +1 -1
- package/tsconfig.json +1 -1
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
import { MongoClient, Db, ObjectId } from 'mongodb';
|
|
3
|
+
import Redis from 'ioredis';
|
|
4
|
+
import jwt from 'jsonwebtoken';
|
|
5
|
+
import * as winston from 'winston';
|
|
6
|
+
import { RedisClientType } from 'redis';
|
|
7
|
+
|
|
8
|
+
declare function authenticateJWT(secretKey?: string): (req: Request, res: Response, next: NextFunction) => void;
|
|
9
|
+
|
|
10
|
+
declare class HttpError extends Error {
|
|
11
|
+
readonly statusCode: number;
|
|
12
|
+
readonly isOperational: boolean;
|
|
13
|
+
constructor(message: string, statusCode: number, isOperational?: boolean);
|
|
14
|
+
}
|
|
15
|
+
declare class BadRequestError extends HttpError {
|
|
16
|
+
constructor(message?: string);
|
|
17
|
+
}
|
|
18
|
+
declare class UnauthorizedError extends HttpError {
|
|
19
|
+
constructor(message?: string);
|
|
20
|
+
}
|
|
21
|
+
declare class ForbiddenError extends HttpError {
|
|
22
|
+
constructor(message?: string);
|
|
23
|
+
}
|
|
24
|
+
declare class NotFoundError extends HttpError {
|
|
25
|
+
constructor(message?: string);
|
|
26
|
+
}
|
|
27
|
+
declare class UnprocessableEntityError extends HttpError {
|
|
28
|
+
constructor(message?: string);
|
|
29
|
+
}
|
|
30
|
+
declare class InternalServerError extends HttpError {
|
|
31
|
+
constructor(message?: string);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
declare const errorHandler: (error: HttpError, req: Request, res: Response, next: NextFunction) => void;
|
|
35
|
+
|
|
36
|
+
interface AtlasConfig {
|
|
37
|
+
uri: string;
|
|
38
|
+
db: string;
|
|
39
|
+
name?: string;
|
|
40
|
+
}
|
|
41
|
+
declare class useAtlas {
|
|
42
|
+
private static mongoClient;
|
|
43
|
+
private static mongoDb;
|
|
44
|
+
static connect(config: AtlasConfig): Promise<void>;
|
|
45
|
+
static getClient(): MongoClient | null;
|
|
46
|
+
static getDb(): Db | null;
|
|
47
|
+
static close(): Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
declare function buildCacheKey(prefix: string, values: Record<string, any>): string;
|
|
51
|
+
|
|
52
|
+
declare function useCache(): {
|
|
53
|
+
get: <T = unknown>(cacheKey: string) => Promise<T | null>;
|
|
54
|
+
set: <T = unknown>(cacheKey: string, data: T, ttl?: number, group?: string) => Promise<void>;
|
|
55
|
+
remove: (cacheKey: string) => Promise<void>;
|
|
56
|
+
clearGroup: (group: string) => Promise<void>;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
declare function getTemplatePath(directory: string, filePath: string): string;
|
|
60
|
+
|
|
61
|
+
interface CompileOptions {
|
|
62
|
+
context?: Record<string, any>;
|
|
63
|
+
filePath: string;
|
|
64
|
+
}
|
|
65
|
+
declare function renderHandlebarsTemplate({ context, filePath, }: CompileOptions): Promise<string>;
|
|
66
|
+
|
|
67
|
+
type RedisOptions = {
|
|
68
|
+
host?: string;
|
|
69
|
+
port?: number;
|
|
70
|
+
username?: string;
|
|
71
|
+
password?: string;
|
|
72
|
+
};
|
|
73
|
+
declare function useRedis(): {
|
|
74
|
+
initialize: (options: RedisOptions) => Redis;
|
|
75
|
+
getClient: () => Redis;
|
|
76
|
+
disconnect: () => void;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
interface JwtSignParams {
|
|
80
|
+
payload?: Record<string, unknown>;
|
|
81
|
+
secretKey: string;
|
|
82
|
+
signOptions?: jwt.SignOptions;
|
|
83
|
+
}
|
|
84
|
+
declare function signJwtToken({ payload, secretKey, signOptions, }: JwtSignParams): string;
|
|
85
|
+
|
|
86
|
+
declare const logger: winston.Logger;
|
|
87
|
+
|
|
88
|
+
interface MailerConfig {
|
|
89
|
+
email: string;
|
|
90
|
+
password: string;
|
|
91
|
+
host: string;
|
|
92
|
+
port: number;
|
|
93
|
+
secure: boolean;
|
|
94
|
+
}
|
|
95
|
+
declare class useMailer {
|
|
96
|
+
private config;
|
|
97
|
+
private transporter;
|
|
98
|
+
constructor(config: MailerConfig);
|
|
99
|
+
sendMail({ sender, to, subject, text, html, }: {
|
|
100
|
+
sender?: string;
|
|
101
|
+
to: string;
|
|
102
|
+
subject: string;
|
|
103
|
+
text?: string;
|
|
104
|
+
html?: string;
|
|
105
|
+
}): Promise<string>;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
declare function toObjectId(id: string | ObjectId): ObjectId;
|
|
109
|
+
|
|
110
|
+
declare function paginate<T>(items: T[], page: number | undefined, limit: number | undefined, total: number): {
|
|
111
|
+
items: T[];
|
|
112
|
+
pages: number;
|
|
113
|
+
pageRange: string;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
declare function comparePasswords(password: string, hashed: string): Promise<boolean>;
|
|
117
|
+
declare function hashPassword(password: string, saltRounds?: number): Promise<string>;
|
|
118
|
+
|
|
119
|
+
declare function initRedisClient(): Promise<RedisClientType | undefined>;
|
|
120
|
+
declare function useRedisClient(): RedisClientType;
|
|
121
|
+
|
|
122
|
+
interface S3Config {
|
|
123
|
+
accessKeyId: string;
|
|
124
|
+
secretAccessKey: string;
|
|
125
|
+
endpoint: string;
|
|
126
|
+
region: string;
|
|
127
|
+
bucket: string;
|
|
128
|
+
forcePathStyle: boolean;
|
|
129
|
+
}
|
|
130
|
+
declare class useS3 {
|
|
131
|
+
private config;
|
|
132
|
+
private client;
|
|
133
|
+
constructor(config: S3Config);
|
|
134
|
+
uploadObject({ key, body, metadata, contentType, }: {
|
|
135
|
+
key: string;
|
|
136
|
+
body: string | Buffer;
|
|
137
|
+
metadata?: Record<string, string>;
|
|
138
|
+
contentType?: string;
|
|
139
|
+
}): Promise<string>;
|
|
140
|
+
deleteObject(key: string): Promise<string>;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export { BadRequestError, ForbiddenError, HttpError, InternalServerError, NotFoundError, UnauthorizedError, UnprocessableEntityError, authenticateJWT, buildCacheKey, comparePasswords, errorHandler, getTemplatePath, hashPassword, initRedisClient, logger, paginate, renderHandlebarsTemplate, signJwtToken, toObjectId, useAtlas, useCache, useMailer, useRedis, useRedisClient, useS3 };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
import { MongoClient, Db, ObjectId } from 'mongodb';
|
|
3
|
+
import Redis from 'ioredis';
|
|
4
|
+
import jwt from 'jsonwebtoken';
|
|
5
|
+
import * as winston from 'winston';
|
|
6
|
+
import { RedisClientType } from 'redis';
|
|
7
|
+
|
|
8
|
+
declare function authenticateJWT(secretKey?: string): (req: Request, res: Response, next: NextFunction) => void;
|
|
9
|
+
|
|
10
|
+
declare class HttpError extends Error {
|
|
11
|
+
readonly statusCode: number;
|
|
12
|
+
readonly isOperational: boolean;
|
|
13
|
+
constructor(message: string, statusCode: number, isOperational?: boolean);
|
|
14
|
+
}
|
|
15
|
+
declare class BadRequestError extends HttpError {
|
|
16
|
+
constructor(message?: string);
|
|
17
|
+
}
|
|
18
|
+
declare class UnauthorizedError extends HttpError {
|
|
19
|
+
constructor(message?: string);
|
|
20
|
+
}
|
|
21
|
+
declare class ForbiddenError extends HttpError {
|
|
22
|
+
constructor(message?: string);
|
|
23
|
+
}
|
|
24
|
+
declare class NotFoundError extends HttpError {
|
|
25
|
+
constructor(message?: string);
|
|
26
|
+
}
|
|
27
|
+
declare class UnprocessableEntityError extends HttpError {
|
|
28
|
+
constructor(message?: string);
|
|
29
|
+
}
|
|
30
|
+
declare class InternalServerError extends HttpError {
|
|
31
|
+
constructor(message?: string);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
declare const errorHandler: (error: HttpError, req: Request, res: Response, next: NextFunction) => void;
|
|
35
|
+
|
|
36
|
+
interface AtlasConfig {
|
|
37
|
+
uri: string;
|
|
38
|
+
db: string;
|
|
39
|
+
name?: string;
|
|
40
|
+
}
|
|
41
|
+
declare class useAtlas {
|
|
42
|
+
private static mongoClient;
|
|
43
|
+
private static mongoDb;
|
|
44
|
+
static connect(config: AtlasConfig): Promise<void>;
|
|
45
|
+
static getClient(): MongoClient | null;
|
|
46
|
+
static getDb(): Db | null;
|
|
47
|
+
static close(): Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
declare function buildCacheKey(prefix: string, values: Record<string, any>): string;
|
|
51
|
+
|
|
52
|
+
declare function useCache(): {
|
|
53
|
+
get: <T = unknown>(cacheKey: string) => Promise<T | null>;
|
|
54
|
+
set: <T = unknown>(cacheKey: string, data: T, ttl?: number, group?: string) => Promise<void>;
|
|
55
|
+
remove: (cacheKey: string) => Promise<void>;
|
|
56
|
+
clearGroup: (group: string) => Promise<void>;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
declare function getTemplatePath(directory: string, filePath: string): string;
|
|
60
|
+
|
|
61
|
+
interface CompileOptions {
|
|
62
|
+
context?: Record<string, any>;
|
|
63
|
+
filePath: string;
|
|
64
|
+
}
|
|
65
|
+
declare function renderHandlebarsTemplate({ context, filePath, }: CompileOptions): Promise<string>;
|
|
66
|
+
|
|
67
|
+
type RedisOptions = {
|
|
68
|
+
host?: string;
|
|
69
|
+
port?: number;
|
|
70
|
+
username?: string;
|
|
71
|
+
password?: string;
|
|
72
|
+
};
|
|
73
|
+
declare function useRedis(): {
|
|
74
|
+
initialize: (options: RedisOptions) => Redis;
|
|
75
|
+
getClient: () => Redis;
|
|
76
|
+
disconnect: () => void;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
interface JwtSignParams {
|
|
80
|
+
payload?: Record<string, unknown>;
|
|
81
|
+
secretKey: string;
|
|
82
|
+
signOptions?: jwt.SignOptions;
|
|
83
|
+
}
|
|
84
|
+
declare function signJwtToken({ payload, secretKey, signOptions, }: JwtSignParams): string;
|
|
85
|
+
|
|
86
|
+
declare const logger: winston.Logger;
|
|
87
|
+
|
|
88
|
+
interface MailerConfig {
|
|
89
|
+
email: string;
|
|
90
|
+
password: string;
|
|
91
|
+
host: string;
|
|
92
|
+
port: number;
|
|
93
|
+
secure: boolean;
|
|
94
|
+
}
|
|
95
|
+
declare class useMailer {
|
|
96
|
+
private config;
|
|
97
|
+
private transporter;
|
|
98
|
+
constructor(config: MailerConfig);
|
|
99
|
+
sendMail({ sender, to, subject, text, html, }: {
|
|
100
|
+
sender?: string;
|
|
101
|
+
to: string;
|
|
102
|
+
subject: string;
|
|
103
|
+
text?: string;
|
|
104
|
+
html?: string;
|
|
105
|
+
}): Promise<string>;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
declare function toObjectId(id: string | ObjectId): ObjectId;
|
|
109
|
+
|
|
110
|
+
declare function paginate<T>(items: T[], page: number | undefined, limit: number | undefined, total: number): {
|
|
111
|
+
items: T[];
|
|
112
|
+
pages: number;
|
|
113
|
+
pageRange: string;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
declare function comparePasswords(password: string, hashed: string): Promise<boolean>;
|
|
117
|
+
declare function hashPassword(password: string, saltRounds?: number): Promise<string>;
|
|
118
|
+
|
|
119
|
+
declare function initRedisClient(): Promise<RedisClientType | undefined>;
|
|
120
|
+
declare function useRedisClient(): RedisClientType;
|
|
121
|
+
|
|
122
|
+
interface S3Config {
|
|
123
|
+
accessKeyId: string;
|
|
124
|
+
secretAccessKey: string;
|
|
125
|
+
endpoint: string;
|
|
126
|
+
region: string;
|
|
127
|
+
bucket: string;
|
|
128
|
+
forcePathStyle: boolean;
|
|
129
|
+
}
|
|
130
|
+
declare class useS3 {
|
|
131
|
+
private config;
|
|
132
|
+
private client;
|
|
133
|
+
constructor(config: S3Config);
|
|
134
|
+
uploadObject({ key, body, metadata, contentType, }: {
|
|
135
|
+
key: string;
|
|
136
|
+
body: string | Buffer;
|
|
137
|
+
metadata?: Record<string, string>;
|
|
138
|
+
contentType?: string;
|
|
139
|
+
}): Promise<string>;
|
|
140
|
+
deleteObject(key: string): Promise<string>;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export { BadRequestError, ForbiddenError, HttpError, InternalServerError, NotFoundError, UnauthorizedError, UnprocessableEntityError, authenticateJWT, buildCacheKey, comparePasswords, errorHandler, getTemplatePath, hashPassword, initRedisClient, logger, paginate, renderHandlebarsTemplate, signJwtToken, toObjectId, useAtlas, useCache, useMailer, useRedis, useRedisClient, useS3 };
|