Skip to content

Commit

Permalink
feat: use runtimeConfig to avoid building when changing Strapi URL
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Jul 16, 2020
1 parent a9b789c commit 4442467
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
14 changes: 9 additions & 5 deletions example/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<template>
<div>
Works!
<h1>@nuxtjs/strapi</h1>
<h2>$strapi.state</h2>
<pre>{{ $strapi.state }}</pre>
<h2>options.url</h2>
<pre>{{ $strapi.$http._defaults.prefixUrl }}</pre>
</div>
</template>

<script>
export default {
<style scoped>
div {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
</script>
</style>
3 changes: 2 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = async function (moduleOptions) {
}, defaults)

this.options.publicRuntimeConfig = this.options.publicRuntimeConfig || {}
this.options.publicRuntimeConfig.strapiUrl = options.url
this.options.publicRuntimeConfig.strapi = this.options.publicRuntimeConfig.strapi || {}
this.options.publicRuntimeConfig.strapi.url = options.url

this.addPlugin({
src: resolve(__dirname, 'plugin.js'),
Expand Down
50 changes: 26 additions & 24 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ class Strapi extends Hookable {
constructor (ctx) {
super()

ctx.$config = ctx.$config || {} // fallback for Nuxt < 2.13
const runtimeConfig = ctx.$config.strapi || {}
this.state = Vue.observable({ user: null })

this.$cookies = ctx.app.$cookies
this.$http = ctx.$http.create({})
this.$http.setBaseURL('<%= options.url %>')
this.$http.setBaseURL(runtimeConfig.url || '<%= options.url %>')
this.$http.onError((err) => {
const { response: { data: { message: msg } } } = err

Expand Down Expand Up @@ -139,32 +141,32 @@ class Strapi extends Hookable {

export default async function (ctx, inject) {
<%= JSON.stringify(options.entities) %>.forEach((entity) => {
Object.defineProperty(Strapi.prototype, `$${entity}`, {
get: function () {
const that = this
return {
find: function (...args) {
return that.find(entity, ...args)
},
findOne: function (...args) {
return that.findOne(entity, ...args)
},
count: function (...args) {
return that.count(entity, ...args)
},
create: function (...args) {
return that.create(entity, ...args)
},
update: function (...args) {
return that.update(entity, ...args)
},
delete: function (...args) {
return that.delete(entity, ...args)
Object.defineProperty(Strapi.prototype, `$${entity}`, {
get () {
const that = this
return {
find (...args) {
return that.find(entity, ...args)
},
findOne (...args) {
return that.findOne(entity, ...args)
},
count (...args) {
return that.count(entity, ...args)
},
create (...args) {
return that.create(entity, ...args)
},
update (...args) {
return that.update(entity, ...args)
},
delete (...args) {
return that.delete(entity, ...args)
}
}
}
}
})
})
})

const strapi = new Strapi(ctx)

Expand Down

0 comments on commit 4442467

Please sign in to comment.