Skip to content

Commit 2e08ec6

Browse files
authored
feat: support proxy and add localStorage (#87)
1 parent 4d83fef commit 2e08ec6

13 files changed

+3137
-3391
lines changed

docs/content/en/hooks.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Hooks
3-
description: ''
3+
description: 'Add hooks on Strapi HTTP calls'
44
position: 4
55
category: Guide
66
---

docs/content/en/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Introduction
3-
description: ''
3+
description: 'Nuxt Strapi is a Nuxt module for first class integration with Strapi'
44
position: 1
55
category: ''
66
features:

docs/content/en/options.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: options
3-
description: ''
2+
title: Options
3+
description: 'Discover the options of the Strapi module for Nuxt'
44
position: 6
55
category: API
66
---

docs/content/en/proxy.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
title: Using a proxy
3+
description: 'Use Strapi behind a proxy with Nuxt Proxy module'
4+
position: 7
5+
category: Advanced
6+
fullscreen: true
7+
---
8+
9+
You can use the [@nuxtjs/proxy](https://github.com/nuxt-community/proxy-module) module if you want to proxy your Strapi URL:
10+
11+
```js [nuxt.config.js]
12+
export default {
13+
modules: [
14+
'@nuxtjs/strapi',
15+
'@nuxtjs/proxy'
16+
],
17+
proxy: {
18+
'/api/strapi': {
19+
target: 'http://localhost:1337',
20+
pathRewrite: {
21+
'^/api/strapi': '/'
22+
}
23+
}
24+
},
25+
strapi: {
26+
url: '/api/strapi'
27+
}
28+
}
29+
```

docs/content/en/setup.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Setup
3-
description: ''
3+
description: 'Learn how to setup Strapi in your Nuxt app'
44
position: 2
55
category: Guide
66
---

docs/content/en/strapi.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: $strapi
3-
description: ''
3+
description: 'Use the $strapi inside your Nuxt app'
44
position: 5
55
category: API
66
---

docs/content/en/usage.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
title: Usage
3-
description: ''
3+
description: 'Learn how to use the Strapi module in your Nuxt app'
44
position: 3
55
category: Guide
66
---
77

88
## Authentication
99

10-
To handle authentication in your Nuxt.js app with Strapi, you can:
10+
To handle authentication in your Nuxt app with Strapi, you can:
1111

1212
### Login
1313

docs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"dependencies": {
1313
"@nuxt/content-theme-docs": "^0.8.2",
14-
"nuxt": "^2.14.7"
14+
"nuxt": "^2.14.12"
1515
},
1616
"devDependencies": {
1717
"nuxt-ackee": "^2.0.0"

docs/yarn.lock

+789-837
Large diffs are not rendered by default.

lib/module.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ const defaults = {
77
}
88

99
module.exports = async function (moduleOptions) {
10-
const options = defu({
11-
...this.options.strapi,
12-
...moduleOptions
13-
}, defaults)
10+
const { nuxt } = this
1411

15-
this.options.publicRuntimeConfig = this.options.publicRuntimeConfig || {}
16-
this.options.publicRuntimeConfig.strapi = this.options.publicRuntimeConfig.strapi || {}
17-
this.options.publicRuntimeConfig.strapi.url = options.url
12+
const options = defu(moduleOptions, nuxt.options.strapi, defaults)
13+
14+
nuxt.options.publicRuntimeConfig = nuxt.options.publicRuntimeConfig || {}
15+
nuxt.options.publicRuntimeConfig.strapi = nuxt.options.publicRuntimeConfig.strapi || {}
16+
nuxt.options.publicRuntimeConfig.strapi.url = options.url
1817

1918
this.addPlugin({
2019
src: resolve(__dirname, 'plugin.js'),

lib/plugin.js

+38-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Vue from 'vue'
22
import Hookable from 'hookable'
3-
import isArray from 'lodash/isArray';
4-
import isObject from 'lodash/isObject';
3+
import reqURL from 'requrl'
4+
import { joinURL } from '@nuxt/ufo'
55

66
const TOKEN_KEY = 'strapi_jwt'
77

@@ -15,8 +15,13 @@ class Strapi extends Hookable {
1515

1616
this.$cookies = ctx.app.$cookies
1717
this.$http = ctx.$http.create({})
18-
this.$http.setToken(this.getToken(), 'Bearer')
19-
this.$http.setBaseURL(runtimeConfig.url || '<%= options.url %>')
18+
this.syncToken()
19+
const url = runtimeConfig.url || '<%= options.url %>'
20+
if (process.server && ctx.req && url.startsWith('/')) {
21+
this.$http.setBaseURL(joinURL(reqURL(ctx.req), url))
22+
} else {
23+
this.$http.setBaseURL(url)
24+
}
2025
this.$http.onError((err) => {
2126
if (!err.response) {
2227
this.callHook('error', err)
@@ -26,9 +31,9 @@ class Strapi extends Hookable {
2631
const { response: { data: { message: msg } } } = err
2732

2833
let message
29-
if (isArray(msg)) {
34+
if (Array.isArray(msg)) {
3035
message = msg[0].messages[0].message
31-
} else if (isObject(msg)) {
36+
} else if (typeof msg === 'object' && msg !== null) {
3237
message = msg.message
3338
} else {
3439
message = msg
@@ -86,13 +91,11 @@ class Strapi extends Hookable {
8691
}
8792

8893
async fetchUser () {
89-
const jwt = this.getToken()
94+
const jwt = this.syncToken()
9095
if (!jwt) {
9196
return null
9297
}
9398

94-
this.$http.setToken(jwt, 'Bearer')
95-
9699
try {
97100
const user = await this.findOne('users', 'me')
98101
this.setUser(user)
@@ -145,18 +148,43 @@ class Strapi extends Hookable {
145148
}
146149

147150
getToken () {
148-
return this.$cookies.get(TOKEN_KEY)
151+
let token
152+
if (process.client && typeof window.localStorage !== 'undefined') {
153+
token = window.localStorage.getItem(TOKEN_KEY)
154+
}
155+
if (!token) {
156+
token = this.$cookies.get(TOKEN_KEY)
157+
}
158+
return token
149159
}
150160

151161
setToken (jwt) {
152162
this.$http.setToken(jwt, 'Bearer')
163+
if (process.client && typeof window.localStorage !== 'undefined') {
164+
window.localStorage.setItem(TOKEN_KEY, jwt)
165+
}
153166
this.$cookies.set(TOKEN_KEY, jwt)
154167
}
155168

156169
clearToken () {
157170
this.$http.setToken(false)
171+
if (process.client && typeof window.localStorage !== 'undefined') {
172+
window.localStorage.removeItem(TOKEN_KEY)
173+
}
158174
this.$cookies.remove(TOKEN_KEY)
159175
}
176+
177+
syncToken (jwt) {
178+
if (!jwt) {
179+
jwt = this.getToken()
180+
}
181+
if (jwt) {
182+
this.setToken(jwt)
183+
} else {
184+
this.clearToken()
185+
}
186+
return jwt
187+
}
160188
}
161189

162190
export default async function (ctx, inject) {

package.json

+15-13
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,27 @@
2222
"test": "yarn lint && jest"
2323
},
2424
"dependencies": {
25-
"@nuxt/http": "^0.6.0",
25+
"@nuxt/http": "^0.6.1",
26+
"@nuxt/ufo": "^0.5.2",
2627
"cookie-universal-nuxt": "^2.1.4",
2728
"defu": "^3.2.2",
28-
"hookable": "^4.3.1"
29+
"hookable": "^4.3.1",
30+
"requrl": "^3.0.1"
2931
},
3032
"devDependencies": {
31-
"@babel/core": "latest",
32-
"@babel/preset-env": "latest",
33-
"@commitlint/cli": "latest",
34-
"@commitlint/config-conventional": "latest",
35-
"@nuxtjs/eslint-config": "^4.0.0",
33+
"@babel/core": "^7.12.10",
34+
"@babel/preset-env": "^7.12.11",
35+
"@commitlint/cli": "^11.0.0",
36+
"@commitlint/config-conventional": "^11.0.0",
37+
"@nuxtjs/eslint-config": "^5.0.0",
3638
"@nuxtjs/module-test-utils": "latest",
3739
"babel-eslint": "latest",
38-
"babel-jest": "latest",
39-
"eslint": "latest",
40-
"husky": "latest",
41-
"jest": "latest",
42-
"nuxt": "2.14.7",
43-
"standard-version": "latest"
40+
"babel-jest": "^26.6.3",
41+
"eslint": "^7.15.0",
42+
"husky": "^4.3.6",
43+
"jest": "^26.6.3",
44+
"nuxt": "2.14.12",
45+
"standard-version": "^9.0.0"
4446
},
4547
"publishConfig": {
4648
"access": "public"

0 commit comments

Comments
 (0)