@mapomodule/core 1.0.0-alpha.9 → 2.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +21 -0
- package/dist/module.d.cts +2 -0
- package/dist/module.d.mts +2 -0
- package/dist/module.d.ts +2 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +20 -0
- package/dist/types.d.mts +13 -0
- package/package.json +58 -15
- package/src/index.ts +4 -0
- package/src/module.ts +102 -0
- package/src/runtime/__tests__/plugins/00.fetch.spec.ts +144 -0
- package/src/runtime/api/crud.ts +130 -0
- package/src/runtime/api/multipart.ts +78 -0
- package/src/runtime/auth/useCanAccessRoute.ts +48 -0
- package/src/runtime/auth/useMapoAuth.ts +91 -0
- package/src/runtime/composables/useMapo.ts +61 -0
- package/src/runtime/middleware/auth.ts +24 -0
- package/src/runtime/middleware/permissions.ts +52 -0
- package/src/runtime/middleware/roles.ts +23 -0
- package/src/runtime/page-meta.nuxt.d.ts +23 -0
- package/src/runtime/plugins/00.fetch.ts +85 -0
- package/src/runtime/plugins/01.init.server.ts +62 -0
- package/src/runtime/types.nuxt.d.ts +18 -0
- package/src/runtime/types.ts +88 -0
- package/src/runtime/utils/useMapoFetch.ts +41 -0
- package/src/types.ts +13 -0
- package/CHANGELOG.md +0 -32
- package/LICENSE.md +0 -20
- package/api/crud.js +0 -107
- package/api/multipart.js +0 -45
- package/index.js +0 -124
- package/interceptor/auth.js +0 -26
- package/interceptor/index.js +0 -5
- package/middleware/auth.js +0 -9
- package/middleware/permissions.js +0 -19
- package/middleware/roles.js +0 -10
- package/module.js +0 -40
package/LICENSE.md
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) Lotrek 2021
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
all copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
package/api/crud.js
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
const trimslashes = (str) => (str || "").replace(/^\/|\/$/g, '')
|
|
2
|
-
const endpointError = "This crud is badly configured. No endpoint was passed"
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* This function generates a fully functional crud helper given the endpoint.
|
|
6
|
-
*
|
|
7
|
-
* @alias $api.crud
|
|
8
|
-
* @param {string} endpoint This is the endpoint that will be used for all the methods exposed by this function.
|
|
9
|
-
* @param {external:AxiosRequestConfig} [higherConf={}] This is an axios configuration object that allows you to inject all the additional options you want.
|
|
10
|
-
*
|
|
11
|
-
*/
|
|
12
|
-
const createRepository = ($axios) => (endpoint, higherConf = {}) => ({
|
|
13
|
-
/**
|
|
14
|
-
* This makes a GET http call to the crud endpoint. Returns as a promise the server response.
|
|
15
|
-
* @alias $api.crud.list
|
|
16
|
-
* @param {external:AxiosRequestConfig} [config]
|
|
17
|
-
* @returns {Promise<external:AxiosResponse>}
|
|
18
|
-
*/
|
|
19
|
-
list(config) {
|
|
20
|
-
const func = $axios.$get(`/${trimslashes(endpoint)}/`, { ...higherConf, ...config })
|
|
21
|
-
return endpoint !== null ? func : Promise.reject(endpointError)
|
|
22
|
-
},
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* This makes a POST http call with the data specified in the payload to the crud endpoint. Returns as a promise the server response.
|
|
26
|
-
* @alias $api.crud.create
|
|
27
|
-
* @param {Object} payload The payload containing the data that we want to send.
|
|
28
|
-
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted.
|
|
29
|
-
* @returns {Promise<external:AxiosResponse>}
|
|
30
|
-
*/
|
|
31
|
-
create(payload, config) {
|
|
32
|
-
const func = $axios.$post(`/${trimslashes(endpoint)}/`, payload, { ...higherConf, ...config })
|
|
33
|
-
return endpoint !== null ? func : Promise.reject(endpointError)
|
|
34
|
-
},
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* This makes a GET http call to the crud endpoint slash the given id. Returns as a promise the server response.
|
|
38
|
-
* @alias $api.crud.detail
|
|
39
|
-
* @param {String} id The id of the data we want to get from the server.
|
|
40
|
-
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted.
|
|
41
|
-
* @returns {Promise<external:AxiosResponse>}
|
|
42
|
-
*/
|
|
43
|
-
detail(id, config) {
|
|
44
|
-
const func = $axios.$get(`/${trimslashes(endpoint)}/${id}/`, { ...higherConf, ...config })
|
|
45
|
-
return endpoint !== null ? func : Promise.reject(endpointError)
|
|
46
|
-
},
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* This shortcut calls the update method or the create method depending on the presence of the id field in the payload.
|
|
50
|
-
* @alias $api.crud.updateOrCreate
|
|
51
|
-
* @param {Object} payload The payload containing the data that we want to send.
|
|
52
|
-
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted.
|
|
53
|
-
* @returns {Promise<external:AxiosResponse>}
|
|
54
|
-
*/
|
|
55
|
-
updateOrCreate(payload, config) {
|
|
56
|
-
const func = payload.id ? this.update(payload.id, payload, { ...higherConf, ...config }) : this.create(payload, { ...higherConf, ...config })
|
|
57
|
-
return endpoint !== null ? func : Promise.reject(endpointError)
|
|
58
|
-
},
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* This makes a PUT http call to the crud endpoint slash the given id, sending the payload. Returns as a promise the server response.
|
|
62
|
-
* @alias $api.crud.update
|
|
63
|
-
* @param {String} id The id of the data we want to update.
|
|
64
|
-
* @param {Object} payload The payload containing the data that we want to send.
|
|
65
|
-
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted.
|
|
66
|
-
* @returns {Promise<external:AxiosResponse>}
|
|
67
|
-
*/
|
|
68
|
-
update(id, payload, config) {
|
|
69
|
-
const func = $axios.$put(`/${trimslashes(endpoint)}/${id}/`, payload, { ...higherConf, ...config })
|
|
70
|
-
return endpoint !== null ? func : Promise.reject(endpointError)
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* This makes a PATCH http call to the crud endpoint slash the given id, sending the payload. Returns as a promise the server response.
|
|
75
|
-
* @alias $api.crud.partialUpdate
|
|
76
|
-
* @param {String} id The id of the data we want to partially update.
|
|
77
|
-
* @param {Object} payload The payload containing the data that we want to send.
|
|
78
|
-
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted.
|
|
79
|
-
* @returns {Promise<external:AxiosResponse>}
|
|
80
|
-
*/
|
|
81
|
-
partialUpdate(id, payload, config) {
|
|
82
|
-
const func = $axios.$patch(`/${trimslashes(endpoint)}/${id}/`, payload, { ...higherConf, ...config })
|
|
83
|
-
return endpoint !== null ? func : Promise.reject(endpointError)
|
|
84
|
-
},
|
|
85
|
-
/**
|
|
86
|
-
* This makes a DELETE http call to the crud endpoint slash the given id. Returns as a promise the server response.
|
|
87
|
-
* @alias $api.crud.delete
|
|
88
|
-
* @param {String} id The id of the data we want to delete from the server.
|
|
89
|
-
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted.
|
|
90
|
-
* @returns {Promise<external:AxiosResponse>}
|
|
91
|
-
*/
|
|
92
|
-
delete(id, config) {
|
|
93
|
-
const func = $axios.$delete(`/${trimslashes(endpoint)}/${id}/`, { ...higherConf, ...config })
|
|
94
|
-
return endpoint !== null ? func : Promise.reject(endpointError)
|
|
95
|
-
}
|
|
96
|
-
})
|
|
97
|
-
/**
|
|
98
|
-
* @external AxiosResponse
|
|
99
|
-
* @see https://axios-http.com/docs/res_schema
|
|
100
|
-
*/
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* @external AxiosRequestConfig
|
|
104
|
-
* @see https://axios-http.com/docs/req_config
|
|
105
|
-
*/
|
|
106
|
-
|
|
107
|
-
module.exports = createRepository
|
package/api/multipart.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
const deepClone = require('@mapomodule/utils/helpers/objHelpers').deepClone
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* This lets you transform a js object containing files in a multipart request with all the classic data in a single attribute (data)
|
|
5
|
-
* and all the files each one in a single attribute named with the dotted path of the file.
|
|
6
|
-
*
|
|
7
|
-
* @alias $api.multipart
|
|
8
|
-
* @param {Object} payload The payload of the request that needs to be trasformed.
|
|
9
|
-
* @param {String[]} file_attributes The list of dotted path of all the file attributes that need to be attached to the request.
|
|
10
|
-
* @returns {external:FormData}
|
|
11
|
-
*/
|
|
12
|
-
function transformRequestInMultipart(payload, file_attributes = []) {
|
|
13
|
-
|
|
14
|
-
const cleanObject = (object, file_attributes, base = '', files = {}) => {
|
|
15
|
-
for (const key in object) {
|
|
16
|
-
if (file_attributes.includes(key) && object[key] && typeof object[key] != 'string') {
|
|
17
|
-
files[base + key] = object[key];
|
|
18
|
-
delete object[key];
|
|
19
|
-
}
|
|
20
|
-
if (typeof object[key] == 'object') {
|
|
21
|
-
cleanObject(object[key], file_attributes, base + key + '.', files);
|
|
22
|
-
}
|
|
23
|
-
if (file_attributes.includes(key) && object[key] && typeof object[key] == 'string') {
|
|
24
|
-
delete object[key];
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return { data: object, files: files };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const tmp_obj = deepClone(payload);
|
|
31
|
-
const blob = cleanObject(tmp_obj, file_attributes);
|
|
32
|
-
const formData = new FormData();
|
|
33
|
-
formData.append('data', JSON.stringify(blob.data));
|
|
34
|
-
for (const key in blob.files) {
|
|
35
|
-
formData.append(key, blob.files[key]);
|
|
36
|
-
}
|
|
37
|
-
return formData;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* @external FormData
|
|
42
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/FormData
|
|
43
|
-
*/
|
|
44
|
-
|
|
45
|
-
module.exports = transformRequestInMultipart
|
package/index.js
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import createRepository from '@mapomodule/core/api/crud'
|
|
2
|
-
import transformRequestInMultipart from '@mapomodule/core/api/multipart'
|
|
3
|
-
import initInterceptors from '@mapomodule/core/interceptor'
|
|
4
|
-
|
|
5
|
-
function getRouteMiddlewares(route) {
|
|
6
|
-
const meta = Array.isArray(route?.meta) ? route.meta[0] : route.meta
|
|
7
|
-
const { middleware } = meta || {}
|
|
8
|
-
return (typeof middleware === "string" ? [middleware] : middleware) || []
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export default (ctx, inject) => {
|
|
13
|
-
|
|
14
|
-
initInterceptors(ctx)
|
|
15
|
-
|
|
16
|
-
inject('mapo', {
|
|
17
|
-
/**
|
|
18
|
-
* From here you can reach some utilities that simplify the interaction with the backend api.
|
|
19
|
-
*/
|
|
20
|
-
$api: {
|
|
21
|
-
crud: createRepository(ctx.$axios),
|
|
22
|
-
multipart: transformRequestInMultipart
|
|
23
|
-
},
|
|
24
|
-
/**
|
|
25
|
-
* From here you can reach some utilities that simplify the interaction with the user authentication flow.
|
|
26
|
-
*/
|
|
27
|
-
$auth: {
|
|
28
|
-
/**
|
|
29
|
-
* Here you can submit your login credentials to the vuex store.
|
|
30
|
-
*
|
|
31
|
-
* @param { Object } payload { username, password } aka user credentials for the login request.
|
|
32
|
-
*/
|
|
33
|
-
login: payload => ctx.store.dispatch("mapo/user/login", payload),
|
|
34
|
-
/**
|
|
35
|
-
*
|
|
36
|
-
* This simply commits a logout to the vuex store.
|
|
37
|
-
*/
|
|
38
|
-
logout: () => ctx.store.dispatch("mapo/user/login"),
|
|
39
|
-
/**
|
|
40
|
-
* This refresh user informations present in vuex store and returns them.
|
|
41
|
-
* @returns {UserData}
|
|
42
|
-
*/
|
|
43
|
-
getInfo: () => ctx.store.dispatch("mapo/user/getInfo"),
|
|
44
|
-
user: {
|
|
45
|
-
get isLoggedIn() { return ctx.store.getters['mapo/user/isLoggedIn'] },
|
|
46
|
-
get token() { return ctx.store.getters['mapo/user/token'] },
|
|
47
|
-
get info() { return ctx.store.getters['mapo/user/info'] },
|
|
48
|
-
get username() { return ctx.store.getters['mapo/user/username'] },
|
|
49
|
-
get permissions() { return ctx.store.getters['mapo/user/pagePermission'](ctx.app?.context?.route?.name || "") },
|
|
50
|
-
get role() { return ctx.store.getters['mapo/user/role'] }
|
|
51
|
-
},
|
|
52
|
-
/**
|
|
53
|
-
*
|
|
54
|
-
* This returns the current route middlewares if routemeta is enabled.
|
|
55
|
-
*/
|
|
56
|
-
get routeMiddlewares() {
|
|
57
|
-
return getRouteMiddlewares(ctx.app?.context?.route || {})
|
|
58
|
-
},
|
|
59
|
-
/**
|
|
60
|
-
*
|
|
61
|
-
* This returns the middlewares of a given route if routemeta is enabled.
|
|
62
|
-
* @param { Object } route { meta } the route object with meta info.
|
|
63
|
-
* @returns { Array }
|
|
64
|
-
*/
|
|
65
|
-
getRouteMiddlewares: getRouteMiddlewares
|
|
66
|
-
},
|
|
67
|
-
/**
|
|
68
|
-
* From here you can manage the default popup messaging system of mapo.
|
|
69
|
-
*/
|
|
70
|
-
$snack: {
|
|
71
|
-
/**
|
|
72
|
-
* This send a message to the snack vuex store bringing up the component that carries the message to the user.
|
|
73
|
-
*
|
|
74
|
-
* @param {SnackMessage} payload The payload containing the message.
|
|
75
|
-
* @returns {Promise}
|
|
76
|
-
*/
|
|
77
|
-
open: payload => ctx.store.dispatch("mapo/app/showSnackMessage", payload),
|
|
78
|
-
/**
|
|
79
|
-
* This closes all the opened snack instances.
|
|
80
|
-
*/
|
|
81
|
-
close: () => ctx.store.dispatch("mapo/app/clearSnackMessage"),
|
|
82
|
-
},
|
|
83
|
-
/**
|
|
84
|
-
* From here you can manage the default confirm dialog of mapo.
|
|
85
|
-
*/
|
|
86
|
-
$confirm: {
|
|
87
|
-
/**
|
|
88
|
-
* This open the Confirm dialog component showing to the user a question. Returns a promise with the user response.
|
|
89
|
-
* @function
|
|
90
|
-
* @param {ConfirmDialogOptions} payload The payload containing the information to show to the user in the confirm dialog.
|
|
91
|
-
* @returns {Promise<boolean>} The response from the user interaction.
|
|
92
|
-
*/
|
|
93
|
-
open: null,
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* This forces the dialog to close, giving a positive response.
|
|
97
|
-
* @function
|
|
98
|
-
*/
|
|
99
|
-
accept: null,
|
|
100
|
-
/**
|
|
101
|
-
* This forces the dialog to close, giving a negative response.
|
|
102
|
-
* @function
|
|
103
|
-
*/
|
|
104
|
-
close: null
|
|
105
|
-
}
|
|
106
|
-
})
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* The payload that need to be sent to the store in order to make the snack appear.
|
|
111
|
-
* @typedef {SnackMessage} SnackMessage
|
|
112
|
-
* @property {string} message - Contains the message to show.
|
|
113
|
-
* @property {string} color - Indicates the color of the close button.
|
|
114
|
-
*/
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* The payload for the open function of the ConfirmDialog.
|
|
118
|
-
* @typedef {ConfirmDialogOptions} ConfirmDialogOptions
|
|
119
|
-
* @property {string} title - The title of the dialog.
|
|
120
|
-
* @property {string} question - The text in the body of the dialog.
|
|
121
|
-
* @property {string} dismissButton - The text in the dismiss Button.
|
|
122
|
-
* @property {string} approveButton - The text in the approve Button.
|
|
123
|
-
* @property {Object} attrs - An object with {@link https://vuetifyjs.com/en/api/v-dialog/|props} you want to inject into the confirm dialog.
|
|
124
|
-
*/
|
package/interceptor/auth.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
module.exports = ({ $axios, store }) => {
|
|
2
|
-
if ($axios && store){
|
|
3
|
-
$axios.onRequest((config) => {
|
|
4
|
-
const token = store.getters['mapo/user/token']
|
|
5
|
-
if (token && token !== 'undefined') {
|
|
6
|
-
config.headers['Authorization'] = `Token ${token}`
|
|
7
|
-
}
|
|
8
|
-
return config
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
$axios.onResponse((response) => {
|
|
12
|
-
if (response.status === 401) {
|
|
13
|
-
store.dispatch('mapo/user/logout');
|
|
14
|
-
}
|
|
15
|
-
return response
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
$axios.onError((error) => {
|
|
19
|
-
const { status } = error.response || {}
|
|
20
|
-
if (status === 401) {
|
|
21
|
-
store.dispatch('mapo/user/logout');
|
|
22
|
-
}
|
|
23
|
-
return Promise.reject(error)
|
|
24
|
-
})
|
|
25
|
-
}
|
|
26
|
-
}
|
package/interceptor/index.js
DELETED
package/middleware/auth.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import Middleware from '../../middleware' // little hack to inject middleware in root project :P
|
|
2
|
-
|
|
3
|
-
Middleware.auth = Middleware.auth || function ({ route, redirect, query, store }) {
|
|
4
|
-
const hasToken = store.getters['mapo/user/token']
|
|
5
|
-
if (!hasToken) {
|
|
6
|
-
query['redirect'] = route.path
|
|
7
|
-
return redirect('/login', query)
|
|
8
|
-
}
|
|
9
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import Middleware from '../../middleware' // little hack to inject middleware in root project :P
|
|
2
|
-
|
|
3
|
-
Middleware.permissions = Middleware.permissions || async function ({ route, error, store }) {
|
|
4
|
-
route.meta = Array.isArray(route.meta) ? route.meta[0] : route.meta
|
|
5
|
-
if (!route.meta.permissions) return
|
|
6
|
-
const userInfo = store.getters['mapo/user/info']
|
|
7
|
-
if (userInfo.is_superuser) {
|
|
8
|
-
await store.dispatch('mapo/user/addPagePermission', {key: route.name, value: ["add", "change", "delete", "view"]})
|
|
9
|
-
return
|
|
10
|
-
}
|
|
11
|
-
const { model } = route.meta.permissions
|
|
12
|
-
const userPermission = userInfo.user_permissions.filter(perm => perm.codename.endsWith(model)).map(({ codename }) => codename.replace(`_${model}`, ''))
|
|
13
|
-
if (!userPermission.includes('view')) {
|
|
14
|
-
return error({ statusCode: 404, message: 'This page could not be found' })
|
|
15
|
-
} else {
|
|
16
|
-
await store.dispatch('mapo/user/addPagePermission', {key: route.name, value: userPermission})
|
|
17
|
-
return
|
|
18
|
-
}
|
|
19
|
-
}
|
package/middleware/roles.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import Middleware from '../../middleware' // little hack to inject middleware in root project :P
|
|
2
|
-
|
|
3
|
-
Middleware.roles = Middleware.roles || async function ({ route, error, store }) {
|
|
4
|
-
route.meta = (Array.isArray(route.meta) ? route.meta[0] : route.meta) || {}
|
|
5
|
-
return (
|
|
6
|
-
route.meta.roles && !store.getters['mapo/user/isSuperUser'] &&
|
|
7
|
-
!route.meta.roles.includes(store.getters['mapo/user/role']) &&
|
|
8
|
-
error({ statusCode: 404, message: 'This page could not be found' })
|
|
9
|
-
)
|
|
10
|
-
}
|
package/module.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
|
-
import Glob from 'glob'
|
|
3
|
-
import pify from 'pify'
|
|
4
|
-
const glob = pify(Glob)
|
|
5
|
-
|
|
6
|
-
class Resolver {
|
|
7
|
-
resolveFiles = async (cwd, dir) => await glob(`${dir}/**/*.{vue,js}`, { cwd })
|
|
8
|
-
resolveRelative = async (cwd, dir) => {
|
|
9
|
-
const dirPrefix = new RegExp(`^${dir}/`)
|
|
10
|
-
return (await this.resolveFiles(cwd, dir)).map(file => ({ src: file.replace(dirPrefix, '') }))
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export default async function (moduleOptions) {
|
|
15
|
-
|
|
16
|
-
const resolver = new Resolver()
|
|
17
|
-
|
|
18
|
-
const namespace = 'mapo'
|
|
19
|
-
|
|
20
|
-
// INJECT MAPO PLUGIN
|
|
21
|
-
this.addPlugin({
|
|
22
|
-
src: path.resolve(__dirname, './index.js'),
|
|
23
|
-
fileName: path.join(namespace, 'mapo.js')
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
// INJECT ALL MIDDLEWARES
|
|
27
|
-
const middlewares = (await resolver.resolveRelative(__dirname, 'middleware')).map(({ src }) => src)
|
|
28
|
-
|
|
29
|
-
for (const middleware of middlewares) {
|
|
30
|
-
this.addPlugin({
|
|
31
|
-
src: path.resolve(__dirname, './middleware', middleware),
|
|
32
|
-
fileName: path.join(namespace, 'middleware', middleware)
|
|
33
|
-
})
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
module.exports.meta = require('./package.json')
|