@khanacademy/wonder-blocks-data 9.1.2 → 10.0.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @khanacademy/wonder-blocks-data
2
2
 
3
+ ## 10.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [175a2dd2]
8
+ - @khanacademy/wonder-blocks-core@4.5.0
9
+
10
+ ## 10.0.0
11
+
12
+ ### Major Changes
13
+
14
+ - 5a3ec7f9: Make sure request ID generation handles objects like Date and Error instances better
15
+
3
16
  ## 9.1.2
4
17
 
5
18
  ### Patch Changes
package/dist/es/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Server, useForceUpdate } from '@khanacademy/wonder-blocks-core';
2
- import { KindError, clone } from '@khanacademy/wonder-stuff-core';
2
+ import { KindError, clone, entries } from '@khanacademy/wonder-stuff-core';
3
3
  import _extends from '@babel/runtime/helpers/extends';
4
4
  import * as React from 'react';
5
5
  import { useContext, useRef, useMemo, useCallback } from 'react';
@@ -690,19 +690,30 @@ const toString = value => {
690
690
  return value;
691
691
  }
692
692
 
693
+ if (typeof value === "object" && value != null) {
694
+ if (value instanceof Date) {
695
+ return value.toISOString();
696
+ } else if (typeof value.toString === "function") {
697
+ return value.toString();
698
+ }
699
+ }
700
+
693
701
  return (_JSON$stringify = JSON.stringify(value)) != null ? _JSON$stringify : "";
694
702
  };
695
703
 
696
704
  const toStringifiedVariables = (acc, key, value) => {
697
705
  if (typeof value === "object" && value !== null) {
698
- return Object.entries(value).reduce((innerAcc, [i, v]) => {
699
- const subKey = `${key}.${i}`;
700
- return toStringifiedVariables(innerAcc, subKey, v);
701
- }, acc);
702
- } else {
703
- acc[key] = toString(value);
706
+ const subValues = entries(value);
707
+
708
+ if (subValues.length !== 0) {
709
+ return subValues.reduce((innerAcc, [i, v]) => {
710
+ const subKey = `${key}.${i}`;
711
+ return toStringifiedVariables(innerAcc, subKey, v);
712
+ }, acc);
713
+ }
704
714
  }
705
715
 
716
+ acc[key] = toString(value);
706
717
  return acc;
707
718
  };
708
719
 
package/dist/index.js CHANGED
@@ -92,7 +92,7 @@ module.exports =
92
92
  "use strict";
93
93
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return DataErrors; });
94
94
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return DataError; });
95
- /* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(7);
95
+ /* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5);
96
96
  /* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__);
97
97
 
98
98
 
@@ -193,7 +193,7 @@ const FetchPolicy = __webpack_require__(23).Mirrored(["CacheBeforeNetwork", "Cac
193
193
  "use strict";
194
194
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return GqlErrors; });
195
195
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return GqlError; });
196
- /* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(7);
196
+ /* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5);
197
197
  /* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__);
198
198
 
199
199
 
@@ -239,6 +239,12 @@ class GqlError extends _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0
239
239
 
240
240
  /***/ }),
241
241
  /* 5 */
242
+ /***/ (function(module, exports) {
243
+
244
+ module.exports = require("@khanacademy/wonder-stuff-core");
245
+
246
+ /***/ }),
247
+ /* 6 */
242
248
  /***/ (function(module, __webpack_exports__, __webpack_require__) {
243
249
 
244
250
  "use strict";
@@ -323,7 +329,7 @@ const useSharedCache = (id, scope, initialValue) => {
323
329
  };
324
330
 
325
331
  /***/ }),
326
- /* 6 */
332
+ /* 7 */
327
333
  /***/ (function(module, __webpack_exports__, __webpack_require__) {
328
334
 
329
335
  "use strict";
@@ -351,12 +357,6 @@ const Status = Object.freeze({
351
357
  })
352
358
  });
353
359
 
354
- /***/ }),
355
- /* 7 */
356
- /***/ (function(module, exports) {
357
-
358
- module.exports = require("@khanacademy/wonder-stuff-core");
359
-
360
360
  /***/ }),
361
361
  /* 8 */
362
362
  /***/ (function(module, __webpack_exports__, __webpack_require__) {
@@ -900,7 +900,7 @@ class ScopedInMemoryCache {
900
900
  /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
901
901
  /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
902
902
  /* harmony import */ var _use_server_effect_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(16);
903
- /* harmony import */ var _use_shared_cache_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(5);
903
+ /* harmony import */ var _use_shared_cache_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(6);
904
904
  /* harmony import */ var _use_cached_effect_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(17);
905
905
  /* harmony import */ var _util_types_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(3);
906
906
 
@@ -1009,7 +1009,7 @@ const useHydratableEffect = (requestId, handler, options = {}) => {
1009
1009
 
1010
1010
  "use strict";
1011
1011
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return SerializableInMemoryCache; });
1012
- /* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(7);
1012
+ /* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5);
1013
1013
  /* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__);
1014
1014
  /* harmony import */ var _data_error_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(0);
1015
1015
  /* harmony import */ var _scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(11);
@@ -1173,8 +1173,8 @@ const useServerEffect = (requestId, handler, options = {}) => {
1173
1173
  /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__);
1174
1174
  /* harmony import */ var _util_data_error_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(0);
1175
1175
  /* harmony import */ var _util_request_fulfillment_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(10);
1176
- /* harmony import */ var _util_status_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(6);
1177
- /* harmony import */ var _use_shared_cache_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(5);
1176
+ /* harmony import */ var _util_status_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(7);
1177
+ /* harmony import */ var _use_shared_cache_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(6);
1178
1178
  /* harmony import */ var _use_request_interception_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(20);
1179
1179
  /* harmony import */ var _util_types_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(3);
1180
1180
 
@@ -1716,7 +1716,7 @@ const abortInflightRequests = () => {
1716
1716
 
1717
1717
  "use strict";
1718
1718
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return purgeCaches; });
1719
- /* harmony import */ var _hooks_use_shared_cache_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5);
1719
+ /* harmony import */ var _hooks_use_shared_cache_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(6);
1720
1720
  /* harmony import */ var _hydration_cache_api_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(15);
1721
1721
 
1722
1722
 
@@ -1843,6 +1843,10 @@ const InterceptRequests = ({
1843
1843
 
1844
1844
  "use strict";
1845
1845
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return getGqlRequestId; });
1846
+ /* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5);
1847
+ /* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__);
1848
+
1849
+
1846
1850
  const toString = value => {
1847
1851
  var _JSON$stringify;
1848
1852
 
@@ -1850,6 +1854,14 @@ const toString = value => {
1850
1854
  return value;
1851
1855
  }
1852
1856
 
1857
+ if (typeof value === "object" && value != null) {
1858
+ if (value instanceof Date) {
1859
+ return value.toISOString();
1860
+ } else if (typeof value.toString === "function") {
1861
+ return value.toString();
1862
+ }
1863
+ }
1864
+
1853
1865
  return (_JSON$stringify = JSON.stringify(value)) != null ? _JSON$stringify : "";
1854
1866
  };
1855
1867
 
@@ -1860,14 +1872,19 @@ const toStringifiedVariables = (acc, key, value) => {
1860
1872
  // extra %-encodings. This means that an object or array variable
1861
1873
  // turns into x variables, where x is the field or element count of
1862
1874
  // variable. See below for example.
1863
- return Object.entries(value).reduce((innerAcc, [i, v]) => {
1864
- const subKey = `${key}.${i}`;
1865
- return toStringifiedVariables(innerAcc, subKey, v);
1866
- }, acc);
1867
- } else {
1868
- acc[key] = toString(value);
1875
+ const subValues = Object(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["entries"])(value); // If we don't get any entries, it's possible this is a Date, Error,
1876
+ // or some other non-standard value. While these generally should be
1877
+ // avoided as variables, we should handle them gracefully.
1878
+
1879
+ if (subValues.length !== 0) {
1880
+ return subValues.reduce((innerAcc, [i, v]) => {
1881
+ const subKey = `${key}.${i}`;
1882
+ return toStringifiedVariables(innerAcc, subKey, v);
1883
+ }, acc);
1884
+ }
1869
1885
  }
1870
1886
 
1887
+ acc[key] = toString(value);
1871
1888
  return acc;
1872
1889
  };
1873
1890
  /**
@@ -2078,7 +2095,7 @@ const useGql = (context = {}) => {
2078
2095
 
2079
2096
  "use strict";
2080
2097
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return resultFromCachedResponse; });
2081
- /* harmony import */ var _status_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(6);
2098
+ /* harmony import */ var _status_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(7);
2082
2099
  /* harmony import */ var _data_error_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(0);
2083
2100
 
2084
2101
 
@@ -2208,7 +2225,7 @@ __webpack_require__.r(__webpack_exports__);
2208
2225
  /* harmony import */ var _hooks_use_cached_effect_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(17);
2209
2226
  /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "useCachedEffect", function() { return _hooks_use_cached_effect_js__WEBPACK_IMPORTED_MODULE_9__["a"]; });
2210
2227
 
2211
- /* harmony import */ var _hooks_use_shared_cache_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(5);
2228
+ /* harmony import */ var _hooks_use_shared_cache_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(6);
2212
2229
  /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "useSharedCache", function() { return _hooks_use_shared_cache_js__WEBPACK_IMPORTED_MODULE_10__["b"]; });
2213
2230
 
2214
2231
  /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "SharedCache", function() { return _hooks_use_shared_cache_js__WEBPACK_IMPORTED_MODULE_10__["a"]; });
@@ -2224,7 +2241,7 @@ __webpack_require__.r(__webpack_exports__);
2224
2241
  /* harmony import */ var _util_serializable_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__(13);
2225
2242
  /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "SerializableInMemoryCache", function() { return _util_serializable_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_13__["a"]; });
2226
2243
 
2227
- /* harmony import */ var _util_status_js__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__(6);
2244
+ /* harmony import */ var _util_status_js__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__(7);
2228
2245
  /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "Status", function() { return _util_status_js__WEBPACK_IMPORTED_MODULE_14__["a"]; });
2229
2246
 
2230
2247
  /* harmony import */ var _util_get_gql_request_id_js__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__(29);
package/legacy-docs.md CHANGED
@@ -1,3 +1,5 @@
1
1
  Documentation for Wonder Blocks Data is now in Storybook.
2
2
 
3
- Either run `yarn start:storybook` locally, or visit the the stories for the `main` branch on [Chromatic](https://main--5e1bf4b385e3fb0020b7073c.chromatic.com).
3
+ Visit the [Data
4
+ Storybook](https://khan.github.io/wonder-blocks/?path=/docs/data) docs on GitHub
5
+ Pages.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@khanacademy/wonder-blocks-data",
3
- "version": "9.1.2",
3
+ "version": "10.0.1",
4
4
  "design": "v1",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -14,7 +14,7 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "@babel/runtime": "^7.18.6",
17
- "@khanacademy/wonder-blocks-core": "^4.4.0"
17
+ "@khanacademy/wonder-blocks-core": "^4.5.0"
18
18
  },
19
19
  "peerDependencies": {
20
20
  "@khanacademy/wonder-stuff-core": "^1.0.1",
@@ -104,4 +104,31 @@ describe("#getGqlRequestId", () => {
104
104
  `variable1=value1&variable2=42&variable3=&variable4=null&variable5=true&variable6.nested1=nested1&variable6.nested2=nested2&variable7.0=1&variable7.1=2&variable7.2=3`,
105
105
  );
106
106
  });
107
+
108
+ it("should handle non-primitive values in variables", () => {
109
+ // Arrange
110
+ const operation = {
111
+ type: "query",
112
+ id: "myQuery",
113
+ };
114
+ const variables = {
115
+ variable1: {
116
+ date: new Date("2020-01-01"),
117
+ error: new Error("BOOM!"),
118
+ },
119
+ };
120
+
121
+ // Act
122
+ const requestId = getGqlRequestId(operation, variables, {
123
+ module: "MODULE",
124
+ curriculum: "CURRICULUM",
125
+ targetLocale: "LOCALE",
126
+ });
127
+ const result = new Set(requestId.split("|"));
128
+
129
+ // Assert
130
+ expect(result).toContain(
131
+ `variable1.date=2020-01-01T00%3A00%3A00.000Z&variable1.error=Error%3A+BOOM%21`,
132
+ );
133
+ });
107
134
  });
@@ -1,10 +1,19 @@
1
1
  // @flow
2
+ import {entries} from "@khanacademy/wonder-stuff-core";
2
3
  import type {GqlOperation, GqlContext} from "./gql-types.js";
3
4
 
4
5
  const toString = (value: mixed): string => {
5
6
  if (typeof value === "string") {
6
7
  return value;
7
8
  }
9
+
10
+ if (typeof value === "object" && value != null) {
11
+ if (value instanceof Date) {
12
+ return value.toISOString();
13
+ } else if (typeof value.toString === "function") {
14
+ return value.toString();
15
+ }
16
+ }
8
17
  return JSON.stringify(value) ?? "";
9
18
  };
10
19
 
@@ -15,13 +24,20 @@ const toStringifiedVariables = (acc: any, key: string, value: mixed): any => {
15
24
  // extra %-encodings. This means that an object or array variable
16
25
  // turns into x variables, where x is the field or element count of
17
26
  // variable. See below for example.
18
- return Object.entries(value).reduce((innerAcc, [i, v]) => {
19
- const subKey = `${key}.${i}`;
20
- return toStringifiedVariables(innerAcc, subKey, v);
21
- }, acc);
22
- } else {
23
- acc[key] = toString(value);
27
+ const subValues = entries(value);
28
+
29
+ // If we don't get any entries, it's possible this is a Date, Error,
30
+ // or some other non-standard value. While these generally should be
31
+ // avoided as variables, we should handle them gracefully.
32
+ if (subValues.length !== 0) {
33
+ return subValues.reduce((innerAcc, [i, v]) => {
34
+ const subKey = `${key}.${i}`;
35
+ return toStringifiedVariables(innerAcc, subKey, v);
36
+ }, acc);
37
+ }
24
38
  }
39
+
40
+ acc[key] = toString(value);
25
41
  return acc;
26
42
  };
27
43