From 6658823ef2d705113139b6f83b16f7e022c3818b Mon Sep 17 00:00:00 2001 From: Stuart Clark Date: Sun, 31 Oct 2021 22:58:00 +0000 Subject: [PATCH] feat(#127): add build mode --- packages/schema/src/nuxtModule.js | 36 ++++++++++++++-------------- packages/schema/src/stores/schema.js | 34 +++++++++++++++++++------- packages/schema/templates/plugin.js | 28 +++++++++------------- 3 files changed, 55 insertions(+), 43 deletions(-) diff --git a/packages/schema/src/nuxtModule.js b/packages/schema/src/nuxtModule.js index b7c2b53a5..654bee99f 100644 --- a/packages/schema/src/nuxtModule.js +++ b/packages/schema/src/nuxtModule.js @@ -35,6 +35,7 @@ const DruxtSchemaNuxtModule = function (moduleOptions = {}) { baseUrl: moduleOptions.baseUrl, ...(this.options || {}).druxt || {}, schema: { + build: true, ...((this.options || {}).druxt || {}).schema || {}, ...moduleOptions, } @@ -47,9 +48,6 @@ const DruxtSchemaNuxtModule = function (moduleOptions = {}) { options }) - // Enable Vuex Store. - this.options.store = true - // Add Vuex plugin. this.addPlugin({ src: resolve(__dirname, '../templates/store.js'), @@ -57,24 +55,26 @@ const DruxtSchemaNuxtModule = function (moduleOptions = {}) { options }) - this.nuxt.hook('builder:prepared', async () => { - // Generate schemas. - const druxtSchema = new DruxtSchema(options.baseUrl, options) - const { schemas } = await druxtSchema.get() + if (options.schema.build) { + this.nuxt.hook('builder:prepared', async () => { + // Generate schemas. + const druxtSchema = new DruxtSchema(options.baseUrl, options) + const { schemas } = await druxtSchema.get() - for (const name in schemas) { - const schema = schemas[name] - if (typeof schema === 'undefined') continue + for (const name in schemas) { + const schema = schemas[name] + if (typeof schema === 'undefined') continue - this.addTemplate({ - src: resolve(__dirname, '../templates/schema.json'), - fileName: `schemas/${name}.json`, - options: { schema } - }) - } + this.addTemplate({ + src: resolve(__dirname, '../templates/schema.json'), + fileName: `schemas/${name}.json`, + options: { schema } + }) + } - consola.success('Druxt schema generated') - }) + consola.success('Druxt schema generated') + }) + } } DruxtSchemaNuxtModule.meta = require('../package.json') diff --git a/packages/schema/src/stores/schema.js b/packages/schema/src/stores/schema.js index e544b59a4..c8ba4876a 100644 --- a/packages/schema/src/stores/schema.js +++ b/packages/schema/src/stores/schema.js @@ -62,13 +62,14 @@ const DruxtSchemaStore = ({ store }) => { * @example @lang js * const schema = await this.$store.dispatch('druxtRouter/get', '/') */ - async get({ state, commit }, resource = {}) { + async get(context, resource = {}) { + const { state, commit } = context resource = { - id: null, - resourceType: null, - entityType: 'node', - bundle: null, - mode: 'default', + id: undefined, + resourceType: undefined, + entityType: undefined, + bundle: undefined, + mode: undefined, schemaType: 'view', ...resource @@ -90,8 +91,25 @@ const DruxtSchemaStore = ({ store }) => { // Only load if we don't have this schema in the store. if (!state.schemas[resource.id]) { - const schema = await this.$druxtSchema.import(resource.id) - commit('addSchema', { id: resource.id, schema }) + let schema = false + // Get schema from filesystem if build mode enabled. + if ((this.$druxt.settings.schema || {}).build || typeof (this.$druxt.settings.schema || {}).build == 'undefined') { + schema = await this.$druxtSchema.import(resource.id) + if (schema) { + commit('addSchema', { id: resource.id, schema }) + } + } + + // Else, get schema from the JSON:API. + if (this.$druxt.settings.schema.build === false || !schema) { + if (!resource.entityType && resource.resourceType) { + const [entityType, bundle] = resource.resourceType.split('--') + resource.entityType = entityType + resource.bundle = bundle + } + const { schema } = await this.$druxtSchema.getSchema(resource) + commit('addSchema', { id: resource.id, schema }) + } } return state.schemas[resource.id] diff --git a/packages/schema/templates/plugin.js b/packages/schema/templates/plugin.js index eb3169fdc..f84a3c230 100644 --- a/packages/schema/templates/plugin.js +++ b/packages/schema/templates/plugin.js @@ -1,21 +1,15 @@ -/** - * Nuxt.js plugin for Druxt.js Schema. - */ -const DruxtSchemaPlugin = { - /** - * Import a generated Druxt.js Schema by ID. - * - * @param {string} id - The Druxt.js Schema ID. - * @returns {Schema} The generated Druxt.js Schema object. - * - * @example @lang js - * const schema = await this.$druxtSchema.import('node--page--default--view') - */ - import: async id => import(`./schemas/${id}.json`).then(m => m.default || m) -} +import { DruxtSchema } from 'druxt-schema' -export default (context, inject) => { - inject('druxtSchema', DruxtSchemaPlugin) +export default ({ $druxt }, inject) => { + const schema = new DruxtSchema($druxt.settings.baseUrl) + schema.import = async id => { + try { + return import(`./schemas/${id}.json`).then(m => m.default || m) + } catch(e) { + return false + } + } + inject('druxtSchema', schema) } /**