@emeryld/rrroutes-client 2.2.11 → 2.2.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 +67 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +67 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -33,11 +33,20 @@ import {
|
|
|
33
33
|
useMutation,
|
|
34
34
|
useQuery
|
|
35
35
|
} from "@tanstack/react-query";
|
|
36
|
+
import { z } from "zod";
|
|
36
37
|
import {
|
|
37
38
|
buildCacheKey,
|
|
38
39
|
compilePath
|
|
39
40
|
} from "@emeryld/rrroutes-contract";
|
|
40
41
|
var toUpper = (m) => m.toUpperCase();
|
|
42
|
+
var defaultFeedQuerySchema = z.object({
|
|
43
|
+
cursor: z.string().optional(),
|
|
44
|
+
limit: z.coerce.number().min(1).max(100).default(20)
|
|
45
|
+
});
|
|
46
|
+
var defaultFeedOutputSchema = z.object({
|
|
47
|
+
items: z.array(z.unknown()),
|
|
48
|
+
nextCursor: z.string().optional()
|
|
49
|
+
});
|
|
41
50
|
function zParse(value, schema) {
|
|
42
51
|
return schema ? schema.parse(value) : value;
|
|
43
52
|
}
|
|
@@ -139,6 +148,37 @@ function extractArgs(args) {
|
|
|
139
148
|
function toArgsTuple(args) {
|
|
140
149
|
return typeof args === "undefined" ? [] : [args];
|
|
141
150
|
}
|
|
151
|
+
function augmentFeedQuerySchema(schema) {
|
|
152
|
+
if (!schema) return defaultFeedQuerySchema;
|
|
153
|
+
if (schema instanceof z.ZodObject) {
|
|
154
|
+
const shape = schema.shape ? schema.shape : schema._def?.shape?.();
|
|
155
|
+
return schema.extend({
|
|
156
|
+
...shape ?? {},
|
|
157
|
+
cursor: defaultFeedQuerySchema.shape.cursor,
|
|
158
|
+
limit: defaultFeedQuerySchema.shape.limit
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
return z.intersection(schema, defaultFeedQuerySchema);
|
|
162
|
+
}
|
|
163
|
+
function augmentFeedOutputSchema(schema) {
|
|
164
|
+
if (!schema) return defaultFeedOutputSchema;
|
|
165
|
+
if (schema instanceof z.ZodObject) {
|
|
166
|
+
const shape = schema.shape ? schema.shape : schema._def?.shape?.();
|
|
167
|
+
const hasItems = Boolean(shape?.items);
|
|
168
|
+
if (hasItems) return schema.extend({ nextCursor: z.string().optional() });
|
|
169
|
+
return z.object({
|
|
170
|
+
items: z.array(schema),
|
|
171
|
+
nextCursor: z.string().optional()
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
if (schema instanceof z.ZodArray) {
|
|
175
|
+
return z.object({
|
|
176
|
+
items: schema,
|
|
177
|
+
nextCursor: z.string().optional()
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
return defaultFeedOutputSchema;
|
|
181
|
+
}
|
|
142
182
|
function buildUrl(leaf, baseUrl, params, query) {
|
|
143
183
|
const normalizedParams = zParse(params, leaf.cfg.paramsSchema);
|
|
144
184
|
const normalizedQuery = zParse(query, leaf.cfg.querySchema);
|
|
@@ -167,8 +207,13 @@ function createRouteClient(opts) {
|
|
|
167
207
|
function buildInternal(leaf, rqOpts, meta) {
|
|
168
208
|
const isGet = leaf.method === "get";
|
|
169
209
|
const isFeed = !!leaf.cfg.feed;
|
|
210
|
+
const leafCfg = isFeed ? {
|
|
211
|
+
...leaf.cfg,
|
|
212
|
+
querySchema: augmentFeedQuerySchema(leaf.cfg.querySchema),
|
|
213
|
+
outputSchema: augmentFeedOutputSchema(leaf.cfg.outputSchema)
|
|
214
|
+
} : leaf.cfg;
|
|
170
215
|
const method = toUpper(leaf.method);
|
|
171
|
-
const expectsArgs = Boolean(
|
|
216
|
+
const expectsArgs = Boolean(leafCfg.paramsSchema || leafCfg.querySchema);
|
|
172
217
|
const leafLabel = `${leaf.method.toUpperCase()} ${String(leaf.path)}`;
|
|
173
218
|
const debugName = meta?.name;
|
|
174
219
|
const emit = (event) => emitDebug(event, debugName);
|
|
@@ -212,13 +257,18 @@ function createRouteClient(opts) {
|
|
|
212
257
|
const a = extractArgs(tuple);
|
|
213
258
|
const params = a?.params;
|
|
214
259
|
const query = options?.queryOverride ?? a?.query;
|
|
215
|
-
const { url, normalizedQuery, normalizedParams } = buildUrl(
|
|
260
|
+
const { url, normalizedQuery, normalizedParams } = buildUrl(
|
|
261
|
+
{ ...leaf, cfg: leafCfg },
|
|
262
|
+
baseUrl,
|
|
263
|
+
params,
|
|
264
|
+
query
|
|
265
|
+
);
|
|
216
266
|
let payload;
|
|
217
|
-
const acceptsBody = Boolean(
|
|
267
|
+
const acceptsBody = Boolean(leafCfg.bodySchema);
|
|
218
268
|
const requiresBody = options?.requireBody ?? (!isGet && acceptsBody);
|
|
219
269
|
if (typeof options?.body !== "undefined") {
|
|
220
|
-
const normalizedBody = zParse(options.body,
|
|
221
|
-
const isMultipart = Array.isArray(
|
|
270
|
+
const normalizedBody = zParse(options.body, leafCfg.bodySchema);
|
|
271
|
+
const isMultipart = Array.isArray(leafCfg.bodyFiles) && leafCfg.bodyFiles.length > 0;
|
|
222
272
|
payload = isMultipart ? toFormData(normalizedBody) : normalizedBody;
|
|
223
273
|
} else if (requiresBody) {
|
|
224
274
|
throw new Error("Body is required when invoking a mutation fetch.");
|
|
@@ -242,7 +292,7 @@ function createRouteClient(opts) {
|
|
|
242
292
|
const out = await fetcher(
|
|
243
293
|
payload === void 0 ? { url, method } : { url, method, body: payload }
|
|
244
294
|
);
|
|
245
|
-
const parsed = zParse(out,
|
|
295
|
+
const parsed = zParse(out, leafCfg.outputSchema);
|
|
246
296
|
emit(
|
|
247
297
|
decorateDebugEvent(
|
|
248
298
|
{
|
|
@@ -278,7 +328,7 @@ function createRouteClient(opts) {
|
|
|
278
328
|
}
|
|
279
329
|
};
|
|
280
330
|
const fetchGet = (...tupleWithBody) => {
|
|
281
|
-
const acceptsBody = Boolean(
|
|
331
|
+
const acceptsBody = Boolean(leafCfg.bodySchema);
|
|
282
332
|
const tupleLength = tupleWithBody.length;
|
|
283
333
|
const maybeBodyIndex = expectsArgs ? 1 : 0;
|
|
284
334
|
const hasBodyCandidate = acceptsBody && tupleLength > maybeBodyIndex;
|
|
@@ -311,7 +361,12 @@ function createRouteClient(opts) {
|
|
|
311
361
|
},
|
|
312
362
|
[]
|
|
313
363
|
);
|
|
314
|
-
const { normalizedQuery, normalizedParams } = buildUrl(
|
|
364
|
+
const { normalizedQuery, normalizedParams } = buildUrl(
|
|
365
|
+
{ ...leaf, cfg: leafCfg },
|
|
366
|
+
baseUrl,
|
|
367
|
+
params,
|
|
368
|
+
query
|
|
369
|
+
);
|
|
315
370
|
const queryResult = useInfiniteQuery({
|
|
316
371
|
...buildOptions,
|
|
317
372
|
queryKey: getQueryKeys(...tuple),
|
|
@@ -365,7 +420,7 @@ function createRouteClient(opts) {
|
|
|
365
420
|
},
|
|
366
421
|
[]
|
|
367
422
|
);
|
|
368
|
-
buildUrl(leaf, baseUrl, params, query);
|
|
423
|
+
buildUrl({ ...leaf, cfg: leafCfg }, baseUrl, params, query);
|
|
369
424
|
const queryResult = useQuery({
|
|
370
425
|
...buildOptions,
|
|
371
426
|
queryKey: getQueryKeys(...tuple),
|
|
@@ -453,9 +508,9 @@ function toFormData(body) {
|
|
|
453
508
|
}
|
|
454
509
|
|
|
455
510
|
// src/sockets/socket.client.sys.ts
|
|
456
|
-
import { z } from "zod";
|
|
457
|
-
var roomValueSchema =
|
|
458
|
-
var buildRoomPayloadSchema = (metaSchema) =>
|
|
511
|
+
import { z as z2 } from "zod";
|
|
512
|
+
var roomValueSchema = z2.union([z2.array(z2.string()), z2.string()]);
|
|
513
|
+
var buildRoomPayloadSchema = (metaSchema) => z2.object({
|
|
459
514
|
rooms: roomValueSchema,
|
|
460
515
|
meta: metaSchema
|
|
461
516
|
});
|