-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.ts
51 lines (49 loc) · 1.43 KB
/
gatsby-node.ts
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
import { SourceNodesArgs } from 'gatsby'
import { Errors } from './lib/errors'
import { EsaApi, PostsPayload } from './lib/esaApi'
import { newEsaNodeCreator } from './lib/esaNodeCreator'
import { isValidPluginOptions, PluginOptions } from './lib/pluginOptions'
import { createSchemaCustomization } from './lib/createSchemaCustomization'
// noinspection JSUnusedGlobalSymbols
export const sourceNodes = async (
{
createNodeId,
createContentDigest,
actions: { createNode, createParentChildLink },
getNode,
reporter,
}: Pick<
SourceNodesArgs,
'createNodeId' | 'createContentDigest' | 'actions' | 'reporter' | 'getNode'
>,
options?: PluginOptions,
): Promise<void> => {
const errors = new Errors()
if (!isValidPluginOptions(options, errors)) {
reporter.error(
`[gatsby-source-esa]\nPlease check your plugin options:\n${errors}`,
)
return
}
const { team, posts: postsParams = {} } = options
const esa = new EsaApi({ team, token: options.token })
const nodeCreator = newEsaNodeCreator({
team,
createNode,
createNodeId,
createContentDigest,
getNode,
createParentChildLink,
})
let page: null | number = 1
while (page) {
const { posts, next_page }: PostsPayload = await esa.getPosts({
page,
...postsParams,
per_page: EsaApi.postsPerPageMax,
})
await nodeCreator.createPostNodes(posts)
page = next_page
}
}
export { createSchemaCustomization }