@emeryld/rrroutes-client 2.6.10 → 2.6.12
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.cjs +202 -190
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +196 -184
- package/dist/index.mjs.map +1 -1
- package/dist/routesV3.client.batch.d.ts +13 -0
- package/dist/sockets/socket.client.context.client.d.ts +1 -1
- package/dist/sockets/socket.client.context.connection.d.ts +1 -1
- package/dist/sockets/socket.client.context.provider.d.ts +1 -1
- package/dist/sockets/socket.client.core.d.ts +5 -5
- package/dist/sockets/socketedRoute/socket.client.helper.rooms.d.ts +3 -3
- package/dist/sockets/socketedRoute/socket.client.helper.route.d.ts +12 -7
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -44,10 +44,145 @@ __export(index_exports, {
|
|
|
44
44
|
});
|
|
45
45
|
module.exports = __toCommonJS(index_exports);
|
|
46
46
|
|
|
47
|
-
// src/routesV3.client.ts
|
|
48
|
-
var
|
|
49
|
-
var
|
|
50
|
-
var
|
|
47
|
+
// src/routesV3.client.batch.ts
|
|
48
|
+
var import_rrroutes_contract = require("@emeryld/rrroutes-contract");
|
|
49
|
+
var import_react_query = require("@tanstack/react-query");
|
|
50
|
+
var import_react = require("react");
|
|
51
|
+
function buildBatchBranch(leaves, options, deps) {
|
|
52
|
+
const {
|
|
53
|
+
fetchRaw,
|
|
54
|
+
queryClient,
|
|
55
|
+
validateResponses,
|
|
56
|
+
getBuiltLeaf,
|
|
57
|
+
encodeLeafKey,
|
|
58
|
+
toArgsTuple: toArgsTuple2
|
|
59
|
+
} = deps;
|
|
60
|
+
const batchMethod = options.method ?? "POST";
|
|
61
|
+
const defaultHeaders = options.headers;
|
|
62
|
+
const getQueryKeys = (input) => {
|
|
63
|
+
const out = {};
|
|
64
|
+
const argsByLeaf = input ?? {};
|
|
65
|
+
for (const [alias, built] of Object.entries(leaves)) {
|
|
66
|
+
const args = argsByLeaf[alias];
|
|
67
|
+
out[alias] = built.getQueryKeys(...toArgsTuple2(args));
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
};
|
|
71
|
+
const invalidateBranch = async (input) => {
|
|
72
|
+
const argsByLeaf = input ?? {};
|
|
73
|
+
await Promise.all(
|
|
74
|
+
Object.entries(leaves).map(([alias, built]) => {
|
|
75
|
+
const args = argsByLeaf[alias];
|
|
76
|
+
return built.invalidate(...toArgsTuple2(args));
|
|
77
|
+
})
|
|
78
|
+
);
|
|
79
|
+
};
|
|
80
|
+
const setDataBranch = (input) => {
|
|
81
|
+
const out = {};
|
|
82
|
+
for (const [alias, def] of Object.entries(input)) {
|
|
83
|
+
if (!def) continue;
|
|
84
|
+
const built = leaves[alias];
|
|
85
|
+
if (!built) continue;
|
|
86
|
+
out[alias] = built.setData(def.updater, ...toArgsTuple2(def.args));
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
};
|
|
90
|
+
const fetchBranch = async (input) => {
|
|
91
|
+
const payload = {};
|
|
92
|
+
for (const [aliasRaw, built] of Object.entries(leaves)) {
|
|
93
|
+
const leaf = getBuiltLeaf(built);
|
|
94
|
+
const encodedLeaf = encodeLeafKey(leaf);
|
|
95
|
+
const alias = aliasRaw;
|
|
96
|
+
const branchInput = input[alias];
|
|
97
|
+
const query = branchInput?.query;
|
|
98
|
+
const params = branchInput?.params;
|
|
99
|
+
const body = branchInput?.body;
|
|
100
|
+
payload[aliasRaw] = {
|
|
101
|
+
encodedLeaf,
|
|
102
|
+
...params !== void 0 ? { params } : {},
|
|
103
|
+
...query !== void 0 ? { query } : {},
|
|
104
|
+
...body !== void 0 ? { body } : {}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
const batchResponse = await fetchRaw({
|
|
108
|
+
path: options.path,
|
|
109
|
+
method: batchMethod,
|
|
110
|
+
body: payload,
|
|
111
|
+
headers: defaultHeaders
|
|
112
|
+
});
|
|
113
|
+
const rawData = batchResponse.data;
|
|
114
|
+
if (!rawData || typeof rawData !== "object" || Array.isArray(rawData)) {
|
|
115
|
+
throw new Error(
|
|
116
|
+
"Batch response must be a plain object keyed by branch aliases."
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
const mapped = {};
|
|
120
|
+
for (const [aliasRaw, built] of Object.entries(leaves)) {
|
|
121
|
+
if (!(aliasRaw in rawData)) {
|
|
122
|
+
throw new Error(`Batch response missing alias "${aliasRaw}".`);
|
|
123
|
+
}
|
|
124
|
+
const leaf = getBuiltLeaf(built);
|
|
125
|
+
const rawLeafData = rawData[aliasRaw];
|
|
126
|
+
const parsedLeafData = validateResponses && leaf.cfg.outputSchema ? (0, import_rrroutes_contract.lowProfileParse)(leaf.cfg.outputSchema, rawLeafData) : rawLeafData;
|
|
127
|
+
if (validateResponses && !leaf.cfg.outputSchema) {
|
|
128
|
+
throw new Error(
|
|
129
|
+
`No output schema defined for leaf ${leaf.method.toUpperCase()} ${leaf.path}, cannot validate batch response.`
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
;
|
|
133
|
+
mapped[aliasRaw] = parsedLeafData;
|
|
134
|
+
}
|
|
135
|
+
return mapped;
|
|
136
|
+
};
|
|
137
|
+
const useEndpoint = (input, rqOpts) => {
|
|
138
|
+
const queryKeys = getQueryKeys(input);
|
|
139
|
+
const branchQueryKey = [
|
|
140
|
+
"batch",
|
|
141
|
+
String(batchMethod).toLowerCase(),
|
|
142
|
+
options.path,
|
|
143
|
+
queryKeys
|
|
144
|
+
];
|
|
145
|
+
const { onReceive, ...useQueryOptions } = rqOpts ?? {};
|
|
146
|
+
const listenersRef = (0, import_react.useRef)(/* @__PURE__ */ new Set());
|
|
147
|
+
const notifyOnReceive = (0, import_react.useCallback)(
|
|
148
|
+
(data) => {
|
|
149
|
+
onReceive?.(data);
|
|
150
|
+
listenersRef.current.forEach((listener) => listener(data));
|
|
151
|
+
},
|
|
152
|
+
[onReceive]
|
|
153
|
+
);
|
|
154
|
+
const registerOnReceive = (0, import_react.useCallback)(
|
|
155
|
+
(listener) => {
|
|
156
|
+
listenersRef.current.add(listener);
|
|
157
|
+
return () => {
|
|
158
|
+
listenersRef.current.delete(listener);
|
|
159
|
+
};
|
|
160
|
+
},
|
|
161
|
+
[]
|
|
162
|
+
);
|
|
163
|
+
const queryResult = (0, import_react_query.useQuery)(
|
|
164
|
+
{
|
|
165
|
+
...useQueryOptions,
|
|
166
|
+
queryKey: branchQueryKey,
|
|
167
|
+
placeholderData: useQueryOptions.placeholderData ?? import_react_query.keepPreviousData,
|
|
168
|
+
queryFn: async () => {
|
|
169
|
+
const result = await fetchBranch(input);
|
|
170
|
+
notifyOnReceive(result);
|
|
171
|
+
return result;
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
queryClient
|
|
175
|
+
);
|
|
176
|
+
return { ...queryResult, onReceive: registerOnReceive };
|
|
177
|
+
};
|
|
178
|
+
return {
|
|
179
|
+
fetch: fetchBranch,
|
|
180
|
+
useEndpoint,
|
|
181
|
+
getQueryKeys,
|
|
182
|
+
invalidate: invalidateBranch,
|
|
183
|
+
setData: setDataBranch
|
|
184
|
+
};
|
|
185
|
+
}
|
|
51
186
|
|
|
52
187
|
// src/routesV3.client.fetch.ts
|
|
53
188
|
var HttpError = class extends Error {
|
|
@@ -101,12 +236,12 @@ var defaultFetcher = async (req) => {
|
|
|
101
236
|
};
|
|
102
237
|
|
|
103
238
|
// src/routesV3.client.get.ts
|
|
104
|
-
var
|
|
105
|
-
var
|
|
106
|
-
var
|
|
239
|
+
var import_rrroutes_contract3 = require("@emeryld/rrroutes-contract");
|
|
240
|
+
var import_react_query2 = require("@tanstack/react-query");
|
|
241
|
+
var import_react2 = require("react");
|
|
107
242
|
|
|
108
243
|
// src/routesV3.client.shared.ts
|
|
109
|
-
var
|
|
244
|
+
var import_rrroutes_contract2 = require("@emeryld/rrroutes-contract");
|
|
110
245
|
var toUpper = (m) => m.toUpperCase();
|
|
111
246
|
function toSearchString(query) {
|
|
112
247
|
if (!query) return "";
|
|
@@ -156,9 +291,9 @@ function toArgsTuple(args) {
|
|
|
156
291
|
return typeof args === "undefined" ? [] : [args];
|
|
157
292
|
}
|
|
158
293
|
function buildUrl(leaf, baseUrl, params, query) {
|
|
159
|
-
const normalizedParams = leaf.cfg.paramsSchema ? (0,
|
|
160
|
-
const normalizedQuery = leaf.cfg.querySchema ? (0,
|
|
161
|
-
const path = (0,
|
|
294
|
+
const normalizedParams = leaf.cfg.paramsSchema ? (0, import_rrroutes_contract2.lowProfileParse)(leaf.cfg.paramsSchema, params) : {};
|
|
295
|
+
const normalizedQuery = leaf.cfg.querySchema ? (0, import_rrroutes_contract2.lowProfileParse)(leaf.cfg.querySchema, query) : {};
|
|
296
|
+
const path = (0, import_rrroutes_contract2.compilePath)(
|
|
162
297
|
leaf.path,
|
|
163
298
|
normalizedParams ?? {}
|
|
164
299
|
);
|
|
@@ -325,7 +460,7 @@ function compileRawPath(path, params) {
|
|
|
325
460
|
if (placeholders.size === 0) {
|
|
326
461
|
return path;
|
|
327
462
|
}
|
|
328
|
-
return (0,
|
|
463
|
+
return (0, import_rrroutes_contract2.compilePath)(path, paramObj);
|
|
329
464
|
}
|
|
330
465
|
|
|
331
466
|
// src/routesV3.client.get.ts
|
|
@@ -348,7 +483,7 @@ function buildGetLeaf(leaf, rqOpts, env) {
|
|
|
348
483
|
const a = extractArgs(tuple);
|
|
349
484
|
const params = a?.params;
|
|
350
485
|
const query = a?.query;
|
|
351
|
-
return (0,
|
|
486
|
+
return (0, import_rrroutes_contract3.buildCacheKey)({
|
|
352
487
|
leaf,
|
|
353
488
|
params,
|
|
354
489
|
query
|
|
@@ -392,7 +527,7 @@ function buildGetLeaf(leaf, rqOpts, env) {
|
|
|
392
527
|
options.body,
|
|
393
528
|
leafCfg.bodyFiles
|
|
394
529
|
);
|
|
395
|
-
const normalizedBody = leafCfg.bodySchema ? (0,
|
|
530
|
+
const normalizedBody = leafCfg.bodySchema ? (0, import_rrroutes_contract3.lowProfileParse)(leafCfg.bodySchema, regularBody) : regularBody;
|
|
396
531
|
if (isMultipart && normalizedBody && typeof normalizedBody === "object") {
|
|
397
532
|
payload = toFormData(
|
|
398
533
|
{
|
|
@@ -449,7 +584,7 @@ function buildGetLeaf(leaf, rqOpts, env) {
|
|
|
449
584
|
`No output schema defined for leaf ${leafLabel}, cannot validate response.`
|
|
450
585
|
);
|
|
451
586
|
}
|
|
452
|
-
out.data = (0,
|
|
587
|
+
out.data = (0, import_rrroutes_contract3.lowProfileParse)(
|
|
453
588
|
leafCfg.outputSchema,
|
|
454
589
|
out.data
|
|
455
590
|
);
|
|
@@ -516,12 +651,12 @@ function buildGetLeaf(leaf, rqOpts, env) {
|
|
|
516
651
|
keys: queryKeys
|
|
517
652
|
});
|
|
518
653
|
const buildOptions = rqOpts ?? {};
|
|
519
|
-
const listenersRef = (0,
|
|
520
|
-
const notifyOnReceive = (0,
|
|
654
|
+
const listenersRef = (0, import_react2.useRef)(/* @__PURE__ */ new Set());
|
|
655
|
+
const notifyOnReceive = (0, import_react2.useCallback)((data) => {
|
|
521
656
|
buildOptions?.onReceive?.(data);
|
|
522
657
|
listenersRef.current.forEach((listener) => listener(data));
|
|
523
658
|
}, []);
|
|
524
|
-
const registerOnReceive = (0,
|
|
659
|
+
const registerOnReceive = (0, import_react2.useCallback)(
|
|
525
660
|
(listener) => {
|
|
526
661
|
listenersRef.current.add(listener);
|
|
527
662
|
return () => {
|
|
@@ -530,11 +665,11 @@ function buildGetLeaf(leaf, rqOpts, env) {
|
|
|
530
665
|
},
|
|
531
666
|
[]
|
|
532
667
|
);
|
|
533
|
-
const queryResult = (0,
|
|
668
|
+
const queryResult = (0, import_react_query2.useQuery)(
|
|
534
669
|
{
|
|
535
670
|
...buildOptions,
|
|
536
671
|
queryKey: getQueryKeys(...tuple),
|
|
537
|
-
placeholderData:
|
|
672
|
+
placeholderData: import_react_query2.keepPreviousData,
|
|
538
673
|
queryFn: () => fetchEndpoint(tuple, {
|
|
539
674
|
onReceive: notifyOnReceive
|
|
540
675
|
})
|
|
@@ -553,9 +688,9 @@ function buildGetLeaf(leaf, rqOpts, env) {
|
|
|
553
688
|
}
|
|
554
689
|
|
|
555
690
|
// src/routesV3.client.infiniteGet.ts
|
|
556
|
-
var
|
|
557
|
-
var
|
|
558
|
-
var
|
|
691
|
+
var import_rrroutes_contract4 = require("@emeryld/rrroutes-contract");
|
|
692
|
+
var import_react_query3 = require("@tanstack/react-query");
|
|
693
|
+
var import_react3 = require("react");
|
|
559
694
|
function mergePageOutputs(prev, next) {
|
|
560
695
|
if (prev == null) return next;
|
|
561
696
|
if (next == null) return prev;
|
|
@@ -621,7 +756,7 @@ function buildInfiniteGetLeaf(leaf, rqOpts, env) {
|
|
|
621
756
|
const params = a?.params;
|
|
622
757
|
const query = a?.query;
|
|
623
758
|
const qForKey = stripKey(query, feedCursorParam);
|
|
624
|
-
return (0,
|
|
759
|
+
return (0, import_rrroutes_contract4.buildCacheKey)({
|
|
625
760
|
leaf,
|
|
626
761
|
params,
|
|
627
762
|
query: qForKey
|
|
@@ -665,7 +800,7 @@ function buildInfiniteGetLeaf(leaf, rqOpts, env) {
|
|
|
665
800
|
options.body,
|
|
666
801
|
leafCfg.bodyFiles
|
|
667
802
|
);
|
|
668
|
-
const normalizedBody = leafCfg.bodySchema ? (0,
|
|
803
|
+
const normalizedBody = leafCfg.bodySchema ? (0, import_rrroutes_contract4.lowProfileParse)(leafCfg.bodySchema, regularBody) : regularBody;
|
|
669
804
|
if (isMultipart && normalizedBody && typeof normalizedBody === "object") {
|
|
670
805
|
payload = toFormData(
|
|
671
806
|
{
|
|
@@ -722,7 +857,7 @@ function buildInfiniteGetLeaf(leaf, rqOpts, env) {
|
|
|
722
857
|
`No output schema defined for leaf ${leafLabel}, cannot validate response.`
|
|
723
858
|
);
|
|
724
859
|
}
|
|
725
|
-
out.data = (0,
|
|
860
|
+
out.data = (0, import_rrroutes_contract4.lowProfileParse)(
|
|
726
861
|
leafCfg.outputSchema,
|
|
727
862
|
out.data
|
|
728
863
|
);
|
|
@@ -791,12 +926,12 @@ function buildInfiniteGetLeaf(leaf, rqOpts, env) {
|
|
|
791
926
|
const params = args?.params;
|
|
792
927
|
const query = args?.query;
|
|
793
928
|
const buildOptions = feedQueryOptions ?? {};
|
|
794
|
-
const listenersRef = (0,
|
|
795
|
-
const notifyOnReceive = (0,
|
|
929
|
+
const listenersRef = (0, import_react3.useRef)(/* @__PURE__ */ new Set());
|
|
930
|
+
const notifyOnReceive = (0, import_react3.useCallback)((data) => {
|
|
796
931
|
buildOptions?.onReceive?.(data);
|
|
797
932
|
listenersRef.current.forEach((listener) => listener(data));
|
|
798
933
|
}, []);
|
|
799
|
-
const registerOnReceive = (0,
|
|
934
|
+
const registerOnReceive = (0, import_react3.useCallback)(
|
|
800
935
|
(listener) => {
|
|
801
936
|
listenersRef.current.add(listener);
|
|
802
937
|
return () => {
|
|
@@ -811,10 +946,10 @@ function buildInfiniteGetLeaf(leaf, rqOpts, env) {
|
|
|
811
946
|
params,
|
|
812
947
|
query
|
|
813
948
|
);
|
|
814
|
-
const queryResult = (0,
|
|
949
|
+
const queryResult = (0, import_react_query3.useInfiniteQuery)(
|
|
815
950
|
{
|
|
816
951
|
...buildOptions,
|
|
817
|
-
placeholderData: buildOptions.placeholderData ??
|
|
952
|
+
placeholderData: buildOptions.placeholderData ?? import_react_query3.keepPreviousData,
|
|
818
953
|
initialPageParam: feedInitialPageParam,
|
|
819
954
|
getNextPageParam: (lastPage) => cursorFromPage(lastPage),
|
|
820
955
|
queryKey: queryKeys,
|
|
@@ -907,9 +1042,9 @@ function buildInfiniteGetLeaf(leaf, rqOpts, env) {
|
|
|
907
1042
|
}
|
|
908
1043
|
|
|
909
1044
|
// src/routesV3.client.mutation.ts
|
|
910
|
-
var
|
|
911
|
-
var
|
|
912
|
-
var
|
|
1045
|
+
var import_rrroutes_contract5 = require("@emeryld/rrroutes-contract");
|
|
1046
|
+
var import_react_query4 = require("@tanstack/react-query");
|
|
1047
|
+
var import_react4 = require("react");
|
|
913
1048
|
function buildMutationLeaf(leaf, rqOpts, env) {
|
|
914
1049
|
const leafCfg = leaf.cfg;
|
|
915
1050
|
const method = toUpper(leaf.method);
|
|
@@ -929,7 +1064,7 @@ function buildMutationLeaf(leaf, rqOpts, env) {
|
|
|
929
1064
|
const a = extractArgs(tuple);
|
|
930
1065
|
const params = a?.params;
|
|
931
1066
|
const query = a?.query;
|
|
932
|
-
return (0,
|
|
1067
|
+
return (0, import_rrroutes_contract5.buildCacheKey)({
|
|
933
1068
|
leaf,
|
|
934
1069
|
params,
|
|
935
1070
|
query
|
|
@@ -973,7 +1108,7 @@ function buildMutationLeaf(leaf, rqOpts, env) {
|
|
|
973
1108
|
options.body,
|
|
974
1109
|
leafCfg.bodyFiles
|
|
975
1110
|
);
|
|
976
|
-
const normalizedBody = leafCfg.bodySchema ? (0,
|
|
1111
|
+
const normalizedBody = leafCfg.bodySchema ? (0, import_rrroutes_contract5.lowProfileParse)(leafCfg.bodySchema, regularBody) : regularBody;
|
|
977
1112
|
if (isMultipart && normalizedBody && typeof normalizedBody === "object") {
|
|
978
1113
|
payload = toFormData(
|
|
979
1114
|
{
|
|
@@ -1030,7 +1165,7 @@ function buildMutationLeaf(leaf, rqOpts, env) {
|
|
|
1030
1165
|
`No output schema defined for leaf ${leafLabel}, cannot validate response.`
|
|
1031
1166
|
);
|
|
1032
1167
|
}
|
|
1033
|
-
out.data = (0,
|
|
1168
|
+
out.data = (0, import_rrroutes_contract5.lowProfileParse)(
|
|
1034
1169
|
leafCfg.outputSchema,
|
|
1035
1170
|
out.data
|
|
1036
1171
|
);
|
|
@@ -1097,11 +1232,11 @@ function buildMutationLeaf(leaf, rqOpts, env) {
|
|
|
1097
1232
|
variant: "mutation",
|
|
1098
1233
|
keys: mutationKey
|
|
1099
1234
|
});
|
|
1100
|
-
const listenersRef = (0,
|
|
1101
|
-
const notifyListeners = (0,
|
|
1235
|
+
const listenersRef = (0, import_react4.useRef)(/* @__PURE__ */ new Set());
|
|
1236
|
+
const notifyListeners = (0, import_react4.useCallback)((data) => {
|
|
1102
1237
|
listenersRef.current.forEach((listener) => listener(data));
|
|
1103
1238
|
}, []);
|
|
1104
|
-
const registerOnReceive = (0,
|
|
1239
|
+
const registerOnReceive = (0, import_react4.useCallback)(
|
|
1105
1240
|
(listener) => {
|
|
1106
1241
|
listenersRef.current.add(listener);
|
|
1107
1242
|
return () => {
|
|
@@ -1110,7 +1245,7 @@ function buildMutationLeaf(leaf, rqOpts, env) {
|
|
|
1110
1245
|
},
|
|
1111
1246
|
[]
|
|
1112
1247
|
);
|
|
1113
|
-
const mutationResult = (0,
|
|
1248
|
+
const mutationResult = (0, import_react_query4.useMutation)(
|
|
1114
1249
|
{
|
|
1115
1250
|
...mutationBuildOptions ?? {},
|
|
1116
1251
|
mutationKey,
|
|
@@ -1340,138 +1475,14 @@ function createRouteClient(opts) {
|
|
|
1340
1475
|
throw error;
|
|
1341
1476
|
}
|
|
1342
1477
|
};
|
|
1343
|
-
const buildBranchInternal = (leaves, options) => {
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
out[alias] = built.getQueryKeys(...toArgsTuple2(args));
|
|
1352
|
-
}
|
|
1353
|
-
return out;
|
|
1354
|
-
};
|
|
1355
|
-
const invalidateBranch = async (input) => {
|
|
1356
|
-
const argsByLeaf = input ?? {};
|
|
1357
|
-
await Promise.all(
|
|
1358
|
-
Object.entries(leaves).map(([alias, built]) => {
|
|
1359
|
-
const args = argsByLeaf[alias];
|
|
1360
|
-
return built.invalidate(...toArgsTuple2(args));
|
|
1361
|
-
})
|
|
1362
|
-
);
|
|
1363
|
-
};
|
|
1364
|
-
const setDataBranch = (input) => {
|
|
1365
|
-
const out = {};
|
|
1366
|
-
for (const [alias, def] of Object.entries(input)) {
|
|
1367
|
-
if (!def) continue;
|
|
1368
|
-
const built = leaves[alias];
|
|
1369
|
-
if (!built) continue;
|
|
1370
|
-
out[alias] = built.setData(
|
|
1371
|
-
def.updater,
|
|
1372
|
-
...toArgsTuple2(def.args)
|
|
1373
|
-
);
|
|
1374
|
-
}
|
|
1375
|
-
return out;
|
|
1376
|
-
};
|
|
1377
|
-
const fetchBranch = async (input) => {
|
|
1378
|
-
const payload = {};
|
|
1379
|
-
for (const [aliasRaw, built] of Object.entries(leaves)) {
|
|
1380
|
-
const leaf = getBuiltLeaf(built);
|
|
1381
|
-
const encodedLeaf = encodeLeafKey(leaf);
|
|
1382
|
-
const alias = aliasRaw;
|
|
1383
|
-
const branchInput = input[alias];
|
|
1384
|
-
const query = branchInput?.query;
|
|
1385
|
-
const params = branchInput?.params;
|
|
1386
|
-
const body = branchInput?.body;
|
|
1387
|
-
payload[aliasRaw] = {
|
|
1388
|
-
encodedLeaf,
|
|
1389
|
-
...params !== void 0 ? { params } : {},
|
|
1390
|
-
...query !== void 0 ? { query } : {},
|
|
1391
|
-
...body !== void 0 ? { body } : {}
|
|
1392
|
-
};
|
|
1393
|
-
}
|
|
1394
|
-
const batchResponse = await fetchRaw({
|
|
1395
|
-
path: options.path,
|
|
1396
|
-
method: batchMethod,
|
|
1397
|
-
body: payload,
|
|
1398
|
-
headers: defaultHeaders
|
|
1399
|
-
});
|
|
1400
|
-
const rawData = batchResponse.data;
|
|
1401
|
-
if (!rawData || typeof rawData !== "object" || Array.isArray(rawData)) {
|
|
1402
|
-
throw new Error(
|
|
1403
|
-
"Batch response must be a plain object keyed by branch aliases."
|
|
1404
|
-
);
|
|
1405
|
-
}
|
|
1406
|
-
const mapped = {};
|
|
1407
|
-
for (const [aliasRaw, built] of Object.entries(leaves)) {
|
|
1408
|
-
if (!(aliasRaw in rawData)) {
|
|
1409
|
-
throw new Error(`Batch response missing alias "${aliasRaw}".`);
|
|
1410
|
-
}
|
|
1411
|
-
const leaf = getBuiltLeaf(built);
|
|
1412
|
-
const rawLeafData = rawData[aliasRaw];
|
|
1413
|
-
const parsedLeafData = validateResponses && leaf.cfg.outputSchema ? (0, import_rrroutes_contract5.lowProfileParse)(leaf.cfg.outputSchema, rawLeafData) : rawLeafData;
|
|
1414
|
-
if (validateResponses && !leaf.cfg.outputSchema) {
|
|
1415
|
-
throw new Error(
|
|
1416
|
-
`No output schema defined for leaf ${leaf.method.toUpperCase()} ${leaf.path}, cannot validate batch response.`
|
|
1417
|
-
);
|
|
1418
|
-
}
|
|
1419
|
-
;
|
|
1420
|
-
mapped[aliasRaw] = parsedLeafData;
|
|
1421
|
-
}
|
|
1422
|
-
return mapped;
|
|
1423
|
-
};
|
|
1424
|
-
const useEndpoint = (input, rqOpts) => {
|
|
1425
|
-
const queryKeys = getQueryKeys(input);
|
|
1426
|
-
const branchQueryKey = [
|
|
1427
|
-
"batch",
|
|
1428
|
-
String(batchMethod).toLowerCase(),
|
|
1429
|
-
options.path,
|
|
1430
|
-
queryKeys
|
|
1431
|
-
];
|
|
1432
|
-
const { onReceive, ...useQueryOptions } = rqOpts ?? {};
|
|
1433
|
-
const listenersRef = (0, import_react4.useRef)(
|
|
1434
|
-
/* @__PURE__ */ new Set()
|
|
1435
|
-
);
|
|
1436
|
-
const notifyOnReceive = (0, import_react4.useCallback)(
|
|
1437
|
-
(data) => {
|
|
1438
|
-
onReceive?.(data);
|
|
1439
|
-
listenersRef.current.forEach((listener) => listener(data));
|
|
1440
|
-
},
|
|
1441
|
-
[onReceive]
|
|
1442
|
-
);
|
|
1443
|
-
const registerOnReceive = (0, import_react4.useCallback)(
|
|
1444
|
-
(listener) => {
|
|
1445
|
-
listenersRef.current.add(listener);
|
|
1446
|
-
return () => {
|
|
1447
|
-
listenersRef.current.delete(listener);
|
|
1448
|
-
};
|
|
1449
|
-
},
|
|
1450
|
-
[]
|
|
1451
|
-
);
|
|
1452
|
-
const queryResult = (0, import_react_query4.useQuery)(
|
|
1453
|
-
{
|
|
1454
|
-
...useQueryOptions,
|
|
1455
|
-
queryKey: branchQueryKey,
|
|
1456
|
-
placeholderData: useQueryOptions.placeholderData ?? import_react_query4.keepPreviousData,
|
|
1457
|
-
queryFn: async () => {
|
|
1458
|
-
const result = await fetchBranch(input);
|
|
1459
|
-
notifyOnReceive(result);
|
|
1460
|
-
return result;
|
|
1461
|
-
}
|
|
1462
|
-
},
|
|
1463
|
-
queryClient
|
|
1464
|
-
);
|
|
1465
|
-
return { ...queryResult, onReceive: registerOnReceive };
|
|
1466
|
-
};
|
|
1467
|
-
return {
|
|
1468
|
-
fetch: fetchBranch,
|
|
1469
|
-
useEndpoint,
|
|
1470
|
-
getQueryKeys,
|
|
1471
|
-
invalidate: invalidateBranch,
|
|
1472
|
-
setData: setDataBranch
|
|
1473
|
-
};
|
|
1474
|
-
};
|
|
1478
|
+
const buildBranchInternal = (leaves, options) => buildBatchBranch(leaves, options, {
|
|
1479
|
+
fetchRaw,
|
|
1480
|
+
queryClient,
|
|
1481
|
+
validateResponses,
|
|
1482
|
+
getBuiltLeaf,
|
|
1483
|
+
encodeLeafKey,
|
|
1484
|
+
toArgsTuple: toArgsTuple2
|
|
1485
|
+
});
|
|
1475
1486
|
return {
|
|
1476
1487
|
queryClient,
|
|
1477
1488
|
invalidate,
|
|
@@ -1497,8 +1508,9 @@ var React = __toESM(require("react"), 1);
|
|
|
1497
1508
|
var SocketCtx = React.createContext(null);
|
|
1498
1509
|
function useSocketClient() {
|
|
1499
1510
|
const ctx = React.useContext(SocketCtx);
|
|
1500
|
-
if (!ctx)
|
|
1501
|
-
|
|
1511
|
+
if (!ctx) {
|
|
1512
|
+
return null;
|
|
1513
|
+
}
|
|
1502
1514
|
return ctx;
|
|
1503
1515
|
}
|
|
1504
1516
|
|
|
@@ -1527,6 +1539,7 @@ function useSocketConnection(args) {
|
|
|
1527
1539
|
[]
|
|
1528
1540
|
);
|
|
1529
1541
|
React2.useEffect(() => {
|
|
1542
|
+
if (!client) return;
|
|
1530
1543
|
if (autoJoin && normalizedRooms.length > 0)
|
|
1531
1544
|
void client.joinRooms(normalizedRooms, args.joinMeta).catch((error) => reportAsyncError("joinRooms", error));
|
|
1532
1545
|
const unsubscribe = client.on(event, (payload, meta) => {
|
|
@@ -2716,12 +2729,12 @@ function mergeRoomState(prev, toRoomsResult) {
|
|
|
2716
2729
|
leaveMeta: toRoomsResult.leaveMeta ?? prev.leaveMeta
|
|
2717
2730
|
};
|
|
2718
2731
|
}
|
|
2719
|
-
function roomsFromData(data, toRooms) {
|
|
2732
|
+
function roomsFromData(data, args, toRooms) {
|
|
2720
2733
|
if (data == null) return { rooms: [] };
|
|
2721
2734
|
let state = { rooms: [] };
|
|
2722
2735
|
const add = (input) => {
|
|
2723
2736
|
const mergeForValue = (value) => {
|
|
2724
|
-
state = mergeRoomState(state, toRooms(value));
|
|
2737
|
+
state = mergeRoomState(state, toRooms(value, args));
|
|
2725
2738
|
};
|
|
2726
2739
|
if (Array.isArray(input)) {
|
|
2727
2740
|
input.forEach((entry) => mergeForValue(entry));
|
|
@@ -2746,27 +2759,22 @@ function roomsFromData(data, toRooms) {
|
|
|
2746
2759
|
}
|
|
2747
2760
|
|
|
2748
2761
|
// src/sockets/socketedRoute/socket.client.helper.route.ts
|
|
2749
|
-
function isSocketClientUnavailableError(err) {
|
|
2750
|
-
return err instanceof Error && err.message.includes("SocketClient not found");
|
|
2751
|
-
}
|
|
2752
2762
|
function buildSocketedRoute(options) {
|
|
2753
2763
|
const { built, toRooms, applySocket, useSocketClient: useSocketClient2, debug } = options;
|
|
2754
2764
|
const { useEndpoint: useInnerEndpoint, ...rest } = built;
|
|
2755
2765
|
const useEndpoint = (...useArgs) => {
|
|
2756
|
-
|
|
2766
|
+
const client = useSocketClient2();
|
|
2757
2767
|
let socketClientError = null;
|
|
2758
|
-
try {
|
|
2759
|
-
client = useSocketClient2();
|
|
2760
|
-
} catch (err) {
|
|
2761
|
-
if (!isSocketClientUnavailableError(err)) throw err;
|
|
2762
|
-
socketClientError = err;
|
|
2763
|
-
}
|
|
2764
2768
|
const endpointResult = useInnerEndpoint(
|
|
2765
2769
|
...useArgs
|
|
2766
2770
|
);
|
|
2767
2771
|
const argsKey = (0, import_react5.useMemo)(() => safeJsonKey(useArgs[0] ?? null), [useArgs]);
|
|
2768
2772
|
const [roomState, setRoomState] = (0, import_react5.useState)(
|
|
2769
|
-
() => roomsFromData(
|
|
2773
|
+
() => roomsFromData(
|
|
2774
|
+
endpointResult.data,
|
|
2775
|
+
useArgs[0],
|
|
2776
|
+
toRooms
|
|
2777
|
+
)
|
|
2770
2778
|
);
|
|
2771
2779
|
const renderCountRef = (0, import_react5.useRef)(0);
|
|
2772
2780
|
const clientReadyRef = (0, import_react5.useRef)(null);
|
|
@@ -2820,7 +2828,10 @@ function buildSocketedRoute(options) {
|
|
|
2820
2828
|
});
|
|
2821
2829
|
const unsubscribe = endpointResult.onReceive((data) => {
|
|
2822
2830
|
setRoomState((prev) => {
|
|
2823
|
-
const next = mergeRoomState(
|
|
2831
|
+
const next = mergeRoomState(
|
|
2832
|
+
prev,
|
|
2833
|
+
toRoomsRef.current(data, useArgs[0])
|
|
2834
|
+
);
|
|
2824
2835
|
return roomStateEqual(prev, next) ? prev : next;
|
|
2825
2836
|
});
|
|
2826
2837
|
});
|
|
@@ -2837,7 +2848,7 @@ function buildSocketedRoute(options) {
|
|
|
2837
2848
|
toRoomsRef: describeObjectReference(toRooms)
|
|
2838
2849
|
}
|
|
2839
2850
|
});
|
|
2840
|
-
const next = roomsFromData(endpointDataRef.current, toRooms);
|
|
2851
|
+
const next = roomsFromData(endpointDataRef.current, useArgs[0], toRooms);
|
|
2841
2852
|
setRoomState((prev) => roomStateEqual(prev, next) ? prev : next);
|
|
2842
2853
|
}, [argsKey, toRooms, debug]);
|
|
2843
2854
|
(0, import_react5.useEffect)(() => {
|
|
@@ -2967,6 +2978,7 @@ function buildSocketedRoute(options) {
|
|
|
2967
2978
|
}
|
|
2968
2979
|
const nextRoomState = roomsFromData(
|
|
2969
2980
|
next,
|
|
2981
|
+
useArgs[0],
|
|
2970
2982
|
toRooms
|
|
2971
2983
|
);
|
|
2972
2984
|
setRoomState(
|