Skip to content

pemedia/payload-fallback-data

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

payload-fallback-data

Want to show a (live) preview of some pages built with payload cms?
While relying on the generated types, the frontend can break easily, if it is coupled to required fields. 💥
(s. https://payloadcms.com/docs/live-preview/client)

This library provides a fallback for every required unset field.

Quick Start

Server

Generate the schema for all collections which needs fallback data.
The easiest way is to provide custom endpoints:

import { PayloadRequest } from "payload";
import { mapFields } from "payload-fallback-data/server";

const endpoint = {
    method: "get",
    path: "/schema",
    handler: async (req: PayloadRequest) => {
        const fields = mapFields(req.payload.collections.posts.config.flattenedFields);

        return Response.json(fields);
    },
};

Client

  1. Get the schema for the collection (for example by using the custom endpoint from above):
import { FieldConfig } from "payload-fallback-data/types";

const schema: FieldConfig[] = await fetch(...)
  1. Use the schema to convert the partial data:
import { convertData } from "payload-fallback-data/client";

// e.g. while using payload live preview
const { data } = useLivePreview(...);

const dataWithFallbacks = convertData(schema, data);

Changelog

v 1.0.0

  • Initial release

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published