Skip to content

Commit

Permalink
docs: add API reference for params passed to gatsby-node APIs (#12087)
Browse files Browse the repository at this point in the history
This includes changes from #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 ( 58c6855 )
 - [x] merge #11597 and remove commits from that PR from this branch

Closes #4120
  • Loading branch information
pieh authored Mar 13, 2019
1 parent 1257a22 commit 6459e4e
Show file tree
Hide file tree
Showing 22 changed files with 1,164 additions and 237 deletions.
7 changes: 0 additions & 7 deletions .prettierrc

This file was deleted.

19 changes: 19 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
endOfLine: "lf",
semi: false,
singleQuote: false,
tabWidth: 2,
trailingComma: "es5",
overrides: [
{
// This file uses semicolons. It's needed here because `documentation`
// package (used to parse jsdoc and provide content for API reference pages)
// behaviour is inconsistent when not using semicolons after
// object declarations.
files: ["**/api-node-helpers-docs.js"],
options: {
semi: true,
},
},
],
}
10 changes: 5 additions & 5 deletions packages/gatsby/cache-dir/api-ssr-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ exports.onRenderBody = true
* replace head components to be rendered in your `html.js`. This is useful if
* you need to reorder scripts or styles added by other plugins.
* @param {Object} $0
* @param {Array} $0.getHeadComponents Returns the current `headComponents` array.
* @param {Array<ReactNode>} $0.getHeadComponents Returns the current `headComponents` array.
* @param {function} $0.replaceHeadComponents Takes an array of components as its
* first argument which replace the `headComponents` array which is passed
* to the `html.js` component. **WARNING** if multiple plugins implement this
* API it's the last plugin that "wins".
* @param {Array} $0.getPreBodyComponents Returns the current `preBodyComponents` array.
* @param {Array<ReactNode>} $0.getPreBodyComponents Returns the current `preBodyComponents` array.
* @param {function} $0.replacePreBodyComponents Takes an array of components as its
* first argument which replace the `preBodyComponents` array which is passed
* to the `html.js` component. **WARNING** if multiple plugins implement this
* API it's the last plugin that "wins".
* @param {Array} $0.getPostBodyComponents Returns the current `postBodyComponents` array.
* @param {Array<ReactNode>} $0.getPostBodyComponents Returns the current `postBodyComponents` array.
* @param {function} $0.replacePostBodyComponents Takes an array of components as its
* first argument which replace the `postBodyComponents` array which is passed
* to the `html.js` component. **WARNING** if multiple plugins implement this
Expand Down Expand Up @@ -140,7 +140,7 @@ exports.onPreRenderHTML = true
*
* _Note:_ [There is equivalent hook in Browser API](/docs/browser-apis/#wrapPageElement)
* @param {object} $0
* @param {object} $0.element The "Page" React Element built by Gatsby.
* @param {ReactNode} $0.element The "Page" React Element built by Gatsby.
* @param {object} $0.props Props object used by page.
* @example
* import React from "react"
Expand All @@ -162,7 +162,7 @@ exports.wrapPageElement = true
*
* _Note:_ [There is equivalent hook in Browser API](/docs/browser-apis/#wrapRootElement)
* @param {object} $0
* @param {object} $0.element The "Root" React Element built by Gatsby.
* @param {ReactNode} $0.element The "Root" React Element built by Gatsby.
* @example
* import React from "react"
* import { Provider } from "react-redux"
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/utils/api-browser-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ exports.replaceComponentRenderer = true
*
* _Note:_ [There is equivalent hook in SSR API](/docs/ssr-apis/#wrapPageElement)
* @param {object} $0
* @param {object} $0.element The "Page" React Element built by Gatsby.
* @param {ReactNode} $0.element The "Page" React Element built by Gatsby.
* @param {object} $0.props Props object used by page.
* @example
* import React from "react"
Expand All @@ -139,7 +139,7 @@ exports.wrapPageElement = true
*
* _Note:_ [There is equivalent hook in SSR API](/docs/ssr-apis/#wrapRootElement)
* @param {object} $0
* @param {object} $0.element The "Root" React Element built by Gatsby.
* @param {ReactNode} $0.element The "Root" React Element built by Gatsby.
* @example
* import React from "react"
* import { Provider } from "react-redux"
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/api-node-docs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Lets plugins implementing support for other compile-to-js add to the list
* of "resolvable" file extensions. Gatsby supports `.js` and `.jsx` by default.
* @returns {Array} array of extensions
* @returns {Array<string>} array of extensions
*/
exports.resolvableExtensions = true

Expand Down
299 changes: 299 additions & 0 deletions packages/gatsby/src/utils/api-node-helpers-docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
/* eslint-disable no-unused-vars */

/** */
const GatsbyReporter = {
/**
* @callback GatsbyReporterFn
* @param {string} message Message to display
* @returns {void}
*/

/**
* @callback GatsbyReporterFnWithError
* @param {string} message Message to display
* @param {Error}[error] Optional error object
* @returns {void}
*/

/**
* @type {GatsbyReporterFn}
* @example
* reporter.info(`text`)
*/
info: true,

/**
* @type {GatsbyReporterFn}
* @example
* reporter.warn(`text`)
*/
warn: true,

/**
* @type {GatsbyReporterFnWithError}
* @example
* reporter.error(`text`, new Error('something'))
*/
error: true,

/**
* @type {GatsbyReporterFnWithError}
* @example
* reporter.panic(`text`, new Error('something'))
*/
panic: true,

/**
* @type {GatsbyReporterFnWithError}
* @example
* reporter.panicOnBuild(`text`, new Error('something'))
*/
panicOnBuild: true,
};

/** */
const GatsbyCache = {
/**
* Retrieve cached value
* @param {string} key Cache key
* @returns {Promise<any>} Promise resolving to cached value
* @example
* const value = await cache.get(`unique-key`)
*/
get: true,

/**
* Cache value
* @param {string} key Cache key
* @param {any} value Value to be cached
* @returns {Promise<any>} Promise resolving to cached value
* @example
* await cache.set(`unique-key`, value)
*/
set: true,
};

/** */
const GatsbyTracing = {
/**
* Global tracer instance. Check
* [opentracing Tracer documentation](https://opentracing-javascript.surge.sh/classes/tracer.html)
* for more details.
* @type {Opentracing.Tracer}
*/
tracer: true,

/**
* Tracer span representing API run. Check
* [opentracing Span documentation](https://opentracing-javascript.surge.sh/classes/span.html)
* for more details.
* @type {Opentracing.Span}
*/
parentSpan: true,

/**
* @callback GatsbyTracing.StartSpan
* @param {string} spanName name of the span
* @returns {Opentracing.Span}
*/

/**
* Start a tracing span. The span will be created as a child of the currently
* running API span. This is a convenience wrapper for
* ```js
* tracing.tracer.startSpan(`span-name`, { childOf: tracing.parentSpan}).
* ```
* @type {GatsbyTracing.StartSpan}
* @example
* exports.sourceNodes = async ({ actions, tracing }) => {
* const span = tracing.startSpan(`foo`)
*
* // Perform any span operations. E.g add a tag to your span
* span.setTag(`bar`, `baz`)
*
* // Rest of your plugin code
*
* span.finish()
* }
*/
startSpan: true,
};

/** */
const GatsbyNodeHelpers = {
/**
* Key-value store used to persist results of time/memory/cpu intensive
* tasks. All functions are async and return promises.
* @type {GatsbyCache}
*/
cache: true,

/**
* Get cache instance by name - this should only be used by plugins that
* accept subplugins.
* @param {string} id Test
* @returns {GatsbyCache} See [`cache`](#cache) section for reference.
*/
getCache: true,

/**
* Create a stable content digest from a string or object, you can use the
* result of this function to set the `internal.contentDigest` field
* on nodes. Gatsby uses the value of this field to invalidate stale data
* when your content changes.
* @param {(string|object)} input
* @returns {string} Hash string
* @example
* const node = {
* ...nodeData,
* internal: {
* type: `TypeOfNode`,
* contentDigest: createContentDigest(nodeData)
* }
* }
*/
createContentDigest: true,

/**
* Collection of functions used to programmatically modify Gatsby’s internal state.
*
* See [`actions`](/docs/actions/) reference.
* @type {Actions}
* @deprecated Will be removed in gatsby 3.0. Use [actions](#actions)
* instead.
*/
boundActionCreators: true,

/**
* Collection of functions used to programmatically modify Gatsby’s internal state.
*
* See [`actions`](/docs/actions/) reference.
* @type {Actions}
*/
actions: true,

/**
* Get content for a node from the plugin that created it.
* @param {Node} node
* @returns {Promise<string>}
* @example
* module.exports = async function onCreateNode(
* { node, loadNodeContent, actions, createNodeId }
* ) {
* if (node.internal.mediaType === 'text/markdown') {
* const { createNode, createParentChildLink } = actions
* const textContent = await loadNodeContent(node)
* // process textContent and create child nodes
* }
* }
*/
loadNodeContent: true,

/**
* Internal redux state used for application state. Do not use, unless you
* absolutely must. Store is considered a private API and can change with
* any version.
* @type {Redux.Store}
*/
store: true,

/**
* Internal event emitter / listener. Do not use, unless you absolutely
* must. Emitter is considered a private API and can change with any version.
* @type {Emitter}
*/
emitter: true,

/**
* Get array of all nodes.
* @returns {Node[]} Array of nodes.
* @example
* const allNodes = getNodes()
*/
getNodes: true,

/**
* Get single node by given ID.
* Don't use this in graphql resolvers - see
* [`getNodeAndSavePathDependency`](#getNodeAndSavePathDependency).
* @param {string} ID id of the node.
* @returns {Node} Single node instance.
* @example
* const node = getNode(id)
*/
getNode: true,

/**
* Get array of nodes of given type.
* @param {string} Type of nodes
* @returns {Node[]} Array of nodes.
* @example
* const markdownNodes = getNodesByType(`MarkdownRemark`)
*/
getNodesByType: true,

/**
* Compares `contentDigest` of cached node with passed value
* to determine if node has changed.
*
* @param {string} id of node
* @param {string} contentDigest of node
* @returns {boolean}
* @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.
*/
hasNodeChanged: true,

/**
* Set of utilities to output information to user
* @type {GatsbyReporter}
*/
reporter: true,

/**
* Get single node by given ID and create dependency for given path.
* This should be used instead of `getNode` in graphql resolvers to enable
* tracking dependencies for query results. If it's not used Gatsby will
* not rerun query if node changes leading to stale query results. See
* [Page -> Node Dependency Tracking](/docs/page-node-dependencies/)
* for more details.
* @param {string} ID id of the node.
* @param {string} path of the node.
* @returns {Node} Single node instance.
*/
getNodeAndSavePathDependency: true,

/**
* Utility function useful to generate globally unique and stable node IDs.
* It will generate different IDs for different plugins if they use same
* input.
*
* @param {string} input
* @returns {string} UUIDv5 ID string
* @example
* const node = {
* id: createNodeId(`${backendData.type}${backendData.id}`),
* ...restOfNodeData
* }
*/
createNodeId: true,

/**
* Set of utilities that allow adding more detailed tracing for plugins.
* Check
* [Performance tracing](https://www.gatsbyjs.org/docs/performance-tracing)
* page for more details.
* @type {GatsbyTracing}
*/
tracing: true,

/**
* Use to prefix resources URLs. `pathPrefix` will be either empty string or
* path that starts with slash and doesn't end with slash. Check
* [Adding a Path Prefix](https://www.gatsbyjs.org/docs/path-prefix/)
* page for details about path prefixing.
* @type {string}
*/
pathPrefix: true,
};

module.exports = GatsbyNodeHelpers;
Loading

0 comments on commit 6459e4e

Please sign in to comment.