vault-sdk-prod 1.0.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 +333 -0
- package/index.js +3 -0
- package/package.json +29 -0
- package/src/Vault.js +1052 -0
- package/src/utils/validationError.js +109 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error class for validation errors in the Vault SDK.
|
|
3
|
+
* Thrown when method parameters fail type or presence checks.
|
|
4
|
+
*/
|
|
5
|
+
export class ValidationError extends Error {
|
|
6
|
+
constructor(operation, param, expectedType, message) {
|
|
7
|
+
super(
|
|
8
|
+
message ||
|
|
9
|
+
`[Vault SDK] Validation failed in '${operation}': Parameter '${param}' must be a non-empty ${expectedType}.`
|
|
10
|
+
);
|
|
11
|
+
this.name = "ValidationError";
|
|
12
|
+
this.code = "INVALID_PARAMETER";
|
|
13
|
+
this.operation = operation;
|
|
14
|
+
this.param = param;
|
|
15
|
+
this.expectedType = expectedType;
|
|
16
|
+
|
|
17
|
+
Error.captureStackTrace(this, ValidationError);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Custom error class for API/network errors from the Vault SDK.
|
|
23
|
+
* Wraps HTTP errors with status codes and structured error data.
|
|
24
|
+
*/
|
|
25
|
+
export class VaultError extends Error {
|
|
26
|
+
constructor(message, { status, code, operation, data } = {}) {
|
|
27
|
+
super(message);
|
|
28
|
+
this.name = "VaultError";
|
|
29
|
+
this.status = status || null;
|
|
30
|
+
this.code = code || "VAULT_ERROR";
|
|
31
|
+
this.operation = operation || null;
|
|
32
|
+
this.data = data || null;
|
|
33
|
+
|
|
34
|
+
Error.captureStackTrace(this, VaultError);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Maps HTTP status codes to user-friendly error descriptions.
|
|
40
|
+
*/
|
|
41
|
+
export const HTTP_ERROR_MAP = {
|
|
42
|
+
400: { code: "BAD_REQUEST", description: "The request was invalid. Check your parameters." },
|
|
43
|
+
401: { code: "UNAUTHORIZED", description: "Authentication failed. Verify your VAULT_ACCESS_KEY and VAULT_SECRET_KEY." },
|
|
44
|
+
403: { code: "FORBIDDEN", description: "Access denied. Your API key may not have permission for this operation." },
|
|
45
|
+
404: { code: "NOT_FOUND", description: "The requested resource was not found." },
|
|
46
|
+
409: { code: "CONFLICT", description: "A conflict occurred. The resource may already exist." },
|
|
47
|
+
413: { code: "FILE_TOO_LARGE", description: "The file exceeds the maximum allowed size." },
|
|
48
|
+
429: { code: "RATE_LIMITED", description: "Too many requests. Please wait and try again." },
|
|
49
|
+
500: { code: "SERVER_ERROR", description: "An internal server error occurred. Please try again later." },
|
|
50
|
+
502: { code: "BAD_GATEWAY", description: "The server is temporarily unavailable. Please try again later." },
|
|
51
|
+
503: { code: "SERVICE_UNAVAILABLE", description: "The service is temporarily unavailable. Please try again later." },
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const validator = {
|
|
55
|
+
types: {
|
|
56
|
+
string: (value) => typeof value === "string" && value.trim() !== "",
|
|
57
|
+
object: (value) => typeof value === "object" && value !== null,
|
|
58
|
+
array: (value) => Array.isArray(value) && value.length > 0,
|
|
59
|
+
number: (value) => typeof value === "number" && !isNaN(value) && value >= 0,
|
|
60
|
+
boolean: (value) => typeof value === "boolean",
|
|
61
|
+
function: (value) => typeof value === "function",
|
|
62
|
+
buffer: (value) => Buffer.isBuffer(value) || (value instanceof Uint8Array),
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
validate: function (params, operation) {
|
|
66
|
+
if (!operation) {
|
|
67
|
+
const stack = new Error().stack;
|
|
68
|
+
const stackLine = stack.split("\n")[2];
|
|
69
|
+
const methodMatch = stackLine.match(/at\s+(?:\w+\.)?(\w+)\s*\(/);
|
|
70
|
+
operation = methodMatch ? methodMatch[1] : "Unknown";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
Object.entries(params).forEach(([param, config]) => {
|
|
74
|
+
const { value, type, required = true, custom, message } = config;
|
|
75
|
+
|
|
76
|
+
if ((value === undefined || value === null) && !required) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if ((value === undefined || value === null) && required) {
|
|
81
|
+
throw new ValidationError(
|
|
82
|
+
operation,
|
|
83
|
+
param,
|
|
84
|
+
type,
|
|
85
|
+
message || `[Vault SDK] '${operation}' requires the '${param}' parameter.`
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const typeValidator = this.types[type];
|
|
90
|
+
if (typeValidator && !typeValidator(value)) {
|
|
91
|
+
throw new ValidationError(
|
|
92
|
+
operation,
|
|
93
|
+
param,
|
|
94
|
+
type,
|
|
95
|
+
message || `[Vault SDK] '${operation}': Parameter '${param}' must be a valid ${type}. Received: ${typeof value}.`
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (custom && typeof custom === "function" && !custom(value)) {
|
|
100
|
+
throw new ValidationError(
|
|
101
|
+
operation,
|
|
102
|
+
param,
|
|
103
|
+
type,
|
|
104
|
+
message || `[Vault SDK] '${operation}': Parameter '${param}' failed validation.`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
};
|