Nuxt 3 module for Kirby's Query Language API

Overview

nuxt-kql

npm version

Kirby KQL module for Nuxt 3.

This module provides a useKql composable, which under the hood uses useFetch. Thus, KQL query fetching in your Nuxt 3 application will behave the same as Nuxt' internal data fetching and also infers its request caching!

Features

ℹ️ For the time being, the module will be available on the server and client. Thus, your username/password pair for the API authentication will be exposed. Please keep that in mind.

Setup

# pnpm
pnpm add -D nuxt-kql

# npm
npm i -D nuxt-kql

Usage

Add nuxt-kql to your Nuxt config:

// `nuxt.config.ts`
import { defineNuxtConfig } from 'nuxt'

export default defineNuxtConfig({
  modules: [
    'nuxt-kql',
  ],
})

Create a .env file in your project and add the following environment variables:

KIRBY_API_URL=https://kirby.example.com/api
KIRBY_API_USERNAME=your-username
KIRBY_API_PASSWORD=your-password

Token-Based Authentication

In combination with the kirby-headless-starter, you can use a bearer token for authentication.

// `nuxt.config.ts`
import { defineNuxtConfig } from 'nuxt'

export default defineNuxtConfig({
  modules: [
    'nuxt-kql',
  ],

  kql: {
    // Enable the token-based authentication
    auth: 'bearer',
    // Needed for the kirby-headless-starter custom KQL endpoint
    endpoint: 'kql',
  },
})

Set the following environment variables in your project's .env file:

KIRBY_API_URL=https://kirby.example.com/api
KIRBY_API_TOKEN=your-token

Data Fetching

Use the globally available useKql composable to fetch queries:

const { data, pending, refresh, error } = await useKql({ query: 'site', select: { title: 'site.title', }, }) ">
<script setup lang="ts">
const { data, pending, refresh, error } = await useKql({
  query: 'site',
  select: {
    title: 'site.title',
  },
})
script>

<template>
  <div>
    <h1>{{ data?.result?.title }}</h1>
    <pre>{{ JSON.stringify(data?.result, undefined, 2) }}</pre>
  </div>
</template>

Options

export interface ModuleOptions {
  /**
   * Kirby API base URL, like `https://kirby.example.com/api`
   * @default 'process.env.KIRBY_API_URL'
   */
  url?: string

  /**
   * Kirby KQL API route path
   * @default 'query'
   */
  endpoint?: string

  /**
   * Authentication method
   * Set to `none` to disable authentication
   * @default 'basic'
   */
  auth?: 'basic' | 'bearer' | 'none'

  /**
   * Token for bearer authentication
   * @default 'process.env.KIRBY_API_TOKEN'
   */
  token?: string

  /**
   * Username/password pair for basic authentication
   * @default { username: process.env.KIRBY_API_USERNAME, password: process.env.KIRBY_API_PASSWORD }
   */
  credentials?: {
    username: string
    password: string
  }
}

Playground

Checkout the playground example.

Development

  1. Clone this repository
  2. Install dependencies using pnpm install
  3. Run pnpm run dev:prepare
  4. Start development server using pnpm run dev

License

MIT License © 2022 Johann Schopplich

You might also like...

Using Cypress with Vite, React, TypeScript, MSW and react-query

Vie + Cypress + MSW + react-query Demo Example of using Cypress with Vite, MSW and react-query. Uses the appReady pattern to signal to Cypress when th

Jul 16, 2022

⚡️ Minimal GraphQL Client + Code Generation for Nuxt

nuxt-graphql-client ⚡️ Minimal GraphQL Client + Code Generation for Nuxt ⚡️ Minimal GraphQL Client + Code Generation for Nuxt Features Zero Configurat

Dec 27, 2022

Easy generation of OpenGraph & Twitter meta-tags in Nuxt 3 📋

nuxt-social-tags Easy generation of OpenGraph & Twitter meta-tags in Nuxt 3 ✨ Release Notes 📖 Read the documentation Features Nuxt3 ready Composables

Dec 17, 2022

Easily connect your Nuxt 3 application with LogSnag 📰

Easily connect your Nuxt 3 application with LogSnag 📰

Nuxt LogSnag 📰 LogSnag integration for Nuxt 3 ✨ Release Notes Features Nuxt 3 ready Easy integration Handy composables TypeScript support Setup Insta

Apr 28, 2022

Nuxt 3 starter with Algolia, Storyblok, and Indexer

Nuxt 3 with Storyblok CMS and Algolia Search (incl. automatic indexing) This is a demo repository for an article in Dev.to. We recommend to look at th

May 24, 2022

End-to-end typesafe APIs with tRPC.io in Nuxt applications.

End-to-end typesafe APIs with tRPC.io in Nuxt applications.

tRPC-Nuxt End-to-end typesafe APIs with tRPC.io in Nuxt applications. The client above is not importing any code from the server, only its type declar

Dec 30, 2022

A modern, zero-dependency uptime monitoring tool & status page based on GitHub Actions & Nuxt Content v2.

A modern, zero-dependency uptime monitoring tool & status page based on GitHub Actions & Nuxt Content v2.

StatusBase Uptime monitoring tool & beautiful status pages Powered by Nuxt Content v2! Free • Open Source • Notification View Demo · Report Bug · Requ

Dec 27, 2022

This repo contains a fully configured nuxt 3 instance supporting TypeScript and several considered as useful libraries, fully configured and ready to use in real world projects!

Nuxt 3 Starter This repo contains a fully configured nuxt 3 instance supporting TypeScript and several considered as useful libraries, fully configure

Dec 27, 2022

Batteries-included, zero-config Ionic integration for Nuxt

Nuxt Ionic Ionic integration for Nuxt ✨ Changelog 📖 Read the documentation ▶️ Online playground Features ⚠️ nuxt-ionic is currently a work in progres

Dec 28, 2022
Comments
  • How to get image file content (e.g. alt text) from blocks?

    How to get image file content (e.g. alt text) from blocks?

    This is a general limitation I've hit with KQL. Wondering if you've figured out any workarounds here.

    Wasn't able to find anything from looking at the source and docs. Your example here shows how to output images from blocks but not how to get their alt text (or any other field content that's stored on the file entry).

    question 
    opened by igregson 4
  • Module improvements

    Module improvements

    Hi @johannschopplich! First congratulations on this module. Looks so promising! I've done a quick review and made some options to improve:

    • Regarding handler, you can register it with method: 'post' and it automatically registers the router to accept POST method only. Also for throwing errors, you can use throw createError({ statusCode, statusMesage, body }) it will automatically fill the response.
    • For naming auto-generated composables, as is documented it would be much better if they start with useKql to avoid name conflicts
    • When validating missing options in the module, does it makes sense to add a return statement disabling module?
    • I find this little bit complicated. Isn't nitro runtime bundled when we add module path to the transpile? Also why you need to force inline it?
    • Is write: true necessary for virtual templates? (Tip: You can debug them using http://localhost:3000/_vfs
    opened by pi0 2
Releases(v0.25.4)
Owner
Johann Schopplich
Web designer/developer. Pharmacist prior to that.
Johann Schopplich
🪅 Nuxt 3 module to connect with any API securely – server proxy & customizable composable names

nuxt-api-party This module provides composables to fetch data from an API of your choice securely. You can customize the composable names! Given json-

Johann Schopplich 65 Dec 26, 2022
Nuxt.js module to use Unleash toggle feature services

nuxt-unleash Nuxt.js module to use Unleash toggle feature services ?? Release Notes Features Use $unleash to access and handle your Unleash feature fl

Juanjo Conejero 15 Dec 3, 2022
🔎 Algolia module for Nuxt

@nuxtjs/algolia Algolia module for Nuxt ✨ Release Notes ?? Read the documentation Features Nuxt 3 ready Easy integration with Algolia Handy composable

Nuxt Community 128 Jan 7, 2023
Nuxt 3 module for Web3.js

nuxt-web3.js Nuxt 3 module for Web3.js. Installation npm install nuxt-web3.js Usage export default defineNuxtConfig({ modules: ['nuxt-web3.js'], })

Robert Soriano 8 Dec 16, 2022
OpenID-Connect(OIDC) integration module for nuxt 3.0.

Nuxt OpenID-Connect OpenID-Connect(OIDC) integration module for nuxt 3.0. Features An Nuxt 3 module. OIDC integration ( implemetation base openid-clie

Aborn Jiang 10 Dec 24, 2022
🔎 Meilisearch module for Nuxt 3

nuxt-meilisearch Meilisearch module for Nuxt Features Nuxt 3 Easy integration with MeilisearchJS lib Support for Vue Algolia Vue 3 InstantSearch compo

Alex Duval 50 Dec 26, 2022
✉️ Nuxt module for first class integration with popular newsletter providers

nuxt-newsletter Newsletter module for Nuxt 3 ✨ Release Notes ?? Read the documentation Features Nuxt 3 ready Easy integration with Mailchimp, Revue, B

Jakub Andrzejewski 39 Jan 5, 2023
🔎 Algolia module for Nuxt

@nuxtjs/algolia Algolia module for Nuxt ✨ Release Notes ?? Read the documentation Features Nuxt 3 ready Easy integration with Algolia Handy composable

Nuxt Community 91 Jul 28, 2022
A Nuxt.js module that injects a server route to serve the GraphQL Playground

@pin-pon/nuxt-graphql-playground A Nuxt.js module that injects a server route to serve the GraphQL Playground Setup Add @pin-pon/nuxt-graphql-playgrou

Pin-Pon 3 Sep 22, 2022
Nuxt-Module, that provides a system to set shopware cache-tags for later use in e.g. a full-page cache

nuxt-shopware-caching Nuxt-Module, that provides a system to set shopware cache-tags for later use in e.g. a full-page cache. This module is meant to

Mothership GmbH 5 Nov 8, 2022