Skip to content

Commit

Permalink
feat(#693): initial druxt-schema update for @nuxt/kit
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed Aug 8, 2024
1 parent 82d8ce9 commit 3da0e91
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 116 deletions.
5 changes: 4 additions & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@
"require": "./dist/druxt-schema.ssr.js",
"import": "./dist/druxt-schema.esm.js"
},
"./components/*": "./dist/components/*"
"./components/*": "./dist/components/*",
"./nuxt": "./nuxt/index.js"
},
"main": "dist/druxt-schema.ssr.js",
"module": "dist/druxt-schema.esm.js",
"files": [
"dist",
"nuxt",
"templates"
],
"dependencies": {
"@nuxt/kit": "^3.12.2",
"consola": "^2.15.3",
"drupal-jsonapi-params": "^2.2.0",
"druxt": "^0.22.0"
Expand Down
24 changes: 0 additions & 24 deletions packages/schema/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { DruxtSchemaNuxtModule } from './nuxtModule'

/**
* The core module functionality.
*
Expand Down Expand Up @@ -44,25 +42,3 @@ export { DruxtSchemaMixin } from './mixins/schema'
* @see {@link /api/packages/schema/store/schema|DruxtSchemaStore}
*/
export { DruxtSchemaStore } from './stores/schema'

/**
* The Nuxt.js module function.
*
* Installs the module functionality in a Nuxt.js frontend.
*
* @type {Function}
* @exports default
* @name DruxtSchemaNuxtModule
* @see {@link ./module|DruxtSchemaNuxtModule}
*
* @example <caption>nuxt.config.js</caption> @lang js
* module.exports = {
* modules: [
* 'druxt-schema'
* ],
* druxt: {
* baseUrl: 'https://example.com'
* }
* }
*/
export default DruxtSchemaNuxtModule
102 changes: 102 additions & 0 deletions packages/schema/src/nuxt/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { addPluginTemplate, addTemplate, defineNuxtModule, useLogger } from '@nuxt/kit'
import { resolve } from 'path'

import { DruxtSchema } from '../schema'

/**
* The Nuxt.js module function.
*
* - Adds the Schema plugin to Nuxt.js.
* - Adds the Schema Vuex store to Nuxt.js.
* - Builds the Schema data via the `builder:prepared` hook.
*
* The module function should not be used directly, but rather installed via yout Nuxt.js configuration file.
*
* Options are set on the root level `druxt` Nuxt.js config object.
*
* @example @lang js
* // `nuxt.config.js`
* module.exports = {
* modules: [
* 'druxt-schema'
* ],
* druxt: {
* baseUrl: 'https://example.com'
* }
* }
*
* @todo Document options.
*
* @param {object} moduleOptions - Nuxt.js module options object.
*/
const DruxtSchemaNuxtModule = defineNuxtModule({
meta: {
name: 'druxt-schema',
},
defaults: {
baseUrl: '',
endpoint: '/jsonapi'
},

async setup(moduleOptions, nuxt) {
const logger = useLogger('druxt-schema')
// Set default options.
const options = {
baseUrl: moduleOptions.baseUrl,
...nuxt.options?.druxt || {},
schema: {
...nuxt.options?.druxt?.schema || {},
...moduleOptions,
}
}

// Add plugin.
addPluginTemplate({
src: resolve(__dirname, '../templates/plugin.js'),
fileName: 'druxt-schema.js',
options
})

// Enable Vuex Store.
nuxt.options.store = true

// Add Vuex plugin.
addPluginTemplate({
src: resolve(__dirname, '../templates/store.js'),
fileName: 'store/druxt-schema.js',
options
})

// Generate schema files.
nuxt.hook('modules:done', async () => {
const druxtSchema = new DruxtSchema(options.baseUrl, {
...options,
// Disable API Proxy, as Proxies aren't available at build.
proxy: { ...options.proxy || {}, api: false },
})
const { schemas } = await druxtSchema.get()

// Throw error if no schema files generated.
if (!Object.entries(schemas).length) {
throw new Error('No Druxt Schema files generated.\n Have you created any content types yet?')
}

for (const name in schemas) {
const schema = schemas[name]
if (typeof schema === 'undefined') continue

addTemplate({
src: resolve(__dirname, '../templates/schema.json'),
fileName: `schemas/${name}.json`,
options: { schema },
// @TODO - Does it need to be written to the file system?
write: true
})
}

logger.success('Druxt schema files generated')
})
}
})

export default DruxtSchemaNuxtModule
91 changes: 0 additions & 91 deletions packages/schema/src/nuxtModule.js

This file was deleted.

0 comments on commit 3da0e91

Please sign in to comment.