tanc / gridsome-cockpit-source

A source plugin for Gridsome to get data from Cockpit CMS API
1 stars 0 forks source link

Cockpit CMS source plugin for Gridsome

This package is under development and API may change before v1 is released.

Install

Use one of:

Usage

module.exports = {
  plugins: [
    {
      use: '@tanc00/gridsome-source-cockpit',
      options: {
        accessToken: process.env.ACCESSTOKEN,
        host: process.env.APIHOST
      }
    }
  ]
}

You will need to provide the ACCESSTOKEN and APIHOST environment variables in a .env or similar file at the root of your project. Gridsome will automatically read this and pass in the variables to the source plugin.

For example:

APIHOST=https://my-cockpit-cms.com
ACCESSTOKEN=b90d9080711bab6e9af34fbea754c5

API limit

You can also set a limit per request if you have thousands of collections or assets and want to pull in a paged set of results. To do this set the APILIMIT environment variable, for example:

APILIMIT=100

You will also need to add a config option in your gridsome.config.js file:

module.exports = {
  plugins: [
    {
      use: '@tanc00/gridsome-source-cockpit',
      options: {
        accessToken: process.env.ACCESSTOKEN,
        host: process.env.APIHOST
        apiLimit: process.env.APILIMIT
      }
    }
  ]
}

Routes

You can configure a route per collection type by defining them in gridsome.config.js. The key is the name of the collection and the value is the path you'd like for that collection. For example:

module.exports = {
  plugins: [
    {
      use: '@tanc00/gridsome-source-cockpit',
      options: {
        accessToken: process.env.ACCESSTOKEN,
        host: process.env.APIHOST
        routes: {
          page: '/:slug',
          blogpost: '/blog/:year/:month/:slug',
        }
      }
    }
  ]
}

i18n

Fields in Cockpit can be localized into different languages. This plugin is able to import localized versions of fields. To import each language you need to specify the language code in a languages array in the plugin options, something like this:

module.exports = {
  plugins: [
    {
      use: '@tanc00/gridsome-source-cockpit',
      options: {
        accessToken: process.env.ACCESSTOKEN,
        host: process.env.APIHOST
        languages: [
          'it',
          'de'
        ]
      }
    }
  ]
}

In the example above, this plugin will add fields (if they exist and are localized) to GraphQL. Each localized field will have the two letter code appended, for example, if you have a field called intro which is localized and you have it and de as your languages you will see three fields added to GraphQL called intro, introIt and introDe

At the time of writing there is currently no officially supported method of handling i18n (internationalisation) content within Gridsome so it will be up to you to decide how you want to display and handle your translated fields.

Cockpit API configuration

Cockpit CMS must be configured to allow access to the API using a token. To set this up visit /restadmin/index and configure the Custom keys section. You may need to generate a key and also allow access to specific api paths, for example:

/api/collections/get
/api/cockpit/assets
/api/cockpit/image
/api/collections/collection
/api/collections/listCollections
/api/singletons/get
/api/singletons/listSingletons

Once configured hit Save at the bottom. You can test access by visiting an endpoint in your browser, for example:

https://my-cockpit-cms.com/api/collections/get/page?token=b90d9080711bab6e9af34fbea754c5

If you get an access denied message you'll need to check your configuration.

Cockpit Collection fields

@tanc/gridsome-souce-cockpit currently works with all collection fields with some caveats:

Future enhancements