@devrev/ts-adaas 1.17.1-beta.1 → 1.17.1-beta.3
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/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/mock-server/mock-server.d.ts +80 -0
- package/dist/mock-server/mock-server.d.ts.map +1 -0
- package/dist/mock-server/mock-server.interfaces.d.ts +57 -0
- package/dist/mock-server/mock-server.interfaces.d.ts.map +1 -0
- package/dist/mock-server/mock-server.interfaces.js +4 -0
- package/dist/mock-server/mock-server.js +271 -0
- package/package.json +4 -2
package/dist/index.d.ts
CHANGED
|
@@ -12,5 +12,5 @@ export { spawn } from './multithreading/spawn/spawn';
|
|
|
12
12
|
export { WorkerAdapter } from './multithreading/worker-adapter/worker-adapter';
|
|
13
13
|
export * from './types/workers';
|
|
14
14
|
export { formatAxiosError, serializeAxiosError } from './logger/logger';
|
|
15
|
-
export { MockServer } from './
|
|
15
|
+
export { MockServer } from './mock-server/mock-server';
|
|
16
16
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AAEtC,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AAExB,cAAc,yCAAyC,CAAC;AAExD,OAAO,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAE7D,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,KAAK,EAAE,MAAM,8BAA8B,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,gDAAgD,CAAC;AAE/E,cAAc,iBAAiB,CAAC;AAEhC,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAExE,OAAO,EAAE,UAAU,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AAEtC,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AAExB,cAAc,yCAAyC,CAAC;AAExD,OAAO,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAE7D,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,KAAK,EAAE,MAAM,8BAA8B,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,gDAAgD,CAAC;AAE/E,cAAc,iBAAiB,CAAC;AAEhC,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAExE,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -34,5 +34,5 @@ __exportStar(require("./types/workers"), exports);
|
|
|
34
34
|
var logger_1 = require("./logger/logger");
|
|
35
35
|
Object.defineProperty(exports, "formatAxiosError", { enumerable: true, get: function () { return logger_1.formatAxiosError; } });
|
|
36
36
|
Object.defineProperty(exports, "serializeAxiosError", { enumerable: true, get: function () { return logger_1.serializeAxiosError; } });
|
|
37
|
-
var mock_server_1 = require("./
|
|
37
|
+
var mock_server_1 = require("./mock-server/mock-server");
|
|
38
38
|
Object.defineProperty(exports, "MockServer", { enumerable: true, get: function () { return mock_server_1.MockServer; } });
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { RequestInfo, RouteConfig } from './mock-server.interfaces';
|
|
2
|
+
/**
|
|
3
|
+
* MockServer used in tests to mock internal AirSync endpoints.
|
|
4
|
+
* This is a simple mock server that listens on a port and responds to requests.
|
|
5
|
+
* Supports per-test route configuration to simulate different response scenarios.
|
|
6
|
+
*/
|
|
7
|
+
export declare class MockServer {
|
|
8
|
+
private app;
|
|
9
|
+
private server;
|
|
10
|
+
private internalPort;
|
|
11
|
+
port: number;
|
|
12
|
+
baseUrl: string;
|
|
13
|
+
private routeHandlers;
|
|
14
|
+
private requests;
|
|
15
|
+
private requestCounts;
|
|
16
|
+
constructor(port?: number);
|
|
17
|
+
start(): Promise<void>;
|
|
18
|
+
stop(): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Sets up routes for the mock server.
|
|
21
|
+
*/
|
|
22
|
+
private setupRoutes;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a route handler that checks for custom handlers before using the default.
|
|
25
|
+
* @param method - The HTTP method
|
|
26
|
+
* @param path - The route path
|
|
27
|
+
* @returns A route handler function
|
|
28
|
+
*/
|
|
29
|
+
private routeHandler;
|
|
30
|
+
/**
|
|
31
|
+
* Default route handler for the mock server. Returns { success: true } for
|
|
32
|
+
* routes that are not explicitly set.
|
|
33
|
+
* @param req - The request object
|
|
34
|
+
* @param res - The response object
|
|
35
|
+
* @returns void
|
|
36
|
+
*/
|
|
37
|
+
private defaultRouteHandler;
|
|
38
|
+
/**
|
|
39
|
+
* Gets the route key for a given method and path.
|
|
40
|
+
* @param method - The HTTP method
|
|
41
|
+
* @param path - The route path
|
|
42
|
+
* @returns The route key in the format 'METHOD:path'
|
|
43
|
+
*/
|
|
44
|
+
private getRouteKey;
|
|
45
|
+
/**
|
|
46
|
+
* Configures a route to return a specific status code and optional response body.
|
|
47
|
+
* @param config - The route configuration object
|
|
48
|
+
* @param config.path - The path of the route (e.g., '/callback_url')
|
|
49
|
+
* @param config.method - The HTTP method (e.g., 'GET', 'POST')
|
|
50
|
+
* @param config.status - The HTTP status code to return (e.g., 200, 401, 500)
|
|
51
|
+
* @param config.body - Optional response body to send as JSON
|
|
52
|
+
* @param config.retry - Optional retry configuration for simulating failures before success
|
|
53
|
+
*/
|
|
54
|
+
setRoute(config: RouteConfig): void;
|
|
55
|
+
/**
|
|
56
|
+
* Resets all custom route handlers, restoring all default handlers.
|
|
57
|
+
* Also clears request tracking data.
|
|
58
|
+
*/
|
|
59
|
+
resetRoutes(): void;
|
|
60
|
+
/**
|
|
61
|
+
* Returns the most recent request or undefined if no requests exist.
|
|
62
|
+
* @returns The last RequestInfo object or undefined
|
|
63
|
+
*/
|
|
64
|
+
getLastRequest(): RequestInfo | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Gets the number of requests made to a specific endpoint.
|
|
67
|
+
* @param method - The HTTP method (e.g., 'GET', 'POST')
|
|
68
|
+
* @param path - The route path (e.g., '/test-endpoint', '/callback_url')
|
|
69
|
+
* @returns The number of requests made to the endpoint
|
|
70
|
+
*/
|
|
71
|
+
getRequestCount(method: string, path: string): number;
|
|
72
|
+
/**
|
|
73
|
+
* Gets all requests made to a specific endpoint.
|
|
74
|
+
* @param method - The HTTP method (e.g., 'GET', 'POST')
|
|
75
|
+
* @param path - The route path (e.g., '/test-endpoint', '/callback_url')
|
|
76
|
+
* @returns An array of RequestInfo objects for the endpoint
|
|
77
|
+
*/
|
|
78
|
+
getRequests(method: string, path: string): RequestInfo[];
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=mock-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mock-server.d.ts","sourceRoot":"","sources":["../../src/mock-server/mock-server.ts"],"names":[],"mappings":"AAGA,OAAO,EAGL,WAAW,EACX,WAAW,EAGZ,MAAM,0BAA0B,CAAC;AAElC;;;;GAIG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,GAAG,CAAU;IACrB,OAAO,CAAC,MAAM,CAAuB;IACrC,OAAO,CAAC,YAAY,CAAS;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IACvB,OAAO,CAAC,aAAa,CAA4B;IACjD,OAAO,CAAC,QAAQ,CAAqB;IACrC,OAAO,CAAC,aAAa,CAA4B;gBAErC,IAAI,GAAE,MAAiC;IAetC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAgBtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAkBlC;;OAEG;IACH,OAAO,CAAC,WAAW;IAsDnB;;;;;OAKG;IACH,OAAO,CAAC,YAAY;IAsBpB;;;;;;OAMG;IACH,OAAO,CAAC,mBAAmB;IA8B3B;;;;;OAKG;IACH,OAAO,CAAC,WAAW;IAInB;;;;;;;;OAQG;IACI,QAAQ,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI;IA2E1C;;;OAGG;IACI,WAAW,IAAI,IAAI;IAM1B;;;OAGG;IACI,cAAc,IAAI,WAAW,GAAG,SAAS;IAOhD;;;;;OAKG;IACI,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM;IAI5D;;;;;OAKG;IACI,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,WAAW,EAAE;CAShE"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Request, Response } from 'express';
|
|
2
|
+
export declare const DEFAULT_MOCK_SERVER_PORT = 3001;
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for retry simulation behavior.
|
|
5
|
+
*/
|
|
6
|
+
export interface RetryConfig {
|
|
7
|
+
/** Number of times to return error before succeeding (default: 4) */
|
|
8
|
+
failureCount?: number;
|
|
9
|
+
/** 5xx status code to return during failures (default: 500) */
|
|
10
|
+
errorStatus?: number;
|
|
11
|
+
/** Optional error response body to send as JSON during failures */
|
|
12
|
+
errorBody?: unknown;
|
|
13
|
+
/** Optional headers to send with the error response */
|
|
14
|
+
headers?: Record<string, string>;
|
|
15
|
+
/** Optional delay in milliseconds before sending each failure response */
|
|
16
|
+
delay?: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Configuration object for setting up a route response.
|
|
20
|
+
*/
|
|
21
|
+
export interface RouteConfig {
|
|
22
|
+
/** The path of the route (e.g., '/callback_url', '/worker_data_url.get') */
|
|
23
|
+
path: string;
|
|
24
|
+
/** The HTTP method (e.g., 'GET', 'POST', 'PUT', 'DELETE') */
|
|
25
|
+
method: string;
|
|
26
|
+
/** The HTTP status code to return (e.g., 200, 401, 500) */
|
|
27
|
+
status: number;
|
|
28
|
+
/** Optional response body to send as JSON */
|
|
29
|
+
body?: unknown;
|
|
30
|
+
/** Optional headers to send with the response */
|
|
31
|
+
headers?: Record<string, string>;
|
|
32
|
+
/** Optional retry configuration for simulating failures before success */
|
|
33
|
+
retry?: RetryConfig;
|
|
34
|
+
/** Optional delay in milliseconds before sending the response */
|
|
35
|
+
delay?: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Type for custom route handler functions.
|
|
39
|
+
*/
|
|
40
|
+
export type RouteHandler = (req: Request, res: Response) => unknown;
|
|
41
|
+
/**
|
|
42
|
+
* Information about a request received by the mock server.
|
|
43
|
+
*/
|
|
44
|
+
export interface RequestInfo {
|
|
45
|
+
/** The HTTP method (e.g., 'GET', 'POST') */
|
|
46
|
+
method: string;
|
|
47
|
+
/** The full URL path of the request */
|
|
48
|
+
url: string;
|
|
49
|
+
/** Optional request body (for POST/PUT requests) */
|
|
50
|
+
body?: unknown;
|
|
51
|
+
}
|
|
52
|
+
export type RouteHandlers = Map<string, RouteHandler>;
|
|
53
|
+
/**
|
|
54
|
+
* Type for tracking request counts per route.
|
|
55
|
+
*/
|
|
56
|
+
export type RequestCounts = Map<string, number>;
|
|
57
|
+
//# sourceMappingURL=mock-server.interfaces.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mock-server.interfaces.d.ts","sourceRoot":"","sources":["../../src/mock-server/mock-server.interfaces.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEjD,eAAO,MAAM,wBAAwB,OAAO,CAAC;AAE7C;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,qEAAqE;IACrE,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,+DAA+D;IAC/D,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,mEAAmE;IACnE,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,uDAAuD;IACvD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,0EAA0E;IAC1E,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,4EAA4E;IAC5E,IAAI,EAAE,MAAM,CAAC;IACb,6DAA6D;IAC7D,MAAM,EAAE,MAAM,CAAC;IACf,2DAA2D;IAC3D,MAAM,EAAE,MAAM,CAAC;IACf,6CAA6C;IAC7C,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,iDAAiD;IACjD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,0EAA0E;IAC1E,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,iEAAiE;IACjE,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,KAAK,OAAO,CAAC;AAEpE;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,4CAA4C;IAC5C,MAAM,EAAE,MAAM,CAAC;IACf,uCAAuC;IACvC,GAAG,EAAE,MAAM,CAAC;IACZ,oDAAoD;IACpD,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,MAAM,MAAM,aAAa,GAAG,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;AAEtD;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC"}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MockServer = void 0;
|
|
4
|
+
const mock_server_interfaces_1 = require("./mock-server.interfaces");
|
|
5
|
+
/**
|
|
6
|
+
* MockServer used in tests to mock internal AirSync endpoints.
|
|
7
|
+
* This is a simple mock server that listens on a port and responds to requests.
|
|
8
|
+
* Supports per-test route configuration to simulate different response scenarios.
|
|
9
|
+
*/
|
|
10
|
+
class MockServer {
|
|
11
|
+
constructor(port = mock_server_interfaces_1.DEFAULT_MOCK_SERVER_PORT) {
|
|
12
|
+
this.server = null;
|
|
13
|
+
this.routeHandlers = new Map();
|
|
14
|
+
this.requests = [];
|
|
15
|
+
this.requestCounts = new Map();
|
|
16
|
+
// Lazy-load express so that importing MockServer doesn't require express at module load time.
|
|
17
|
+
// This allows the SDK to be used in production (Lambda) without express installed.
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
19
|
+
const express = require('express');
|
|
20
|
+
this.internalPort = port;
|
|
21
|
+
this.port = port;
|
|
22
|
+
this.baseUrl = `http://localhost:${this.port}`;
|
|
23
|
+
this.app = express();
|
|
24
|
+
// Increase limit to handle size limit tests that send large artifact arrays
|
|
25
|
+
this.app.use(express.json({ limit: '10mb' }));
|
|
26
|
+
this.setupRoutes();
|
|
27
|
+
}
|
|
28
|
+
async start() {
|
|
29
|
+
return new Promise((resolve) => {
|
|
30
|
+
this.server = this.app.listen(this.internalPort, () => {
|
|
31
|
+
var _a, _b;
|
|
32
|
+
// Get the actual port assigned by the OS (important for port 0 = dynamic allocation)
|
|
33
|
+
const actualPort = (_b = (_a = this.server) === null || _a === void 0 ? void 0 : _a.address()) === null || _b === void 0 ? void 0 : _b.port;
|
|
34
|
+
if (actualPort) {
|
|
35
|
+
this.port = actualPort;
|
|
36
|
+
this.baseUrl = `http://localhost:${this.port}`;
|
|
37
|
+
}
|
|
38
|
+
console.log(`Mock server running on http://localhost:${this.port}.`);
|
|
39
|
+
resolve();
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async stop() {
|
|
44
|
+
if (!this.server) {
|
|
45
|
+
return Promise.resolve();
|
|
46
|
+
}
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
this.server.close((err) => {
|
|
49
|
+
if (err) {
|
|
50
|
+
reject(err);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
console.log('Mock server stopped.');
|
|
54
|
+
this.server = null;
|
|
55
|
+
resolve();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Sets up routes for the mock server.
|
|
62
|
+
*/
|
|
63
|
+
setupRoutes() {
|
|
64
|
+
// TEST ENDPOINT
|
|
65
|
+
this.app.get('/test-endpoint', this.routeHandler('GET', '/test-endpoint'));
|
|
66
|
+
// CALLBACK URL
|
|
67
|
+
this.app.post('/callback_url', this.routeHandler('POST', '/callback_url'));
|
|
68
|
+
// WORKER DATA URL
|
|
69
|
+
this.app.post('/worker_data_url', this.routeHandler('POST', '/worker_data_url'));
|
|
70
|
+
this.app.get('/worker_data_url.get', this.routeHandler('GET', '/worker_data_url.get'));
|
|
71
|
+
this.app.post('/worker_data_url.update', this.routeHandler('POST', '/worker_data_url.update'));
|
|
72
|
+
// SNAP-INS URL
|
|
73
|
+
this.app.get('/internal/snap-ins.get', this.routeHandler('GET', '/internal/snap-ins.get'));
|
|
74
|
+
// AIRDROP RECIPE INITIAL DOMAIN MAPPINGS INSTALL URL
|
|
75
|
+
this.app.post('/internal/airdrop.recipe.initial-domain-mappings.install', this.routeHandler('POST', '/internal/airdrop.recipe.initial-domain-mappings.install'));
|
|
76
|
+
// ARTIFACTS URL
|
|
77
|
+
this.app.get('/internal/airdrop.artifacts.upload-url', this.routeHandler('GET', '/internal/airdrop.artifacts.upload-url'));
|
|
78
|
+
this.app.post('/internal/airdrop.artifacts.confirm-upload', this.routeHandler('POST', '/internal/airdrop.artifacts.confirm-upload'));
|
|
79
|
+
// FILE UPLOAD URL
|
|
80
|
+
this.app.post('/file-upload-url', this.routeHandler('POST', '/file-upload-url'));
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Creates a route handler that checks for custom handlers before using the default.
|
|
84
|
+
* @param method - The HTTP method
|
|
85
|
+
* @param path - The route path
|
|
86
|
+
* @returns A route handler function
|
|
87
|
+
*/
|
|
88
|
+
routeHandler(method, path) {
|
|
89
|
+
return (req, res) => {
|
|
90
|
+
const requestInfo = Object.assign({ method: req.method, url: req.url || req.path }, (req.body !== undefined && req.body !== null
|
|
91
|
+
? { body: req.body }
|
|
92
|
+
: {}));
|
|
93
|
+
this.requests.push(requestInfo);
|
|
94
|
+
const key = `${method}:${path}`;
|
|
95
|
+
const customHandler = this.routeHandlers.get(key);
|
|
96
|
+
if (customHandler) {
|
|
97
|
+
customHandler(req, res);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
this.defaultRouteHandler(req, res);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Default route handler for the mock server. Returns { success: true } for
|
|
106
|
+
* routes that are not explicitly set.
|
|
107
|
+
* @param req - The request object
|
|
108
|
+
* @param res - The response object
|
|
109
|
+
* @returns void
|
|
110
|
+
*/
|
|
111
|
+
defaultRouteHandler(req, res) {
|
|
112
|
+
if (req.method === 'GET' && req.path === '/worker_data_url.get') {
|
|
113
|
+
res.status(200).json({
|
|
114
|
+
state: JSON.stringify({}),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
else if (req.method === 'GET' && req.path === '/internal/snap-ins.get') {
|
|
118
|
+
res.status(200).json({
|
|
119
|
+
snap_in: {
|
|
120
|
+
imports: [{ name: 'test_import_slug' }],
|
|
121
|
+
snap_in_version: { slug: 'test_snap_in_slug' },
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
else if (req.method === 'GET' &&
|
|
126
|
+
req.path === '/internal/airdrop.artifacts.upload-url') {
|
|
127
|
+
// Generate a unique artifact ID for each request
|
|
128
|
+
const artifactId = `artifact-${Date.now()}-${Math.random()
|
|
129
|
+
.toString(36)
|
|
130
|
+
.substr(2, 9)}`;
|
|
131
|
+
res.status(200).json({
|
|
132
|
+
upload_url: `${this.baseUrl}/file-upload-url`,
|
|
133
|
+
artifact_id: artifactId,
|
|
134
|
+
form_data: [], // Empty form_data for local development
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
res.status(200).json({ success: true });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Gets the route key for a given method and path.
|
|
143
|
+
* @param method - The HTTP method
|
|
144
|
+
* @param path - The route path
|
|
145
|
+
* @returns The route key in the format 'METHOD:path'
|
|
146
|
+
*/
|
|
147
|
+
getRouteKey(method, path) {
|
|
148
|
+
return `${method.toUpperCase()}:${path}`;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Configures a route to return a specific status code and optional response body.
|
|
152
|
+
* @param config - The route configuration object
|
|
153
|
+
* @param config.path - The path of the route (e.g., '/callback_url')
|
|
154
|
+
* @param config.method - The HTTP method (e.g., 'GET', 'POST')
|
|
155
|
+
* @param config.status - The HTTP status code to return (e.g., 200, 401, 500)
|
|
156
|
+
* @param config.body - Optional response body to send as JSON
|
|
157
|
+
* @param config.retry - Optional retry configuration for simulating failures before success
|
|
158
|
+
*/
|
|
159
|
+
setRoute(config) {
|
|
160
|
+
const { path, method, status, body, retry, headers, delay } = config;
|
|
161
|
+
const key = this.getRouteKey(method, path);
|
|
162
|
+
// Reset request count for this route if retry is configured
|
|
163
|
+
// This ensures a clean state each time setRoute is called
|
|
164
|
+
if (retry) {
|
|
165
|
+
this.requestCounts.set(key, 0);
|
|
166
|
+
}
|
|
167
|
+
this.routeHandlers.set(key, (req, res) => {
|
|
168
|
+
const sendResponse = (responseDelay) => {
|
|
169
|
+
const send = () => {
|
|
170
|
+
var _a, _b;
|
|
171
|
+
if (retry) {
|
|
172
|
+
const currentCount = this.requestCounts.get(key) || 0;
|
|
173
|
+
const failureCount = (_a = retry.failureCount) !== null && _a !== void 0 ? _a : 4;
|
|
174
|
+
const errorStatus = (_b = retry.errorStatus) !== null && _b !== void 0 ? _b : 500;
|
|
175
|
+
if (currentCount < failureCount) {
|
|
176
|
+
this.requestCounts.set(key, currentCount + 1);
|
|
177
|
+
const sendFailure = () => {
|
|
178
|
+
if (retry.headers) {
|
|
179
|
+
res.set(retry.headers);
|
|
180
|
+
}
|
|
181
|
+
if (retry.errorBody !== undefined) {
|
|
182
|
+
res.status(errorStatus).json(retry.errorBody);
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
res.status(errorStatus).send();
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
if (retry.delay) {
|
|
189
|
+
setTimeout(sendFailure, retry.delay);
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
sendFailure();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
this.requestCounts.set(key, currentCount + 1);
|
|
197
|
+
if (headers) {
|
|
198
|
+
res.set(headers);
|
|
199
|
+
}
|
|
200
|
+
if (body !== undefined) {
|
|
201
|
+
res.status(status).json(body);
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
this.defaultRouteHandler(req, res);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
if (headers) {
|
|
210
|
+
res.set(headers);
|
|
211
|
+
}
|
|
212
|
+
if (body !== undefined) {
|
|
213
|
+
res.status(status).json(body);
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
res.status(status).send();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
if (responseDelay) {
|
|
221
|
+
setTimeout(send, responseDelay);
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
send();
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
sendResponse(delay);
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Resets all custom route handlers, restoring all default handlers.
|
|
232
|
+
* Also clears request tracking data.
|
|
233
|
+
*/
|
|
234
|
+
resetRoutes() {
|
|
235
|
+
this.routeHandlers.clear();
|
|
236
|
+
this.requestCounts.clear();
|
|
237
|
+
this.requests = [];
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Returns the most recent request or undefined if no requests exist.
|
|
241
|
+
* @returns The last RequestInfo object or undefined
|
|
242
|
+
*/
|
|
243
|
+
getLastRequest() {
|
|
244
|
+
if (this.requests.length === 0) {
|
|
245
|
+
return undefined;
|
|
246
|
+
}
|
|
247
|
+
return this.requests[this.requests.length - 1];
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Gets the number of requests made to a specific endpoint.
|
|
251
|
+
* @param method - The HTTP method (e.g., 'GET', 'POST')
|
|
252
|
+
* @param path - The route path (e.g., '/test-endpoint', '/callback_url')
|
|
253
|
+
* @returns The number of requests made to the endpoint
|
|
254
|
+
*/
|
|
255
|
+
getRequestCount(method, path) {
|
|
256
|
+
return this.getRequests(method, path).length;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Gets all requests made to a specific endpoint.
|
|
260
|
+
* @param method - The HTTP method (e.g., 'GET', 'POST')
|
|
261
|
+
* @param path - The route path (e.g., '/test-endpoint', '/callback_url')
|
|
262
|
+
* @returns An array of RequestInfo objects for the endpoint
|
|
263
|
+
*/
|
|
264
|
+
getRequests(method, path) {
|
|
265
|
+
// Remove query parameters for comparison
|
|
266
|
+
const pathWithoutQuery = path.split('?')[0];
|
|
267
|
+
return this.requests.filter((req) => req.method.toUpperCase() === method.toUpperCase() &&
|
|
268
|
+
req.url.split('?')[0] === pathWithoutQuery);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
exports.MockServer = MockServer;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devrev/ts-adaas",
|
|
3
|
-
"version": "1.17.1-beta.
|
|
3
|
+
"version": "1.17.1-beta.3",
|
|
4
4
|
"description": "Typescript library containing the ADaaS(AirDrop as a Service) control protocol.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -38,7 +38,6 @@
|
|
|
38
38
|
"eslint": "9.32.0",
|
|
39
39
|
"eslint-config-prettier": "^9.1.2",
|
|
40
40
|
"eslint-plugin-prettier": "4.0.0",
|
|
41
|
-
"express": "^5.2.1",
|
|
42
41
|
"jest": "^29.7.0",
|
|
43
42
|
"jiti": "^2.6.1",
|
|
44
43
|
"prettier": "^2.8.3",
|
|
@@ -55,6 +54,9 @@
|
|
|
55
54
|
"ts-node": "^10.9.2",
|
|
56
55
|
"yargs": "^17.7.2"
|
|
57
56
|
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"express": "^4.21.0 || ^5.0.0"
|
|
59
|
+
},
|
|
58
60
|
"files": [
|
|
59
61
|
"dist/**/*",
|
|
60
62
|
"!dist/tests/**/*"
|