Skip to content

Commit 6459e4e

Browse files
authored
docs: add API reference for params passed to gatsby-node APIs (gatsbyjs#12087)
This includes changes from gatsbyjs#11597 which is needed for nested and also some really unrelated changes (to make preview work on netlify). Actual changes for review are limited to pieh/gatsby@58c6855...pieh:www-doc-preview .org preview available at https://www-doc-preview--tender-curie-facda8.netlify.com/docs/node-api-helpers/ TO-DO before the merge: - [x] change new path to `/docs/node-api-helpers` (from `/docs/node-apis-helpers`) - [x] remove temporary commit that allow to build previews on netlify ( gatsbyjs@58c6855 ) - [x] merge gatsbyjs#11597 and remove commits from that PR from this branch Closes gatsbyjs#4120
1 parent 1257a22 commit 6459e4e

22 files changed

+1164
-237
lines changed

.prettierrc

-7
This file was deleted.

.prettierrc.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
endOfLine: "lf",
3+
semi: false,
4+
singleQuote: false,
5+
tabWidth: 2,
6+
trailingComma: "es5",
7+
overrides: [
8+
{
9+
// This file uses semicolons. It's needed here because `documentation`
10+
// package (used to parse jsdoc and provide content for API reference pages)
11+
// behaviour is inconsistent when not using semicolons after
12+
// object declarations.
13+
files: ["**/api-node-helpers-docs.js"],
14+
options: {
15+
semi: true,
16+
},
17+
},
18+
],
19+
}

packages/gatsby/cache-dir/api-ssr-docs.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ exports.onRenderBody = true
9999
* replace head components to be rendered in your `html.js`. This is useful if
100100
* you need to reorder scripts or styles added by other plugins.
101101
* @param {Object} $0
102-
* @param {Array} $0.getHeadComponents Returns the current `headComponents` array.
102+
* @param {Array<ReactNode>} $0.getHeadComponents Returns the current `headComponents` array.
103103
* @param {function} $0.replaceHeadComponents Takes an array of components as its
104104
* first argument which replace the `headComponents` array which is passed
105105
* to the `html.js` component. **WARNING** if multiple plugins implement this
106106
* API it's the last plugin that "wins".
107-
* @param {Array} $0.getPreBodyComponents Returns the current `preBodyComponents` array.
107+
* @param {Array<ReactNode>} $0.getPreBodyComponents Returns the current `preBodyComponents` array.
108108
* @param {function} $0.replacePreBodyComponents Takes an array of components as its
109109
* first argument which replace the `preBodyComponents` array which is passed
110110
* to the `html.js` component. **WARNING** if multiple plugins implement this
111111
* API it's the last plugin that "wins".
112-
* @param {Array} $0.getPostBodyComponents Returns the current `postBodyComponents` array.
112+
* @param {Array<ReactNode>} $0.getPostBodyComponents Returns the current `postBodyComponents` array.
113113
* @param {function} $0.replacePostBodyComponents Takes an array of components as its
114114
* first argument which replace the `postBodyComponents` array which is passed
115115
* to the `html.js` component. **WARNING** if multiple plugins implement this
@@ -140,7 +140,7 @@ exports.onPreRenderHTML = true
140140
*
141141
* _Note:_ [There is equivalent hook in Browser API](/docs/browser-apis/#wrapPageElement)
142142
* @param {object} $0
143-
* @param {object} $0.element The "Page" React Element built by Gatsby.
143+
* @param {ReactNode} $0.element The "Page" React Element built by Gatsby.
144144
* @param {object} $0.props Props object used by page.
145145
* @example
146146
* import React from "react"
@@ -162,7 +162,7 @@ exports.wrapPageElement = true
162162
*
163163
* _Note:_ [There is equivalent hook in Browser API](/docs/browser-apis/#wrapRootElement)
164164
* @param {object} $0
165-
* @param {object} $0.element The "Root" React Element built by Gatsby.
165+
* @param {ReactNode} $0.element The "Root" React Element built by Gatsby.
166166
* @example
167167
* import React from "react"
168168
* import { Provider } from "react-redux"

packages/gatsby/src/utils/api-browser-docs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ exports.replaceComponentRenderer = true
117117
*
118118
* _Note:_ [There is equivalent hook in SSR API](/docs/ssr-apis/#wrapPageElement)
119119
* @param {object} $0
120-
* @param {object} $0.element The "Page" React Element built by Gatsby.
120+
* @param {ReactNode} $0.element The "Page" React Element built by Gatsby.
121121
* @param {object} $0.props Props object used by page.
122122
* @example
123123
* import React from "react"
@@ -139,7 +139,7 @@ exports.wrapPageElement = true
139139
*
140140
* _Note:_ [There is equivalent hook in SSR API](/docs/ssr-apis/#wrapRootElement)
141141
* @param {object} $0
142-
* @param {object} $0.element The "Root" React Element built by Gatsby.
142+
* @param {ReactNode} $0.element The "Root" React Element built by Gatsby.
143143
* @example
144144
* import React from "react"
145145
* import { Provider } from "react-redux"

packages/gatsby/src/utils/api-node-docs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Lets plugins implementing support for other compile-to-js add to the list
33
* of "resolvable" file extensions. Gatsby supports `.js` and `.jsx` by default.
4-
* @returns {Array} array of extensions
4+
* @returns {Array<string>} array of extensions
55
*/
66
exports.resolvableExtensions = true
77

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
/* eslint-disable no-unused-vars */
2+
3+
/** */
4+
const GatsbyReporter = {
5+
/**
6+
* @callback GatsbyReporterFn
7+
* @param {string} message Message to display
8+
* @returns {void}
9+
*/
10+
11+
/**
12+
* @callback GatsbyReporterFnWithError
13+
* @param {string} message Message to display
14+
* @param {Error}[error] Optional error object
15+
* @returns {void}
16+
*/
17+
18+
/**
19+
* @type {GatsbyReporterFn}
20+
* @example
21+
* reporter.info(`text`)
22+
*/
23+
info: true,
24+
25+
/**
26+
* @type {GatsbyReporterFn}
27+
* @example
28+
* reporter.warn(`text`)
29+
*/
30+
warn: true,
31+
32+
/**
33+
* @type {GatsbyReporterFnWithError}
34+
* @example
35+
* reporter.error(`text`, new Error('something'))
36+
*/
37+
error: true,
38+
39+
/**
40+
* @type {GatsbyReporterFnWithError}
41+
* @example
42+
* reporter.panic(`text`, new Error('something'))
43+
*/
44+
panic: true,
45+
46+
/**
47+
* @type {GatsbyReporterFnWithError}
48+
* @example
49+
* reporter.panicOnBuild(`text`, new Error('something'))
50+
*/
51+
panicOnBuild: true,
52+
};
53+
54+
/** */
55+
const GatsbyCache = {
56+
/**
57+
* Retrieve cached value
58+
* @param {string} key Cache key
59+
* @returns {Promise<any>} Promise resolving to cached value
60+
* @example
61+
* const value = await cache.get(`unique-key`)
62+
*/
63+
get: true,
64+
65+
/**
66+
* Cache value
67+
* @param {string} key Cache key
68+
* @param {any} value Value to be cached
69+
* @returns {Promise<any>} Promise resolving to cached value
70+
* @example
71+
* await cache.set(`unique-key`, value)
72+
*/
73+
set: true,
74+
};
75+
76+
/** */
77+
const GatsbyTracing = {
78+
/**
79+
* Global tracer instance. Check
80+
* [opentracing Tracer documentation](https://opentracing-javascript.surge.sh/classes/tracer.html)
81+
* for more details.
82+
* @type {Opentracing.Tracer}
83+
*/
84+
tracer: true,
85+
86+
/**
87+
* Tracer span representing API run. Check
88+
* [opentracing Span documentation](https://opentracing-javascript.surge.sh/classes/span.html)
89+
* for more details.
90+
* @type {Opentracing.Span}
91+
*/
92+
parentSpan: true,
93+
94+
/**
95+
* @callback GatsbyTracing.StartSpan
96+
* @param {string} spanName name of the span
97+
* @returns {Opentracing.Span}
98+
*/
99+
100+
/**
101+
* Start a tracing span. The span will be created as a child of the currently
102+
* running API span. This is a convenience wrapper for
103+
* ```js
104+
* tracing.tracer.startSpan(`span-name`, { childOf: tracing.parentSpan}).
105+
* ```
106+
* @type {GatsbyTracing.StartSpan}
107+
* @example
108+
* exports.sourceNodes = async ({ actions, tracing }) => {
109+
* const span = tracing.startSpan(`foo`)
110+
*
111+
* // Perform any span operations. E.g add a tag to your span
112+
* span.setTag(`bar`, `baz`)
113+
*
114+
* // Rest of your plugin code
115+
*
116+
* span.finish()
117+
* }
118+
*/
119+
startSpan: true,
120+
};
121+
122+
/** */
123+
const GatsbyNodeHelpers = {
124+
/**
125+
* Key-value store used to persist results of time/memory/cpu intensive
126+
* tasks. All functions are async and return promises.
127+
* @type {GatsbyCache}
128+
*/
129+
cache: true,
130+
131+
/**
132+
* Get cache instance by name - this should only be used by plugins that
133+
* accept subplugins.
134+
* @param {string} id Test
135+
* @returns {GatsbyCache} See [`cache`](#cache) section for reference.
136+
*/
137+
getCache: true,
138+
139+
/**
140+
* Create a stable content digest from a string or object, you can use the
141+
* result of this function to set the `internal.contentDigest` field
142+
* on nodes. Gatsby uses the value of this field to invalidate stale data
143+
* when your content changes.
144+
* @param {(string|object)} input
145+
* @returns {string} Hash string
146+
* @example
147+
* const node = {
148+
* ...nodeData,
149+
* internal: {
150+
* type: `TypeOfNode`,
151+
* contentDigest: createContentDigest(nodeData)
152+
* }
153+
* }
154+
*/
155+
createContentDigest: true,
156+
157+
/**
158+
* Collection of functions used to programmatically modify Gatsby’s internal state.
159+
*
160+
* See [`actions`](/docs/actions/) reference.
161+
* @type {Actions}
162+
* @deprecated Will be removed in gatsby 3.0. Use [actions](#actions)
163+
* instead.
164+
*/
165+
boundActionCreators: true,
166+
167+
/**
168+
* Collection of functions used to programmatically modify Gatsby’s internal state.
169+
*
170+
* See [`actions`](/docs/actions/) reference.
171+
* @type {Actions}
172+
*/
173+
actions: true,
174+
175+
/**
176+
* Get content for a node from the plugin that created it.
177+
* @param {Node} node
178+
* @returns {Promise<string>}
179+
* @example
180+
* module.exports = async function onCreateNode(
181+
* { node, loadNodeContent, actions, createNodeId }
182+
* ) {
183+
* if (node.internal.mediaType === 'text/markdown') {
184+
* const { createNode, createParentChildLink } = actions
185+
* const textContent = await loadNodeContent(node)
186+
* // process textContent and create child nodes
187+
* }
188+
* }
189+
*/
190+
loadNodeContent: true,
191+
192+
/**
193+
* Internal redux state used for application state. Do not use, unless you
194+
* absolutely must. Store is considered a private API and can change with
195+
* any version.
196+
* @type {Redux.Store}
197+
*/
198+
store: true,
199+
200+
/**
201+
* Internal event emitter / listener. Do not use, unless you absolutely
202+
* must. Emitter is considered a private API and can change with any version.
203+
* @type {Emitter}
204+
*/
205+
emitter: true,
206+
207+
/**
208+
* Get array of all nodes.
209+
* @returns {Node[]} Array of nodes.
210+
* @example
211+
* const allNodes = getNodes()
212+
*/
213+
getNodes: true,
214+
215+
/**
216+
* Get single node by given ID.
217+
* Don't use this in graphql resolvers - see
218+
* [`getNodeAndSavePathDependency`](#getNodeAndSavePathDependency).
219+
* @param {string} ID id of the node.
220+
* @returns {Node} Single node instance.
221+
* @example
222+
* const node = getNode(id)
223+
*/
224+
getNode: true,
225+
226+
/**
227+
* Get array of nodes of given type.
228+
* @param {string} Type of nodes
229+
* @returns {Node[]} Array of nodes.
230+
* @example
231+
* const markdownNodes = getNodesByType(`MarkdownRemark`)
232+
*/
233+
getNodesByType: true,
234+
235+
/**
236+
* Compares `contentDigest` of cached node with passed value
237+
* to determine if node has changed.
238+
*
239+
* @param {string} id of node
240+
* @param {string} contentDigest of node
241+
* @returns {boolean}
242+
* @deprecated This check is done internally in Gatsby and it's not necessary to use it in plugins. Will be removed in gatsby 3.0.
243+
*/
244+
hasNodeChanged: true,
245+
246+
/**
247+
* Set of utilities to output information to user
248+
* @type {GatsbyReporter}
249+
*/
250+
reporter: true,
251+
252+
/**
253+
* Get single node by given ID and create dependency for given path.
254+
* This should be used instead of `getNode` in graphql resolvers to enable
255+
* tracking dependencies for query results. If it's not used Gatsby will
256+
* not rerun query if node changes leading to stale query results. See
257+
* [Page -> Node Dependency Tracking](/docs/page-node-dependencies/)
258+
* for more details.
259+
* @param {string} ID id of the node.
260+
* @param {string} path of the node.
261+
* @returns {Node} Single node instance.
262+
*/
263+
getNodeAndSavePathDependency: true,
264+
265+
/**
266+
* Utility function useful to generate globally unique and stable node IDs.
267+
* It will generate different IDs for different plugins if they use same
268+
* input.
269+
*
270+
* @param {string} input
271+
* @returns {string} UUIDv5 ID string
272+
* @example
273+
* const node = {
274+
* id: createNodeId(`${backendData.type}${backendData.id}`),
275+
* ...restOfNodeData
276+
* }
277+
*/
278+
createNodeId: true,
279+
280+
/**
281+
* Set of utilities that allow adding more detailed tracing for plugins.
282+
* Check
283+
* [Performance tracing](https://www.gatsbyjs.org/docs/performance-tracing)
284+
* page for more details.
285+
* @type {GatsbyTracing}
286+
*/
287+
tracing: true,
288+
289+
/**
290+
* Use to prefix resources URLs. `pathPrefix` will be either empty string or
291+
* path that starts with slash and doesn't end with slash. Check
292+
* [Adding a Path Prefix](https://www.gatsbyjs.org/docs/path-prefix/)
293+
* page for details about path prefixing.
294+
* @type {string}
295+
*/
296+
pathPrefix: true,
297+
};
298+
299+
module.exports = GatsbyNodeHelpers;

0 commit comments

Comments
 (0)