Skip to content

Commit 12a0b97

Browse files
committed
feat(lib): update
1 parent f7fd613 commit 12a0b97

File tree

5 files changed

+276
-81
lines changed

5 files changed

+276
-81
lines changed

README.md

+29-17
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,49 @@
1-
# strapi-module
1+
# @nuxtjs/strapi
22

33
[![npm version][npm-version-src]][npm-version-href]
44
[![npm downloads][npm-downloads-src]][npm-downloads-href]
55
[![Github Actions CI][github-actions-ci-src]][github-actions-ci-href]
66
[![Codecov][codecov-src]][codecov-href]
77
[![License][license-src]][license-href]
88

9-
>
9+
>
1010
1111
[📖 **Release Notes**](./CHANGELOG.md)
1212

1313
## Setup
1414

15-
1. Add `strapi-module` dependency to your project
15+
1. Add `@nuxtjs/strapi` dependency to your project
1616

1717
```bash
18-
yarn add strapi-module # or npm install strapi-module
18+
yarn add @nuxtjs/strapi # or npm install @nuxtjs/strapi
1919
```
2020

21-
2. Add `strapi-module` to the `modules` section of `nuxt.config.js`
21+
2. Add `@nuxtjs/strapi` to the `modules` section of `nuxt.config.js`
2222

2323
```js
2424
{
2525
modules: [
2626
// Simple usage
27-
'strapi-module',
28-
29-
// With options
30-
['strapi-module', { /* module options */ }]
27+
'@nuxtjs/strapi'
3128
]
3229
}
3330
```
3431

32+
## Options
33+
34+
You can define the options in the `strapi` property of your `nuxt.config.js`:
35+
36+
```js
37+
export default {
38+
strapi: {
39+
url: 'http://localhost:1337'
40+
}
41+
}
42+
```
43+
44+
You can also give the Strapi URL with `STRAPI_URL` environement.
45+
46+
3547
## Development
3648

3749
1. Clone this repository
@@ -45,17 +57,17 @@ yarn add strapi-module # or npm install strapi-module
4557
Copyright (c) Benjamin Canac <[email protected]>
4658

4759
<!-- Badges -->
48-
[npm-version-src]: https://img.shields.io/npm/v/strapi-module/latest.svg
49-
[npm-version-href]: https://npmjs.com/package/strapi-module
60+
[npm-version-src]: https://img.shields.io/npm/v/@nuxtjs/strapi/latest.svg
61+
[npm-version-href]: https://npmjs.com/package/@nuxtjs/strapi
5062

51-
[npm-downloads-src]: https://img.shields.io/npm/dt/strapi-module.svg
52-
[npm-downloads-href]: https://npmjs.com/package/strapi-module
63+
[npm-downloads-src]: https://img.shields.io/npm/dt/@nuxtjs/strapi.svg
64+
[npm-downloads-href]: https://npmjs.com/package/@nuxtjs/strapi
5365

54-
[github-actions-ci-src]: https://github.com//workflows/ci/badge.svg
55-
[github-actions-ci-href]: https://github.com//actions?query=workflow%3Aci
66+
[github-actions-ci-src]: https://github.com/nuxt-company/strapi-module/workflows/ci/badge.svg
67+
[github-actions-ci-href]: https://github.com/nuxt-company/strapi-module/actions?query=workflow%3Aci
5668

5769
[codecov-src]: https://img.shields.io/codecov/c/github/.svg
5870
[codecov-href]: https://codecov.io/gh/
5971

60-
[license-src]: https://img.shields.io/npm/l/strapi-module.svg
61-
[license-href]: https://npmjs.com/package/strapi-module
72+
[license-src]: https://img.shields.io/npm/l/@nuxtjs/strapi.svg
73+
[license-href]: https://npmjs.com/package/@nuxtjs/strapi

lib/module.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
const { resolve } = require('path')
2+
const defu = require('defu')
3+
4+
const defaults = {
5+
url: process.env.STRAPI_URL || 'http://localhost:1337'
6+
}
27

38
module.exports = async function (moduleOptions) {
4-
const options = {
5-
...this.options['strapi-module'],
9+
const options = defu({
10+
...this.options.strapi,
611
...moduleOptions
7-
}
12+
}, defaults)
13+
14+
this.options.publicRuntimeConfig = this.options.publicRuntimeConfig || {}
15+
this.options.publicRuntimeConfig.strapiUrl = options.url
816

917
this.addPlugin({
1018
src: resolve(__dirname, 'plugin.js'),
11-
fileName: 'strapi-module.js',
19+
fileName: 'strapi.js',
1220
options
1321
})
22+
23+
await this.requireModule('@nuxt/http')
24+
await this.requireModule('cookie-universal-nuxt')
1425
}
1526

1627
module.exports.meta = require('../package.json')

lib/plugin.js

+100-57
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,148 @@
1+
import Vue from 'vue'
2+
import Hookable from 'hookable'
13
import { isArray, isObject } from 'lodash'
24

3-
class Strapi {
4-
constructor ($axios, $cookies) {
5-
this.$axios = $axios
6-
this.$cookies = $cookies
5+
const TOKEN_KEY = 'strapi_jwt'
6+
7+
class Strapi extends Hookable {
8+
constructor (ctx) {
9+
super()
10+
11+
this.state = Vue.observable({ user: null })
12+
13+
this.$cookies = ctx.app.$cookies
14+
this.$http = ctx.$http.create({})
15+
this.$http.setBaseURL('<%= options.url %>')
16+
this.$http.onError(async (err) => {
17+
const { status } = err.response
18+
const { message: msg } = await err.response.json()
19+
20+
let message
21+
if (isArray(msg)) {
22+
message = msg[0].messages[0].message
23+
} else if (isObject(msg)) {
24+
message = msg.message
25+
} else {
26+
message = msg
27+
}
28+
29+
err.message = message
30+
err.statusCode = status
31+
this.callHook('error', err)
32+
})
733
}
834

9-
async request (method, url, config) {
10-
const { data } = await this.$axios({
11-
method,
12-
url,
13-
...config
14-
})
35+
get user () {
36+
return this.state.user
37+
}
1538

16-
return data
39+
set user (user) {
40+
Vue.set(this.state, 'user', user)
41+
}
42+
43+
async register (data) {
44+
this.clearToken()
45+
const { user, jwt } = await this.$http.$post('/auth/local/register', data)
46+
this.setToken(jwt)
47+
this.setUser(user)
48+
return { user, jwt }
1749
}
1850

19-
async register (email, password) {
51+
async login (data) {
2052
this.clearToken()
21-
const authentication = await this.$axios.$post('/auth/local/register', { email, password })
22-
this.setToken(authentication.jwt)
23-
return authentication
53+
const { user, jwt } = await this.$http.$post('/auth/local', data)
54+
this.setToken(jwt)
55+
this.setUser(user)
56+
return { user, jwt }
2457
}
2558

26-
async login (email, password) {
59+
forgotPassword (data) {
2760
this.clearToken()
28-
const authentication = await this.$axios.$post('/auth/local', { email, password })
29-
this.setToken(authentication.jwt)
30-
return authentication
61+
return this.$http.$post('/auth/forgot-password', data)
3162
}
3263

33-
forgotPassword (email, url) {
64+
async resetPassword (data) {
3465
this.clearToken()
35-
return this.$axios.$post('/auth/forgot-password', { email, url })
66+
const { user, jwt } = await this.$http.$post('/auth/reset-password', data)
67+
this.setToken(jwt)
68+
this.setUser(user)
69+
return { user, jwt }
3670
}
3771

38-
async resetPassword (code, password, passwordConfirmation) {
72+
logout () {
73+
this.setUser(null)
3974
this.clearToken()
40-
const authentication = await this.$axios.$post('/auth/reset-password', { code, password, passwordConfirmation })
41-
this.setToken(authentication.jwt)
42-
return authentication
75+
}
76+
77+
async fetchUser () {
78+
const jwt = this.getToken()
79+
if (!jwt) {
80+
return null
81+
}
82+
83+
this.$http.setToken(jwt, 'Bearer')
84+
85+
try {
86+
const user = await this.getEntry('users', 'me')
87+
this.setUser(user)
88+
} catch (e) {
89+
this.clearToken()
90+
}
91+
92+
return this.user
93+
}
94+
95+
setUser (user) {
96+
this.user = user
4397
}
4498

4599
getEntries (contentTypePluralized, params) {
46-
return this.$axios.$get(`/${contentTypePluralized}`, { params })
100+
return this.$http.$get(`/${contentTypePluralized}`, { params })
47101
}
48102

49103
getEntryCount (contentType, params) {
50-
return this.$axios.$get(`/${contentType}/count`, { params })
104+
return this.$http.$get(`/${contentType}/count`, { params })
51105
}
52106

53107
getEntry (contentTypePluralized, id) {
54-
return this.$axios.$get(`/${contentTypePluralized}/${id}`)
108+
return this.$http.$get(`/${contentTypePluralized}/${id}`)
55109
}
56110

57111
createEntry (contentTypePluralized, data) {
58-
return this.$axios.$post(`/${contentTypePluralized}`, data)
112+
return this.$http.$post(`/${contentTypePluralized}`, data)
59113
}
60114

61115
updateEntry (contentTypePluralized, id, data) {
62-
return this.$axios.$put(`/${contentTypePluralized}/${id}`, data)
116+
return this.$http.$put(`/${contentTypePluralized}/${id}`, data)
63117
}
64118

65119
deleteEntry (contentTypePluralized, id) {
66-
return this.$axios.$delete(`/${contentTypePluralized}/${id}`)
120+
return this.$http.$delete(`/${contentTypePluralized}/${id}`)
67121
}
68122

69-
setToken (jwt, comesFromStorage) {
70-
this.$axios.setToken(jwt, 'Bearer')
71-
if (!comesFromStorage) {
72-
this.$cookies.set('jwt', jwt)
73-
}
123+
getToken () {
124+
return this.$cookies.get(TOKEN_KEY)
125+
}
126+
127+
setToken (jwt) {
128+
this.$http.setToken(jwt, 'Bearer')
129+
this.$cookies.set(TOKEN_KEY, jwt)
74130
}
75131

76132
clearToken () {
77-
this.$axios.setToken(false)
78-
this.$cookies.remove('jwt')
133+
this.$http.setToken(false)
134+
this.$cookies.remove(TOKEN_KEY)
79135
}
80136
}
81137

82-
export default function ({ $axios, app }, inject) {
83-
const strapi = new Strapi($axios, app.$cookies)
138+
export default async function (ctx, inject) {
139+
const strapi = new Strapi(ctx)
84140

85-
$axios.onError((error) => {
86-
const { response: { data: { message } } } = error
141+
// Check if jwt to get user
142+
await strapi.fetchUser()
87143

88-
let description
89-
if (isArray(message)) {
90-
description = message[0].messages[0].message
91-
} else if (isObject(message)) {
92-
description = message.message
93-
} else {
94-
description = message
95-
}
96-
97-
if (app.$toast) {
98-
app.$toast.error(description)
99-
} else {
100-
throw new Error(description)
101-
}
102-
})
144+
// TODO: Fetch only server-side universal
103145

104146
inject('strapi', strapi)
147+
ctx.$strapi = strapi
105148
}

package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "strapi-module",
2+
"name": "@nuxtjs/strapi",
33
"version": "0.0.0",
44
"description": "",
55
"repository": "",
@@ -19,7 +19,12 @@
1919
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
2020
"test": "yarn lint && jest"
2121
},
22-
"dependencies": {},
22+
"dependencies": {
23+
"@nuxt/http": "^0.5.3",
24+
"cookie-universal-nuxt": "^2.1.4",
25+
"defu": "^2.0.4",
26+
"hookable": "^4.1.1"
27+
},
2328
"devDependencies": {
2429
"@babel/core": "latest",
2530
"@babel/preset-env": "latest",

0 commit comments

Comments
 (0)