/
index.js
141 lines (119 loc) · 3.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { createHash } from 'crypto'
import {
assoc,
camelCase,
cloneDeep,
identity,
isPlainObject,
lowerFirst,
upperFirst,
} from 'lodash/fp'
import isPromise from 'p-is-promise'
import stringify from 'json-stringify-safe'
// Default parent ID for all nodes.
const DEFAULT_PARENT_ID = `__SOURCE__`
// Node fields used internally by Gatsby.
const RESTRICTED_NODE_FIELDS = [
`id`,
`children`,
`parent`,
`fields`,
`internal`,
]
// Generates an MD5 hash from a string.
const digest = str =>
createHash(`md5`)
.update(str)
.digest(`hex`)
// Generates an MD5 hash of an object and assign it to the internal.contentDigest key.
const withDigest = obj =>
assoc([`internal`, `contentDigest`], digest(stringify(obj)), obj)
// Returns node helpers for creating new nodes.
const createNodeHelpers = (options = {}) => {
if (!isPlainObject(options))
throw new Error(
`Options must be an object. An argument of type ${typeof options} was provided.`,
)
if (
typeof options.sourceId !== `undefined` &&
typeof options.sourceId !== `string`
)
throw new Error(
`options.sourceId must be a string. A value of type ${typeof options.sourceId} was provided.`,
)
if (typeof options.typePrefix !== `string`)
throw new Error(
`options.typePrefix must be a string. A value of type ${typeof options.typePrefix} was provided.`,
)
if (
typeof options.conflictFieldPrefix !== `undefined` &&
typeof options.conflictFieldPrefix !== `string`
)
throw new Error(
`options.conflictFieldPrefix must be a string. A value of type ${typeof options.conflictFieldPrefix} was provided.`,
)
const {
sourceId = DEFAULT_PARENT_ID,
typePrefix,
conflictFieldPrefix = lowerFirst(typePrefix),
} = options
// Generates a node ID from a given type and node ID.
const generateNodeId = (type, id) =>
`${typePrefix}__${upperFirst(camelCase(type))}__${id}`
// Generates a node type name from a given type.
const generateTypeName = type =>
upperFirst(camelCase(`${typePrefix} ${type}`))
// Prefixes conflicting node fields.
const prefixConflictingKeys = obj => {
Object.keys(obj).forEach(key => {
if (RESTRICTED_NODE_FIELDS.includes(key)) {
obj[conflictFieldPrefix + upperFirst(key)] = obj[key]
delete obj[key]
}
})
return obj
}
// Creates a node factory with a given type and middleware processor.
const createNodeFactory = (type, middleware = identity) => (
obj,
overrides = {},
) => {
// if (!isPlainObject(obj))
// throw new Error(
// `The source object must be a plain object. An argument of type "${typeof obj}" was provided.`,
// )
// if (!isPlainObject(overrides))
// throw new Error(
// `Node overrides must be a plain object. An argument of type "${typeof overrides}" was provided.`,
// )
const clonedObj = cloneDeep(obj)
const safeObj = prefixConflictingKeys(clonedObj)
let node = {
...safeObj,
id: generateNodeId(type, obj.id),
parent: sourceId,
children: [],
internal: {
type: generateTypeName(type),
},
}
node = middleware(node)
if (isPromise(node))
return node.then(resolvedNode =>
withDigest({
...resolvedNode,
...overrides,
}),
)
return withDigest({
...node,
...overrides,
})
}
return {
createNodeFactory,
generateNodeId,
generateTypeName,
}
}
export default createNodeHelpers