Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gatsby-node-helpers

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gatsby-node-helpers - npm Package Compare versions

Comparing version 0.1.2 to 0.1.3

2

package.json
{
"name": "gatsby-node-helpers",
"version": "0.1.2",
"version": "0.1.3",
"description": "Gatsby node helper functions to aid node creation.",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -61,3 +61,29 @@ # gatsby-node-helpers

export const ProductNode = createNodeFactory(`Product`)
const PRODUCT_TYPE = `Product`
const PRODUCT_VARIANT_TYPE = `ProductVariant`
export const ProductNode = createNodeFactory(PRODUCT_TYPE, node => {
if (node.variants) {
const variants = node.variants.edges.map(edge => edge.node)
// Set additional fields
const variantPrices = variants
.map(variant => Number.parseFloat(variant.price))
.filter(Boolean)
node.minPrice = Math.min(...variantPrices) || 0
node.maxPrice = Math.max(...variantPrices) || 0
// Set children
node.children = variants.map(variant =>
generateNodeId(PRODUCT_VARIANT_TYPE, variant.id),
)
// Remove unnecessary fields
delete node.variants
}
return node
})
export const ProductVariantNode = createNodeFactory(PRODUCT_VARIANT_TYPE)
```

@@ -85,4 +111,12 @@

products.forEach(product => {
const node = ProductNode(product)
createNode(node)
const productNode = ProductNode(product)
createNode(productNode)
product.variants.edges.forEach(edge => {
const variant = edge.node
const productVariantNode = ProductVariantNode(variant, {
parent: productNode.id,
})
createNode(productVariantNode)
})
})

@@ -104,5 +138,5 @@ }

}) => ({
createNodeFactory: (type: String, middleware?: Node => Node),
generateNodeId: (type: String, id: String),
generateTypeName: (type: String)
createNodeFactory: (type: String, middleware?: Node => Node) => (obj: Object, overrides?: Object),
generateNodeId: (type: String, id: String) => String,
generateTypeName: (type: String) => String
})

@@ -115,20 +149,20 @@ ```

* `sourceId?: String` - Optional (default: `__SOURCE__`)
##### `sourceId?: String` - Optional (default: `__SOURCE__`)
Default source parent ID. If not defined, the node is set as a top-level node.
Default source parent ID. If not defined, the node is set as a top-level node.
* `typePrefix: String` - **Required**
##### `typePrefix: String` - **Required**
Prefix for all nodes. Used a namespace for node type names and IDs. Must be
PascalCase.
Prefix for all nodes. Used a namespace for node type names and IDs. Must be
PascalCase.
* `conflictFieldPrefix?: String` - Optional (default: camelcased `typePrefix`)
##### `conflictFieldPrefix?: String` - Optional (default: camelcased `typePrefix`)
Prefix for all fields conflicting with Gatsby's internal fields:
Prefix for all fields conflicting with Gatsby's internal fields:
* `id`
* `children`
* `parent`
* `fields`
* `internal`
* `id`
* `children`
* `parent`
* `fields`
* `internal`

@@ -139,13 +173,13 @@ #### Outputs

* `createNodeFactory: (type: String, middleware?: Node => Node)`
##### `createNodeFactory: (type: String, middleware?: Node => Node) => (obj: Object, overrides?: Object)`
Jump to documentation: [`createNodeFactory`](#createNodeFactory)
Jump to documentation: [`createNodeFactory`](#createnodefactory)
* `generateNodeId: (type: String, id: String)`
##### `generateNodeId: (type: String, id: String) => String`
Jump to documentation: [`generateNodeId`](#generateNodeId)
Jump to documentation: [`generateNodeId`](#generatenodeid)
* `generateTypeName: (type: String)`
##### `generateTypeName: (type: String) => String`
Jump to documentation: [`generateTypeName`](#generateTypeName)
Jump to documentation: [`generateTypeName`](#generatetypename)

@@ -158,52 +192,104 @@ ---

Creates a factory function that takes in an object and outputs a Gatsby
`createNode` compatible object.
```js
;(type: String, middleware?: Node => Node) => (
obj: Object,
overrides?: Object,
) => Node
```
By default, the input object is kept intact, with the following exceptions:
#### Inputs
* Fields conflicting with Gatsby's internal fields are prefixed.
* `obj.id` is set to a generated string containing `typePrefix` set in
`createNodeHelpers`, `type` set in the factory function, and `obj.id`
* `obj.parent` is set to `sourceId` set in `createNodeHelpers` (`__SOURCE__` by
default)
* `obj.children` is set to `[]` by default
* `obj.internal.type` is set to a generated string containing `typePrefix` set
in `createNodeHelpers` and `type` set in the factory function
* `obj.internal.contentDigest` is set to the MD5 hash of the object including
Gatsby's internal fields.
##### `type: String` - **Required**
A middleware function can be provided to modify the node beyond these
exceptions. This can be used, for example, to set `obj.children`. The middleware
function is passed the modified object and must return a Gatsby-comptabible
The type of the node the resulting function will create.
##### `middleware?: Node => Node` - Optional (default: identity function)
Allows for modifying the node beyond the default Gatsby fields. This function
recieves the Node object and must return the Node object, with modifications if
necessary.
The middleware function is called at the following point:
1. Clone the input object and add Gatsby internal fields.
2. **HERE =>**: Run Node through middleware, modifiying fields as necessary.
3. Merge node with overrides provided to `createNodeFactory`'s resulting
function.
Typical uses of the middleware function is setting up the `children` field,
adding new fields based on the object, and removing unecessary fields.
#### Outputs
##### `(obj: Object, overrides?: Object) => Node`
Factory function to create a Gatsby `createNode` compatible object.
It takes in an object, sets it up for Gatsby using the previously defined
options (see Inputs above), merges any overrides provided, and returns an
object.
The resulting function of `createNodeFactory` can take in an object and an
overrides object. This can be used, for example, to set `obj.parent`. The
overrides object is merged with the modified object using `Object.assign`; no
mutations are performed, so be careful with what you set.
By default, the input object is kept intact, with the following exceptions:
| Field | Description |
| ------------------------ | ------------------------------------------------------------------------- |
| `id` | A generated string containing `typePrefix`, `type`, and the original `id` |
| `parent` | `sourceId` set in `createNodeHelpers` (`__SOURCE__` by default) |
| `children` | `[]` by default |
| `internal.type` | A generated string containing `typePrefix` and `type` |
| `internal.contentDigest` | MD5 hash of the object including the above added fields |
The `overrides` parameter allows for a final chance to override any node field.
The object is merged directly with no modifications.
### `generateNodeId`
Creates a function that takes in a node type and node ID and returns a formatted
string. It is used internally to create a node's ID, but it can be useful when
```js
;(type: String, id: String) => String
```
Function that takes in a node type and node ID and returns a formatted string.
It is used internally to create a node's ID.
Because it is used internally to create a node's ID, it can be useful when
setting `obj.parent` and `obj.children` in a middleware function or overrides
object.
object. The result will always be the same as long as the same type and ID are
provided.
### `generateTypeName`
#### Inputs
Creates a function that takes in a node type and returns a formatted string. It
is used internally to create a node's type name.
##### `type: String` - **Required**
#### Type signature
Type of the node.
##### `id: String` - **Required**
ID of the node. Must be unique scoped to the type.
#### Outputs
##### `String`
A formatted string containing `typePrefix`, `type` and `id`. Used internally to
consistently generate node IDs.
### `generateTypeName`
```js
(options: {
sourceId?: String,
typePrefix: String,
conflictFieldPrefix?: String
}) => {
createNodeFactory: (type: String, middleware: )
}
(type: String): String
```
#### Inputs
##### `type: String` - **Required**
Type of the node.
#### Outputs
##### `String`
A formatted string containing `typePrefix` and `type`. Used internally to
consistently generate node type names.
[gatsby-source-plugins]: https://www.gatsbyjs.org/docs/create-source-plugin/
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc