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

contentful-api-generator

v1.0.0

Published

A contentful TypeScript API generation tool

Downloads

26

Readme

Contentful API Generator

Description

A Contentful API Generator for local usage with TypeScript cover.

Motivation

The purpose of this package is to generate a local API for your Contentful space. It saves you from using the API provided by Contentful as it's not absolutely free. This way you can tweak things as you want and have more impact on how to use the data. Very handy if some data processing needed to be done at built time (like processing assets for SSG) . And as it comes with TS you can easily cover your interfaces with those types from Contentful Entries with no extra effort.

Installation

npm i -D contentful-api-generator

Usage

// For ESM
import {
    generateContentfulApi,
    fetchContentfulSpaceData,
    moveAssetsToPublicFolder,
} from 'contentful-api-generator'

// For CJS
const {
    generateContentfulApi,
    fetchContentfulSpaceData,
    moveAssetsToPublicFolder,
} = require('contentful-api-generator')
  • To generate the whole API, types, extra jsons simply run generateContentfulApi
  • To only fetch the contentful-export.json use fetchContentfulSpaceData
  • To only manipulate where your assets are stored (once fetched) use moveAssetsToPublicFolder

Interface

Below you will find short description of the API generator's interface.

  interface CAGOptions {
    spaceId: string;
    // - Contentful Space ID can be found in settings
    managementToken: string;
    // - Contentful Space mandatory token (as well in settings)
    basePath: string;
    // - Base folder where generated files will be placed in
    // DEFAULT = "/api/contentful"
    assetsPath?: string;
    // - Base path to folder where assets will be stored (if downloadAssets === true)
    // DEFAULT: same as basePath
    contentFile?: string;
    // - Name of the file to store exported contentful data
    // DEFAULT: "contentful-export.json"
    exportDir?: string;
    // - Directory to which you want your contentful data to be stored
    // DEFAULT: basePath + '/data'
    errorLogFile?: string;
    // - Error log file path
    // DEFAULT: "error.log"
    downloadAssets?: boolean;
    // - Defines if you want to download Contentful Assets or not
    // DEFAULT: true
    contentEntriesJSONPath?: string;
    // - Path to where to store entries data
    //  DEFAULT: basePath + '/contentEntries.json'
    contentTypesJSONPath?: string;
    // - Path to where to store entries types
    //  DEFAULT: basePath + '/contentTypes.json'
    apiTSPath?: string;
    // - Path to where to store the generated API
    //  DEFAULT: basePath + '/api.ts'
}

Output files

The structure of the output files is following (within the default folder or the one that was provided by user):

  • api.ts - main entrypoint with all the contentful models to be accessed through. Also keeps the types for the models.
  • contentEntries.json - all the contentful entries with Links transformed to eventual entries (like images).
  • contentTypes.json - content model (used for creating contentTypes & slugs)
  • contentTypes.ts - content model types declarations can be found here
  • locales.json - exported file with available locales
  • locales.ts - type for used (available) locales
  • slugs.ts - types for all the slugs
  • data/contentful-export.json - the initial export file from contentful (can be deleted after api is generated)
  • your-path/contentful/(images | assets | downloads).ctfassets.net - folders with assets

Issues & Contributions

Issues can be raised here

Contributions section and guides to be implemented.