core-services-sdk 1.3.22 → 1.3.24
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/package.json +5 -2
- package/src/core/index.js +1 -0
- package/src/core/normalize-premitives-types-or-default.js +215 -0
- package/src/index.js +1 -0
- package/src/util/index.js +12 -0
- package/src/util/mask-sensitive.js +78 -0
- package/tests/core/normalize-premitives-types-or-default.unit.test.js +72 -0
- package/tests/rabbit-mq/rabbit-mq.test.js +1 -0
- package/tests/util/mask-sensitive.unit.test.js +79 -0
- package/tsconfig.json +9 -0
- package/types/core/combine-unique-arrays.d.ts +1 -0
- package/types/core/index.d.ts +8 -0
- package/types/core/normalize-min-max.d.ts +10 -0
- package/types/core/normalize-phone-number.d.ts +48 -0
- package/types/core/normalize-premitives-types-or-default.d.ts +172 -0
- package/types/core/normalize-to-array.d.ts +1 -0
- package/types/core/otp-generators.d.ts +56 -0
- package/types/core/regex-utils.d.ts +1 -0
- package/types/core/sanitize-objects.d.ts +4 -0
- package/types/crypto/crypto.d.ts +18 -0
- package/types/crypto/encryption.d.ts +6 -0
- package/types/crypto/index.d.ts +2 -0
- package/types/fastify/error-codes.d.ts +29 -0
- package/types/fastify/error-handlers/with-error-handling.d.ts +15 -0
- package/types/fastify/index.d.ts +2 -0
- package/types/http/HttpError.d.ts +82 -0
- package/types/http/http-method.d.ts +7 -0
- package/types/http/http.d.ts +36 -0
- package/types/http/index.d.ts +4 -0
- package/types/http/responseType.d.ts +20 -0
- package/types/ids/generators.d.ts +10 -0
- package/types/ids/index.d.ts +2 -0
- package/types/ids/prefixes.d.ts +34 -0
- package/types/index.d.ts +11 -0
- package/types/logger/get-logger.d.ts +23 -0
- package/types/logger/index.d.ts +1 -0
- package/types/mailer/index.d.ts +2 -0
- package/types/mailer/mailer.service.d.ts +21 -0
- package/types/mailer/transport.factory.d.ts +48 -0
- package/types/mongodb/connect.d.ts +4 -0
- package/types/mongodb/index.d.ts +3 -0
- package/types/mongodb/initialize-mongodb.d.ts +13 -0
- package/types/mongodb/validate-mongo-uri.d.ts +15 -0
- package/types/rabbit-mq/index.d.ts +1 -0
- package/types/rabbit-mq/rabbit-mq.d.ts +40 -0
- package/types/templates/index.d.ts +1 -0
- package/types/templates/template-loader.d.ts +3 -0
- package/types/util/index.d.ts +7 -0
- package/types/util/mask-sensitive.d.ts +2 -0
- package/vitest.config.js +13 -3
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize Utilities
|
|
3
|
+
*
|
|
4
|
+
* Small helpers to guarantee stable, predictable values from user/config inputs.
|
|
5
|
+
* When an incoming value is missing, malformed, or in a different-but-supported
|
|
6
|
+
* representation (e.g., number/boolean as string), these utilities either accept
|
|
7
|
+
* it (after safe normalization) or return a default you control.
|
|
8
|
+
*
|
|
9
|
+
* Design highlights:
|
|
10
|
+
* - Keep call sites compact and intention-revealing.
|
|
11
|
+
* - Be strict for strings (no implicit type coercion).
|
|
12
|
+
* - Be permissive for number/boolean when the input is a string in an accepted form.
|
|
13
|
+
* - Fast, side-effect free, no deep cloning — values are returned by reference when valid.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Generic predicate-based normalization.
|
|
17
|
+
*
|
|
18
|
+
* Purpose:
|
|
19
|
+
* Ensure a value conforms to a caller-provided predicate; otherwise return a provided default.
|
|
20
|
+
* Useful when you need a single reusable pattern for custom shapes or policies
|
|
21
|
+
* (e.g., "must be a non-empty array of strings", "must be a plain object", etc.).
|
|
22
|
+
*
|
|
23
|
+
* Behavior:
|
|
24
|
+
* - Calls `isValid(value)`.
|
|
25
|
+
* - If the predicate returns true → returns `value` (as-is).
|
|
26
|
+
* - Otherwise → returns `defaultValue` (as-is).
|
|
27
|
+
*
|
|
28
|
+
* Performance & Safety:
|
|
29
|
+
* - O(1) aside from your predicate cost.
|
|
30
|
+
* - No cloning or sanitization is performed.
|
|
31
|
+
* - Ensure `isValid` is pure and fast; avoid throwing inside it.
|
|
32
|
+
*
|
|
33
|
+
* @template T
|
|
34
|
+
* @param {any} value
|
|
35
|
+
* Candidate input to validate.
|
|
36
|
+
* @param {(v:any)=>boolean} isValid
|
|
37
|
+
* Validation predicate. Return true iff the input is acceptable.
|
|
38
|
+
* @param {T} defaultValue
|
|
39
|
+
* Fallback to return when `value` fails validation.
|
|
40
|
+
* @returns {T}
|
|
41
|
+
* The original `value` when valid; otherwise `defaultValue`.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* // Ensure a finite number
|
|
45
|
+
* normalizeOrDefault(5, v => typeof v === 'number' && Number.isFinite(v), 0) // → 5
|
|
46
|
+
* normalizeOrDefault('x', v => typeof v === 'number', 0) // → 0
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* // Ensure an object (non-null, non-array)
|
|
50
|
+
* const cfg = normalizeOrDefault(
|
|
51
|
+
* maybeCfg,
|
|
52
|
+
* v => v && typeof v === 'object' && !Array.isArray(v),
|
|
53
|
+
* {}
|
|
54
|
+
* )
|
|
55
|
+
*/
|
|
56
|
+
export function normalizeOrDefault<T>(value: any, isValid: (v: any) => boolean, defaultValue: T): T;
|
|
57
|
+
/**
|
|
58
|
+
* Normalize a value to a non-empty, trimmed string; otherwise return a default (also trimmed).
|
|
59
|
+
*
|
|
60
|
+
* Purpose:
|
|
61
|
+
* Guarantee that downstream code receives a usable, non-empty string
|
|
62
|
+
* without performing implicit type coercion.
|
|
63
|
+
*
|
|
64
|
+
* Acceptance Criteria:
|
|
65
|
+
* - Accept only actual strings whose `trim()` length is > 0.
|
|
66
|
+
* - Return `value.trim()` when valid.
|
|
67
|
+
* - Otherwise return `defaultValue.trim()`.
|
|
68
|
+
*
|
|
69
|
+
* Why strict for strings?
|
|
70
|
+
* - Silent coercion from non-strings to string can hide bugs.
|
|
71
|
+
* - If you need to stringify other types, do it explicitly at the call site.
|
|
72
|
+
*
|
|
73
|
+
* Edge Cases:
|
|
74
|
+
* - If `defaultValue` is empty or whitespace-only, the function returns an empty string.
|
|
75
|
+
* Prefer providing a meaningful, non-empty default for clarity.
|
|
76
|
+
*
|
|
77
|
+
* @param {any} value
|
|
78
|
+
* Candidate to normalize (must be a string to be accepted).
|
|
79
|
+
* @param {string} defaultValue
|
|
80
|
+
* Fallback used when `value` is not a non-empty string. Will be `trim()`ed.
|
|
81
|
+
* @returns {string}
|
|
82
|
+
* A trimmed non-empty string when `value` is valid; otherwise `defaultValue.trim()`.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* normalizeStringOrDefault(' user-roles-management:edit ', 'fallback')
|
|
86
|
+
* // → 'user-roles-management:edit'
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* normalizeStringOrDefault('', 'user-roles-management:edit')
|
|
90
|
+
* // → 'user-roles-management:edit'
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* normalizeStringOrDefault(42, 'user-roles-management:edit')
|
|
94
|
+
* // → 'user-roles-management:edit'
|
|
95
|
+
*/
|
|
96
|
+
export function normalizeStringOrDefault(value: any, defaultValue: string): string;
|
|
97
|
+
/**
|
|
98
|
+
* Normalize a value to a valid number (with safe string coercion); otherwise return a default.
|
|
99
|
+
*
|
|
100
|
+
* Purpose:
|
|
101
|
+
* Accept numeric inputs that may arrive as strings (e.g., from env vars or config files)
|
|
102
|
+
* while keeping semantics explicit and predictable.
|
|
103
|
+
*
|
|
104
|
+
* Acceptance Criteria:
|
|
105
|
+
* - Accepts finite numbers (`typeof value === 'number' && Number.isFinite(value)`).
|
|
106
|
+
* - Accepts strings that become a finite number via `Number(trimmed)`.
|
|
107
|
+
* Examples: "42", " 3.14 ", "1e3", "-7", "0x10" (JS Number semantics).
|
|
108
|
+
* - Rejects non-numeric strings (e.g., "", " ", "abc") and non-number types.
|
|
109
|
+
* - Returns `defaultValue` when not acceptable.
|
|
110
|
+
*
|
|
111
|
+
* Parsing Semantics:
|
|
112
|
+
* - Uses `Number(s)` which requires the whole trimmed string to be numeric.
|
|
113
|
+
* - Honors JavaScript numeric literal rules (including hex and scientific notation).
|
|
114
|
+
* - If you want base-10 only or looser parsing, do it explicitly before calling.
|
|
115
|
+
*
|
|
116
|
+
* @param {any} value
|
|
117
|
+
* Candidate to normalize (number or numeric string).
|
|
118
|
+
* @param {number} defaultValue
|
|
119
|
+
* Fallback used when `value` is neither a finite number nor a numeric string.
|
|
120
|
+
* @returns {number}
|
|
121
|
+
* A finite number derived from `value`, or `defaultValue`.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* normalizeNumberOrDefault(42, 0) // → 42
|
|
125
|
+
* normalizeNumberOrDefault(' 3.14 ', 0) // → 3.14
|
|
126
|
+
* normalizeNumberOrDefault('1e3', 0) // → 1000
|
|
127
|
+
* normalizeNumberOrDefault('-7', 0) // → -7
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* normalizeNumberOrDefault('abc', 7) // → 7
|
|
131
|
+
* normalizeNumberOrDefault(NaN, 7) // → 7
|
|
132
|
+
* normalizeNumberOrDefault({}, 7) // → 7
|
|
133
|
+
*/
|
|
134
|
+
export function normalizeNumberOrDefault(value: any, defaultValue: number): number;
|
|
135
|
+
/**
|
|
136
|
+
* Normalize a value to a boolean (with "true"/"false" string support); otherwise return a default.
|
|
137
|
+
*
|
|
138
|
+
* Purpose:
|
|
139
|
+
* Stabilize feature flags and binary config values that might be provided as either booleans
|
|
140
|
+
* or as canonical strings.
|
|
141
|
+
*
|
|
142
|
+
* Acceptance Criteria:
|
|
143
|
+
* - Accepts actual booleans (`true` / `false`) → returned as-is.
|
|
144
|
+
* - Accepts strings equal to "true" or "false" (case-insensitive, trimmed).
|
|
145
|
+
* "true" → true
|
|
146
|
+
* "false" → false
|
|
147
|
+
* - Rejects other strings ("yes", "1", "0", etc.) and other types → returns `defaultValue`.
|
|
148
|
+
*
|
|
149
|
+
* Rationale:
|
|
150
|
+
* - Limiting string forms to the canonical words avoids accidental truthiness/falseyness.
|
|
151
|
+
* - If you need to accept "1"/"0" or other variants, coerce at the call site so intent is explicit.
|
|
152
|
+
*
|
|
153
|
+
* @param {any} value
|
|
154
|
+
* Candidate to normalize (boolean or "true"/"false" string).
|
|
155
|
+
* @param {boolean} defaultValue
|
|
156
|
+
* Fallback used when `value` is neither a boolean nor an accepted string form.
|
|
157
|
+
* @returns {boolean}
|
|
158
|
+
* A boolean derived from `value`, or `defaultValue`.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* normalizeBooleanOrDefault(true, false) // → true
|
|
162
|
+
* normalizeBooleanOrDefault(false, true) // → false
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* normalizeBooleanOrDefault('true', false) // → true
|
|
166
|
+
* normalizeBooleanOrDefault(' FALSE ', true) // → false
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* normalizeBooleanOrDefault('yes', false) // → false (rejected → default)
|
|
170
|
+
* normalizeBooleanOrDefault(1, true) // → true (rejected → default)
|
|
171
|
+
*/
|
|
172
|
+
export function normalizeBooleanOrDefault(value: any, defaultValue: boolean): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function normalizeToArray(value: any): string[];
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a one-time password (OTP) code based on a specified type or character set.
|
|
3
|
+
*
|
|
4
|
+
* @param {Object} [options={}] - The options object.
|
|
5
|
+
* @param {number} [options.length=4] - The desired length of the generated code (1-30).
|
|
6
|
+
* @param {string} [options.type='numeric'] - The type of characters to use. One of:
|
|
7
|
+
* 'any', 'alpha', 'numeric', 'symbols', 'alphaLower', 'alphaUpper', 'alphanumeric', 'alphanumericSymbols'.
|
|
8
|
+
* @param {string} [options.charset] - A custom string of characters to use instead of the predefined types.
|
|
9
|
+
* @returns {string} The generated code.
|
|
10
|
+
* @throws {Error} If the length is not a number between 1 and 30.
|
|
11
|
+
* @throws {Error} If charset is provided and is not a non-empty string.
|
|
12
|
+
* @throws {Error} If the type is invalid and no valid charset is provided.
|
|
13
|
+
*/
|
|
14
|
+
export function generateCode({ length, type, charset }?: {
|
|
15
|
+
length?: number;
|
|
16
|
+
type?: string;
|
|
17
|
+
charset?: string;
|
|
18
|
+
}): string;
|
|
19
|
+
/**
|
|
20
|
+
* Generates an OTP code using alphabetic characters (both lowercase and uppercase).
|
|
21
|
+
*
|
|
22
|
+
* @param {number} [length=4] - The desired length of the code.
|
|
23
|
+
* @returns {string} The generated code.
|
|
24
|
+
*/
|
|
25
|
+
export function generateCodeAlpha(length?: number): string;
|
|
26
|
+
/**
|
|
27
|
+
* Generates an OTP code using only numeric digits (0-9).
|
|
28
|
+
*
|
|
29
|
+
* @param {number} [length=4] - The desired length of the code.
|
|
30
|
+
* @returns {string} The generated code.
|
|
31
|
+
*/
|
|
32
|
+
export function generateCodeDigits(length?: number): string;
|
|
33
|
+
/**
|
|
34
|
+
* Generates an OTP code using alphabetic characters and digits.
|
|
35
|
+
*
|
|
36
|
+
* @param {number} [length=4] - The desired length of the code.
|
|
37
|
+
* @returns {string} The generated code.
|
|
38
|
+
*/
|
|
39
|
+
export function generateCodeAlphaNumeric(length?: number): string;
|
|
40
|
+
/**
|
|
41
|
+
* Generates an OTP code using alphabetic characters, digits, and symbols.
|
|
42
|
+
*
|
|
43
|
+
* @param {number} [length=4] - The desired length of the code.
|
|
44
|
+
* @returns {string} The generated code.
|
|
45
|
+
*/
|
|
46
|
+
export function generateCodeAlphaNumericSymbols(length?: number): string;
|
|
47
|
+
export const OTP_GENERATOR_TYPES: Readonly<{
|
|
48
|
+
any: "any";
|
|
49
|
+
alpha: "alpha";
|
|
50
|
+
numeric: "numeric";
|
|
51
|
+
symbols: "symbols";
|
|
52
|
+
alphaLower: "alphaLower";
|
|
53
|
+
alphaUpper: "alphaUpper";
|
|
54
|
+
alphanumeric: "alphanumeric";
|
|
55
|
+
alphanumericSymbols: "alphanumericSymbols";
|
|
56
|
+
}>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function isValidRegex(pattern: string | RegExp): boolean;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export function sanitizeObject(obj: any, filter: (entry: [string, any]) => boolean): any;
|
|
2
|
+
export function sanitizeUndefinedFields(obj: any): any;
|
|
3
|
+
export function sanitizeObjectAllowProps(obj: any, allowedFields?: string[]): any;
|
|
4
|
+
export function sanitizeObjectDisallowProps(obj: any, disallowedFields?: string[]): any;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function getSalt(length: number): Buffer;
|
|
2
|
+
export function getSaltHex(length: number): string;
|
|
3
|
+
export function getEncryptedBuffer({ salt, expression, length, }: {
|
|
4
|
+
expression: string;
|
|
5
|
+
salt: string;
|
|
6
|
+
length?: number;
|
|
7
|
+
}): Promise<Buffer>;
|
|
8
|
+
export function encrypt({ salt, expression, passwordPrivateKey }: {
|
|
9
|
+
expression: string;
|
|
10
|
+
salt: string;
|
|
11
|
+
passwordPrivateKey?: string;
|
|
12
|
+
}): Promise<string>;
|
|
13
|
+
export function isPasswordMatch({ salt, password, encryptedPassword, passwordPrivateKey, }: {
|
|
14
|
+
salt: string;
|
|
15
|
+
password: string;
|
|
16
|
+
encryptedPassword: string;
|
|
17
|
+
passwordPrivateKey?: string;
|
|
18
|
+
}): Promise<boolean>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A reusable generic error object representing a server-side failure (HTTP 500).
|
|
3
|
+
* Useful as a fallback error descriptor for unhandled or unexpected failures.
|
|
4
|
+
*
|
|
5
|
+
* @typedef {Object} GeneralError
|
|
6
|
+
* @property {number} httpStatusCode - The HTTP status code (500).
|
|
7
|
+
* @property {string} httpStatusText - The human-readable status text ("Internal Server Error").
|
|
8
|
+
* @property {string} code - An application-specific error code in the format "GENERAL.<StatusText>".
|
|
9
|
+
*/
|
|
10
|
+
/** @type {GeneralError} */
|
|
11
|
+
export const GENERAL_ERROR: GeneralError;
|
|
12
|
+
/**
|
|
13
|
+
* A reusable generic error object representing a server-side failure (HTTP 500).
|
|
14
|
+
* Useful as a fallback error descriptor for unhandled or unexpected failures.
|
|
15
|
+
*/
|
|
16
|
+
export type GeneralError = {
|
|
17
|
+
/**
|
|
18
|
+
* - The HTTP status code (500).
|
|
19
|
+
*/
|
|
20
|
+
httpStatusCode: number;
|
|
21
|
+
/**
|
|
22
|
+
* - The human-readable status text ("Internal Server Error").
|
|
23
|
+
*/
|
|
24
|
+
httpStatusText: string;
|
|
25
|
+
/**
|
|
26
|
+
* - An application-specific error code in the format "GENERAL.<StatusText>".
|
|
27
|
+
*/
|
|
28
|
+
code: string;
|
|
29
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function withErrorHandling(log: object, defaultError: HttpError): (funcToInvoke: () => Promise<any>) => Promise<any>;
|
|
2
|
+
export function withErrorHandlingReply({ reply, log, defaultError }: {
|
|
3
|
+
reply: Reply;
|
|
4
|
+
log: Logger;
|
|
5
|
+
defaultError?: HttpError;
|
|
6
|
+
}): (funcToInvoke: any) => Promise<any>;
|
|
7
|
+
export function replyOnErrorOnly({ reply, log, defaultError }: {
|
|
8
|
+
reply: Reply;
|
|
9
|
+
log: Logger;
|
|
10
|
+
defaultError?: HttpError;
|
|
11
|
+
}): (funcToInvoke: any) => Promise<any>;
|
|
12
|
+
export type Reply = import("fastify").FastifyReply;
|
|
13
|
+
export type Request = import("fastify").FastifyRequest;
|
|
14
|
+
export type Logger = import("pino").Logger;
|
|
15
|
+
import { HttpError } from '../../http/HttpError.js';
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a custom HTTP error with optional status code, status text, error code, and extra metadata.
|
|
3
|
+
* Useful for consistent error handling across services.
|
|
4
|
+
*/
|
|
5
|
+
export class HttpError extends Error {
|
|
6
|
+
/**
|
|
7
|
+
* Checks if a given object is an instance of `HttpError`.
|
|
8
|
+
*
|
|
9
|
+
* @param {*} instance - The object to check.
|
|
10
|
+
* @returns {boolean} True if `instance` is an instance of `HttpError`.
|
|
11
|
+
*/
|
|
12
|
+
static isInstanceOf(instance: any): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Checks if the error is an instance of `HttpError` or has similar shape.
|
|
15
|
+
*
|
|
16
|
+
* @param {object} error
|
|
17
|
+
* @returns {error is HttpError}
|
|
18
|
+
*/
|
|
19
|
+
static isHttpError(error: object): error is HttpError;
|
|
20
|
+
/**
|
|
21
|
+
* Creates an HttpError from a generic Error instance or returns it if already an HttpError.
|
|
22
|
+
*
|
|
23
|
+
* @param {Error | HttpError} error
|
|
24
|
+
* @returns {HttpError}
|
|
25
|
+
*/
|
|
26
|
+
static FromError(error: Error | HttpError): HttpError;
|
|
27
|
+
/**
|
|
28
|
+
* Creates an instance of HttpError.
|
|
29
|
+
*
|
|
30
|
+
* @param {Object} [error] - Optional error object.
|
|
31
|
+
* @param {string | number} [error.code] - Application-specific error code.
|
|
32
|
+
* @param {string} [error.message] - Custom error message.
|
|
33
|
+
* @param {number} [error.httpStatusCode] - HTTP status code (e.g., 404, 500).
|
|
34
|
+
* @param {string} [error.httpStatusText] - Optional human-readable HTTP status text.
|
|
35
|
+
* @param {object} [error.extendInfo] - Optional extended metadata for diagnostics.
|
|
36
|
+
*/
|
|
37
|
+
constructor(error?: {
|
|
38
|
+
code?: string | number;
|
|
39
|
+
message?: string;
|
|
40
|
+
httpStatusCode?: number;
|
|
41
|
+
httpStatusText?: string;
|
|
42
|
+
extendInfo?: object;
|
|
43
|
+
});
|
|
44
|
+
/**
|
|
45
|
+
* @type {string | number | undefined}
|
|
46
|
+
* A short application-specific error code (e.g., "INVALID_INPUT" or a numeric code).
|
|
47
|
+
*/
|
|
48
|
+
code: string | number | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* @type {number | undefined}
|
|
51
|
+
* HTTP status code associated with the error (e.g., 400, 500).
|
|
52
|
+
*/
|
|
53
|
+
httpStatusCode: number | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* @type {string | undefined}
|
|
56
|
+
* Human-readable HTTP status text (e.g., "Bad Request").
|
|
57
|
+
*/
|
|
58
|
+
httpStatusText: string | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* @type {object | undefined}
|
|
61
|
+
* Optional metadata for debugging/logging (e.g., request ID, user ID, retryAfter).
|
|
62
|
+
*/
|
|
63
|
+
extendInfo: object | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* Converts the error to a plain object (useful for logging or sending as JSON).
|
|
66
|
+
*
|
|
67
|
+
* @returns {{
|
|
68
|
+
* code: string | number | undefined,
|
|
69
|
+
* message: string,
|
|
70
|
+
* httpStatusCode: number | undefined,
|
|
71
|
+
* httpStatusText: string | undefined,
|
|
72
|
+
* extendInfo?: object
|
|
73
|
+
* }}
|
|
74
|
+
*/
|
|
75
|
+
toJSON(): {
|
|
76
|
+
code: string | number | undefined;
|
|
77
|
+
message: string;
|
|
78
|
+
httpStatusCode: number | undefined;
|
|
79
|
+
httpStatusText: string | undefined;
|
|
80
|
+
extendInfo?: object;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export function get({ url, headers, expectedType, }: {
|
|
2
|
+
url: any;
|
|
3
|
+
headers?: {};
|
|
4
|
+
expectedType?: "json";
|
|
5
|
+
}): Promise<any>;
|
|
6
|
+
export function post({ url, body, headers, expectedType, }: {
|
|
7
|
+
url: any;
|
|
8
|
+
body: any;
|
|
9
|
+
headers?: {};
|
|
10
|
+
expectedType?: "json";
|
|
11
|
+
}): Promise<any>;
|
|
12
|
+
export function put({ url, body, headers, expectedType, }: {
|
|
13
|
+
url: any;
|
|
14
|
+
body: any;
|
|
15
|
+
headers?: {};
|
|
16
|
+
expectedType?: "json";
|
|
17
|
+
}): Promise<any>;
|
|
18
|
+
export function patch({ url, body, headers, expectedType, }: {
|
|
19
|
+
url: any;
|
|
20
|
+
body: any;
|
|
21
|
+
headers?: {};
|
|
22
|
+
expectedType?: "json";
|
|
23
|
+
}): Promise<any>;
|
|
24
|
+
export function deleteApi({ url, body, headers, expectedType, }: {
|
|
25
|
+
url: any;
|
|
26
|
+
body: any;
|
|
27
|
+
headers?: {};
|
|
28
|
+
expectedType?: "json";
|
|
29
|
+
}): Promise<any>;
|
|
30
|
+
export namespace http {
|
|
31
|
+
export { get };
|
|
32
|
+
export { put };
|
|
33
|
+
export { post };
|
|
34
|
+
export { patch };
|
|
35
|
+
export { deleteApi };
|
|
36
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* *
|
|
3
|
+
*/
|
|
4
|
+
export type ResponseType = string;
|
|
5
|
+
/**
|
|
6
|
+
* Enum representing supported response types for HTTP client parsing.
|
|
7
|
+
*
|
|
8
|
+
* @readonly
|
|
9
|
+
* @enum {string}
|
|
10
|
+
* @property {string} xml - XML response (parsed using xml2js).
|
|
11
|
+
* @property {string} json - JSON response (parsed via JSON.parse).
|
|
12
|
+
* @property {string} text - Plain text response.
|
|
13
|
+
* @property {string} file - Binary file or blob (not automatically parsed).
|
|
14
|
+
*/
|
|
15
|
+
export const ResponseType: Readonly<{
|
|
16
|
+
xml: "xml";
|
|
17
|
+
json: "json";
|
|
18
|
+
text: "text";
|
|
19
|
+
file: "file";
|
|
20
|
+
}>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function generateId(): string;
|
|
2
|
+
export function generatePrefixedId(prefix: string): string;
|
|
3
|
+
export function generateUserId(): string;
|
|
4
|
+
export function generateTenantId(): string;
|
|
5
|
+
export function generatePermissionId(): string;
|
|
6
|
+
export function generateCorrelationId(): string;
|
|
7
|
+
export function generateVerificationId(): string;
|
|
8
|
+
export function generateRolePermissionsId(): string;
|
|
9
|
+
export function generateOnboardingId(): string;
|
|
10
|
+
export function generateSessionId(): string;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mapping of entity types to their unique ID prefixes.
|
|
3
|
+
*
|
|
4
|
+
* These prefixes are prepended to ULIDs to create consistent and identifiable IDs across the system.
|
|
5
|
+
* For example: 'usr_01HZY3M7K4FJ9A8Q4Y1ZB5NX3T'
|
|
6
|
+
*/
|
|
7
|
+
export type ID_PREFIXES = string;
|
|
8
|
+
/**
|
|
9
|
+
* Mapping of entity types to their unique ID prefixes.
|
|
10
|
+
*
|
|
11
|
+
* These prefixes are prepended to ULIDs to create consistent and identifiable IDs across the system.
|
|
12
|
+
* For example: 'usr_01HZY3M7K4FJ9A8Q4Y1ZB5NX3T'
|
|
13
|
+
*
|
|
14
|
+
* @readonly
|
|
15
|
+
* @enum {string}
|
|
16
|
+
*/
|
|
17
|
+
export const ID_PREFIXES: Readonly<{
|
|
18
|
+
/** User entity ID prefix */
|
|
19
|
+
USER: "usr";
|
|
20
|
+
/** Tenant entity ID prefix */
|
|
21
|
+
TENANT: "tnt";
|
|
22
|
+
/** Permission entity ID prefix */
|
|
23
|
+
PERMISSION: "prm";
|
|
24
|
+
/** Correlation ID prefix (e.g., for tracing requests) */
|
|
25
|
+
CORRELATION: "crln";
|
|
26
|
+
/** Verification entity ID prefix (e.g., email/phone code) */
|
|
27
|
+
VERIFICATION: "vrf";
|
|
28
|
+
/** Role-permissions mapping ID prefix */
|
|
29
|
+
ROLE_PERMISSIONS: "role";
|
|
30
|
+
/** Onboarding mapping ID prefix */
|
|
31
|
+
ONBOARDING: "onb";
|
|
32
|
+
/** Session mapping ID prefix */
|
|
33
|
+
SESSION: "sess";
|
|
34
|
+
}>;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from "./core/index.js";
|
|
2
|
+
export * from "./crypto/index.js";
|
|
3
|
+
export * from "./fastify/index.js";
|
|
4
|
+
export * from "./http/index.js";
|
|
5
|
+
export * from "./ids/index.js";
|
|
6
|
+
export * from "./mongodb/index.js";
|
|
7
|
+
export * from "./logger/index.js";
|
|
8
|
+
export * from "./mailer/index.js";
|
|
9
|
+
export * from "./rabbit-mq/index.js";
|
|
10
|
+
export * from "./templates/index.js";
|
|
11
|
+
export * from "./util/index.js";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export function getLogger(option: true | any | undefined): import("pino").Logger | typeof dummyLogger;
|
|
2
|
+
declare namespace dummyLogger {
|
|
3
|
+
function info(): void;
|
|
4
|
+
function warn(): void;
|
|
5
|
+
function trace(): void;
|
|
6
|
+
function debug(): void;
|
|
7
|
+
function error(): void;
|
|
8
|
+
function fatal(): void;
|
|
9
|
+
function levels(): void;
|
|
10
|
+
function silent(): void;
|
|
11
|
+
function child(): {
|
|
12
|
+
info: () => void;
|
|
13
|
+
warn: () => void;
|
|
14
|
+
trace: () => void;
|
|
15
|
+
debug: () => void;
|
|
16
|
+
error: () => void;
|
|
17
|
+
fatal: () => void;
|
|
18
|
+
levels: () => void;
|
|
19
|
+
silent: () => void;
|
|
20
|
+
child(): /*elided*/ any;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./get-logger.js";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export class Mailer {
|
|
2
|
+
/**
|
|
3
|
+
* @param {object} transporter - Nodemailer transporter instance
|
|
4
|
+
*/
|
|
5
|
+
constructor(transporter: object);
|
|
6
|
+
transporter: any;
|
|
7
|
+
/**
|
|
8
|
+
* Send an email
|
|
9
|
+
*/
|
|
10
|
+
send({ to, subject, html, text, from, cc, bcc, replyTo, attachments }: {
|
|
11
|
+
to: any;
|
|
12
|
+
subject: any;
|
|
13
|
+
html: any;
|
|
14
|
+
text: any;
|
|
15
|
+
from: any;
|
|
16
|
+
cc: any;
|
|
17
|
+
bcc: any;
|
|
18
|
+
replyTo: any;
|
|
19
|
+
attachments: any;
|
|
20
|
+
}): Promise<any>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Factory for creating email transporters based on configuration.
|
|
3
|
+
*/
|
|
4
|
+
export class TransportFactory {
|
|
5
|
+
/**
|
|
6
|
+
* Create a Nodemailer transporter
|
|
7
|
+
*
|
|
8
|
+
* @param {object} config - Transport configuration object
|
|
9
|
+
* @param {'smtp' | 'gmail' | 'sendgrid' | 'ses'} config.type - Type of email transport
|
|
10
|
+
*
|
|
11
|
+
* For type 'smtp':
|
|
12
|
+
* @param {string} config.host - SMTP server host
|
|
13
|
+
* @param {number} config.port - SMTP server port
|
|
14
|
+
* @param {boolean} config.secure - Use TLS
|
|
15
|
+
* @param {{ user: string, pass: string }} config.auth - SMTP auth credentials
|
|
16
|
+
*
|
|
17
|
+
* For type 'gmail':
|
|
18
|
+
* @param {{ user: string, pass: string }} config.auth - Gmail credentials
|
|
19
|
+
*
|
|
20
|
+
* For type 'sendgrid':
|
|
21
|
+
* @param {string} config.apiKey - SendGrid API key
|
|
22
|
+
*
|
|
23
|
+
* For type 'ses':
|
|
24
|
+
* @param {string} config.accessKeyId - AWS access key
|
|
25
|
+
* @param {string} config.secretAccessKey - AWS secret
|
|
26
|
+
* @param {string} config.region - AWS region
|
|
27
|
+
*
|
|
28
|
+
* @returns {import('nodemailer').Transporter | typeof import('@sendgrid/mail')}
|
|
29
|
+
*/
|
|
30
|
+
static create(config: {
|
|
31
|
+
type: "smtp" | "gmail" | "sendgrid" | "ses";
|
|
32
|
+
host: string;
|
|
33
|
+
port: number;
|
|
34
|
+
secure: boolean;
|
|
35
|
+
auth: {
|
|
36
|
+
user: string;
|
|
37
|
+
pass: string;
|
|
38
|
+
};
|
|
39
|
+
auth: {
|
|
40
|
+
user: string;
|
|
41
|
+
pass: string;
|
|
42
|
+
};
|
|
43
|
+
apiKey: string;
|
|
44
|
+
accessKeyId: string;
|
|
45
|
+
secretAccessKey: string;
|
|
46
|
+
region: string;
|
|
47
|
+
}): any | typeof import("@sendgrid/mail");
|
|
48
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function initializeMongoDb({ config, collectionNames }: {
|
|
2
|
+
config: {
|
|
3
|
+
uri: string;
|
|
4
|
+
options: {
|
|
5
|
+
dbName: string;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
collectionNames: Record<string, string>;
|
|
9
|
+
}): Promise<Record<string, import("mongodb").Collection> & {
|
|
10
|
+
withTransaction: <T>(action: ({
|
|
11
|
+
session: import("mongodb").ClientSession;
|
|
12
|
+
})) => Promise<T>;
|
|
13
|
+
}>;
|