tag-rpc 1.0.1 → 1.0.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 +55 -32
- package/dist/index.js +39 -32
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,30 +1,8 @@
|
|
|
1
|
-
type
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
Query: any;
|
|
7
|
-
}>;
|
|
8
|
-
}>;
|
|
9
|
-
interface BaseNamedRoutes {
|
|
10
|
-
[key: string]: {
|
|
11
|
-
path: string;
|
|
12
|
-
method: string;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
type TagRPCOptions<T extends BaseApiRoutes, TPath extends keyof T, TMethod extends keyof T[TPath]["Methods"]> = Omit<RequestInit, "method" | "body"> & (T[TPath]["PathParams"] extends never ? {
|
|
16
|
-
pathParams?: never;
|
|
17
|
-
} : {
|
|
18
|
-
pathParams: T[TPath]["PathParams"];
|
|
19
|
-
}) & (T[TPath]["Methods"][TMethod]["Body"] extends never ? {
|
|
20
|
-
body?: never;
|
|
21
|
-
} : {
|
|
22
|
-
body: T[TPath]["Methods"][TMethod]["Body"];
|
|
23
|
-
}) & (T[TPath]["Methods"][TMethod]["Query"] extends never ? {
|
|
24
|
-
query?: never;
|
|
25
|
-
} : {
|
|
26
|
-
query?: T[TPath]["Methods"][TMethod]["Query"];
|
|
27
|
-
});
|
|
1
|
+
type MethodShape = {
|
|
2
|
+
Response: unknown;
|
|
3
|
+
Body: unknown;
|
|
4
|
+
Query: unknown;
|
|
5
|
+
};
|
|
28
6
|
declare class TAG_API_ERROR extends Error {
|
|
29
7
|
status: number;
|
|
30
8
|
statusText: string;
|
|
@@ -32,10 +10,55 @@ declare class TAG_API_ERROR extends Error {
|
|
|
32
10
|
url: string;
|
|
33
11
|
constructor(status: number, statusText: string, body: any, url: string);
|
|
34
12
|
}
|
|
35
|
-
declare function createTagRPC<
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
13
|
+
declare function createTagRPC<TApiRoutes extends Record<string, {
|
|
14
|
+
Methods: Record<string, MethodShape>;
|
|
15
|
+
PathParams: any;
|
|
16
|
+
}>, TNamedRoutes extends Record<string, {
|
|
17
|
+
path: keyof TApiRoutes;
|
|
18
|
+
method: string;
|
|
19
|
+
}>>(registry: {
|
|
20
|
+
ApiRoutes: TApiRoutes;
|
|
21
|
+
namedRoutes: TNamedRoutes;
|
|
22
|
+
}): { [K in keyof TNamedRoutes]: <TPath extends TNamedRoutes[K]["path"] & keyof TApiRoutes, TMethod extends TNamedRoutes[K]["method"] & keyof TApiRoutes[TPath]["Methods"] & string>(options: Omit<RequestInit, "method" | "body"> & (TApiRoutes[TPath]["PathParams"] extends never ? {
|
|
23
|
+
pathParams?: never;
|
|
24
|
+
} : {
|
|
25
|
+
pathParams: TApiRoutes[TPath]["PathParams"];
|
|
26
|
+
}) & (TApiRoutes[TPath]["Methods"][TMethod]["Body"] extends never ? {
|
|
27
|
+
body?: never;
|
|
28
|
+
} : {
|
|
29
|
+
body: TApiRoutes[TPath]["Methods"][TMethod]["Body"];
|
|
30
|
+
}) & (TApiRoutes[TPath]["Methods"][TMethod]["Query"] extends never ? {
|
|
31
|
+
query?: never;
|
|
32
|
+
} : {
|
|
33
|
+
query?: TApiRoutes[TPath]["Methods"][TMethod]["Query"] | undefined;
|
|
34
|
+
})) => Promise<TApiRoutes[TPath]["Methods"][TMethod]["Response"]>; } & {
|
|
35
|
+
fetch: <TPath extends keyof TApiRoutes, TMethod extends keyof TApiRoutes[TPath]["Methods"] & string>(path: TPath, method: TMethod, options: Omit<RequestInit, "method" | "body"> & (TApiRoutes[TPath]["PathParams"] extends never ? {
|
|
36
|
+
pathParams?: never;
|
|
37
|
+
} : {
|
|
38
|
+
pathParams: TApiRoutes[TPath]["PathParams"];
|
|
39
|
+
}) & (TApiRoutes[TPath]["Methods"][TMethod]["Body"] extends never ? {
|
|
40
|
+
body?: never;
|
|
41
|
+
} : {
|
|
42
|
+
body: TApiRoutes[TPath]["Methods"][TMethod]["Body"];
|
|
43
|
+
}) & (TApiRoutes[TPath]["Methods"][TMethod]["Query"] extends never ? {
|
|
44
|
+
query?: never;
|
|
45
|
+
} : {
|
|
46
|
+
query?: TApiRoutes[TPath]["Methods"][TMethod]["Query"] | undefined;
|
|
47
|
+
})) => Promise<TApiRoutes[TPath]["Methods"][TMethod]["Response"]>;
|
|
48
|
+
routes: TNamedRoutes;
|
|
49
|
+
call: <K_1 extends keyof TNamedRoutes, TRoute extends TNamedRoutes[K_1], TPath extends TRoute["path"] & keyof TApiRoutes, TMethod extends TRoute["method"] & keyof TApiRoutes[TPath]["Methods"] & string>(route: TRoute, options: Omit<RequestInit, "method" | "body"> & (TApiRoutes[TPath]["PathParams"] extends never ? {
|
|
50
|
+
pathParams?: never;
|
|
51
|
+
} : {
|
|
52
|
+
pathParams: TApiRoutes[TPath]["PathParams"];
|
|
53
|
+
}) & (TApiRoutes[TPath]["Methods"][TMethod]["Body"] extends never ? {
|
|
54
|
+
body?: never;
|
|
55
|
+
} : {
|
|
56
|
+
body: TApiRoutes[TPath]["Methods"][TMethod]["Body"];
|
|
57
|
+
}) & (TApiRoutes[TPath]["Methods"][TMethod]["Query"] extends never ? {
|
|
58
|
+
query?: never;
|
|
59
|
+
} : {
|
|
60
|
+
query?: TApiRoutes[TPath]["Methods"][TMethod]["Query"] | undefined;
|
|
61
|
+
})) => Promise<TApiRoutes[TPath]["Methods"][TMethod]["Response"]>;
|
|
39
62
|
};
|
|
40
63
|
|
|
41
|
-
export {
|
|
64
|
+
export { TAG_API_ERROR, createTagRPC };
|
package/dist/index.js
CHANGED
|
@@ -6,47 +6,54 @@ var TAG_API_ERROR = class extends Error {
|
|
|
6
6
|
this.statusText = statusText;
|
|
7
7
|
this.body = body;
|
|
8
8
|
this.url = url;
|
|
9
|
-
this.name = "
|
|
9
|
+
this.name = "ApiError";
|
|
10
10
|
}
|
|
11
11
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
function createTagRPC(registry) {
|
|
13
|
+
async function fetchData(path, method, options) {
|
|
14
|
+
let url = path;
|
|
15
|
+
if ("pathParams" in options && options.pathParams) {
|
|
16
|
+
for (const [key, value] of Object.entries(options.pathParams)) {
|
|
17
|
+
url = url.replace(`[${key}]`, encodeURIComponent(String(value)));
|
|
18
|
+
}
|
|
17
19
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const res = await fetch(url, {
|
|
24
|
-
...options,
|
|
25
|
-
method,
|
|
26
|
-
body: options?.body ? JSON.stringify(options.body) : void 0,
|
|
27
|
-
headers: { "Content-Type": "application/json", ...options?.headers }
|
|
28
|
-
});
|
|
29
|
-
if (!res.ok) {
|
|
30
|
-
let errorBody;
|
|
31
|
-
try {
|
|
32
|
-
errorBody = await res.json();
|
|
33
|
-
} catch {
|
|
34
|
-
errorBody = { message: "Unknown error occurred" };
|
|
20
|
+
if ("query" in options) {
|
|
21
|
+
const qs = new URLSearchParams(
|
|
22
|
+
options.query
|
|
23
|
+
).toString();
|
|
24
|
+
if (qs) url += `?${qs}`;
|
|
35
25
|
}
|
|
36
|
-
|
|
26
|
+
const res = await fetch(url, {
|
|
27
|
+
...options,
|
|
28
|
+
method,
|
|
29
|
+
body: "body" in options ? JSON.stringify(options.body) : void 0,
|
|
30
|
+
headers: {
|
|
31
|
+
"Content-Type": "application/json",
|
|
32
|
+
...options.headers
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
if (!res.ok) {
|
|
36
|
+
let errorBody;
|
|
37
|
+
try {
|
|
38
|
+
errorBody = await res.json();
|
|
39
|
+
} catch {
|
|
40
|
+
errorBody = { message: "Unknown error occurred" };
|
|
41
|
+
}
|
|
42
|
+
throw new TAG_API_ERROR(res.status, res.statusText, errorBody, url);
|
|
43
|
+
}
|
|
44
|
+
return res.json();
|
|
37
45
|
}
|
|
38
|
-
return res.json();
|
|
39
|
-
}
|
|
40
|
-
function createTagRPC(namedRoutes) {
|
|
41
46
|
const baseFetcher = {
|
|
42
|
-
fetch:
|
|
43
|
-
|
|
44
|
-
|
|
47
|
+
fetch: fetchData,
|
|
48
|
+
routes: registry.namedRoutes,
|
|
49
|
+
call: (route, options) => fetchData(route.path, route.method, options)
|
|
45
50
|
};
|
|
46
51
|
return new Proxy(baseFetcher, {
|
|
47
52
|
get(target, prop) {
|
|
48
|
-
if (prop in target)
|
|
49
|
-
|
|
53
|
+
if (prop in target) {
|
|
54
|
+
return target[prop];
|
|
55
|
+
}
|
|
56
|
+
const route = registry.namedRoutes[prop];
|
|
50
57
|
if (route) {
|
|
51
58
|
return (options) => fetchData(route.path, route.method, options);
|
|
52
59
|
}
|