npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prismic-utils

v7.0.0

Published

A collection of functions for parsing data from a Prismic CMS

Downloads

347

Readme

Prismic Utils

A collection of functions for fetching and parsing data from a Prismic CMS

Build Status npm

Important Links


Table of Contents

  1. Installation
  2. Deserializing Documents
  3. Reducers
  4. Action Creators
  5. Fetching Data
  6. Utils

Installation

Yarn

yarn add prismic-utils

NPM

npm install prismic-utils --save


Deserializing Data

Deserialize a Prismic document into a simple object

import { deserializeDocument } from 'prismic-utils'

const page = deserializeDocument(doc, options)

Params

doc: the Prismic document that has been fetched from the Prismic API

Nest deserialized data

Use kebab case when naming your Prismic fields to nest your deserialized data.

E.g. header-title will deserialize to { header: { title: '' } }


Reducers

Create a Redux reducer to manage Prismic doc(s)

Eliminate the need to write repetitive reducers to manage data from Prismic docs.

import { createDocumentsReducer } from 'prismic-utils'

combineReducers({
	pages: createDocumentsReducer('pages')
})

Params

namespace: this will be used to decide which actions to act upon

  • createDocumentsReducer('pages') will listen for pages/FETCH, pages/FETCH_SUCCESS and pages/FETCH_FAILURE

options: additional options

  • fetch: override the default handler used for FETCH action
  • fetchSuccess: override the default handler used for FETCH_SUCCESS action
  • fetchFailure: override the default handler used for FETCH_FAILURE action
  • initialState: override the default initial state i.e. {}

Action Creators

Create a Redux action creator

Eliminate the need to write repetitive files to fetch Prismic docs and dispatch the relevant actions.

import { createDocumentsAction } from 'prismic-utils'

dispatch(createDocumentsAction('page', options))

createDocumentsAction returns a function to be used by Redux Thunk to fetch your documents and dispatch the relevant actions.

Params

namespace: this will be used to namespace the actions

  • dispatch(createDocumentsAction('pages', options)) will dispatch the following actions
  • pages/FETCH when the request is being fired off
  • pages/FETCH_SUCCESS when the request is fulfilled, with the data key of the payload containing the found documents
  • pages/FETCH_FAILURE if there was an error during the request
  • token can be an actual token, or you can pass in a function that will be called with the redux store, so you can retrieve your token from your store

options: additional options that will be used by fetchDocuments

createDocumentAction is the same as createDocumentsAction, except it will only retrieve and return a single document.


Fetching Data

fetchDocuments

Fetch documents from the Prismic API

import { fetchDocuments } from 'prismic-utils'

fetchDocuments(options)
	.then((docs) => docs) // docs will be an array of documents

Params

options: various options to use when fetching:

  • type: the type of document to fetch e.g. page
  • token: the document ref to fetch (defaults to Api.master())
  • options: Query options, as in https://prismic.io/docs/javascript/query-the-api/query-options-reference
  • predicates (string | array): Predicates options, as in https://prismic.io/docs/javascript/query-the-api/query-predicates-reference
  • repository: Repository to query from

fetchDocument

Fetch a single document

import { fetchDocument } from 'prismic-utils'

fetchDocument(options)
	.then((doc) => doc)

Utils

Prismic utils also exports a couple of useful utilities

PrismicRichText

This is a React component that can be used to render Rich Text fields using all react components, rather than relying on inserting raw HTML using dangerouslySetInnerHTML.

import { PrismicRichText } from 'prismic-utils'

const MyComponent = ({ myRichTextField }) => (
	<PrismicRichText>
		{myRichTextField.data}
	</PrismicRichText>
)