All Projects → nascentdigital → contentfully

nascentdigital / contentfully

Licence: MIT License
A simple but performant REST client for Contentful.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to contentfully

Server Manager
This repository holds the IntISP Interface. It can be rebuilt to interface with any other hosting panel.
Stars: ✭ 31 (+138.46%)
Mutual labels:  fast, simple
Ataraxia
Simple and lightweight source-based multi-platform Linux distribution with musl libc.
Stars: ✭ 226 (+1638.46%)
Mutual labels:  fast, simple
Derrick
🙌 Derrick is a clean minimal and fast theme for a personal blog.
Stars: ✭ 51 (+292.31%)
Mutual labels:  fast, simple
Maroto
A maroto way to create PDFs. Maroto is inspired in Bootstrap and uses gofpdf. Fast and simple.
Stars: ✭ 334 (+2469.23%)
Mutual labels:  fast, simple
base64.c
Base64 Library in C
Stars: ✭ 60 (+361.54%)
Mutual labels:  fast, simple
Wyhash
The FASTEST QUALITY hash function, random number generators (PRNG) and hash map.
Stars: ✭ 410 (+3053.85%)
Mutual labels:  fast, simple
Paris
Logger in Rust for pretty colors and text in the terminal. Aiming for a relatively simple API
Stars: ✭ 162 (+1146.15%)
Mutual labels:  fast, simple
core
🌈 light, fast, and easy to use, dependencies free javascript syntax highlighter, with automatic language detection
Stars: ✭ 40 (+207.69%)
Mutual labels:  fast, simple
smk
SMK - Simple multimedia kit - C++ WebAssembly
Stars: ✭ 89 (+584.62%)
Mutual labels:  fast, simple
mocha-simple-html-reporter
Simple HTML reporter for Mocha
Stars: ✭ 16 (+23.08%)
Mutual labels:  fast, simple
Wondercms
WonderCMS - fast and small flat file CMS (5 files)
Stars: ✭ 330 (+2438.46%)
Mutual labels:  fast, simple
node-perj
A fast, flexible JSON logger.
Stars: ✭ 16 (+23.08%)
Mutual labels:  fast, simple
Begin Latex In Minutes
📜 Brief Intro to LaTeX for beginners that helps you use LaTeX with ease.
Stars: ✭ 3,082 (+23607.69%)
Mutual labels:  fast, simple
Try
Dead simple CLI tool to try Python packages - It's never been easier! 📦
Stars: ✭ 588 (+4423.08%)
Mutual labels:  fast, simple
Kemal
Fast, Effective, Simple Web Framework
Stars: ✭ 3,227 (+24723.08%)
Mutual labels:  fast, simple
Borm
【🔥今日热门】🏎️ 更好的ORM库 (Better ORM library that is simple, fast and self-mockable for Go)
Stars: ✭ 102 (+684.62%)
Mutual labels:  fast, simple
toro
Tree oriented routing
Stars: ✭ 116 (+792.31%)
Mutual labels:  fast, simple
vertx-start
简单地、快速地启动vert.x的手脚架,保留了vert.x原汁原味的开发方式
Stars: ✭ 102 (+684.62%)
Mutual labels:  fast, simple
ByteCopy
Simple C99 program and API for copying files.
Stars: ✭ 16 (+23.08%)
Mutual labels:  fast, simple
logquacious
Logquacious (lq) is a fast and simple log viewer.
Stars: ✭ 55 (+323.08%)
Mutual labels:  fast, simple

Contentfully

A simple but fast API client for Contentful that lets developers focus on data instead of Contentful metadata and REST structure.

NPM version downloads Node version Build Status Code Coverage Known Vulnerabilities

Features

  • Transforms Contentful responses into simple / flat JavaScript objects.
  • Stripping of metadata, retaining the basics that you need (e.g. ID, contentType, and fields).
  • Recursive folding of linked entries and assets without cloning (i.e. an Entry / Asset that is reference multiple times is only created once).
  • Supports custom transforms of assets URLs to allow caching or rewrites.
  • Supports full Content Delivery API, including custom environments and preview servers.
  • Typescript 4 support.
  • React Native support.
  • Customizable retries when Contentful rate-limit throttling occurs.

Installation

Current stable release (1.x)

$ npm i -s contentfully

Prerequisites

You'll need the Space ID and API key for the space that you wish to access. You can get these by doing the following after logging into the Contentful Web App:

  1. Navigate to your Organization / Space (usually from the upper-left space selector in the top toolbar).
  2. Select Settings → General Settings to find your Space ID.
  3. Select Settings → API keys to see your generated API keys (you will need to have the correct permissions on the space to do this). Create a new API key if you need to.

Basic Usage

Getting started is really easy. First you'll need to create and configure a ContentfulClient instance.

Option Type Required? Default
accessToken string YES
spaceId string YES
environmentId string NO master
preview boolean NO false
fetch Function NO fetch / node-fetch
onRateLimitError Function NO () => false

Once configured, pass the client into a Contentfully instance:

import {ContentfulClient, Contentfully} from "contentfully";

// create the contentful client (we can use this later)
const contentfulClient = new ContentfulClient({

    // credentials for the space
    accessToken: "YOUR_API_KEY",
    spaceId:     "YOUR_SPACE_ID",

    // setup a handler to auto-retry when a rate-limit error occurs
    onRateLimitError: ExponentialBackoffHandler.create()
});

// create a Contentfully instance
const contentfully = new Contentfully(contentfulClient);

Next, we can now query Contentful using Contenfully's getModels() method. The first argument to getModels() is a query object that takes the same query parameters as a direct Content Delivery API call.

async function query() {

    // get the 3rd page of my model objects
    const json = await contentfully.getModels({
        content_type: "myModel",
        skip: 20,
        limit: 10
    });

    // print the result
    console.log(json);
};

query();

Contentfully will execute the query, recursively linking any assets or embedded content models, returning a basic JavaScript object without the Contentful metadata that you don't need.

It should look something like this:

{
    total: 10,
    skip: 20,
    limit: 10,
    items: [
        {
            _id: "5nZHNlP6zCESgGuMGKG2Q8",
            _type: "myModel",
            field1: "value1",
            field2: "value2",
            field3: false,
            field5: {
                _id: "m972ick1jqhi",
                _type: "myModelDependency",
                field1: "foo",
                field2: "bar",
                field3: true,
            },
            field6: [
                1, 4, 10, 20
            ]
        },
        ...
    ]
}

Localization with the wildcard locale parameter

Just as with the Content Delivery API, you can query entries to retrieve all localized versions of an entry by using the 'wildcard' locale=* parameter.

However the response is different from Contentful API. The locales will be lifted to top level objects so each locale can be used holistically. Please refer to the example response below. The default locale from the space will be used to for values not defined in any locale. Fallback locales are implemented for lifted responses following Contentful's "Considerations on fallback locales" documentation. Flattening can be disabled for a query by passing the Query Option flatten=false

async function query() {

    const json = await contentfully.getModels({
        content_type: "myModel",
        skip: 20,
        limit: 10,
        locale: '*'
    });

    // print the result
    console.log(json);
};

query();

Which would return models mapped by locale:

{
    total: 10,
    skip: 20,
    limit: 10,
    items: {
      "en-CA": [
        {
          _id: "5nZHNlP6zCESgGuMGKG2Q8",
          _type: "myModel",
          field1: "Hello",
          field2: "",
          field3: "foo",
          field4: true,
          field5: {
              _id: "m972ick1jqhi",
              _type: "myModelDependency",
              field1: "foo",
              field2: "bar",
              field3: true
          },
          field6: [1, 4, 10, 20],
          image: {
            _id: "m12mkd123fdr4",
            url: "foo.png",
            title: "title",
            dimensions: {
              width: 1,
              height: 1
            },
            size: 44335
          }
        }
      ],
      "fr-CA": [
        {
          _id: "5nZHNlP6zCESgGuMGKG2Q8",
          _type: "myModel",
          field1: "Bonjour",
          field2: "Comment vas-tu",
          field3: "foo",
          field3: false,
          field5: {
              _id: "m972ick1jqhi",
              _type: "myModelDependency",
              field1: "foo",
              field2: "bar",
              field3: true
          },
          field6: [2, 8, 20, 40],
          image: {
            _id: "m12mkd123fdr4",
            url: "bar.jpg",
            title: "french title",
            dimensions: {
              width: 2,
              height: 2
            },
            size: 123124
          }
        }
      ]
    }
}

Enabling experimental features

There are a couple of early-access features that have been included in the v1.x.x builds which you can enable as follows:

// create a Contentfully instance with experimental features enabled
const contentfully = new Contentfully(contentfulClient, {
    experimental: true
});

This gets you access to:

  • Consolidates metadata (i.e. type, revision, createdAt, updatedAt) into a new _metadata property of each entity, with dates translated to native Javascript Date objects.
  • More to come...

IE Support

TL;DR - We don't support IE.

By default, Contentfully uses the native fetch client in the browser, otherwise it will use node-fetch. Since IE does not have fetch native to it, use the fetch option with something like isomorphic-fetch when instantiating ContentfulClient.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].