@m1212e/rumble 0.12.17 → 0.12.19

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/out/index.cjs CHANGED
@@ -23,6 +23,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
23
  //#endregion
24
24
  let node_fs_promises = require("node:fs/promises");
25
25
  let node_path = require("node:path");
26
+ let __urql_introspection = require("@urql/introspection");
27
+ let devalue = require("devalue");
26
28
  let graphql = require("graphql");
27
29
  let es_toolkit = require("es-toolkit");
28
30
  let wonka = require("wonka");
@@ -46,25 +48,30 @@ __pothos_plugin_smart_subscriptions = __toESM(__pothos_plugin_smart_subscription
46
48
  let graphql_scalars = require("graphql-scalars");
47
49
 
48
50
  //#region lib/client/generate/client.ts
49
- function generateClient({ apiUrl, rumbleImportPath, useExternalUrqlClient, availableSubscriptions }) {
51
+ function generateClient({ apiUrl, rumbleImportPath, useExternalUrqlClient, availableSubscriptions, schema }) {
50
52
  const imports = [];
51
53
  let code = "";
52
54
  if (typeof useExternalUrqlClient === "string") imports.push(`import { urqlClient } from "${useExternalUrqlClient}";`);
53
- else {
54
- imports.push(`import { Client, fetchExchange } from '@urql/core';`);
55
- imports.push(`import { cacheExchange } from '@urql/exchange-graphcache';`);
56
- }
55
+ imports.push(`import { Client, fetchExchange } from '@urql/core';`);
56
+ imports.push(`import { cacheExchange } from '@urql/exchange-graphcache';`);
57
+ imports.push(`import { nativeDateExchange } from '${rumbleImportPath}';`);
57
58
  imports.push(`import { makeLiveQuery, makeMutation, makeSubscription, makeQuery } from '${rumbleImportPath}';`);
58
- if (!useExternalUrqlClient) code += `
59
- const urqlClient = new Client({
59
+ code += `
60
+ export const defaultOptions: ConstructorParameters<Client>[0] = {
60
61
  url: "${apiUrl ?? "PLEASE PROVIDE A URL WHEN GENERATING OR IMPORT AN EXTERNAL URQL CLIENT"}",
61
62
  fetchSubscriptions: true,
62
- exchanges: [cacheExchange({}), fetchExchange],
63
+ exchanges: [cacheExchange({
64
+ // @ts-ignore
65
+ schema: ${schema ? (0, devalue.uneval)((0, __urql_introspection.minifyIntrospectionQuery)((0, __urql_introspection.getIntrospectedSchema)(schema))) : "undefined"}
66
+ }), nativeDateExchange, fetchExchange],
63
67
  fetchOptions: {
64
68
  credentials: "include",
65
69
  },
66
70
  requestPolicy: "cache-and-network",
67
- });
71
+ }
72
+ `;
73
+ if (!useExternalUrqlClient) code += `
74
+ const urqlClient = new Client(defaultOptions);
68
75
  `;
69
76
  code += `
70
77
  export const client = {
@@ -227,6 +234,7 @@ export type ${key} = ${rep};
227
234
  }
228
235
  const c = generateClient({
229
236
  apiUrl,
237
+ schema,
230
238
  useExternalUrqlClient,
231
239
  rumbleImportPath,
232
240
  availableSubscriptions: new Set(Object.keys(schema.getSubscriptionType()?.getFields() || {}))
@@ -241,8 +249,8 @@ export type ${key} = ${rep};
241
249
  const argsKey = "__args";
242
250
  function makeGraphQLQuery({ queryName, input, client, enableSubscription = false }) {
243
251
  const otwQueryName = `${(0, es_toolkit.capitalize)(queryName)}Query`;
244
- const argsString = stringifyArgumentObjectToGraphqlList(input[argsKey] ?? {});
245
- const operationString = (operationVerb) => `${operationVerb} ${otwQueryName} { ${queryName}${argsString} { ${stringifySelection(input)} }}`;
252
+ const argsString = stringifyArgumentObjectToGraphqlList(input?.[argsKey] ?? {});
253
+ const operationString = (operationVerb) => `${operationVerb} ${otwQueryName} { ${queryName}${argsString} ${input ? `{ ${stringifySelection(input)} }` : ""}}`;
246
254
  let awaitedReturnValueReference;
247
255
  const observable = (0, wonka.pipe)((0, wonka.merge)([client.query(operationString("query"), {}), enableSubscription ? client.subscription(operationString("subscription"), {}) : wonka.empty]), (0, wonka.map)((v) => {
248
256
  const data = v.data?.[queryName];
@@ -349,6 +357,73 @@ function makeMutation({ urqlClient }) {
349
357
  } });
350
358
  }
351
359
 
360
+ //#endregion
361
+ //#region lib/helpers/deepMap.ts
362
+ /**
363
+ * Recursively applies a mapping function to every value in a nested structure.
364
+ *
365
+ * This helper will traverse arrays and plain objects (objects with `constructor === Object`)
366
+ * and apply the provided `fn` to any value that is not an array or a plain object.
367
+ * - Arrays are mapped to new arrays (a fresh array is returned).
368
+ * - Plain objects are traversed and their own enumerable properties are replaced in-place.
369
+ * - Non-plain objects (e.g. Date, Map, Set, class instances, functions) are treated as leaves
370
+ * and passed directly to `fn`.
371
+ * - `null` and `undefined` are passed to `fn`.
372
+ * - Circular references are detected using a `WeakSet`. When a circular reference is encountered,
373
+ * the original reference is returned unchanged (it is not re-traversed or re-mapped).
374
+ *
375
+ * Note: Because plain objects are mutated in-place, callers who need immutability should first
376
+ * clone the object graph or pass a deep-cloned input to avoid modifying the original.
377
+ *
378
+ * @param input - The value to traverse and map. May be any value (primitive, array, object, etc.).
379
+ * @param fn - A function invoked for every non-array, non-plain-object value encountered.
380
+ * Receives the current value and should return the mapped value.
381
+ * @returns The transformed structure: arrays are returned as new arrays, plain objects are the
382
+ * same object instances with their property values replaced, and other values are the
383
+ * result of `fn`.
384
+ *
385
+ * @example
386
+ * // Map all primitive values to their string representation:
387
+ * // const result = mapValuesDeep({ a: 1, b: [2, { c: 3 }] }, v => String(v));
388
+ * // result => { a: "1", b: ["2", { c: "3" }] }
389
+ *
390
+ * @remarks
391
+ * - Only plain objects (constructed by `Object`) are recursively traversed. This avoids
392
+ * unintentionally iterating internal structure of class instances or built-in collections.
393
+ * - Circular structures are preserved by returning the original reference when detected.
394
+ */
395
+ function mapValuesDeep(input, fn) {
396
+ const seen = /* @__PURE__ */ new WeakSet();
397
+ const recurse = (value) => {
398
+ if (Array.isArray(value)) return value.map(recurse);
399
+ if (value !== null && value !== void 0 && typeof value === "object" && value.constructor === Object) {
400
+ if (seen.has(value)) return value;
401
+ seen.add(value);
402
+ for (const key of Object.keys(value)) value[key] = recurse(value[key]);
403
+ return value;
404
+ }
405
+ return fn(value);
406
+ };
407
+ return recurse(input);
408
+ }
409
+
410
+ //#endregion
411
+ //#region lib/client/nativeDateExchange.ts
412
+ const dateIsoRegex = /^\d{4}-\d{2}-\d{2}(?:[Tt ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:[Zz]|[+-]\d{2}:\d{2})?)?$/;
413
+ const nativeDateExchange = ({ client, forward }) => {
414
+ return (operations$) => {
415
+ return (0, wonka.pipe)(forward(operations$), (0, wonka.map)((r) => {
416
+ r.data = mapValuesDeep(r.data, (value) => {
417
+ if (typeof value !== "string" || !dateIsoRegex.test(value)) return value;
418
+ const date = Date.parse(value);
419
+ if (!Number.isNaN(date)) return new Date(date);
420
+ return value;
421
+ });
422
+ return r;
423
+ }));
424
+ };
425
+ };
426
+
352
427
  //#endregion
353
428
  //#region lib/client/query.ts
354
429
  function makeQuery({ urqlClient }) {
@@ -1939,5 +2014,6 @@ exports.makeMutation = makeMutation;
1939
2014
  exports.makeQuery = makeQuery;
1940
2015
  exports.makeSubscription = makeSubscription;
1941
2016
  exports.mapNullFieldsToUndefined = mapNullFieldsToUndefined;
2017
+ exports.nativeDateExchange = nativeDateExchange;
1942
2018
  exports.rumble = rumble;
1943
2019
  //# sourceMappingURL=index.cjs.map