1
+ const JsonSchemaRefParser = require ( "json-schema-ref-parser" ) ;
1
2
const path = require ( "path" ) ;
2
3
const _ = require ( "lodash" ) ;
4
+ // const axios = require("axios");
3
5
4
- exports . createPages = ( { actions, graphql } ) => {
6
+ const specs = require ( "./src/data/spec.json" ) ;
7
+ const crypto = require ( "crypto" ) ;
8
+ const parser = new JsonSchemaRefParser ( ) ;
9
+
10
+ exports . sourceNodes = async ( { actions } ) => {
11
+ const { createNode } = actions ;
12
+ const res = await parser . dereference ( specs ) ;
13
+
14
+ // map into these results and create nodes
15
+ Object . keys ( res . paths ) . map ( ( path , i ) => {
16
+ // Create your node object
17
+ const pathNode = {
18
+ // Required fields
19
+ id : `${ i } ` ,
20
+ parent : `__SOURCE__` ,
21
+ internal : {
22
+ type : `Paths` // name of the graphQL query --> allRandomUser {}
23
+ // contentDigest will be added just after
24
+ // but it is required
25
+ } ,
26
+ children : [ ] ,
27
+
28
+ // Other fields that you want to query with graphQl
29
+ name : path ,
30
+ get : res . paths [ path ] . get ,
31
+ post : res . paths [ path ] . post ,
32
+ put : res . paths [ path ] . put ,
33
+ responses : res . paths [ path ] . responses
34
+ // name: {
35
+ // title: user.name.title,
36
+ // first: user.name.first,
37
+ // last: user.name.last
38
+ // },
39
+ // picture: {
40
+ // large: user.picture.large,
41
+ // medium: user.picture.medium,
42
+ // thumbnail: user.picture.thumbnail
43
+ // }
44
+ // etc...
45
+ } ;
46
+
47
+ // Get content digest of node. (Required field)
48
+ const contentDigest = crypto
49
+ . createHash ( `md5` )
50
+ . update ( JSON . stringify ( pathNode ) )
51
+ . digest ( `hex` ) ;
52
+ // add it to userNode
53
+ pathNode . internal . contentDigest = contentDigest ;
54
+
55
+ // Create node with the gatsby createNode() API
56
+ createNode ( pathNode ) ;
57
+ } ) ;
58
+
59
+ return ;
60
+ } ;
61
+
62
+ exports . createPages = async ( { actions, graphql } ) => {
5
63
const { createPage } = actions ;
6
64
const changelogTemplate = path . resolve (
7
65
"src/components/5_templates/changelogs.js"
8
66
) ;
67
+ const apiTemplate = path . resolve ( "src/components/5_templates/api.js" ) ;
9
68
10
- return graphql ( `
69
+ const changelogs = await graphql ( `
11
70
{
12
71
allMarkdownRemark(
13
72
sort: { order: DESC, fields: [frontmatter___date] }
@@ -39,7 +98,6 @@ exports.createPages = ({ actions, graphql }) => {
39
98
40
99
// Make tag pages
41
100
tags . forEach ( changelog => {
42
- console . log ( changelog ) ;
43
101
createPage ( {
44
102
path : `/changelog/${ _ . kebabCase ( changelog ) } /` ,
45
103
component : changelogTemplate ,
@@ -49,4 +107,40 @@ exports.createPages = ({ actions, graphql }) => {
49
107
} ) ;
50
108
} ) ;
51
109
} ) ;
110
+
111
+ const specsPages = await graphql ( `
112
+ {
113
+ allPaths {
114
+ edges {
115
+ node {
116
+ id
117
+ name
118
+ }
119
+ }
120
+ }
121
+ }
122
+ ` ) . then ( result => {
123
+ if ( result . errors ) {
124
+ return Promise . reject ( result . errors ) ;
125
+ }
126
+ const res = result . data . allPaths . edges ;
127
+ let paths = [ ] ;
128
+ _ . each ( res , edge => {
129
+ if ( _ . get ( edge , "node.name" ) ) {
130
+ paths = paths . concat ( edge . node . name ) ;
131
+ }
132
+ } ) ;
133
+ paths = _ . uniq ( paths ) ;
134
+ paths . forEach ( name => {
135
+ createPage ( {
136
+ path : `api/v4/${ _ . kebabCase ( name ) } ` ,
137
+ component : apiTemplate ,
138
+ context : {
139
+ name
140
+ }
141
+ } ) ;
142
+ } ) ;
143
+ } ) ;
144
+
145
+ return Promise . all ( [ changelogs , specsPages ] ) ;
52
146
} ;
0 commit comments