@travetto/web-rpc 8.0.0-alpha.17 → 8.0.0-alpha.18
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/package.json +1 -1
- package/support/client/rpc.ts +23 -0
package/package.json
CHANGED
package/support/client/rpc.ts
CHANGED
|
@@ -5,6 +5,17 @@ type MethodKeys<C extends {}> = {
|
|
|
5
5
|
type PromiseFn = (...args: any) => Promise<unknown>;
|
|
6
6
|
type PromiseResult<V extends PromiseFn> = Awaited<ReturnType<V>>;
|
|
7
7
|
|
|
8
|
+
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
const triggerRedirect = (world: any, url: URL): boolean => {
|
|
11
|
+
const location = world?.window?.location;
|
|
12
|
+
if (location) {
|
|
13
|
+
location.href = url.toString();
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
};
|
|
18
|
+
|
|
8
19
|
const isBlobMap = (value: unknown): value is Record<string, Blob> => {
|
|
9
20
|
if (typeof value !== 'object' || value === null) {
|
|
10
21
|
return false;
|
|
@@ -52,6 +63,7 @@ export type RpcRequest = {
|
|
|
52
63
|
controller: string;
|
|
53
64
|
endpoint: string;
|
|
54
65
|
method: string;
|
|
66
|
+
redirect?: boolean;
|
|
55
67
|
};
|
|
56
68
|
url: URL | string;
|
|
57
69
|
consumeJSON?: <T>(text?: unknown) => (T | Promise<T>);
|
|
@@ -97,7 +109,13 @@ function registerTimeout<T>(
|
|
|
97
109
|
|
|
98
110
|
function buildRequest<T extends RequestInit>(base: T, controller: string, endpoint: string): T {
|
|
99
111
|
let verb: string;
|
|
112
|
+
let redirect = false;
|
|
100
113
|
switch (endpoint.split(/[A-Z]/)[0]) {
|
|
114
|
+
case 'redirect': {
|
|
115
|
+
verb = 'GET';
|
|
116
|
+
redirect = true;
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
101
119
|
case 'get': verb = 'GET'; break;
|
|
102
120
|
case 'update': verb = 'PUT'; break;
|
|
103
121
|
case 'delete': verb = 'DELETE'; break;
|
|
@@ -105,6 +123,7 @@ function buildRequest<T extends RequestInit>(base: T, controller: string, endpoi
|
|
|
105
123
|
}
|
|
106
124
|
return {
|
|
107
125
|
...base,
|
|
126
|
+
redirect,
|
|
108
127
|
method: verb,
|
|
109
128
|
path: `${controller}:${endpoint}`,
|
|
110
129
|
controller,
|
|
@@ -235,6 +254,10 @@ export async function invokeFetch<T>(request: RpcRequest, ...params: unknown[]):
|
|
|
235
254
|
url.searchParams.append(key, value);
|
|
236
255
|
}
|
|
237
256
|
|
|
257
|
+
if (core.redirect && triggerRedirect(globalThis, url)) {
|
|
258
|
+
return Promise.resolve(undefined!);
|
|
259
|
+
}
|
|
260
|
+
|
|
238
261
|
let resolved: Response | undefined;
|
|
239
262
|
for (let i = 0; i <= (core.retriesOnConnectFailure ?? 0); i += 1) {
|
|
240
263
|
try {
|