reqforge 0.9.0 → 0.11.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/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "reqforge",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "description": "A lightweight, flexible HTTP client library with request interception, automatic retry, and logging capabilities, designed for modern web applications.",
5
5
  "main": "src/index.js",
6
+ "types": "types/index.d.ts",
6
7
  "scripts": {
7
8
  "test": "echo \"No tests yet\" && exit 0"
8
9
  },
package/src/cache.js ADDED
@@ -0,0 +1,100 @@
1
+ /**
2
+ * Cache module for ReqForge
3
+ * @module cache
4
+ */
5
+
6
+ /**
7
+ * Simple in-memory cache with TTL support
8
+ */
9
+ class MemoryCache {
10
+ constructor(options = {}) {
11
+ this.store = new Map();
12
+ this.defaultTTL = options.defaultTTL || 60000;
13
+ }
14
+
15
+ /**
16
+ * Get value from cache
17
+ * @param {string} key - Cache key
18
+ * @returns {*|null} Cached value or null
19
+ */
20
+ get(key) {
21
+ const entry = this.store.get(key);
22
+ if (!entry) return null;
23
+
24
+ if (Date.now() > entry.expiry) {
25
+ this.store.delete(key);
26
+ return null;
27
+ }
28
+
29
+ return entry.value;
30
+ }
31
+
32
+ /**
33
+ * Set value in cache
34
+ * @param {string} key - Cache key
35
+ * @param {*} value - Value to cache
36
+ * @param {number} ttl - Time to live in ms
37
+ */
38
+ set(key, value, ttl) {
39
+ this.store.set(key, {
40
+ value,
41
+ expiry: Date.now() + (ttl || this.defaultTTL)
42
+ });
43
+ }
44
+
45
+ /**
46
+ * Check if key exists and is not expired
47
+ * @param {string} key - Cache key
48
+ * @returns {boolean} True if valid
49
+ */
50
+ has(key) {
51
+ return this.get(key) !== null;
52
+ }
53
+
54
+ /**
55
+ * Delete key from cache
56
+ * @param {string} key - Cache key
57
+ */
58
+ delete(key) {
59
+ this.store.delete(key);
60
+ }
61
+
62
+ /**
63
+ * Clear all cached entries
64
+ */
65
+ clear() {
66
+ this.store.clear();
67
+ }
68
+
69
+ /**
70
+ * Get cache size
71
+ * @returns {number} Number of entries
72
+ */
73
+ size() {
74
+ let count = 0;
75
+ this.store.forEach((entry, key) => {
76
+ if (Date.now() <= entry.expiry) {
77
+ count++;
78
+ } else {
79
+ this.store.delete(key);
80
+ }
81
+ });
82
+ return count;
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Create cache key from URL and params
88
+ * @param {string} url - Request URL
89
+ * @param {Object} params - Request params
90
+ * @returns {string} Cache key
91
+ */
92
+ function createCacheKey(url, params = {}) {
93
+ const normalized = { url, ...params };
94
+ return JSON.stringify(normalized);
95
+ }
96
+
97
+ module.exports = {
98
+ MemoryCache,
99
+ createCacheKey
100
+ };
package/src/index.js CHANGED
@@ -11,6 +11,7 @@ const auth = require('./auth');
11
11
  const errors = require('./errors');
12
12
  const retry = require('./retry');
13
13
  const interceptor = require('./interceptor');
14
+ const cache = require('./cache');
14
15
 
15
16
  module.exports = {
16
17
  config,
@@ -20,5 +21,6 @@ module.exports = {
20
21
  auth,
21
22
  errors,
22
23
  retry,
23
- interceptor
24
+ interceptor,
25
+ cache
24
26
  };
@@ -0,0 +1,118 @@
1
+ declare module 'reqforge' {
2
+ // Config
3
+ export const config: {
4
+ getBaseURL(): string;
5
+ API_VERSION: string;
6
+ defaultConfig: RequestConfig;
7
+ mergeConfig(customConfig?: Partial<RequestConfig>): RequestConfig;
8
+ };
9
+
10
+ // Request
11
+ export const request: {
12
+ sendRequest(endpoint: string, data?: any, options?: RequestOptions): Promise<Response>;
13
+ get(endpoint: string, options?: RequestOptions): Promise<Response>;
14
+ post(endpoint: string, data?: any, options?: RequestOptions): Promise<Response>;
15
+ put(endpoint: string, data?: any, options?: RequestOptions): Promise<Response>;
16
+ del(endpoint: string, options?: RequestOptions): Promise<Response>;
17
+ };
18
+
19
+ // Logger
20
+ export class Logger {
21
+ constructor(prefix?: string);
22
+ log(message: string): void;
23
+ info(message: string): void;
24
+ warn(message: string): void;
25
+ error(message: string): void;
26
+ }
27
+
28
+ // Auth
29
+ export const auth: {
30
+ login(username: string, password: string): Promise<any>;
31
+ logout(): Promise<void>;
32
+ getToken(): string | null;
33
+ setToken(token: string): void;
34
+ isAuthenticated(): boolean;
35
+ };
36
+
37
+ // Errors
38
+ export const errors: {
39
+ ReqForgeError: typeof ReqForgeError;
40
+ NetworkError: typeof NetworkError;
41
+ TimeoutError: typeof TimeoutError;
42
+ AuthError: typeof AuthError;
43
+ ValidationError: typeof ValidationError;
44
+ ErrorCodes: ErrorCodes;
45
+ createErrorFromResponse(response: Response): ReqForgeError;
46
+ };
47
+
48
+ // Retry
49
+ export const retry: {
50
+ withRetry<T>(fn: () => Promise<T>, options?: RetryOptions): Promise<T>;
51
+ calculateBackoff(attempt: number, baseDelay?: number, maxDelay?: number): number;
52
+ };
53
+
54
+ // Interceptor
55
+ export const interceptor: {
56
+ createRequestInterceptor(): InterceptorManager;
57
+ createResponseInterceptor(): InterceptorManager;
58
+ };
59
+
60
+ // Cache
61
+ export const cache: {
62
+ MemoryCache: typeof MemoryCache;
63
+ createCacheKey(url: string, params?: Record<string, any>): string;
64
+ };
65
+
66
+ // Types
67
+ interface RequestConfig {
68
+ baseURL: string;
69
+ timeout: number;
70
+ headers: Record<string, string>;
71
+ }
72
+
73
+ interface RequestOptions {
74
+ method?: string;
75
+ headers?: Record<string, string>;
76
+ timeout?: number;
77
+ }
78
+
79
+ interface RetryOptions {
80
+ maxRetries?: number;
81
+ delay?: number;
82
+ backoff?: number;
83
+ shouldRetry?: (error: Error) => boolean;
84
+ }
85
+
86
+ class ReqForgeError extends Error {
87
+ code: string;
88
+ }
89
+
90
+ class NetworkError extends ReqForgeError {}
91
+ class TimeoutError extends ReqForgeError {}
92
+ class AuthError extends ReqForgeError {}
93
+ class ValidationError extends ReqForgeError {}
94
+
95
+ interface ErrorCodes {
96
+ NETWORK_ERROR: string;
97
+ TIMEOUT_ERROR: string;
98
+ AUTH_ERROR: string;
99
+ VALIDATION_ERROR: string;
100
+ UNKNOWN_ERROR: string;
101
+ }
102
+
103
+ class InterceptorManager {
104
+ use(fulfilled: (data: any) => any, rejected?: (error: Error) => any): number;
105
+ eject(id: number): void;
106
+ clear(): void;
107
+ }
108
+
109
+ class MemoryCache {
110
+ constructor(options?: { defaultTTL?: number });
111
+ get(key: string): any | null;
112
+ set(key: string, value: any, ttl?: number): void;
113
+ has(key: string): boolean;
114
+ delete(key: string): void;
115
+ clear(): void;
116
+ size(): number;
117
+ }
118
+ }