@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.cjs
CHANGED
|
@@ -71,8 +71,17 @@ var defaultFetcher = async (req) => {
|
|
|
71
71
|
// src/routesV3.client.index.ts
|
|
72
72
|
var import_react = require("react");
|
|
73
73
|
var import_react_query = require("@tanstack/react-query");
|
|
74
|
+
var import_zod = require("zod");
|
|
74
75
|
var import_rrroutes_contract = require("@emeryld/rrroutes-contract");
|
|
75
76
|
var toUpper = (m) => m.toUpperCase();
|
|
77
|
+
var defaultFeedQuerySchema = import_zod.z.object({
|
|
78
|
+
cursor: import_zod.z.string().optional(),
|
|
79
|
+
limit: import_zod.z.coerce.number().min(1).max(100).default(20)
|
|
80
|
+
});
|
|
81
|
+
var defaultFeedOutputSchema = import_zod.z.object({
|
|
82
|
+
items: import_zod.z.array(import_zod.z.unknown()),
|
|
83
|
+
nextCursor: import_zod.z.string().optional()
|
|
84
|
+
});
|
|
76
85
|
function zParse(value, schema) {
|
|
77
86
|
return schema ? schema.parse(value) : value;
|
|
78
87
|
}
|
|
@@ -174,6 +183,37 @@ function extractArgs(args) {
|
|
|
174
183
|
function toArgsTuple(args) {
|
|
175
184
|
return typeof args === "undefined" ? [] : [args];
|
|
176
185
|
}
|
|
186
|
+
function augmentFeedQuerySchema(schema) {
|
|
187
|
+
if (!schema) return defaultFeedQuerySchema;
|
|
188
|
+
if (schema instanceof import_zod.z.ZodObject) {
|
|
189
|
+
const shape = schema.shape ? schema.shape : schema._def?.shape?.();
|
|
190
|
+
return schema.extend({
|
|
191
|
+
...shape ?? {},
|
|
192
|
+
cursor: defaultFeedQuerySchema.shape.cursor,
|
|
193
|
+
limit: defaultFeedQuerySchema.shape.limit
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
return import_zod.z.intersection(schema, defaultFeedQuerySchema);
|
|
197
|
+
}
|
|
198
|
+
function augmentFeedOutputSchema(schema) {
|
|
199
|
+
if (!schema) return defaultFeedOutputSchema;
|
|
200
|
+
if (schema instanceof import_zod.z.ZodObject) {
|
|
201
|
+
const shape = schema.shape ? schema.shape : schema._def?.shape?.();
|
|
202
|
+
const hasItems = Boolean(shape?.items);
|
|
203
|
+
if (hasItems) return schema.extend({ nextCursor: import_zod.z.string().optional() });
|
|
204
|
+
return import_zod.z.object({
|
|
205
|
+
items: import_zod.z.array(schema),
|
|
206
|
+
nextCursor: import_zod.z.string().optional()
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
if (schema instanceof import_zod.z.ZodArray) {
|
|
210
|
+
return import_zod.z.object({
|
|
211
|
+
items: schema,
|
|
212
|
+
nextCursor: import_zod.z.string().optional()
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return defaultFeedOutputSchema;
|
|
216
|
+
}
|
|
177
217
|
function buildUrl(leaf, baseUrl, params, query) {
|
|
178
218
|
const normalizedParams = zParse(params, leaf.cfg.paramsSchema);
|
|
179
219
|
const normalizedQuery = zParse(query, leaf.cfg.querySchema);
|
|
@@ -202,8 +242,13 @@ function createRouteClient(opts) {
|
|
|
202
242
|
function buildInternal(leaf, rqOpts, meta) {
|
|
203
243
|
const isGet = leaf.method === "get";
|
|
204
244
|
const isFeed = !!leaf.cfg.feed;
|
|
245
|
+
const leafCfg = isFeed ? {
|
|
246
|
+
...leaf.cfg,
|
|
247
|
+
querySchema: augmentFeedQuerySchema(leaf.cfg.querySchema),
|
|
248
|
+
outputSchema: augmentFeedOutputSchema(leaf.cfg.outputSchema)
|
|
249
|
+
} : leaf.cfg;
|
|
205
250
|
const method = toUpper(leaf.method);
|
|
206
|
-
const expectsArgs = Boolean(
|
|
251
|
+
const expectsArgs = Boolean(leafCfg.paramsSchema || leafCfg.querySchema);
|
|
207
252
|
const leafLabel = `${leaf.method.toUpperCase()} ${String(leaf.path)}`;
|
|
208
253
|
const debugName = meta?.name;
|
|
209
254
|
const emit = (event) => emitDebug(event, debugName);
|
|
@@ -247,13 +292,18 @@ function createRouteClient(opts) {
|
|
|
247
292
|
const a = extractArgs(tuple);
|
|
248
293
|
const params = a?.params;
|
|
249
294
|
const query = options?.queryOverride ?? a?.query;
|
|
250
|
-
const { url, normalizedQuery, normalizedParams } = buildUrl(
|
|
295
|
+
const { url, normalizedQuery, normalizedParams } = buildUrl(
|
|
296
|
+
{ ...leaf, cfg: leafCfg },
|
|
297
|
+
baseUrl,
|
|
298
|
+
params,
|
|
299
|
+
query
|
|
300
|
+
);
|
|
251
301
|
let payload;
|
|
252
|
-
const acceptsBody = Boolean(
|
|
302
|
+
const acceptsBody = Boolean(leafCfg.bodySchema);
|
|
253
303
|
const requiresBody = options?.requireBody ?? (!isGet && acceptsBody);
|
|
254
304
|
if (typeof options?.body !== "undefined") {
|
|
255
|
-
const normalizedBody = zParse(options.body,
|
|
256
|
-
const isMultipart = Array.isArray(
|
|
305
|
+
const normalizedBody = zParse(options.body, leafCfg.bodySchema);
|
|
306
|
+
const isMultipart = Array.isArray(leafCfg.bodyFiles) && leafCfg.bodyFiles.length > 0;
|
|
257
307
|
payload = isMultipart ? toFormData(normalizedBody) : normalizedBody;
|
|
258
308
|
} else if (requiresBody) {
|
|
259
309
|
throw new Error("Body is required when invoking a mutation fetch.");
|
|
@@ -277,7 +327,7 @@ function createRouteClient(opts) {
|
|
|
277
327
|
const out = await fetcher(
|
|
278
328
|
payload === void 0 ? { url, method } : { url, method, body: payload }
|
|
279
329
|
);
|
|
280
|
-
const parsed = zParse(out,
|
|
330
|
+
const parsed = zParse(out, leafCfg.outputSchema);
|
|
281
331
|
emit(
|
|
282
332
|
decorateDebugEvent(
|
|
283
333
|
{
|
|
@@ -313,7 +363,7 @@ function createRouteClient(opts) {
|
|
|
313
363
|
}
|
|
314
364
|
};
|
|
315
365
|
const fetchGet = (...tupleWithBody) => {
|
|
316
|
-
const acceptsBody = Boolean(
|
|
366
|
+
const acceptsBody = Boolean(leafCfg.bodySchema);
|
|
317
367
|
const tupleLength = tupleWithBody.length;
|
|
318
368
|
const maybeBodyIndex = expectsArgs ? 1 : 0;
|
|
319
369
|
const hasBodyCandidate = acceptsBody && tupleLength > maybeBodyIndex;
|
|
@@ -346,7 +396,12 @@ function createRouteClient(opts) {
|
|
|
346
396
|
},
|
|
347
397
|
[]
|
|
348
398
|
);
|
|
349
|
-
const { normalizedQuery, normalizedParams } = buildUrl(
|
|
399
|
+
const { normalizedQuery, normalizedParams } = buildUrl(
|
|
400
|
+
{ ...leaf, cfg: leafCfg },
|
|
401
|
+
baseUrl,
|
|
402
|
+
params,
|
|
403
|
+
query
|
|
404
|
+
);
|
|
350
405
|
const queryResult = (0, import_react_query.useInfiniteQuery)({
|
|
351
406
|
...buildOptions,
|
|
352
407
|
queryKey: getQueryKeys(...tuple),
|
|
@@ -400,7 +455,7 @@ function createRouteClient(opts) {
|
|
|
400
455
|
},
|
|
401
456
|
[]
|
|
402
457
|
);
|
|
403
|
-
buildUrl(leaf, baseUrl, params, query);
|
|
458
|
+
buildUrl({ ...leaf, cfg: leafCfg }, baseUrl, params, query);
|
|
404
459
|
const queryResult = (0, import_react_query.useQuery)({
|
|
405
460
|
...buildOptions,
|
|
406
461
|
queryKey: getQueryKeys(...tuple),
|
|
@@ -488,9 +543,9 @@ function toFormData(body) {
|
|
|
488
543
|
}
|
|
489
544
|
|
|
490
545
|
// src/sockets/socket.client.sys.ts
|
|
491
|
-
var
|
|
492
|
-
var roomValueSchema =
|
|
493
|
-
var buildRoomPayloadSchema = (metaSchema) =>
|
|
546
|
+
var import_zod2 = require("zod");
|
|
547
|
+
var roomValueSchema = import_zod2.z.union([import_zod2.z.array(import_zod2.z.string()), import_zod2.z.string()]);
|
|
548
|
+
var buildRoomPayloadSchema = (metaSchema) => import_zod2.z.object({
|
|
494
549
|
rooms: roomValueSchema,
|
|
495
550
|
meta: metaSchema
|
|
496
551
|
});
|