NuxtJS module to easily interact with the 👻 Ghost API
To start using the module you first need to install it using the package manager of your choice.
Installation with yarn
yarn add nuxtjs-ghost
Installation with npm
npm install nuxtjs-ghost
After that you need to register the plugin, that NuxtJS can pick it up. To do this, add nuxtjs-ghost
to your modules
section of nuxt.config.js
.
{
modules: [
'nuxtjs-ghost',
]
}
The plugin needs an API key of your site and its endpoint URL. Optionally you can pass the version of the Ghost API you want to use. Typically the endpoint for your API is your sites hostname.
To set these values you have two options:
- Use the default module options
- Add your options globally to the
nuxt.config.js
- Use environment variables
When registering the module, don't register it as a string, but an array. The syntax should be:
{
modules: [
[
'nuxtjs-ghost',
{
url: 'YOUR_API_ENDPOINT',
key: 'YOUR_API_KEY'
}
]
]
}
To set up a global configuration, add the following object to your export
of nuxt.config.js
:
ghost: {
url: 'YOUR_API_ENDPOINT',
key: 'YOUR_API_KEY'
}
If you don't want sensitive data in your code, or got multiple environments, you can use environment variables to configure this plugin.
GHOST_API_KEY
: Sets the API key.
GHOST_API_URL
: Sets the API endpoint.
The usage is pretty straight forward. This package is just a wrapper for the official JavaScript Content API. Please check out their documentation to learn about filtering or pagination. The filter
parameter of the following methods is an object
representation of the available query filters.
To access the wrapper it is getting exposed to the application context as $ghost
. Use it in your pages created()
, or mounted()
functions using this
.
export default {
async created() {
const posts = await this.$ghost.getPosts()
}
}
Or use it in SSR-mode inside of asyncData()
export default {
async asyncData({ $ghost }) {
const posts = await $ghost.getPosts()
return {
posts
}
}
}
All available methods are documented below:
Parameters:
- (optional)
filter
: Object used for filtering. E.g.:{limit: 2, include: 'tags,authors'}
Returns: An array
of posts
Parameters:
query
: Object giving the identifier of the post. Can beslug
orid
. E.g.:{slug: 'my-post'}
- (optional)
filter
: Object used for filtering. E.g.:{formats: ['html', 'plaintext']}}
Returns: An object
representing a post
Parameters:
- (optional)
filter
: Object used for filtering. E.g.:{include: 'count.posts'}
Returns: An array
of authors
Parameters:
query
: Object giving the identifier of the author. Can beslug
orid
. E.g.:{id: '1234'}
- (optional)
filter
: Object used for filtering. E.g.:{page: 2}
Returns: An object
representing an author
Parameters:
- (optional)
filter
: Object used for filtering. E.g.:{include: 'count.posts'}
Returns: An array
of tags
Parameters:
query
: Object giving the identifier of the tag. Can beslug
orid
. E.g.:{id: '1234'}
- (optional)
filter
: Object used for filtering. E.g.:{include: 'count.posts'}
Returns: An object
representing a tag
Parameters:
- (optional)
filter
: Object used for filtering. E.g.:{limit: 2}
Returns: An array
of pages
Parameters:
query
: Object giving the identifier of the page. Can beslug
orid
. E.g.:{id: '1234'}
- (optional)
filter
: Object used for filtering. E.g.:{fields: ['title']}
Returns: An object
representing a page
Returns: An object
representing your settings
- Clone this repository
- Install dependencies using
yarn install
ornpm install
- Start development server using
npm run dev
Copyright (c) Tobias Dittmann