0.0.11 • Published 1 year ago

contentlayer-mock v0.0.11

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

contentlayer-mock

Generate typed mocks for Contentlayer documents.

npm version size

Usage

Generate a mocked document

dummy generates a single mocked document. The argument properties must contain all required fields of the document type.

import { type Post } from "contentlayer/generated";
import { dummy } from "contentlayer-mock";

const dummyPost = dummy<Post>({
  title: "Dummy Post",
  description: "This is a dummy post",
});

// equivalent to

const dummyPost: Post = {
  _id: "dummy",
  _raw: {
    contentType: "mdx",
    flattenedPath: "posts/dummy",
    sourceFileDir: "posts",
    sourceFileName: "dummy-post.mdx",
    sourceFilePath: "posts/dummy-post.mdx",
  },
  body: {
    code: "",
    raw: "dummy document raw content",
  },
  type: "Post",
  title: "Dummy Post",
  description: "This is a dummy post",
};

properties can also be used to overwrite the default properties of the document.

const dummyPost = dummy<Post>({
  title: "Dummy Post",
  description: "This is a dummy post",
  _id: "custom-id",
  body: {
    raw: "custom raw content",
  },
});

dummyPost._id; // "custom-id"
dummyPost.body.raw; // "custom raw content"

Generate an array of mocked documents

dummyArray generates an array of mocked documents. The first argument, length, is the number of documents to generate. The second argument, properties, must contain all required fields of the document type.

import { type Post } from "contentlayer/generated";
import { dummyArray } from "contentlayer-mock";

const dummyPosts = dummyArray<Post>(10, {
  title: "Dummy Post",
  description: "This is a dummy post",
});

properties can also be used to overwrite the default properties of the documents.

const dummyPosts = dummyArray<Post>(10, {
  title: "Dummy Post",
  description: "This is a dummy post",
  _id: "custom-id",
  _raw: {
    flattenedPath: "posts/custom-id",
  },
});

You can also pass a function to properties to generate different values for each document.

const dummyPosts = dummyArray<Post>(10, (index) => ({
  title: `Dummy Post ${index}`,
  description: `This is a dummy post ${index}`,
  _id: `custom-id-${index}`,
}));

Limitations

Currently only supports mdx documents.

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago