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

wagtail-js

v1.1.2

Published

Wagtail JavaScript SDK

Downloads

567

Readme

Wagtail TypeScript Client

The wagtail-js package is designed to provide a client for fetching content from a Wagtail-based CMS using TypeScript. This client facilitates the retrieval of various types of content, such as pages, images, and documents, from the CMS.

Installation

You can install the wagtail-js package using npm, pnpm, or yarn:

npm install wagtail-js
bun install wagtail-js
pnpm install wagtail-js
yarn add wagtail-js

Usage

To use the wagtail-js package, you need to create an instance of the CMSClient class, which provides methods for fetching different types of content from the CMS. The package also includes utility functions and types for handling CMS content.

Import Statements

import { CMSClient, FetchError, fetchContent, fetchRequest } from "wagtail-js";
import {
  ClientOptions,
  CMSContent,
  CMSContentPath,
  CMSContents,
  CMSMediaMeta,
  CMSQueries,
  NotFoundContents,
} from "wagtail-js";

Creating a CMSClient

To fetch content from the CMS, you need to create an instance of the CMSClient class:

const client = new CMSClient({
  baseURL: "https://example.com",
  apiPath: "/api/v2",
  mediaBaseURL: "https://cdn.example.com", // Optional media base URL, falls back to baseURL
  headers: {}, // Optional additional headers
  cache: "force-cache", // Optional caching strategy
});

Fetching Content

You can use the methods provided by the CMSClient class to fetch different types of content from the CMS. The methods handle response and error cases and return Promises that resolve with the parsed JSON response data.

Fetching Pages

const pages = await client.fetchPages({ limit: 10 });
console.log("Pages:", pages);

Fetching Images

const images = await client.fetchImages({ limit: 5 });
console.log("Images:", images);

Fetching Documents

const documents = await client.fetchDocuments({ limit: 3 });
console.log("Documents:", documents);

Fetching a Page

You can fetch a single page based on its ID (number) or slug (string):

const pageIdOrSlug = "home";
const page = await client.fetchPage(pageIdOrSlug);
console.log("Page:", page);

Fetching an Image

You can fetch a single image based on its ID:

const imageId = 1;
const image = await client.fetchImage(imageId);
console.log("Image:", image);

Fetching a Document

You can fetch a single document based on its ID:

const documentId = 2;
const document = await client.fetchDocument(documentId);
console.log("Document:", document);

Retrieving Media Source URL

The getMediaSrc method in the CMSClient class allows you to retrieve the source URL of a media item, such as an image or document, based on its type. This method is particularly useful when you need to display media content from your Wagtail-based CMS.

const imageMedia: CMSMediaMeta = {
  type: "wagtailimages.Image",
  detail_url: "https://demo.traleor.com/api/cms/v2/images/2/",
  download_url: "/images/1/image.jpg",
};
const documentMedia: CMSMediaMeta = {
  type: "wagtaildocs.Document",
  detail_url: "https://demo.traleor.com/api/cms/v2/documents/2/",
  download_url: "https://demo.traleor.com/docs/2/document.pdf",
};

const imageURL = client.getMediaSrc(imageMeta);
const documentURL = client.getMediaSrc(documentMeta);

console.log("Image URL:", imageURL); // https://cdn.traleor.com/images/1/image.jpg
console.log("Document URL:", documentURL); // https://cdn.traleor.com/docs/2/document.pdf

Utility Functions

The wagtail-js package includes utility functions for fetching content and constructing URLs for media items.

fetchContent

The fetchContent function allows you to fetch CMS content using the provided parameters and handle response and error cases:

try {
  const response = await fetchContent(
    "https://example.com",
    "/api/v2",
    "pages",
    { limit: 10 }
  );
  console.log("Response:", response);
} catch (error) {
  if (error instanceof FetchError) {
    console.error("Fetch error:", error.message);
  } else {
    console.error("Unknown error:", error);
  }
}

Types and Interfaces

The package provides various types and interfaces to help you work with CMS content and requests. These include:

  • ClientOptions: Configuration options for the CMSClient class.
  • CMSContentPath: Path to various types of CMS content.
  • CMSQueries: Optional queries to filter content.
  • CMSContent, CMSContents, CMSMediaMeta, CMSPageMeta: Interfaces for representing CMS content and metadata.
  • NotFoundContents: Interface representing "not found" response data.

Conclusion

The wagtail-js package simplifies the process of fetching content from a Wagtail-based CMS using TypeScript. It provides a convenient CMSClient class with methods for fetching different types of content and handling response and error cases. Additionally, utility functions and types help you work with CMS content and requests effectively.

Resources