gatsby-transformer-json 4.12.0 → 4.13.0

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
@@ -3,6 +3,16 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ### [4.12.1](https://github.com/gatsbyjs/gatsby/commits/gatsby-transformer-json@4.12.1/packages/gatsby-transformer-json) (2022-04-13)
7
+
8
+ **Note:** Version bump only for package gatsby-transformer-json
9
+
10
+ ## [4.12.0](https://github.com/gatsbyjs/gatsby/commits/gatsby-transformer-json@4.12.0/packages/gatsby-transformer-json) (2022-04-12)
11
+
12
+ [🧾 Release notes](https://www.gatsbyjs.com/docs/reference/release-notes/v4.12)
13
+
14
+ **Note:** Version bump only for package gatsby-transformer-json
15
+
6
16
  ## [4.11.0](https://github.com/gatsbyjs/gatsby/commits/gatsby-transformer-json@4.11.0/packages/gatsby-transformer-json) (2022-03-29)
7
17
 
8
18
  [🧾 Release notes](https://www.gatsbyjs.com/docs/reference/release-notes/v4.11)
package/gatsby-node.js ADDED
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+
3
+ const _ = require(`lodash`);
4
+
5
+ const path = require(`path`);
6
+
7
+ function unstable_shouldOnCreateNode({
8
+ node
9
+ }) {
10
+ // We only care about JSON content.
11
+ return node.internal.mediaType === `application/json`;
12
+ }
13
+
14
+ async function onCreateNode({
15
+ node,
16
+ actions,
17
+ loadNodeContent,
18
+ createNodeId,
19
+ createContentDigest
20
+ }, pluginOptions) {
21
+ if (!unstable_shouldOnCreateNode({
22
+ node
23
+ })) {
24
+ return;
25
+ }
26
+
27
+ function getType({
28
+ node,
29
+ object,
30
+ isArray
31
+ }) {
32
+ if (pluginOptions && _.isFunction(pluginOptions.typeName)) {
33
+ return pluginOptions.typeName({
34
+ node,
35
+ object,
36
+ isArray
37
+ });
38
+ } else if (pluginOptions && _.isString(pluginOptions.typeName)) {
39
+ return pluginOptions.typeName;
40
+ } else if (node.internal.type !== `File`) {
41
+ return _.upperFirst(_.camelCase(`${node.internal.type} Json`));
42
+ } else if (isArray) {
43
+ return _.upperFirst(_.camelCase(`${node.name} Json`));
44
+ } else {
45
+ return _.upperFirst(_.camelCase(`${path.basename(node.dir)} Json`));
46
+ }
47
+ }
48
+
49
+ async function transformObject(obj, id, type) {
50
+ const jsonNode = { ...obj,
51
+ id,
52
+ children: [],
53
+ parent: node.id,
54
+ internal: {
55
+ contentDigest: createContentDigest(obj),
56
+ type
57
+ }
58
+ };
59
+
60
+ if (obj.id) {
61
+ jsonNode[`jsonId`] = obj.id;
62
+ }
63
+
64
+ await createNode(jsonNode);
65
+ createParentChildLink({
66
+ parent: node,
67
+ child: jsonNode
68
+ });
69
+ }
70
+
71
+ const {
72
+ createNode,
73
+ createParentChildLink
74
+ } = actions;
75
+ const content = await loadNodeContent(node);
76
+ let parsedContent;
77
+
78
+ try {
79
+ parsedContent = JSON.parse(content);
80
+ } catch {
81
+ const hint = node.absolutePath ? `file ${node.absolutePath}` : `in node ${node.id}`;
82
+ throw new Error(`Unable to parse JSON: ${hint}`);
83
+ }
84
+
85
+ if (_.isArray(parsedContent)) {
86
+ for (let i = 0, l = parsedContent.length; i < l; i++) {
87
+ const obj = parsedContent[i];
88
+ await transformObject(obj, createNodeId(`${node.id} [${i}] >>> JSON`), getType({
89
+ node,
90
+ object: obj,
91
+ isArray: true
92
+ }));
93
+ }
94
+ } else if (_.isPlainObject(parsedContent)) {
95
+ await transformObject(parsedContent, createNodeId(`${node.id} >>> JSON`), getType({
96
+ node,
97
+ object: parsedContent,
98
+ isArray: false
99
+ }));
100
+ }
101
+ }
102
+
103
+ exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode;
104
+ exports.onCreateNode = onCreateNode;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gatsby-transformer-json",
3
3
  "description": "Gatsby transformer plugin for JSON files",
4
- "version": "4.12.0",
4
+ "version": "4.13.0",
5
5
  "author": "Kyle Mathews <mathews.kyle@gmail.com>",
6
6
  "bugs": {
7
7
  "url": "https://github.com/gatsbyjs/gatsby/issues"
@@ -13,7 +13,7 @@
13
13
  "devDependencies": {
14
14
  "@babel/cli": "^7.15.4",
15
15
  "@babel/core": "^7.15.5",
16
- "babel-preset-gatsby-package": "^2.12.0",
16
+ "babel-preset-gatsby-package": "^2.13.0",
17
17
  "cross-env": "^7.0.3"
18
18
  },
19
19
  "homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-transformer-json#readme",
@@ -39,5 +39,5 @@
39
39
  "engines": {
40
40
  "node": ">=14.15.0"
41
41
  },
42
- "gitHead": "12e90fce284eb0a64b7d1f15e293027483826372"
42
+ "gitHead": "89509ff7893787cf7359bc64b66a2ec32ced51d0"
43
43
  }