decap-cms-lib-util 3.8.0 → 3.8.2

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.
@@ -1,9 +1,15 @@
1
1
  import flow from 'lodash/flow';
2
2
  import fromPairs from 'lodash/fromPairs';
3
3
  import { map } from 'lodash/fp';
4
- import { fromJS } from 'immutable';
4
+ import { Map as ImmutableMap } from 'immutable';
5
5
  import unsentRequest from './unsentRequest';
6
6
  import APIError from './APIError';
7
+
8
+ // A formatter yields a string, a Blob or parsed JSON depending on the requested
9
+ // format, so its result is only known at the call site - hence `any`. Callers
10
+ // narrow it themselves (e.g. `.then<Blob | string>(...)`).
11
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
+
7
13
  export function filterByExtension(file, extension) {
8
14
  const path = file?.path || '';
9
15
  return path.endsWith(extension.startsWith('.') ? extension : `.${extension}`);
@@ -17,7 +23,10 @@ function catchFormatErrors(format, formatter) {
17
23
  }
18
24
  };
19
25
  }
20
- const responseFormatters = fromJS({
26
+
27
+ // NOTE: `Map` rather than `fromJS` on purpose - the values are functions, which
28
+ // `fromJS` would leave untouched anyway, and `Map` keeps them properly typed.
29
+ const responseFormatters = ImmutableMap({
21
30
  json: async res => {
22
31
  const contentType = res.headers.get('Content-Type') || '';
23
32
  if (!contentType.startsWith('application/json') && !contentType.startsWith('text/json')) {
@@ -31,6 +31,29 @@ function fetchWithTimeout(input, init) {
31
31
  function decodeParams(paramsString) {
32
32
  return List(paramsString.split('&')).map(s => List(s.split('=')).map(decodeURIComponent)).update(Map);
33
33
  }
34
+
35
+ // Only `params` and `headers` are ever traversed as collections by the helpers
36
+ // below; every other value of a request is passed straight through to `fetch`.
37
+ const NESTED_REQUEST_KEYS = ['params', 'headers'];
38
+
39
+ /**
40
+ * Turns a request-like object into a Map, converting only the keys the request
41
+ * helpers actually operate on.
42
+ *
43
+ * NOTE: this cannot be `fromJS`. immutable 4 recurses into any iterable, not
44
+ * just plain objects and arrays, so a `FormData` or `URLSearchParams` body
45
+ * would be turned into a collection and the `toJS()` in `toFetchArguments`
46
+ * would then hand `fetch` a plain object instead of the original body.
47
+ */
48
+ function toRequestMap(req) {
49
+ return Map(req).withMutations(map => {
50
+ NESTED_REQUEST_KEYS.forEach(key => {
51
+ if (map.has(key)) {
52
+ map.set(key, fromJS(map.get(key)));
53
+ }
54
+ });
55
+ });
56
+ }
34
57
  function fromURL(wholeURL) {
35
58
  const [url, allParamsString] = wholeURL.split('?');
36
59
  return Map({
@@ -41,7 +64,7 @@ function fromURL(wholeURL) {
41
64
  });
42
65
  }
43
66
  function fromFetchArguments(wholeURL, options) {
44
- return fromURL(wholeURL).merge((options ? fromJS(options) : Map()).remove('url').remove('params'));
67
+ return fromURL(wholeURL).merge((options ? toRequestMap(options) : Map()).remove('url').remove('params'));
45
68
  }
46
69
  function encodeParams(params) {
47
70
  return params.entrySeq().map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('&');
@@ -57,7 +80,7 @@ function maybeRequestArg(req) {
57
80
  return fromURL(req);
58
81
  }
59
82
  if (req) {
60
- return fromJS(req);
83
+ return toRequestMap(req);
61
84
  }
62
85
  return Map();
63
86
  }
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "decap-cms-lib-util",
3
3
  "description": "Shared utilities for Decap CMS.",
4
- "version": "3.8.0",
5
- "repository": "https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-lib-util",
4
+ "version": "3.8.2",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-lib-util"
8
+ },
6
9
  "bugs": "https://github.com/decaporg/decap-cms/issues",
7
10
  "module": "dist/esm/index.js",
8
11
  "main": "dist/decap-cms-lib-util.js",
@@ -14,22 +17,22 @@
14
17
  "decap-cms"
15
18
  ],
16
19
  "sideEffects": false,
17
- "scripts": {
18
- "develop": "npm run build:esm -- --watch",
19
- "build": "cross-env NODE_ENV=production webpack",
20
- "build:esm": "cross-env NODE_ENV=esm babel src --out-dir dist/esm --ignore \"**/__tests__\" --root-mode upward --extensions \".js,.jsx,.ts,.tsx\""
21
- },
22
20
  "dependencies": {
23
21
  "js-sha256": "^0.9.0",
24
22
  "localforage": "^1.7.3",
25
23
  "semaphore": "^1.1.0"
26
24
  },
27
25
  "peerDependencies": {
28
- "immutable": "^3.7.6",
26
+ "common-tags": "1.8.2",
27
+ "immutable": "^4.3.9",
29
28
  "lodash": "^4.17.11"
30
29
  },
31
30
  "devDependencies": {
32
- "common-tags": "^1.8.0"
31
+ "common-tags": "1.8.2"
33
32
  },
34
- "gitHead": "ffe5ab7e61f4e7cb374a413bf9fe55f088d20ba2"
35
- }
33
+ "scripts": {
34
+ "develop": "pnpm run build:esm --watch",
35
+ "build": "cross-env NODE_ENV=production webpack",
36
+ "build:esm": "cross-env NODE_ENV=esm babel src --out-dir dist/esm --ignore \"**/__tests__\" --root-mode upward --extensions \".js,.jsx,.ts,.tsx\""
37
+ }
38
+ }
package/LICENSE DELETED
@@ -1,22 +0,0 @@
1
- Copyright (c) 2016 Netlify <decap@p-m.si>
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/dist/esm/types.js DELETED
@@ -1,7 +0,0 @@
1
- import { Map as ImmutableMap, List } from 'immutable';
2
- export function isImmutableMap(value) {
3
- return ImmutableMap.isMap(value);
4
- }
5
- export function isImmutableList(value) {
6
- return List.isList(value);
7
- }