@forklaunch/universal-sdk 0.1.1 → 0.2.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/lib/index.d.mts +3 -0
- package/lib/index.d.ts +3 -2
- package/lib/index.js +213 -16
- package/lib/index.mjs +190 -0
- package/package.json +12 -3
- package/lib/eslint.config.d.mts +0 -1134
- package/lib/eslint.config.d.mts.map +0 -1
- package/lib/eslint.config.mjs +0 -10
- package/lib/index.d.ts.map +0 -1
- package/lib/jest.config.d.ts +0 -4
- package/lib/jest.config.d.ts.map +0 -1
- package/lib/jest.config.js +0 -19
- package/lib/src/types/sdkTypes.d.ts +0 -25
- package/lib/src/types/sdkTypes.d.ts.map +0 -1
- package/lib/src/types/sdkTypes.js +0 -8
- package/lib/src/universalSdk.d.ts +0 -30
- package/lib/src/universalSdk.d.ts.map +0 -1
- package/lib/src/universalSdk.js +0 -73
- package/lib/src/utils/regex.d.ts +0 -9
- package/lib/src/utils/regex.d.ts.map +0 -1
- package/lib/src/utils/regex.js +0 -111
- package/lib/src/utils/resolvePath.d.ts +0 -9
- package/lib/src/utils/resolvePath.d.ts.map +0 -1
- package/lib/src/utils/resolvePath.js +0 -21
- package/lib/tests/universalSdk.test.d.ts +0 -2
- package/lib/tests/universalSdk.test.d.ts.map +0 -1
- package/lib/tests/universalSdk.test.js +0 -94
- package/lib/tsconfig.tsbuildinfo +0 -1
- package/lib/vitest.config.d.ts +0 -3
- package/lib/vitest.config.d.ts.map +0 -1
- package/lib/vitest.config.js +0 -7
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"eslint.config.d.mts","sourceRoot":"","sources":["../eslint.config.mjs"],"names":[],"mappings":""}
|
package/lib/eslint.config.mjs
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import pluginJs from '@eslint/js';
|
|
2
|
-
import globals from 'globals';
|
|
3
|
-
import tseslint from 'typescript-eslint';
|
|
4
|
-
export default [
|
|
5
|
-
{ files: ['**/*.{ts}'] },
|
|
6
|
-
{ ignores: ['tests/**/*', 'dist/**/*', 'lib/**/*', 'node_modules/**/*'] },
|
|
7
|
-
{ languageOptions: { globals: globals.browser } },
|
|
8
|
-
pluginJs.configs.recommended,
|
|
9
|
-
...tseslint.configs.recommended
|
|
10
|
-
];
|
package/lib/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,YAAY,GAAI,eAAe,QAAQ,MAAM,KAYhC,eACzB,CAAC"}
|
package/lib/jest.config.d.ts
DELETED
package/lib/jest.config.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"jest.config.d.ts","sourceRoot":"","sources":["../jest.config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAEpD,QAAA,MAAM,UAAU,EAAE,oBAiBjB,CAAC;AAEF,eAAe,UAAU,CAAC"}
|
package/lib/jest.config.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
const jestConfig = {
|
|
2
|
-
preset: 'ts-jest/presets/default-esm', // or other ESM presets
|
|
3
|
-
moduleNameMapper: {
|
|
4
|
-
'^(\\.{1,2}/.*)\\.js$': '$1'
|
|
5
|
-
},
|
|
6
|
-
transform: {
|
|
7
|
-
// '^.+\\.[tj]sx?$' to process ts,js,tsx,jsx with `ts-jest`
|
|
8
|
-
// '^.+\\.m?[tj]sx?$' to process ts,js,tsx,jsx,mts,mjs,mtsx,mjsx with `ts-jest`
|
|
9
|
-
'^.+\\.[tj]sx?$': [
|
|
10
|
-
'ts-jest',
|
|
11
|
-
{
|
|
12
|
-
useESM: true
|
|
13
|
-
}
|
|
14
|
-
],
|
|
15
|
-
'^.+\\.js$': 'babel-jest'
|
|
16
|
-
},
|
|
17
|
-
testPathIgnorePatterns: ['.*dist/', '.*node_modules/']
|
|
18
|
-
};
|
|
19
|
-
export default jestConfig;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef {Object} RequestType
|
|
3
|
-
* @property {Record<string, string | number | boolean>} [params] - URL parameters.
|
|
4
|
-
* @property {Record<string, unknown>} [body] - Request body.
|
|
5
|
-
* @property {Record<string, string | number | boolean>} [query] - Query parameters.
|
|
6
|
-
* @property {Record<string, string>} [headers] - Request headers.
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* @typedef {Object} ResponseType
|
|
10
|
-
* @property {number} code - The HTTP response code.
|
|
11
|
-
* @property {any} response - The response body.
|
|
12
|
-
* @property {Headers} headers - The response headers.
|
|
13
|
-
*/
|
|
14
|
-
export interface RequestType {
|
|
15
|
-
params?: Record<string, string | number | boolean>;
|
|
16
|
-
body?: Record<string, unknown>;
|
|
17
|
-
query?: Record<string, string | number | boolean>;
|
|
18
|
-
headers?: Record<string, string>;
|
|
19
|
-
}
|
|
20
|
-
export interface ResponseType {
|
|
21
|
-
code: number;
|
|
22
|
-
content: unknown;
|
|
23
|
-
headers: Headers;
|
|
24
|
-
}
|
|
25
|
-
//# sourceMappingURL=sdkTypes.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sdkTypes.d.ts","sourceRoot":"","sources":["../../../src/types/sdkTypes.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;;;;GAKG;AACH,MAAM,WAAW,WAAW;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC;IACnD,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC;IAClD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,OAAO,CAAC;CAClB"}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef {Object} RequestType
|
|
3
|
-
* @property {Record<string, string | number | boolean>} [params] - URL parameters.
|
|
4
|
-
* @property {Record<string, unknown>} [body] - Request body.
|
|
5
|
-
* @property {Record<string, string | number | boolean>} [query] - Query parameters.
|
|
6
|
-
* @property {Record<string, string>} [headers] - Request headers.
|
|
7
|
-
*/
|
|
8
|
-
export {};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { RequestType, ResponseType } from './types/sdkTypes';
|
|
2
|
-
/**
|
|
3
|
-
* A class representing the Forklaunch SDK.
|
|
4
|
-
*/
|
|
5
|
-
export declare class UniversalSdk {
|
|
6
|
-
private host;
|
|
7
|
-
/**
|
|
8
|
-
* Creates an instance of UniversalSdk.
|
|
9
|
-
*
|
|
10
|
-
* @param {string} host - The host URL for the SDK.
|
|
11
|
-
*/
|
|
12
|
-
constructor(host: string);
|
|
13
|
-
/**
|
|
14
|
-
* Executes an HTTP request.
|
|
15
|
-
*
|
|
16
|
-
* @param {string} route - The route path for the request.
|
|
17
|
-
* @param {'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'} method - The HTTP method.
|
|
18
|
-
* @param {RequestType} [request] - The request object.
|
|
19
|
-
* @returns {Promise<ResponseType>} - The response object.
|
|
20
|
-
*/
|
|
21
|
-
private execute;
|
|
22
|
-
pathParamRequest(route: string, method: 'GET' | 'DELETE', request?: RequestType): Promise<ResponseType>;
|
|
23
|
-
bodyRequest(route: string, method: 'POST' | 'PUT' | 'PATCH', request?: RequestType): Promise<ResponseType>;
|
|
24
|
-
get(route: string, request?: RequestType): Promise<ResponseType>;
|
|
25
|
-
post(route: string, request?: RequestType): Promise<ResponseType>;
|
|
26
|
-
put(route: string, request?: RequestType): Promise<ResponseType>;
|
|
27
|
-
patch(route: string, request?: RequestType): Promise<ResponseType>;
|
|
28
|
-
delete(route: string, request?: RequestType): Promise<ResponseType>;
|
|
29
|
-
}
|
|
30
|
-
//# sourceMappingURL=universalSdk.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"universalSdk.d.ts","sourceRoot":"","sources":["../../src/universalSdk.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAG7D;;GAEG;AACH,qBAAa,YAAY;IAMX,OAAO,CAAC,IAAI;IALxB;;;;OAIG;gBACiB,IAAI,EAAE,MAAM;IAEhC;;;;;;;OAOG;YACW,OAAO;IA0Cf,gBAAgB,CACpB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,KAAK,GAAG,QAAQ,EACxB,OAAO,CAAC,EAAE,WAAW;IAKjB,WAAW,CACf,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,GAAG,KAAK,GAAG,OAAO,EAChC,OAAO,CAAC,EAAE,WAAW;IAKjB,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW;IAIxC,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW;IAIzC,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW;IAIxC,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW;IAI1C,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW;CAGlD"}
|
package/lib/src/universalSdk.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { getSdkPath } from './utils/resolvePath';
|
|
2
|
-
/**
|
|
3
|
-
* A class representing the Forklaunch SDK.
|
|
4
|
-
*/
|
|
5
|
-
export class UniversalSdk {
|
|
6
|
-
host;
|
|
7
|
-
/**
|
|
8
|
-
* Creates an instance of UniversalSdk.
|
|
9
|
-
*
|
|
10
|
-
* @param {string} host - The host URL for the SDK.
|
|
11
|
-
*/
|
|
12
|
-
constructor(host) {
|
|
13
|
-
this.host = host;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Executes an HTTP request.
|
|
17
|
-
*
|
|
18
|
-
* @param {string} route - The route path for the request.
|
|
19
|
-
* @param {'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'} method - The HTTP method.
|
|
20
|
-
* @param {RequestType} [request] - The request object.
|
|
21
|
-
* @returns {Promise<ResponseType>} - The response object.
|
|
22
|
-
*/
|
|
23
|
-
async execute(route, method, request) {
|
|
24
|
-
const { params, body, query, headers } = request || {};
|
|
25
|
-
let url = getSdkPath(this.host + route);
|
|
26
|
-
if (params) {
|
|
27
|
-
for (const key in params) {
|
|
28
|
-
url = url.replace(`:${key}`, encodeURIComponent(params[key]));
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
if (query) {
|
|
32
|
-
const queryString = new URLSearchParams(query).toString();
|
|
33
|
-
url += queryString ? `?${queryString}` : '';
|
|
34
|
-
}
|
|
35
|
-
const response = await fetch(encodeURI(url), {
|
|
36
|
-
method,
|
|
37
|
-
headers: headers
|
|
38
|
-
? { ...headers, 'Content-Type': 'application/json' }
|
|
39
|
-
: undefined,
|
|
40
|
-
body: body ? JSON.stringify(body) : undefined
|
|
41
|
-
});
|
|
42
|
-
const contentType = response.headers.get('content-type');
|
|
43
|
-
const responseBody = contentType && contentType.includes('application/json')
|
|
44
|
-
? await response.json()
|
|
45
|
-
: await response.text();
|
|
46
|
-
return {
|
|
47
|
-
code: response.status,
|
|
48
|
-
content: responseBody,
|
|
49
|
-
headers: response.headers
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
async pathParamRequest(route, method, request) {
|
|
53
|
-
return this.execute(route, method, request);
|
|
54
|
-
}
|
|
55
|
-
async bodyRequest(route, method, request) {
|
|
56
|
-
return this.execute(route, method, request);
|
|
57
|
-
}
|
|
58
|
-
async get(route, request) {
|
|
59
|
-
return this.pathParamRequest(route, 'GET', request);
|
|
60
|
-
}
|
|
61
|
-
async post(route, request) {
|
|
62
|
-
return this.bodyRequest(route, 'POST', request);
|
|
63
|
-
}
|
|
64
|
-
async put(route, request) {
|
|
65
|
-
return this.bodyRequest(route, 'PUT', request);
|
|
66
|
-
}
|
|
67
|
-
async patch(route, request) {
|
|
68
|
-
return this.bodyRequest(route, 'PATCH', request);
|
|
69
|
-
}
|
|
70
|
-
async delete(route, request) {
|
|
71
|
-
return this.pathParamRequest(route, 'DELETE', request);
|
|
72
|
-
}
|
|
73
|
-
}
|
package/lib/src/utils/regex.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generates a string from a given regular expression or string representation of a regex.
|
|
3
|
-
*
|
|
4
|
-
* @param {RegExp | string} regex - The regular expression or string representation of a regex.
|
|
5
|
-
* @returns {string} - The generated string based on the regex pattern.
|
|
6
|
-
* @throws {Error} - Throws an error if there are unmatched brackets or groups.
|
|
7
|
-
*/
|
|
8
|
-
export declare function generateStringFromRegex(regex: RegExp | string): string;
|
|
9
|
-
//# sourceMappingURL=regex.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"regex.d.ts","sourceRoot":"","sources":["../../../src/utils/regex.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAwGtE"}
|
package/lib/src/utils/regex.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generates a string from a given regular expression or string representation of a regex.
|
|
3
|
-
*
|
|
4
|
-
* @param {RegExp | string} regex - The regular expression or string representation of a regex.
|
|
5
|
-
* @returns {string} - The generated string based on the regex pattern.
|
|
6
|
-
* @throws {Error} - Throws an error if there are unmatched brackets or groups.
|
|
7
|
-
*/
|
|
8
|
-
export function generateStringFromRegex(regex) {
|
|
9
|
-
let regexStr = typeof regex === 'object' ? regex.source : regex;
|
|
10
|
-
// Remove leading and trailing slashes if present
|
|
11
|
-
if (regexStr.startsWith('/'))
|
|
12
|
-
regexStr = regexStr.slice(1);
|
|
13
|
-
if (regexStr.endsWith('/g'))
|
|
14
|
-
regexStr = regexStr.slice(0, -2); // Remove the global flag
|
|
15
|
-
if (regexStr.endsWith('/'))
|
|
16
|
-
regexStr = regexStr.slice(0, -1);
|
|
17
|
-
let result = '';
|
|
18
|
-
let i = 0;
|
|
19
|
-
while (i < regexStr.length) {
|
|
20
|
-
const char = regexStr[i];
|
|
21
|
-
switch (char) {
|
|
22
|
-
case '\\': {
|
|
23
|
-
// Handle escaped characters
|
|
24
|
-
const nextChar = regexStr[i + 1];
|
|
25
|
-
switch (nextChar) {
|
|
26
|
-
case 'b':
|
|
27
|
-
// Word boundary, ensure we start a new word
|
|
28
|
-
if (result.length > 0 && /\w/.test(result[result.length - 1])) {
|
|
29
|
-
result += ' ';
|
|
30
|
-
}
|
|
31
|
-
break;
|
|
32
|
-
case 'd':
|
|
33
|
-
result += '0'; // Match a digit
|
|
34
|
-
break;
|
|
35
|
-
case 'w':
|
|
36
|
-
result += 'a'; // Match a word character
|
|
37
|
-
break;
|
|
38
|
-
case 's':
|
|
39
|
-
result += ' '; // Match a whitespace character
|
|
40
|
-
break;
|
|
41
|
-
default:
|
|
42
|
-
result += nextChar;
|
|
43
|
-
}
|
|
44
|
-
i += 2;
|
|
45
|
-
break;
|
|
46
|
-
}
|
|
47
|
-
case '.':
|
|
48
|
-
// Match any character (using 'a' for simplicity)
|
|
49
|
-
result += 'a';
|
|
50
|
-
i++;
|
|
51
|
-
break;
|
|
52
|
-
case '[': {
|
|
53
|
-
// Handle character classes
|
|
54
|
-
const endIdx = regexStr.indexOf(']', i);
|
|
55
|
-
if (endIdx === -1) {
|
|
56
|
-
throw new Error('Unmatched [');
|
|
57
|
-
}
|
|
58
|
-
const charClass = regexStr.slice(i + 1, endIdx);
|
|
59
|
-
result += charClass[0]; // Use the first character in the class
|
|
60
|
-
i = endIdx + 1;
|
|
61
|
-
break;
|
|
62
|
-
}
|
|
63
|
-
case '(': {
|
|
64
|
-
// Handle groups (non-capturing groups (?:...) are simplified to normal groups)
|
|
65
|
-
const endGroupIdx = regexStr.indexOf(')', i);
|
|
66
|
-
if (endGroupIdx === -1) {
|
|
67
|
-
throw new Error('Unmatched (');
|
|
68
|
-
}
|
|
69
|
-
const groupContent = regexStr.slice(i + 1, endGroupIdx);
|
|
70
|
-
result += generateStringFromRegex(groupContent); // Recursively handle group content
|
|
71
|
-
i = endGroupIdx + 1;
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
case '{': {
|
|
75
|
-
// Handle quantifiers {n} or {n,m}
|
|
76
|
-
const endQuantIdx = regexStr.indexOf('}', i);
|
|
77
|
-
if (endQuantIdx === -1) {
|
|
78
|
-
throw new Error('Unmatched {');
|
|
79
|
-
}
|
|
80
|
-
const quantifier = regexStr.slice(i + 1, endQuantIdx);
|
|
81
|
-
const min = parseInt(quantifier.split(',')[0], 10) || 1;
|
|
82
|
-
const lastChar = result[result.length - 1];
|
|
83
|
-
result += lastChar.repeat(min - 1);
|
|
84
|
-
i = endQuantIdx + 1;
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
case '*':
|
|
88
|
-
case '+':
|
|
89
|
-
case '?': {
|
|
90
|
-
// Handle *, +, and ? quantifiers (simplified handling)
|
|
91
|
-
const prevChar = result[result.length - 1];
|
|
92
|
-
if (char === '*') {
|
|
93
|
-
// Match zero or more (using one for simplicity)
|
|
94
|
-
result += prevChar;
|
|
95
|
-
}
|
|
96
|
-
else if (char === '+') {
|
|
97
|
-
// Match one or more (already have one, so add one more)
|
|
98
|
-
result += prevChar;
|
|
99
|
-
}
|
|
100
|
-
i++;
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
default:
|
|
104
|
-
// Default case: add character to result
|
|
105
|
-
result += char;
|
|
106
|
-
i++;
|
|
107
|
-
break;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
return result;
|
|
111
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Extracts the SDK path from the given path.
|
|
3
|
-
*
|
|
4
|
-
* @param {string | RegExp | (string | RegExp)[]} path - The provided path.
|
|
5
|
-
* @returns {string} - The extracted SDK path.
|
|
6
|
-
* @throws {Error} - Throws an error if the path is not defined.
|
|
7
|
-
*/
|
|
8
|
-
export declare function getSdkPath(path: string | RegExp | (string | RegExp)[]): string;
|
|
9
|
-
//# sourceMappingURL=resolvePath.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"resolvePath.d.ts","sourceRoot":"","sources":["../../../src/utils/resolvePath.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,wBAAgB,UAAU,CACxB,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,GAC1C,MAAM,CAgBR"}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { generateStringFromRegex } from './regex';
|
|
2
|
-
/**
|
|
3
|
-
* Extracts the SDK path from the given path.
|
|
4
|
-
*
|
|
5
|
-
* @param {string | RegExp | (string | RegExp)[]} path - The provided path.
|
|
6
|
-
* @returns {string} - The extracted SDK path.
|
|
7
|
-
* @throws {Error} - Throws an error if the path is not defined.
|
|
8
|
-
*/
|
|
9
|
-
export function getSdkPath(path) {
|
|
10
|
-
let sdkPath = path;
|
|
11
|
-
if (Array.isArray(path)) {
|
|
12
|
-
sdkPath = path.pop() || path[0];
|
|
13
|
-
}
|
|
14
|
-
if (!sdkPath) {
|
|
15
|
-
throw new Error('Path is not defined');
|
|
16
|
-
}
|
|
17
|
-
if (sdkPath instanceof RegExp) {
|
|
18
|
-
sdkPath = generateStringFromRegex(sdkPath);
|
|
19
|
-
}
|
|
20
|
-
return sdkPath;
|
|
21
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"universalSdk.test.d.ts","sourceRoot":"","sources":["../../tests/universalSdk.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import fetchMock from 'fetch-mock';
|
|
2
|
-
import { universalSdk } from '../index';
|
|
3
|
-
describe('universalSdk tests', () => {
|
|
4
|
-
const sdk = universalSdk('https://api.example.com');
|
|
5
|
-
beforeEach(() => {
|
|
6
|
-
fetchMock.clearHistory();
|
|
7
|
-
fetchMock.removeRoutes();
|
|
8
|
-
});
|
|
9
|
-
afterAll(() => {
|
|
10
|
-
fetchMock.clearHistory();
|
|
11
|
-
fetchMock.removeRoutes();
|
|
12
|
-
});
|
|
13
|
-
test('GET request should be called with correct URL and method', async () => {
|
|
14
|
-
fetchMock.getOnce('https://api.example.com/test', {
|
|
15
|
-
status: 200,
|
|
16
|
-
body: { message: 'Success' },
|
|
17
|
-
headers: { 'Content-Type': 'application/json' }
|
|
18
|
-
});
|
|
19
|
-
const response = await sdk.get('/test');
|
|
20
|
-
expect(fetchMock.callHistory.called('https://api.example.com/test')).toBe(true);
|
|
21
|
-
expect(await response.content).toEqual({ message: 'Success' });
|
|
22
|
-
});
|
|
23
|
-
test('POST request should be called with correct URL, method, headers, and body', async () => {
|
|
24
|
-
fetchMock.postOnce('https://api.example.com/test', {
|
|
25
|
-
status: 201,
|
|
26
|
-
body: { message: 'Created' },
|
|
27
|
-
headers: { 'Content-Type': 'application/json' }
|
|
28
|
-
});
|
|
29
|
-
const response = await sdk.post('/test', {
|
|
30
|
-
body: { key: 'value' },
|
|
31
|
-
headers: { Authorization: 'Bearer token' }
|
|
32
|
-
});
|
|
33
|
-
expect(fetchMock.callHistory.called('https://api.example.com/test')).toBe(true);
|
|
34
|
-
const lastCall = fetchMock.callHistory.lastCall('https://api.example.com/test');
|
|
35
|
-
expect(lastCall?.request?.method).toBe('POST');
|
|
36
|
-
expect(lastCall?.request?.headers).toEqual({
|
|
37
|
-
'Content-Type': 'application/json',
|
|
38
|
-
Authorization: 'Bearer token'
|
|
39
|
-
});
|
|
40
|
-
expect(lastCall?.request?.body).toBe(JSON.stringify({ key: 'value' }));
|
|
41
|
-
expect(await response.content).toEqual({ message: 'Created' });
|
|
42
|
-
});
|
|
43
|
-
test('PUT request should be called with correct URL and method', async () => {
|
|
44
|
-
fetchMock.putOnce('https://api.example.com/test/123', {
|
|
45
|
-
status: 200,
|
|
46
|
-
body: { message: 'Updated' },
|
|
47
|
-
headers: { 'Content-Type': 'application/json' }
|
|
48
|
-
});
|
|
49
|
-
const response = await sdk.put('/test/123', {
|
|
50
|
-
body: { key: 'updatedValue' },
|
|
51
|
-
headers: { Authorization: 'Bearer token' }
|
|
52
|
-
});
|
|
53
|
-
expect(fetchMock.callHistory.called('https://api.example.com/test/123')).toBe(true);
|
|
54
|
-
const lastCall = fetchMock.callHistory.lastCall('https://api.example.com/test/123');
|
|
55
|
-
expect(lastCall?.request?.method).toBe('PUT');
|
|
56
|
-
expect(lastCall?.request?.headers).toEqual({
|
|
57
|
-
'Content-Type': 'application/json',
|
|
58
|
-
Authorization: 'Bearer token'
|
|
59
|
-
});
|
|
60
|
-
expect(lastCall?.request?.body).toBe(JSON.stringify({ key: 'updatedValue' }));
|
|
61
|
-
expect(await response.content).toEqual({ message: 'Updated' });
|
|
62
|
-
});
|
|
63
|
-
test('PATCH request should be called with correct URL and method', async () => {
|
|
64
|
-
fetchMock.patchOnce('https://api.example.com/test/123', {
|
|
65
|
-
status: 200,
|
|
66
|
-
body: { message: 'Patched' },
|
|
67
|
-
headers: { 'Content-Type': 'application/json' }
|
|
68
|
-
});
|
|
69
|
-
const response = await sdk.patch('/test/123', {
|
|
70
|
-
body: { key: 'patchedValue' },
|
|
71
|
-
headers: { Authorization: 'Bearer token' }
|
|
72
|
-
});
|
|
73
|
-
expect(fetchMock.callHistory.called('https://api.example.com/test/123')).toBe(true);
|
|
74
|
-
const lastCall = fetchMock.callHistory.lastCall('https://api.example.com/test/123');
|
|
75
|
-
expect(lastCall?.request?.method).toBe('PATCH');
|
|
76
|
-
expect(lastCall?.request?.headers).toEqual({
|
|
77
|
-
'Content-Type': 'application/json',
|
|
78
|
-
Authorization: 'Bearer token'
|
|
79
|
-
});
|
|
80
|
-
expect(lastCall?.request?.body).toBe(JSON.stringify({ key: 'patchedValue' }));
|
|
81
|
-
expect(await response.content).toEqual({ message: 'Patched' });
|
|
82
|
-
});
|
|
83
|
-
test('DELETE request should be called with correct URL and method', async () => {
|
|
84
|
-
fetchMock.deleteOnce('https://api.example.com/test/123', {
|
|
85
|
-
status: 204,
|
|
86
|
-
headers: {}
|
|
87
|
-
});
|
|
88
|
-
const response = await sdk.delete('/test/123');
|
|
89
|
-
expect(fetchMock.callHistory.called('https://api.example.com/test/123')).toBe(true);
|
|
90
|
-
const lastCall = fetchMock.callHistory.lastCall('https://api.example.com/test/123');
|
|
91
|
-
expect(lastCall?.request?.method).toBe('DELETE');
|
|
92
|
-
expect(response.code).toBe(204);
|
|
93
|
-
});
|
|
94
|
-
});
|