@@ -2,6 +2,7 @@ const JsonSchemaRefParser = require("json-schema-ref-parser");
2
2
const path = require ( "path" ) ;
3
3
const _ = require ( "lodash" ) ;
4
4
const fs = require ( "fs" ) ;
5
+ const axios = require ( "axios" ) ;
5
6
6
7
const specs = require ( "./static/api/docs/v4/spec.json" ) ;
7
8
const crypto = require ( "crypto" ) ;
@@ -13,6 +14,7 @@ exports.sourceNodes = async ({ actions }) => {
13
14
const { createNode, createTypes } = actions ;
14
15
const res = await parser . dereference ( specs ) ;
15
16
17
+ // CREATING NODES FROM API SPECS
16
18
const typeDefs = `
17
19
type MarkdownRemark implements Node {
18
20
frontmatter: Frontmatter!
@@ -26,55 +28,146 @@ exports.sourceNodes = async ({ actions }) => {
26
28
27
29
createTypes ( typeDefs ) ;
28
30
29
- // map into these results and create nodes
30
- Object . keys ( res . paths ) . map ( ( path , i ) => {
31
- // Create your node object
32
- const modes = {
33
- get : "get" ,
34
- post : "post" ,
35
- put : "put" ,
36
- delete : "delete"
37
- } ;
38
- const pathNode = {
39
- // Required fields
40
- id : `${ i } ` ,
41
- parent : `__SOURCE__` ,
42
- internal : {
43
- type : `Paths` // name of the graphQL query --> allRandomUser {}
44
- // contentDigest will be added just after
45
- // but it is required
46
- } ,
47
- children : [ ] ,
31
+ const allSpecs =
32
+ // map into these results and create nodes
33
+ Object . keys ( res . paths ) . map ( async ( path , i ) => {
34
+ // Create your node object
35
+ const pathNode = {
36
+ // Required fields
37
+ id : `${ i } ` ,
38
+ parent : `__SOURCE__` ,
39
+ internal : {
40
+ type : `Paths` // name of the graphQL query --> allRandomUser {}
41
+ } ,
42
+ children : [ ] ,
48
43
49
- // Other fields that you want to query with graphQl
50
- name : path ,
51
- get : res . paths [ path ] . get ,
52
- post : res . paths [ path ] . post ,
53
- put : res . paths [ path ] . put ,
54
- delete : res . paths [ path ] . delete ,
55
- responses : res . paths [ path ] . responses ,
56
- requestBody : res . paths [ path ] . requestBody ,
57
- parameters : res . paths [ path ] . parameters ,
58
- tagGroup :
59
- ( res . paths [ path ] [ "get" ] && res . paths [ path ] [ "get" ] . tags ) ||
60
- ( res . paths [ path ] [ "put" ] && res . paths [ path ] [ "put" ] . tags ) ||
61
- ( res . paths [ path ] [ "post" ] && res . paths [ path ] [ "post" ] . tags ) ||
62
- ( res . paths [ path ] [ "delete" ] && res . paths [ path ] [ "delete" ] . tags )
63
- } ;
44
+ // Other fields that you want to query with graphQl
45
+ name : path ,
46
+ get : res . paths [ path ] . get ,
47
+ post : res . paths [ path ] . post ,
48
+ put : res . paths [ path ] . put ,
49
+ delete : res . paths [ path ] . delete ,
50
+ responses : res . paths [ path ] . responses ,
51
+ requestBody : res . paths [ path ] . requestBody ,
52
+ parameters : res . paths [ path ] . parameters ,
53
+ tagGroup :
54
+ ( res . paths [ path ] [ "get" ] && res . paths [ path ] [ "get" ] . tags ) ||
55
+ ( res . paths [ path ] [ "put" ] && res . paths [ path ] [ "put" ] . tags ) ||
56
+ ( res . paths [ path ] [ "post" ] && res . paths [ path ] [ "post" ] . tags ) ||
57
+ ( res . paths [ path ] [ "delete" ] && res . paths [ path ] [ "delete" ] . tags )
58
+ } ;
64
59
65
- // Get content digest of node. (Required field)
66
- const contentDigest = crypto
67
- . createHash ( `md5` )
68
- . update ( JSON . stringify ( pathNode ) )
69
- . digest ( `hex` ) ;
70
- // add it to userNode
71
- pathNode . internal . contentDigest = contentDigest ;
60
+ // Get content digest of node. (Required field)
61
+ const contentDigest = crypto
62
+ . createHash ( `md5` )
63
+ . update ( JSON . stringify ( pathNode ) )
64
+ . digest ( `hex` ) ;
65
+ // add it to userNode
66
+ pathNode . internal . contentDigest = contentDigest ;
72
67
73
- // Create node with the gatsby createNode() API
74
- createNode ( pathNode ) ;
68
+ // Create node with the gatsby createNode() API
69
+ createNode ( pathNode ) ;
70
+ } ) ;
71
+
72
+ const baseUrl =
73
+ "https://linode.com/wp-json/menus/v1/menus" ;
74
+
75
+ // CREATING MENU NODES FROM WP API
76
+ const wpMenus = [
77
+ {
78
+ path : `${ baseUrl } /header-utility` ,
79
+ name : "HeaderUtility"
80
+ } ,
81
+ {
82
+ path : `${ baseUrl } /header-primary` ,
83
+ name : "HeaderPrimary"
84
+ } ,
85
+ {
86
+ path : `${ baseUrl } /submenu-why-linode-primary` ,
87
+ name : "WhyPrimary"
88
+ } ,
89
+ {
90
+ path : `${ baseUrl } /submenu-why-linode-services` ,
91
+ name : "WhyServices"
92
+ } ,
93
+ {
94
+ path : `${ baseUrl } /submenu-products-featured` ,
95
+ name : "ProductsFeatured"
96
+ } ,
97
+ {
98
+ path : `${ baseUrl } /submenu-products-compute` ,
99
+ name : "ProductsCompute"
100
+ } ,
101
+ {
102
+ path : `${ baseUrl } /submenu-products-storage` ,
103
+ name : "ProductsStorage"
104
+ } ,
105
+ {
106
+ path : `${ baseUrl } /submenu-products-services` ,
107
+ name : "ProductsServices"
108
+ } ,
109
+ {
110
+ path : `${ baseUrl } /submenu-products-networking` ,
111
+ name : "ProductsNetworking"
112
+ } ,
113
+ {
114
+ path : `${ baseUrl } /submenu-products-developer-tools` ,
115
+ name : "ProductsDevTools"
116
+ } ,
117
+ {
118
+ path : `${ baseUrl } /submenu-community-primary` ,
119
+ name : "CommunityPrimary"
120
+ } ,
121
+ {
122
+ path : `${ baseUrl } /submenu-community-secondary` ,
123
+ name : "CommunityServices"
124
+ } ,
125
+ {
126
+ path : `${ baseUrl } /submenu-mobile-primary` ,
127
+ name : "PrimaryMobile"
128
+ } ,
129
+ {
130
+ path : `${ baseUrl } /submenu-mobile-utility` ,
131
+ name : "UtilityMobile"
132
+ }
133
+ ] ;
134
+
135
+ const allMenus = wpMenus . map ( async menu => {
136
+ const endpoint = ( ) => axios . get ( menu . path ) ;
137
+ // await for results
138
+ const list = await endpoint ( ) ;
139
+
140
+ list . data . items . map ( ( menuItem , i ) => {
141
+ // Create your node object
142
+ const itemNode = {
143
+ // Required fields
144
+ id : `${ i } ` ,
145
+ parent : `__SOURCE__` ,
146
+ internal : {
147
+ type : menu . name
148
+ } ,
149
+ children : [ ] ,
150
+ title : menuItem . title ,
151
+ url : menuItem . url ,
152
+ switch_on : menuItem . switch_on ? menuItem . switch_on : undefined ,
153
+ description : menuItem . description ? menuItem . description : undefined ,
154
+ icon : menuItem . icon ? menuItem . icon : undefined
155
+ } ;
156
+
157
+ // Get content digest of node. (Required field)
158
+ const contentDigest = crypto
159
+ . createHash ( `md5` )
160
+ . update ( JSON . stringify ( itemNode ) )
161
+ . digest ( `hex` ) ;
162
+ // add it to userNod e
163
+ itemNode . internal . contentDigest = contentDigest ;
164
+
165
+ // Create node with the gatsby createNode() API
166
+ return createNode ( itemNode ) ;
167
+ } ) ;
75
168
} ) ;
76
169
77
- return ;
170
+ return Promise . all ( allMenus , allSpecs ) ;
78
171
} ;
79
172
80
173
exports . createPages = async ( { actions, graphql } ) => {
0 commit comments