-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
normalize.js
150 lines (138 loc) · 3.77 KB
/
normalize.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
142
143
144
145
146
147
148
149
150
const crypto = require(`crypto`)
const deepMapKeys = require(`deep-map-keys`)
const stringify = require(`json-stringify-safe`)
const conflictFieldPrefix = `lever_`
// restrictedNodeFields from here https://www.gatsbyjs.org/docs/node-interface/
const restrictedNodeFields = [`id`, `children`, `parent`, `fields`, `internal`]
/**
* Encrypts a String using md5 hash of hexadecimal digest.
*
* @param {any} str
*/
const digest = str =>
crypto
.createHash(`md5`)
.update(str)
.digest(`hex`)
/**
* Create the Graph QL Node
*
* @param {any} ent
* @param {any} type
* @param {any} createNode
*/
async function createGraphQLNode(ent, type, createNode, store, cache) {
let id = !ent.id ? (!ent.ID ? 0 : ent.ID) : ent.id
let node = {
id: `${type}_${id.toString()}`,
children: [],
parent: null,
internal: {
type: type,
},
}
node = recursiveAddFields(ent, node)
node.internal.content = JSON.stringify(node)
node.internal.contentDigest = digest(stringify(node))
createNode(node)
}
exports.createGraphQLNode = createGraphQLNode
/**
* Add fields recursively
*
* @param {any} ent
* @param {any} newEnt
* @returns the new node
*/
function recursiveAddFields(ent, newEnt) {
for (let k of Object.keys(ent)) {
if (!newEnt.hasOwnProperty(k)) {
let key = getValidKey(k)
newEnt[key] = ent[k]
// Nested Objects & Arrays of Objects
if (typeof ent[key] === `object`) {
if (!Array.isArray(ent[key]) && ent[key] != null) {
newEnt[key] = recursiveAddFields(ent[key], {})
} else if (Array.isArray(ent[key])) {
if (ent[key].length > 0 && typeof ent[key][0] === `object`) {
ent[k].map((el, i) => {
newEnt[key][i] = recursiveAddFields(el, {})
})
}
}
}
}
}
return newEnt
}
exports.recursiveAddFields = recursiveAddFields
/**
* Validate the GraphQL naming convetions & protect specific fields.
*
* @param {any} key
* @returns the valid name
*/
function getValidKey({ key, verbose = false }) {
let nkey = String(key)
const NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/
let changed = false
// Replace invalid characters
if (!NAME_RX.test(nkey)) {
changed = true
nkey = nkey.replace(/-|__|:|\.|\s/g, `_`)
}
// Prefix if first character isn't a letter.
if (!NAME_RX.test(nkey.slice(0, 1))) {
changed = true
nkey = `${conflictFieldPrefix}${nkey}`
}
if (restrictedNodeFields.includes(nkey)) {
changed = true
nkey = `${conflictFieldPrefix}${nkey}`.replace(/-|__|:|\.|\s/g, `_`)
}
if (changed && verbose)
console.log(
`Object with key "${key}" breaks GraphQL naming convention. Renamed to "${nkey}"`
)
return nkey
}
exports.getValidKey = getValidKey
// Create entities from the few the lever API returns as an object for presumably
// legacy reasons.
exports.normalizeEntities = entities =>
entities.reduce((acc, e) => acc.concat(e), [])
// Standardize ids + make sure keys are valid.
exports.standardizeKeys = entities =>
entities.map(e =>
deepMapKeys(e, key =>
key === `ID` ? getValidKey({ key: `id` }) : getValidKey({ key })
)
)
// Standardize dates on ISO 8601 version.
exports.standardizeDates = entities =>
entities.map(e => {
if (e.createdAt) {
e.createdAt = new Date(e.createdAt).toJSON()
}
return e
})
exports.createGatsbyIds = (createNodeId, entities) =>
entities.map(e => {
e.id = createNodeId(e.lever_id.toString())
return e
})
exports.createNodesFromEntities = ({ entities, createNode }) => {
entities.forEach(e => {
let { ...entity } = e
let node = {
...entity,
parent: null,
children: [],
internal: {
type: `lever`,
contentDigest: digest(JSON.stringify(entity)),
},
}
createNode(node)
})
}