@devorash/core 0.1.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/LICENSE +21 -0
- package/README.md +187 -0
- package/dist/constants/api-url.gen.d.ts +4 -0
- package/dist/constants/api-url.gen.d.ts.map +1 -0
- package/dist/constants/api-url.gen.example.d.ts +7 -0
- package/dist/constants/api-url.gen.example.d.ts.map +1 -0
- package/dist/constants/api-url.gen.example.js +7 -0
- package/dist/constants/api-url.gen.example.js.map +1 -0
- package/dist/constants/api-url.gen.js +4 -0
- package/dist/constants/api-url.gen.js.map +1 -0
- package/dist/constants/index.d.ts +203 -0
- package/dist/constants/index.d.ts.map +1 -0
- package/dist/constants/index.js +236 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/constants/sessionLifecycle.d.ts +22 -0
- package/dist/constants/sessionLifecycle.d.ts.map +1 -0
- package/dist/constants/sessionLifecycle.js +21 -0
- package/dist/constants/sessionLifecycle.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +72 -0
- package/dist/index.js.map +1 -0
- package/dist/security/index.d.ts +118 -0
- package/dist/security/index.d.ts.map +1 -0
- package/dist/security/index.js +345 -0
- package/dist/security/index.js.map +1 -0
- package/dist/security/request-body.d.ts +16 -0
- package/dist/security/request-body.d.ts.map +1 -0
- package/dist/security/request-body.js +84 -0
- package/dist/security/request-body.js.map +1 -0
- package/dist/security/signing.d.ts +127 -0
- package/dist/security/signing.d.ts.map +1 -0
- package/dist/security/signing.js +297 -0
- package/dist/security/signing.js.map +1 -0
- package/dist/types/index.d.ts +487 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +19 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/index.d.ts +211 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +600 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +53 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for Devora SDKs
|
|
3
|
+
* @module @devorash/core/utils
|
|
4
|
+
*/
|
|
5
|
+
import type { DevoraResponse, ImpersonationScope, LogEvent, SDKInfo } from "../types/index.js";
|
|
6
|
+
/**
|
|
7
|
+
* Create a successful SDK response
|
|
8
|
+
*/
|
|
9
|
+
export declare function createSuccessResponse<T>(data: T): DevoraResponse<T>;
|
|
10
|
+
/**
|
|
11
|
+
* Create an error SDK response
|
|
12
|
+
*/
|
|
13
|
+
export declare function createErrorResponse(error: string, errorCode?: string): DevoraResponse<never>;
|
|
14
|
+
/**
|
|
15
|
+
* Extract URL parameters.
|
|
16
|
+
*/
|
|
17
|
+
export declare function extractUrlParams(url: string): Record<string, string | undefined>;
|
|
18
|
+
/**
|
|
19
|
+
* Clean impersonation parameters from URL
|
|
20
|
+
* Handles both absolute and relative URLs
|
|
21
|
+
*/
|
|
22
|
+
export declare function cleanImpersonationParams(url: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Match a path pattern against an actual path
|
|
25
|
+
* Supports :param patterns (e.g., /impersonate/:id)
|
|
26
|
+
*/
|
|
27
|
+
export declare function matchPath(pattern: string, path: string): {
|
|
28
|
+
match: boolean;
|
|
29
|
+
params: Record<string, string>;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Extract path parameters from a matched route
|
|
33
|
+
*/
|
|
34
|
+
export declare function extractPathParams(pattern: string, path: string): Record<string, string>;
|
|
35
|
+
/**
|
|
36
|
+
* Check if a scope allows write operations
|
|
37
|
+
*/
|
|
38
|
+
export declare function scopeAllowsWrite(scope: ImpersonationScope): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Get human-readable scope description
|
|
41
|
+
*/
|
|
42
|
+
export declare function getScopeDescription(scope: ImpersonationScope): string;
|
|
43
|
+
/**
|
|
44
|
+
* Create a log event
|
|
45
|
+
*/
|
|
46
|
+
export declare function createLogEvent(type: LogEvent["type"], sessionId: string, options?: Partial<Omit<LogEvent, "type" | "sessionId" | "timestamp">>): LogEvent;
|
|
47
|
+
/**
|
|
48
|
+
* Create SDK info object
|
|
49
|
+
*/
|
|
50
|
+
export declare function createSDKInfo(name: string, type: SDKInfo["type"], runtime?: SDKInfo["runtime"]): SDKInfo;
|
|
51
|
+
/**
|
|
52
|
+
* Log levels in order of severity (lower = more verbose)
|
|
53
|
+
*/
|
|
54
|
+
export declare enum LogLevel {
|
|
55
|
+
DEBUG = 0,
|
|
56
|
+
INFO = 1,
|
|
57
|
+
WARN = 2,
|
|
58
|
+
ERROR = 3,
|
|
59
|
+
SILENT = 4
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Logger configuration options
|
|
63
|
+
*/
|
|
64
|
+
export interface LoggerConfig {
|
|
65
|
+
/** Minimum log level to output (default: WARN in production, DEBUG in development) */
|
|
66
|
+
level?: LogLevel;
|
|
67
|
+
/** Custom prefix for log messages (default: "Devora") */
|
|
68
|
+
prefix?: string;
|
|
69
|
+
/** Enable/disable logging entirely (overrides level) */
|
|
70
|
+
enabled?: boolean;
|
|
71
|
+
/** Include timestamp in log messages */
|
|
72
|
+
includeTimestamp?: boolean;
|
|
73
|
+
/** Custom log handler (for testing or custom logging backends) */
|
|
74
|
+
handler?: LogHandler;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Custom log handler interface
|
|
78
|
+
*/
|
|
79
|
+
export interface LogHandler {
|
|
80
|
+
debug: (...args: unknown[]) => void;
|
|
81
|
+
info: (...args: unknown[]) => void;
|
|
82
|
+
warn: (...args: unknown[]) => void;
|
|
83
|
+
error: (...args: unknown[]) => void;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Logger instance interface
|
|
87
|
+
*/
|
|
88
|
+
export interface Logger {
|
|
89
|
+
debug: (...args: unknown[]) => void;
|
|
90
|
+
log: (...args: unknown[]) => void;
|
|
91
|
+
info: (...args: unknown[]) => void;
|
|
92
|
+
warn: (...args: unknown[]) => void;
|
|
93
|
+
error: (...args: unknown[]) => void;
|
|
94
|
+
setLevel: (level: LogLevel) => void;
|
|
95
|
+
setEnabled: (enabled: boolean) => void;
|
|
96
|
+
isEnabled: () => boolean;
|
|
97
|
+
getLevel: () => LogLevel;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Configure the global logger settings.
|
|
101
|
+
* Call this once at your application's entry point.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* import { configureLogger, LogLevel } from "@devorash/core"
|
|
106
|
+
*
|
|
107
|
+
* // Enable verbose logging in development
|
|
108
|
+
* configureLogger({
|
|
109
|
+
* level: LogLevel.DEBUG,
|
|
110
|
+
* enabled: true,
|
|
111
|
+
* includeTimestamp: true,
|
|
112
|
+
* })
|
|
113
|
+
*
|
|
114
|
+
* // Disable all logging in production
|
|
115
|
+
* configureLogger({
|
|
116
|
+
* enabled: false,
|
|
117
|
+
* })
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export declare function configureLogger(config: Partial<LoggerConfig>): void;
|
|
121
|
+
/**
|
|
122
|
+
* Get the current global logger configuration
|
|
123
|
+
*/
|
|
124
|
+
export declare function getLoggerConfig(): LoggerConfig;
|
|
125
|
+
/**
|
|
126
|
+
* Reset logger configuration to defaults
|
|
127
|
+
*/
|
|
128
|
+
export declare function resetLoggerConfig(): void;
|
|
129
|
+
/**
|
|
130
|
+
* Create a logger instance with the given options.
|
|
131
|
+
*
|
|
132
|
+
* @param prefix - Prefix for log messages (e.g., "Devora SDK", "Devora React")
|
|
133
|
+
* @param enabled - Override to enable/disable this specific logger
|
|
134
|
+
* @returns Logger instance
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const logger = createLogger("MyComponent")
|
|
139
|
+
* logger.debug("Initializing...")
|
|
140
|
+
* logger.info("Ready")
|
|
141
|
+
* logger.warn("Something unexpected happened")
|
|
142
|
+
* logger.error("Failed!", error)
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
export declare function createLogger(prefix?: string, enabled?: boolean): Logger;
|
|
146
|
+
/**
|
|
147
|
+
* Validate API key format
|
|
148
|
+
*/
|
|
149
|
+
export declare function validateApiKeyFormat(apiKey: string): {
|
|
150
|
+
valid: boolean;
|
|
151
|
+
type?: "server" | "client";
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Validate organization ID format
|
|
155
|
+
*/
|
|
156
|
+
export declare function validateOrgIdFormat(orgId: string): boolean;
|
|
157
|
+
/**
|
|
158
|
+
* Get header value from headers object (handles array values and case-insensitive lookup)
|
|
159
|
+
*/
|
|
160
|
+
export declare function getHeaderValue(headers: Record<string, string | string[] | undefined>, key: string): string | undefined;
|
|
161
|
+
/**
|
|
162
|
+
* Get HTTP status code from Devora error code
|
|
163
|
+
*/
|
|
164
|
+
export declare function getErrorStatusCode(errorCode?: string): number;
|
|
165
|
+
/**
|
|
166
|
+
* Simple rate limiter for SDK API calls
|
|
167
|
+
*/
|
|
168
|
+
export declare class RateLimiter {
|
|
169
|
+
private maxRequests;
|
|
170
|
+
private windowMs;
|
|
171
|
+
private requests;
|
|
172
|
+
constructor(maxRequests?: number, windowMs?: number);
|
|
173
|
+
/**
|
|
174
|
+
* Check if a request is allowed
|
|
175
|
+
*/
|
|
176
|
+
isAllowed(): boolean;
|
|
177
|
+
/**
|
|
178
|
+
* Get remaining requests in current window
|
|
179
|
+
*/
|
|
180
|
+
getRemaining(): number;
|
|
181
|
+
/**
|
|
182
|
+
* Reset the rate limiter
|
|
183
|
+
*/
|
|
184
|
+
reset(): void;
|
|
185
|
+
/**
|
|
186
|
+
* Wait until a request is allowed
|
|
187
|
+
* Uses iterative approach to avoid stack overflow under sustained load
|
|
188
|
+
*/
|
|
189
|
+
waitForSlot(): Promise<void>;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Safely parse JSON
|
|
193
|
+
*/
|
|
194
|
+
export declare function safeJsonParse<T>(json: string, fallback: T): T;
|
|
195
|
+
/**
|
|
196
|
+
* Safely stringify JSON
|
|
197
|
+
*/
|
|
198
|
+
export declare function safeJsonStringify(value: unknown): string;
|
|
199
|
+
/**
|
|
200
|
+
* Deep freeze an object (make immutable)
|
|
201
|
+
*/
|
|
202
|
+
export declare function deepFreeze<T extends object>(obj: T): Readonly<T>;
|
|
203
|
+
/**
|
|
204
|
+
* Create a deferred promise
|
|
205
|
+
*/
|
|
206
|
+
export declare function createDeferred<T>(): {
|
|
207
|
+
promise: Promise<T>;
|
|
208
|
+
resolve: (value: T) => void;
|
|
209
|
+
reject: (reason?: unknown) => void;
|
|
210
|
+
};
|
|
211
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAO9F;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,CAMnE;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,cAAc,CAAC,KAAK,CAAC,CAO5F;AAMD;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAahF;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAoC5D;AAMD;;;GAGG;AACH,wBAAgB,SAAS,CACxB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,GACV;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC,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