@hyper-fetch/core 7.3.0 → 7.4.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/dist/browser/index.js +27 -6
- package/dist/browser/index.js.map +2 -2
- package/dist/index.d.ts +8 -2
- package/dist/index.js +27 -6
- package/dist/index.js.map +2 -2
- package/package.json +1 -1
- package/src/sdk/sdk.ts +41 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/core",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.4.0",
|
|
4
4
|
"description": "Cache, Queue and Persist your requests no matter if you are online or offline!",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|
package/src/sdk/sdk.ts
CHANGED
|
@@ -5,7 +5,19 @@ export type RecursiveSchemaType = Record<
|
|
|
5
5
|
any
|
|
6
6
|
>;
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
export type CreateSdkOptions = {
|
|
9
|
+
/** @default true */
|
|
10
|
+
camelCaseToKebabCase?: boolean;
|
|
11
|
+
/** @default (method) => method.toUpperCase() */
|
|
12
|
+
methodTransform?: (method: string) => string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const getMethod = (key: string, options?: CreateSdkOptions) => {
|
|
16
|
+
const { methodTransform = (method: string) => method.toUpperCase() } = options ?? {};
|
|
17
|
+
return methodTransform(key);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const createRecursiveProxy = (client: ClientInstance, path: string[], options?: CreateSdkOptions): any => {
|
|
9
21
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
10
22
|
return new Proxy(() => {}, {
|
|
11
23
|
get: (_target, key: string) => {
|
|
@@ -13,14 +25,31 @@ const createRecursiveProxy = (client: ClientInstance, path: string[]): any => {
|
|
|
13
25
|
return undefined;
|
|
14
26
|
}
|
|
15
27
|
|
|
16
|
-
//
|
|
28
|
+
// Check if this is a method (starts with $) or a path segment
|
|
29
|
+
let isMethod = false;
|
|
30
|
+
let methodName = key;
|
|
31
|
+
let pathSegment = key;
|
|
32
|
+
|
|
33
|
+
if (key.startsWith("$")) {
|
|
34
|
+
// This could be either a method or a parameter
|
|
35
|
+
// Try to determine by checking if it's a terminal access (method)
|
|
36
|
+
// For now, assume it's a method and strip the $ prefix
|
|
37
|
+
isMethod = true;
|
|
38
|
+
methodName = key.slice(1);
|
|
39
|
+
pathSegment = `:${key.slice(1)}`; // Convert to parameter format for path building
|
|
40
|
+
} else if (options?.camelCaseToKebabCase) {
|
|
41
|
+
// Convert camelCase to kebab-case for path segments if option is enabled
|
|
42
|
+
pathSegment = key.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Always create a request assuming this is a method call
|
|
17
46
|
const endpoint = `/${path.join("/")}`;
|
|
18
|
-
const method = key
|
|
47
|
+
const method = getMethod(isMethod ? methodName : key, options);
|
|
19
48
|
const request = client.createRequest()({ endpoint, method });
|
|
20
49
|
|
|
21
50
|
// But also, assume the key is a new path segment for a deeper call
|
|
22
|
-
const newPath = [...path,
|
|
23
|
-
const deeperProxy = createRecursiveProxy(client, newPath);
|
|
51
|
+
const newPath = [...path, pathSegment];
|
|
52
|
+
const deeperProxy = createRecursiveProxy(client, newPath, options);
|
|
24
53
|
|
|
25
54
|
// Return a new proxy that wraps both the request and the deeper proxy
|
|
26
55
|
return new Proxy(request, {
|
|
@@ -39,6 +68,12 @@ const createRecursiveProxy = (client: ClientInstance, path: string[]): any => {
|
|
|
39
68
|
|
|
40
69
|
export const createSdk = <Client extends ClientInstance, RecursiveSchema extends RecursiveSchemaType>(
|
|
41
70
|
client: Client,
|
|
71
|
+
options?: CreateSdkOptions,
|
|
42
72
|
): RecursiveSchema => {
|
|
43
|
-
|
|
73
|
+
const {
|
|
74
|
+
camelCaseToKebabCase = true,
|
|
75
|
+
methodTransform = (method: string) => method.toUpperCase(),
|
|
76
|
+
...rest
|
|
77
|
+
} = options ?? {};
|
|
78
|
+
return createRecursiveProxy(client, [], { camelCaseToKebabCase, methodTransform, ...rest }) as RecursiveSchema;
|
|
44
79
|
};
|