nodecore-kit 0.1.0 → 0.3.0
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/README.md +138 -0
- package/dist/index.cjs +675 -41
- package/dist/index.d.ts +205 -13
- package/dist/index.js +648 -37
- package/package.json +25 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,21 +1,213 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import RedisClient, { RedisOptions } from 'ioredis';
|
|
2
|
+
import jwt, { SignOptions } from 'jsonwebtoken';
|
|
3
|
+
|
|
4
|
+
declare const makeRequest: ({ url, method, headers, token, data, }: {
|
|
5
|
+
url: string;
|
|
6
|
+
method?: "GET" | "DELETE" | "POST" | "PATCH" | "PUT";
|
|
7
|
+
headers?: Record<string, any>;
|
|
8
|
+
token?: string;
|
|
9
|
+
data?: Record<string, any>;
|
|
10
|
+
}) => Promise<Record<string, any>>;
|
|
11
|
+
|
|
12
|
+
declare function joiValidator(constraint: any, isMiddleware?: boolean): any;
|
|
13
|
+
|
|
14
|
+
declare const HTTP_STATUS: {
|
|
15
|
+
readonly OK: {
|
|
16
|
+
readonly code: 200;
|
|
17
|
+
readonly message: "OK";
|
|
18
|
+
};
|
|
19
|
+
readonly CREATED: {
|
|
20
|
+
readonly code: 201;
|
|
21
|
+
readonly message: "Created";
|
|
22
|
+
};
|
|
23
|
+
readonly NO_CONTENT: {
|
|
24
|
+
readonly code: 204;
|
|
25
|
+
readonly message: "No Content";
|
|
26
|
+
};
|
|
27
|
+
readonly BAD_REQUEST: {
|
|
28
|
+
readonly code: 400;
|
|
29
|
+
readonly message: "Bad Request";
|
|
30
|
+
};
|
|
31
|
+
readonly UNAUTHORIZED: {
|
|
32
|
+
readonly code: 401;
|
|
33
|
+
readonly message: "Unauthorized";
|
|
34
|
+
};
|
|
35
|
+
readonly FORBIDDEN: {
|
|
36
|
+
readonly code: 403;
|
|
37
|
+
readonly message: "Forbidden";
|
|
38
|
+
};
|
|
39
|
+
readonly NOT_FOUND: {
|
|
40
|
+
readonly code: 404;
|
|
41
|
+
readonly message: "Not Found";
|
|
42
|
+
};
|
|
43
|
+
readonly CONFLICT: {
|
|
44
|
+
readonly code: 409;
|
|
45
|
+
readonly message: "Conflict";
|
|
46
|
+
};
|
|
47
|
+
readonly UNPROCESSABLE_ENTITY: {
|
|
48
|
+
readonly code: 422;
|
|
49
|
+
readonly message: "Unprocessable Entity";
|
|
50
|
+
};
|
|
51
|
+
readonly TOKEN_EXPIRED: {
|
|
52
|
+
readonly code: 498;
|
|
53
|
+
readonly message: "Token Expired";
|
|
54
|
+
};
|
|
55
|
+
readonly TOKEN_INVALID: {
|
|
56
|
+
readonly code: 499;
|
|
57
|
+
readonly message: "Token Invalid";
|
|
58
|
+
};
|
|
59
|
+
readonly SERVER_ERROR: {
|
|
60
|
+
readonly code: 500;
|
|
61
|
+
readonly message: "Internal Server Error";
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
type HttpStatusKey = keyof typeof HTTP_STATUS;
|
|
65
|
+
type HttpStatus = (typeof HTTP_STATUS)[HttpStatusKey];
|
|
66
|
+
declare const HTTP_STATUS_CODE_ERROR: Record<number, string>;
|
|
67
|
+
declare class AppError extends Error {
|
|
68
|
+
readonly statusCode: number;
|
|
69
|
+
readonly statusMessage: string;
|
|
70
|
+
readonly errorCode?: string;
|
|
71
|
+
readonly meta?: Record<string, any>;
|
|
72
|
+
constructor(status: HttpStatus, message?: string | null, errorCode?: string, meta?: Record<string, any>);
|
|
73
|
+
}
|
|
74
|
+
declare class ValidationError extends AppError {
|
|
75
|
+
constructor(message?: string | null, meta?: Record<string, any>);
|
|
76
|
+
}
|
|
77
|
+
declare class AuthenticationError extends AppError {
|
|
78
|
+
constructor(message?: string | null, meta?: Record<string, any>);
|
|
79
|
+
}
|
|
80
|
+
declare class AuthorizationError extends AppError {
|
|
81
|
+
constructor(message?: string | null, meta?: Record<string, any>);
|
|
82
|
+
}
|
|
83
|
+
declare class NotFoundError extends AppError {
|
|
84
|
+
constructor(message?: string | null, meta?: Record<string, any>);
|
|
85
|
+
}
|
|
86
|
+
declare class TokenExpiredError extends AppError {
|
|
87
|
+
constructor(message?: string | null, meta?: Record<string, any>);
|
|
88
|
+
}
|
|
89
|
+
declare class TokenInvalidError extends AppError {
|
|
90
|
+
constructor(message?: string | null, meta?: Record<string, any>);
|
|
91
|
+
}
|
|
92
|
+
declare class BadRequestError extends AppError {
|
|
93
|
+
constructor(message?: string | null, meta?: Record<string, any>);
|
|
94
|
+
}
|
|
95
|
+
declare class ServerError extends AppError {
|
|
96
|
+
constructor(message?: string | null, meta?: Record<string, any>);
|
|
97
|
+
}
|
|
98
|
+
declare class ExistingError extends AppError {
|
|
99
|
+
constructor(message?: string | null, meta?: Record<string, any>);
|
|
100
|
+
}
|
|
101
|
+
declare class NoContent extends AppError {
|
|
102
|
+
constructor(message?: string | null, meta?: Record<string, any>);
|
|
103
|
+
}
|
|
104
|
+
declare const errorHandler: (err: any, ERROR_TYPE?: string, service?: string) => {
|
|
105
|
+
message: any;
|
|
106
|
+
error: any;
|
|
107
|
+
httpStatusCode: any;
|
|
108
|
+
success: boolean;
|
|
109
|
+
service: string;
|
|
110
|
+
};
|
|
111
|
+
declare const expressErrorMiddleware: () => (err: any, req: any, res: any, next: any) => void;
|
|
2
112
|
|
|
3
113
|
declare const paginate: (totalCount: number, currentPage: number, perPage: number) => {
|
|
4
114
|
pageCount: number;
|
|
5
115
|
offset: number;
|
|
6
116
|
};
|
|
117
|
+
declare const formatDate: (date: Date) => string;
|
|
118
|
+
declare const parseJSON: (value: any) => any;
|
|
119
|
+
declare const stringifyJSON: (value: any) => any;
|
|
120
|
+
declare const isObject: (val: any) => boolean;
|
|
121
|
+
declare const sleep: (ms: number) => Promise<unknown>;
|
|
122
|
+
declare const capitalize: (str: string) => string;
|
|
123
|
+
declare const isEmpty: (val: any) => boolean;
|
|
7
124
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
125
|
+
declare const uuid: {
|
|
126
|
+
toBinary: (uuid?: any) => any;
|
|
127
|
+
toString: (binary: any) => string;
|
|
128
|
+
get: (version?: "v1" | "v4") => string;
|
|
129
|
+
isValid: (uuid: string) => boolean;
|
|
130
|
+
manyToString: (data: any, keys?: never[]) => any;
|
|
131
|
+
manyToBinary: (data: any, keys?: never[]) => any;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
declare class Redis {
|
|
135
|
+
client: RedisClient;
|
|
136
|
+
constructor(url: string, options?: RedisOptions);
|
|
137
|
+
private registerListeners;
|
|
138
|
+
start(): Promise<void>;
|
|
139
|
+
disconnect(): Promise<void>;
|
|
140
|
+
keys(pattern: string): Promise<string[]>;
|
|
141
|
+
private serialize;
|
|
142
|
+
private deserialize;
|
|
143
|
+
set(key: string, data: any): Promise<"OK">;
|
|
144
|
+
setEx(key: string, data: any, duration: number | string): Promise<"OK">;
|
|
145
|
+
get<T = any>(key: string, parse?: boolean): Promise<T | null>;
|
|
146
|
+
delete(key: string): Promise<boolean>;
|
|
147
|
+
deleteAll(prefix: string): Promise<number>;
|
|
148
|
+
exists(key: string): Promise<boolean>;
|
|
149
|
+
ttl(key: string): Promise<number>;
|
|
150
|
+
expire(key: string, duration: number | string): Promise<boolean>;
|
|
151
|
+
flush(): Promise<void>;
|
|
152
|
+
getCachedUser<T = any>(id: string, throwError?: boolean): Promise<T | null>;
|
|
153
|
+
cacheUser(user: any, ttl?: number | string): Promise<void>;
|
|
154
|
+
updateAuthData(userId: string, key: string, value: string, action?: "ADD" | "REMOVE"): Promise<any>;
|
|
155
|
+
private parseDuration;
|
|
13
156
|
}
|
|
14
|
-
declare const makeRequest: <T = any>(options: RequestOptions) => Promise<T>;
|
|
15
|
-
declare const get: <T = any>(url: string, options?: Omit<RequestOptions, "url" | "method">) => Promise<T>;
|
|
16
|
-
declare const post: <T = any>(url: string, data?: any, options?: Omit<RequestOptions, "url" | "method">) => Promise<T>;
|
|
17
|
-
declare const put: <T = any>(url: string, data?: any, options?: Omit<RequestOptions, "url" | "method">) => Promise<T>;
|
|
18
|
-
declare const patch: <T = any>(url: string, data?: any, options?: Omit<RequestOptions, "url" | "method">) => Promise<T>;
|
|
19
|
-
declare const del: <T = any>(url: string, options?: Omit<RequestOptions, "url" | "method">) => Promise<T>;
|
|
20
157
|
|
|
21
|
-
|
|
158
|
+
interface SQSDequeueInt {
|
|
159
|
+
queueUrl: string;
|
|
160
|
+
consumerFunction: (message: any) => Promise<any>;
|
|
161
|
+
maxNumberOfMessages?: number;
|
|
162
|
+
waitTimeSeconds?: number;
|
|
163
|
+
dlqUrl?: string;
|
|
164
|
+
}
|
|
165
|
+
interface SQSEqueueInt {
|
|
166
|
+
queueUrl: string;
|
|
167
|
+
message: any;
|
|
168
|
+
}
|
|
169
|
+
interface Logger {
|
|
170
|
+
info(message: string, meta?: unknown): void;
|
|
171
|
+
error(message: string, meta?: unknown): void;
|
|
172
|
+
warn(message: string, meta?: unknown): void;
|
|
173
|
+
debug?(message: string, meta?: unknown): void;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
interface SqsConfig {
|
|
177
|
+
region: string;
|
|
178
|
+
accessKeyId: string;
|
|
179
|
+
secretAccessKey: string;
|
|
180
|
+
}
|
|
181
|
+
declare class SQS {
|
|
182
|
+
private client;
|
|
183
|
+
private logger;
|
|
184
|
+
constructor(config: SqsConfig, logger?: Logger);
|
|
185
|
+
enqueue({ queueUrl, message }: SQSEqueueInt): Promise<boolean>;
|
|
186
|
+
dequeue(fields: SQSDequeueInt): Promise<void>;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
declare class WinstonLogger implements Logger {
|
|
190
|
+
private logger;
|
|
191
|
+
info(message: string, meta?: unknown): void;
|
|
192
|
+
error(message: string, meta?: unknown): void;
|
|
193
|
+
warn(message: string, meta?: unknown): void;
|
|
194
|
+
debug(message: string, meta?: unknown): void;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
interface JwtEncodeOptions {
|
|
198
|
+
data: string | object | Buffer;
|
|
199
|
+
secretKey: string;
|
|
200
|
+
expiresIn?: string | number;
|
|
201
|
+
algorithm?: SignOptions["algorithm"];
|
|
202
|
+
}
|
|
203
|
+
interface JwtDecodeOptions {
|
|
204
|
+
token: string;
|
|
205
|
+
secretKey: string;
|
|
206
|
+
algorithms?: string[];
|
|
207
|
+
}
|
|
208
|
+
declare const jwtService: {
|
|
209
|
+
encode({ data, secretKey, expiresIn, algorithm, }: JwtEncodeOptions): Promise<string>;
|
|
210
|
+
decode<T = jwt.JwtPayload>({ token, secretKey, algorithms, }: JwtDecodeOptions): Promise<T>;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
export { AppError, AuthenticationError, AuthorizationError, BadRequestError, ExistingError, HTTP_STATUS, HTTP_STATUS_CODE_ERROR, HttpStatus, HttpStatusKey, JwtDecodeOptions, JwtEncodeOptions, NoContent, NotFoundError, Redis, SQS, ServerError, SqsConfig, TokenExpiredError, TokenInvalidError, ValidationError, WinstonLogger, capitalize, errorHandler, expressErrorMiddleware, formatDate, isEmpty, isObject, joiValidator, jwtService, makeRequest, paginate, parseJSON, sleep, stringifyJSON, uuid };
|