@forklaunch/universal-sdk 0.1.0 → 0.1.1
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/eslint.config.d.mts +1132 -1
- package/lib/eslint.config.mjs +1 -1
- package/lib/jest.config.d.ts.map +1 -1
- package/lib/jest.config.js +0 -2
- package/lib/src/universalSdk.d.ts.map +1 -1
- package/lib/src/universalSdk.js +2 -1
- package/lib/src/utils/resolvePath.d.ts +9 -0
- package/lib/src/utils/resolvePath.d.ts.map +1 -0
- package/lib/src/utils/resolvePath.js +21 -0
- package/lib/tests/universalSdk.test.js +23 -21
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/vitest.config.js +1 -1
- package/package.json +6 -6
package/lib/eslint.config.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import globals from 'globals';
|
|
|
3
3
|
import tseslint from 'typescript-eslint';
|
|
4
4
|
export default [
|
|
5
5
|
{ files: ['**/*.{ts}'] },
|
|
6
|
-
{ ignores: ['tests/**/*', 'dist/**/*', 'node_modules/**/*'] },
|
|
6
|
+
{ ignores: ['tests/**/*', 'dist/**/*', 'lib/**/*', 'node_modules/**/*'] },
|
|
7
7
|
{ languageOptions: { globals: globals.browser } },
|
|
8
8
|
pluginJs.configs.recommended,
|
|
9
9
|
...tseslint.configs.recommended
|
package/lib/jest.config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jest.config.d.ts","sourceRoot":"","sources":["../jest.config.ts"],"names":[],"mappings":"
|
|
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
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"universalSdk.d.ts","sourceRoot":"","sources":["../../src/universalSdk.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;
|
|
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
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getSdkPath } from './utils/resolvePath';
|
|
1
2
|
/**
|
|
2
3
|
* A class representing the Forklaunch SDK.
|
|
3
4
|
*/
|
|
@@ -21,7 +22,7 @@ export class UniversalSdk {
|
|
|
21
22
|
*/
|
|
22
23
|
async execute(route, method, request) {
|
|
23
24
|
const { params, body, query, headers } = request || {};
|
|
24
|
-
let url = this.host + route;
|
|
25
|
+
let url = getSdkPath(this.host + route);
|
|
25
26
|
if (params) {
|
|
26
27
|
for (const key in params) {
|
|
27
28
|
url = url.replace(`:${key}`, encodeURIComponent(params[key]));
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
}
|
|
@@ -3,10 +3,12 @@ import { universalSdk } from '../index';
|
|
|
3
3
|
describe('universalSdk tests', () => {
|
|
4
4
|
const sdk = universalSdk('https://api.example.com');
|
|
5
5
|
beforeEach(() => {
|
|
6
|
-
fetchMock.
|
|
6
|
+
fetchMock.clearHistory();
|
|
7
|
+
fetchMock.removeRoutes();
|
|
7
8
|
});
|
|
8
9
|
afterAll(() => {
|
|
9
|
-
fetchMock.
|
|
10
|
+
fetchMock.clearHistory();
|
|
11
|
+
fetchMock.removeRoutes();
|
|
10
12
|
});
|
|
11
13
|
test('GET request should be called with correct URL and method', async () => {
|
|
12
14
|
fetchMock.getOnce('https://api.example.com/test', {
|
|
@@ -15,7 +17,7 @@ describe('universalSdk tests', () => {
|
|
|
15
17
|
headers: { 'Content-Type': 'application/json' }
|
|
16
18
|
});
|
|
17
19
|
const response = await sdk.get('/test');
|
|
18
|
-
expect(fetchMock.called('https://api.example.com/test')).toBe(true);
|
|
20
|
+
expect(fetchMock.callHistory.called('https://api.example.com/test')).toBe(true);
|
|
19
21
|
expect(await response.content).toEqual({ message: 'Success' });
|
|
20
22
|
});
|
|
21
23
|
test('POST request should be called with correct URL, method, headers, and body', async () => {
|
|
@@ -28,14 +30,14 @@ describe('universalSdk tests', () => {
|
|
|
28
30
|
body: { key: 'value' },
|
|
29
31
|
headers: { Authorization: 'Bearer token' }
|
|
30
32
|
});
|
|
31
|
-
expect(fetchMock.called('https://api.example.com/test')).toBe(true);
|
|
32
|
-
const lastCall = fetchMock.lastCall('https://api.example.com/test');
|
|
33
|
-
expect(lastCall?.
|
|
34
|
-
expect(lastCall?.
|
|
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({
|
|
35
37
|
'Content-Type': 'application/json',
|
|
36
38
|
Authorization: 'Bearer token'
|
|
37
39
|
});
|
|
38
|
-
expect(lastCall?.
|
|
40
|
+
expect(lastCall?.request?.body).toBe(JSON.stringify({ key: 'value' }));
|
|
39
41
|
expect(await response.content).toEqual({ message: 'Created' });
|
|
40
42
|
});
|
|
41
43
|
test('PUT request should be called with correct URL and method', async () => {
|
|
@@ -48,14 +50,14 @@ describe('universalSdk tests', () => {
|
|
|
48
50
|
body: { key: 'updatedValue' },
|
|
49
51
|
headers: { Authorization: 'Bearer token' }
|
|
50
52
|
});
|
|
51
|
-
expect(fetchMock.called('https://api.example.com/test/123')).toBe(true);
|
|
52
|
-
const lastCall = fetchMock.lastCall('https://api.example.com/test/123');
|
|
53
|
-
expect(lastCall?.
|
|
54
|
-
expect(lastCall?.
|
|
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({
|
|
55
57
|
'Content-Type': 'application/json',
|
|
56
58
|
Authorization: 'Bearer token'
|
|
57
59
|
});
|
|
58
|
-
expect(lastCall?.
|
|
60
|
+
expect(lastCall?.request?.body).toBe(JSON.stringify({ key: 'updatedValue' }));
|
|
59
61
|
expect(await response.content).toEqual({ message: 'Updated' });
|
|
60
62
|
});
|
|
61
63
|
test('PATCH request should be called with correct URL and method', async () => {
|
|
@@ -68,14 +70,14 @@ describe('universalSdk tests', () => {
|
|
|
68
70
|
body: { key: 'patchedValue' },
|
|
69
71
|
headers: { Authorization: 'Bearer token' }
|
|
70
72
|
});
|
|
71
|
-
expect(fetchMock.called('https://api.example.com/test/123')).toBe(true);
|
|
72
|
-
const lastCall = fetchMock.lastCall('https://api.example.com/test/123');
|
|
73
|
-
expect(lastCall?.
|
|
74
|
-
expect(lastCall?.
|
|
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({
|
|
75
77
|
'Content-Type': 'application/json',
|
|
76
78
|
Authorization: 'Bearer token'
|
|
77
79
|
});
|
|
78
|
-
expect(lastCall?.
|
|
80
|
+
expect(lastCall?.request?.body).toBe(JSON.stringify({ key: 'patchedValue' }));
|
|
79
81
|
expect(await response.content).toEqual({ message: 'Patched' });
|
|
80
82
|
});
|
|
81
83
|
test('DELETE request should be called with correct URL and method', async () => {
|
|
@@ -84,9 +86,9 @@ describe('universalSdk tests', () => {
|
|
|
84
86
|
headers: {}
|
|
85
87
|
});
|
|
86
88
|
const response = await sdk.delete('/test/123');
|
|
87
|
-
expect(fetchMock.called('https://api.example.com/test/123')).toBe(true);
|
|
88
|
-
const lastCall = fetchMock.lastCall('https://api.example.com/test/123');
|
|
89
|
-
expect(lastCall?.
|
|
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');
|
|
90
92
|
expect(response.code).toBe(204);
|
|
91
93
|
});
|
|
92
94
|
});
|