├── .gitignore ├── yarn.lock ├── package.json ├── index.js └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nuxt-kirby-kql", 3 | "version": "1.0.0", 4 | "description": "A simple nuxt plugin to help to consume KQL", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/DanielRivers/nuxt-kirby-kql" 9 | }, 10 | "email": "git@danielrivers.com", 11 | "author": "Daniel Rivers", 12 | "license": "MIT" 13 | } 14 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export default function ({ 2 | $config 3 | }, inject) { 4 | const config = $config.kirby 5 | inject('kirby', { 6 | find 7 | }) 8 | 9 | const headers = { 10 | Authorization: 'Basic ' + Buffer.from(config.username + ':' + config.password).toString('base64'), 11 | 'Content-Type': 'application/json' 12 | } 13 | 14 | async function find ({ 15 | request 16 | }) { 17 | try { 18 | return unWrap(await fetch(`${config.url}/api/query`, { 19 | headers, 20 | method: 'POST', 21 | body: JSON.stringify(request), 22 | mode: 'cors' 23 | })) 24 | } catch (error) { 25 | return getErrorResponse(error) 26 | } 27 | } 28 | 29 | async function unWrap (response) { 30 | const jsonResponse = await response.json() 31 | const { 32 | ok, 33 | status, 34 | statusText 35 | } = response 36 | return { 37 | json: jsonResponse.result, 38 | ok, 39 | status, 40 | statusText 41 | } 42 | } 43 | 44 | function getErrorResponse (error) { 45 | return { 46 | ok: false, 47 | status: 500, 48 | statusText: error.message, 49 | json: {} 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Kirby KQL Nuxt plugin 2 | 3 | This plugin allows ease of access to all of your Kirby pages on sites that use the KQL plugin 4 | 5 | ## Installation 6 | 7 | ### npm 8 | 9 | ``` 10 | npm i nuxt-kirby-kql 11 | ``` 12 | 13 | ### yarn 14 | 15 | ``` 16 | yarn add nuxt-kirby-kql 17 | ``` 18 | 19 | ## Configuration 20 | 21 | nuxt.conf 22 | 23 | ```js 24 | publicRuntimeConfig: { 25 | kirby: { 26 | url: process.env.KIRBY_SITE || 'XXX', 27 | username: process.env.KIRBY_USERNAME || 'XXX', 28 | password: process.env.KIRBY_PASSWORD || 'XXX' 29 | } 30 | }, 31 | plugins: [ 32 | './node_modules/nuxt-kirby-kql' 33 | ], 34 | ``` 35 | 36 | ## Usage 37 | 38 | The plugin exposes globally `$kirby` on the components `this` 39 | 40 | To make a KQL request, simply call the `find()` method passing the KQL request. 41 | 42 | Reponse includes 43 | 44 | | Property | Type | Description | 45 | | :--- | :---- | :--- | 46 | | ok | Boolean | If the request was successful or not | 47 | | status | Number | HTTP response code | 48 | | statusText | Strong | HTTP status text | 49 | | json | String | Reponse payload | 50 | 51 | ### Example 52 | 53 | ```js 54 | const { json: page } = await this.$kirby.find({ 55 | "query": "page('photography').children", 56 | "select": { 57 | "url": true, 58 | "title": true, 59 | "text": "page.text.markdown", 60 | "images": { 61 | "query": "page.images", 62 | "select": { 63 | "url": true 64 | } 65 | } 66 | }, 67 | "pagination": { 68 | "limit": 10 69 | } 70 | }) 71 | return { page } 72 | 73 | ``` 74 | 75 | ## Licence 76 | 77 | MIT 78 | 79 | ## Support Me! :) 80 | 81 | I release stuff for free, feel free to use this however you wish, if you like it, I am a coffee addict, help me pay for more coffee 82 | https://www.buymeacoffee.com/danielrivers 83 | 84 | ## Credits 85 | 86 | Thanks to 87 | - @HashandSalt for being first user and tester 88 | - The whole @getkirby team --------------------------------------------------------------------------------