@mapomodule/core 1.0.0 → 2.0.0-beta.2
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.mts +13 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +3 -0
- package/dist/module.d.mts +10 -0
- package/dist/module.d.ts +10 -0
- package/dist/module.d.ts.map +1 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +73 -0
- package/dist/runtime/__tests__/plugins/00.fetch.spec.d.ts +2 -0
- package/dist/runtime/__tests__/plugins/00.fetch.spec.d.ts.map +1 -0
- package/dist/runtime/api/crud.d.ts +23 -0
- package/dist/runtime/api/crud.d.ts.map +1 -0
- package/dist/runtime/api/crud.js +74 -0
- package/dist/runtime/api/multipart.d.ts +15 -0
- package/dist/runtime/api/multipart.d.ts.map +1 -0
- package/dist/runtime/api/multipart.js +31 -0
- package/dist/runtime/auth/useCanAccessRoute.d.ts +12 -0
- package/dist/runtime/auth/useCanAccessRoute.d.ts.map +1 -0
- package/dist/runtime/auth/useCanAccessRoute.js +28 -0
- package/dist/runtime/auth/useMapoAuth.d.ts +28 -0
- package/dist/runtime/auth/useMapoAuth.d.ts.map +1 -0
- package/dist/runtime/auth/useMapoAuth.js +38 -0
- package/dist/runtime/composables/useMapo.d.ts +48 -0
- package/dist/runtime/composables/useMapo.d.ts.map +1 -0
- package/dist/runtime/composables/useMapo.js +15 -0
- package/dist/runtime/middleware/auth.d.ts +10 -0
- package/dist/runtime/middleware/auth.d.ts.map +1 -0
- package/dist/runtime/middleware/auth.js +15 -0
- package/dist/runtime/middleware/permissions.d.ts +20 -0
- package/dist/runtime/middleware/permissions.d.ts.map +1 -0
- package/dist/runtime/middleware/permissions.js +27 -0
- package/dist/runtime/middleware/roles.d.ts +12 -0
- package/dist/runtime/middleware/roles.d.ts.map +1 -0
- package/dist/runtime/middleware/roles.js +13 -0
- package/dist/runtime/page-meta.nuxt.d.ts +23 -0
- package/dist/runtime/plugins/00.fetch.d.ts +21 -0
- package/dist/runtime/plugins/00.fetch.d.ts.map +1 -0
- package/dist/runtime/plugins/00.fetch.js +57 -0
- package/dist/runtime/plugins/01.init.server.d.ts +14 -0
- package/dist/runtime/plugins/01.init.server.d.ts.map +1 -0
- package/dist/runtime/plugins/01.init.server.js +37 -0
- package/dist/runtime/types.d.ts +62 -0
- package/dist/runtime/types.d.ts.map +1 -0
- package/dist/runtime/types.js +10 -0
- package/dist/runtime/types.nuxt.d.ts +18 -0
- package/dist/runtime/utils/useMapoFetch.d.ts +30 -0
- package/dist/runtime/utils/useMapoFetch.d.ts.map +1 -0
- package/dist/runtime/utils/useMapoFetch.js +8 -0
- package/dist/types.d.mts +13 -0
- package/dist/types.d.ts +13 -0
- package/dist/types.d.ts.map +1 -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 -248
- package/api/crud.js +0 -141
- package/api/multipart.js +0 -41
- package/index.js +0 -134
- package/interceptor/auth.js +0 -26
- package/interceptor/index.js +0 -5
- package/middleware/auth.js +0 -10
- package/middleware/permissions.js +0 -36
- package/middleware/roles.js +0 -10
- package/module.js +0 -44
- package/templates/options.js +0 -1
package/api/crud.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
const transformRequestInMultipart = require('@mapomodule/core/api/multipart')
|
|
2
|
-
const trimslashes = (str) => (str || "").replace(/^\/|\/$/g, '')
|
|
3
|
-
const preparePayload = (payload, options) => {
|
|
4
|
-
const { multipart } = (options || {})
|
|
5
|
-
if (!(payload instanceof FormData) && multipart !== 'disable') {
|
|
6
|
-
payload = transformRequestInMultipart(payload, multipart || 'auto');
|
|
7
|
-
}
|
|
8
|
-
return payload
|
|
9
|
-
}
|
|
10
|
-
const endpointError = "This crud is badly configured. No endpoint was passed"
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* This function generates a fully functional crud helper given the endpoint.
|
|
14
|
-
*
|
|
15
|
-
* @alias $api.crud
|
|
16
|
-
* @param {string} endpoint This is the endpoint that will be used for all the methods exposed by this function.
|
|
17
|
-
* @param {external:AxiosRequestConfig} [higherConf={}] This is an axios configuration object that allows you to inject all the additional options you want.
|
|
18
|
-
*
|
|
19
|
-
*/
|
|
20
|
-
const createRepository = ($axios) => (endpoint, higherConf = {}) => ({
|
|
21
|
-
/**
|
|
22
|
-
* This makes a GET http call to the crud endpoint. Returns as a promise the server response.
|
|
23
|
-
* @alias $api.crud.list
|
|
24
|
-
* @param {external:AxiosRequestConfig} [config]
|
|
25
|
-
* @returns {Promise<external:AxiosResponse>}
|
|
26
|
-
*/
|
|
27
|
-
list(config) {
|
|
28
|
-
const func = $axios.$get(`/${trimslashes(endpoint)}/`, { ...higherConf, ...config })
|
|
29
|
-
return endpoint !== null ? func : Promise.reject(endpointError)
|
|
30
|
-
},
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* This makes a POST http call with the data specified in the payload to the crud endpoint. Returns as a promise the server response.
|
|
34
|
-
* @alias $api.crud.create
|
|
35
|
-
* @param {Object} payload The payload containing the data that we want to send.
|
|
36
|
-
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted.
|
|
37
|
-
* @param {MapoRequestConfig} options Additional option configuration. Here you can configure some mapo reserved options. Like the multipart autodiscover politic.
|
|
38
|
-
* @returns {Promise<external:AxiosResponse>}
|
|
39
|
-
*/
|
|
40
|
-
create(payload, config, options) {
|
|
41
|
-
|
|
42
|
-
const func = $axios.$post(`/${trimslashes(endpoint)}/`, preparePayload(payload, options), { ...higherConf, ...config })
|
|
43
|
-
return endpoint !== null ? func : Promise.reject(endpointError)
|
|
44
|
-
},
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* This makes a GET http call to the crud endpoint slash the given id. Returns as a promise the server response.
|
|
48
|
-
* @alias $api.crud.detail
|
|
49
|
-
* @param {String} id The id of the data we want to get from the server.
|
|
50
|
-
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted.
|
|
51
|
-
* @returns {Promise<external:AxiosResponse>}
|
|
52
|
-
*/
|
|
53
|
-
detail(id, config) {
|
|
54
|
-
const func = $axios.$get(`/${trimslashes(endpoint)}/${id}/`, { ...higherConf, ...config })
|
|
55
|
-
return endpoint !== null ? func : Promise.reject(endpointError)
|
|
56
|
-
},
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* This shortcut calls the update method or the create method depending on the presence of the id field in the payload.
|
|
60
|
-
* @alias $api.crud.updateOrCreate
|
|
61
|
-
* @param {Object} payload The payload containing the data that we want to send.
|
|
62
|
-
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted.
|
|
63
|
-
* @param {MapoRequestConfig} options Additional option configuration. Here you can configure some mapo reserved options. Like the multipart autodiscover politic.
|
|
64
|
-
* @returns {Promise<external:AxiosResponse>}
|
|
65
|
-
*/
|
|
66
|
-
updateOrCreate(payload, config, options) {
|
|
67
|
-
const func = payload.id ? this.update(payload.id, payload, { ...higherConf, ...config }, options) : this.create(payload, { ...higherConf, ...config }, options)
|
|
68
|
-
return endpoint !== null ? func : Promise.reject(endpointError)
|
|
69
|
-
},
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* This makes a PUT http call to the crud endpoint slash the given id, sending the payload. Returns as a promise the server response.
|
|
73
|
-
* @alias $api.crud.update
|
|
74
|
-
* @param {String} id The id of the data we want to update.
|
|
75
|
-
* @param {Object} payload The payload containing the data that we want to send.
|
|
76
|
-
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted.
|
|
77
|
-
* @param {MapoRequestConfig} options Additional option configuration. Here you can configure some mapo reserved options. Like the multipart autodiscover politic.
|
|
78
|
-
* @returns {Promise<external:AxiosResponse>}
|
|
79
|
-
*/
|
|
80
|
-
update(id, payload, config, options) {
|
|
81
|
-
const func = $axios.$put(`/${trimslashes(endpoint)}/${id}/`, preparePayload(payload, options), { ...higherConf, ...config })
|
|
82
|
-
return endpoint !== null ? func : Promise.reject(endpointError)
|
|
83
|
-
},
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* This makes a PATCH http call to the crud endpoint slash the given id, sending the payload. Returns as a promise the server response.
|
|
87
|
-
* @alias $api.crud.partialUpdate
|
|
88
|
-
* @param {String} id The id of the data we want to partially update.
|
|
89
|
-
* @param {Object} payload The payload containing the data that we want to send.
|
|
90
|
-
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted.
|
|
91
|
-
* @param {MapoRequestConfig} options Additional option configuration. Here you can configure some mapo reserved options. Like the multipart autodiscover politic.
|
|
92
|
-
* @returns {Promise<external:AxiosResponse>}
|
|
93
|
-
*/
|
|
94
|
-
partialUpdate(id, payload, config, options) {
|
|
95
|
-
const func = $axios.$patch(`/${trimslashes(endpoint)}/${id}/`, preparePayload(payload, options), { ...higherConf, ...config })
|
|
96
|
-
return endpoint !== null ? func : Promise.reject(endpointError)
|
|
97
|
-
},
|
|
98
|
-
/**
|
|
99
|
-
* This makes a DELETE http call to the crud endpoint slash the given id. Returns as a promise the server response.
|
|
100
|
-
* @alias $api.crud.delete
|
|
101
|
-
* @param {String} id The id of the data we want to delete from the server.
|
|
102
|
-
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted.
|
|
103
|
-
* @returns {Promise<external:AxiosResponse>}
|
|
104
|
-
*/
|
|
105
|
-
delete(id, config) {
|
|
106
|
-
const func = $axios.$delete(`/${trimslashes(endpoint)}/${id}/`, { ...higherConf, ...config })
|
|
107
|
-
return endpoint !== null ? func : Promise.reject(endpointError)
|
|
108
|
-
},
|
|
109
|
-
/**
|
|
110
|
-
* This makes a POST http call to the crud endpoint slash update_order. Returns as a promise the server response.
|
|
111
|
-
* Use this endpoint to change the order of the items in the list.
|
|
112
|
-
* @alias $api.crud.update_order
|
|
113
|
-
* @param {String} startId The id of the item we want to move in the list.
|
|
114
|
-
* @param {String} endId The id of the item in the position where we want to move.
|
|
115
|
-
* @param {external:AxiosRequestConfig} [config] This is a further axios configuration object that allows you to override the options previously setted.
|
|
116
|
-
* @returns {Promise<external:AxiosResponse>}
|
|
117
|
-
*/
|
|
118
|
-
update_order(startId, endId, config) {
|
|
119
|
-
const func = $axios.$post(`/${trimslashes(endpoint)}/update_order/`, { startId, endId }, { ...higherConf, ...config })
|
|
120
|
-
return endpoint !== null ? func : Promise.reject(endpointError)
|
|
121
|
-
},
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
})
|
|
125
|
-
/**
|
|
126
|
-
* @external AxiosResponse
|
|
127
|
-
* @see https://axios-http.com/docs/res_schema
|
|
128
|
-
*/
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* @external AxiosRequestConfig
|
|
132
|
-
* @see https://axios-http.com/docs/req_config
|
|
133
|
-
*/
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Additional option configuration. Here you can configure some mapo reserved options.
|
|
137
|
-
* @typedef {MapoRequestConfig} MapoRequestConfig
|
|
138
|
-
* @property {string} multipart - Set the multipart politic. Accepts `'auto'|'force|'disable'`. If auto is set the request is transformed in multipart if any file is in the payload. If set to force the request is transformed in multipart no matter if files are found. If set to `'disable'` the request is never transformed in multipart.
|
|
139
|
-
*/
|
|
140
|
-
|
|
141
|
-
module.exports = createRepository
|
package/api/multipart.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
const { deepClone, getPointed, setPointed, filesInObject } = require('@mapomodule/utils/helpers/objHelpers')
|
|
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 | String[]} file_attributes The list of dotted path of all the file attributes that need to be attached to the request. If unset autodiscovery is applyed. If set to 'fallback' if no files are found it returns the payload as it is.
|
|
10
|
-
* @returns {external:FormData}
|
|
11
|
-
*/
|
|
12
|
-
function transformRequestInMultipart(payload, file_attributes = "auto") {
|
|
13
|
-
var file_paths = []
|
|
14
|
-
if (["auto", "force"].includes(file_attributes)) {
|
|
15
|
-
file_paths = filesInObject(payload)
|
|
16
|
-
if (file_attributes == "auto" && !file_paths.length){
|
|
17
|
-
return payload
|
|
18
|
-
}
|
|
19
|
-
} else if (Array.isArray(file_attributes)) {
|
|
20
|
-
file_paths = file_attributes
|
|
21
|
-
}
|
|
22
|
-
const data = deepClone(payload);
|
|
23
|
-
const files = file_paths.reduce((acc, path) => {
|
|
24
|
-
const file = getPointed(data, path, undefined)
|
|
25
|
-
setPointed(data, path, undefined);
|
|
26
|
-
return { [path]: file, ...acc }
|
|
27
|
-
}, {})
|
|
28
|
-
const formData = new FormData();
|
|
29
|
-
formData.append('data', JSON.stringify(data));
|
|
30
|
-
for (const key in files) {
|
|
31
|
-
formData.append(key, files[key]);
|
|
32
|
-
}
|
|
33
|
-
return formData;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* @external FormData
|
|
38
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/FormData
|
|
39
|
-
*/
|
|
40
|
-
|
|
41
|
-
module.exports = transformRequestInMultipart
|
package/index.js
DELETED
|
@@ -1,134 +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
|
-
try {
|
|
6
|
-
var options = require('./options').default;
|
|
7
|
-
} catch (error) {
|
|
8
|
-
var options = {};
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function getRouteMiddlewares(route) {
|
|
12
|
-
const meta = Array.isArray(route?.meta) ? route.meta[0] : route.meta
|
|
13
|
-
const { middleware } = meta || {}
|
|
14
|
-
return (typeof middleware === "string" ? [middleware] : middleware) || []
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
export default (ctx, inject) => {
|
|
19
|
-
|
|
20
|
-
initInterceptors(ctx)
|
|
21
|
-
|
|
22
|
-
const mapo = {
|
|
23
|
-
/**
|
|
24
|
-
* From here you can reach some utilities that simplify the interaction with the backend api.
|
|
25
|
-
*/
|
|
26
|
-
$api: {
|
|
27
|
-
crud: createRepository(ctx.$axios),
|
|
28
|
-
multipart: transformRequestInMultipart,
|
|
29
|
-
},
|
|
30
|
-
/**
|
|
31
|
-
* From here you can reach some utilities that simplify the interaction with the user authentication flow.
|
|
32
|
-
*/
|
|
33
|
-
$auth: {
|
|
34
|
-
/**
|
|
35
|
-
* Here you can submit your login credentials to the vuex store.
|
|
36
|
-
*
|
|
37
|
-
* @param { Object } payload { username, password } aka user credentials for the login request.
|
|
38
|
-
*/
|
|
39
|
-
login: payload => ctx.store.dispatch("mapo/user/login", payload),
|
|
40
|
-
/**
|
|
41
|
-
*
|
|
42
|
-
* This simply commits a logout to the vuex store.
|
|
43
|
-
*/
|
|
44
|
-
logout: () => ctx.store.dispatch("mapo/user/logout"),
|
|
45
|
-
/**
|
|
46
|
-
* This refresh user informations present in vuex store and returns them.
|
|
47
|
-
* @returns {UserData}
|
|
48
|
-
*/
|
|
49
|
-
getInfo: () => ctx.store.dispatch("mapo/user/getInfo"),
|
|
50
|
-
user: {
|
|
51
|
-
get isLoggedIn() { return ctx.store.getters['mapo/user/isLoggedIn'] },
|
|
52
|
-
get token() { return ctx.store.getters['mapo/user/token'] },
|
|
53
|
-
get info() { return ctx.store.getters['mapo/user/info'] },
|
|
54
|
-
get username() { return ctx.store.getters['mapo/user/username'] },
|
|
55
|
-
get permissions() { return ctx.store.getters['mapo/user/pagePermission'](ctx.app?.context?.route?.name || "") },
|
|
56
|
-
get role() { return ctx.store.getters['mapo/user/role'] }
|
|
57
|
-
},
|
|
58
|
-
/**
|
|
59
|
-
*
|
|
60
|
-
* This returns the current route middlewares if routemeta is enabled.
|
|
61
|
-
*/
|
|
62
|
-
get routeMiddlewares() {
|
|
63
|
-
return getRouteMiddlewares(ctx.app?.context?.route || {})
|
|
64
|
-
},
|
|
65
|
-
/**
|
|
66
|
-
*
|
|
67
|
-
* This returns the middlewares of a given route if routemeta is enabled.
|
|
68
|
-
* @param { Object } route { meta } the route object with meta info.
|
|
69
|
-
* @returns { Array }
|
|
70
|
-
*/
|
|
71
|
-
getRouteMiddlewares: getRouteMiddlewares
|
|
72
|
-
},
|
|
73
|
-
/**
|
|
74
|
-
* From here you can manage the default popup messaging system of mapo.
|
|
75
|
-
*/
|
|
76
|
-
$snack: {
|
|
77
|
-
/**
|
|
78
|
-
* This send a message to the snack vuex store bringing up the component that carries the message to the user.
|
|
79
|
-
*
|
|
80
|
-
* @param {SnackMessage} payload The payload containing the message.
|
|
81
|
-
* @returns {Promise}
|
|
82
|
-
*/
|
|
83
|
-
open: payload => ctx.store.dispatch("mapo/app/showSnackMessage", payload),
|
|
84
|
-
/**
|
|
85
|
-
* This closes all the opened snack instances.
|
|
86
|
-
*/
|
|
87
|
-
close: () => ctx.store.dispatch("mapo/app/clearSnackMessage"),
|
|
88
|
-
},
|
|
89
|
-
/**
|
|
90
|
-
* From here you can manage the default confirm dialog of mapo.
|
|
91
|
-
*/
|
|
92
|
-
$confirm: {
|
|
93
|
-
/**
|
|
94
|
-
* This open the Confirm dialog component showing to the user a question. Returns a promise with the user response.
|
|
95
|
-
* @function
|
|
96
|
-
* @param {ConfirmDialogOptions} payload The payload containing the information to show to the user in the confirm dialog.
|
|
97
|
-
* @returns {Promise<boolean>} The response from the user interaction.
|
|
98
|
-
*/
|
|
99
|
-
open: null,
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* This forces the dialog to close, giving a positive response.
|
|
103
|
-
* @function
|
|
104
|
-
*/
|
|
105
|
-
accept: null,
|
|
106
|
-
/**
|
|
107
|
-
* This forces the dialog to close, giving a negative response.
|
|
108
|
-
* @function
|
|
109
|
-
*/
|
|
110
|
-
close: null
|
|
111
|
-
},
|
|
112
|
-
$options: options
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
inject('mapo', mapo);
|
|
116
|
-
ctx.$mapo = mapo;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* The payload that need to be sent to the store in order to make the snack appear.
|
|
121
|
-
* @typedef {SnackMessage} SnackMessage
|
|
122
|
-
* @property {string} message - Contains the message to show.
|
|
123
|
-
* @property {string} color - Indicates the color of the close button.
|
|
124
|
-
*/
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* The payload for the open function of the ConfirmDialog.
|
|
128
|
-
* @typedef {ConfirmDialogOptions} ConfirmDialogOptions
|
|
129
|
-
* @property {string} title - The title of the dialog.
|
|
130
|
-
* @property {string} question - The text in the body of the dialog.
|
|
131
|
-
* @property {string} dismissButton - The text in the dismiss Button.
|
|
132
|
-
* @property {string} approveButton - The text in the approve Button.
|
|
133
|
-
* @property {Object} attrs - An object with {@link https://vuetifyjs.com/en/api/v-dialog/|props} you want to inject into the confirm dialog.
|
|
134
|
-
*/
|
package/interceptor/auth.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
module.exports = ({ $axios, store }) => {
|
|
2
|
-
if ($axios && store) {
|
|
3
|
-
$axios.onResponse((response) => {
|
|
4
|
-
if (response.status === 401) {
|
|
5
|
-
store.dispatch('mapo/user/logout');
|
|
6
|
-
}
|
|
7
|
-
if (response.status == 403) {
|
|
8
|
-
store.dispatch("mapo/app/showSnackMessage", { message: "Permission denied..", color: "error" })
|
|
9
|
-
}
|
|
10
|
-
return response
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
$axios.onError((error) => {
|
|
14
|
-
const { status, request } = error.response || {}
|
|
15
|
-
const baseURL = ($axios.defaults.baseURL || "").replace(/https?:\/\/[^\/]+/i, "")
|
|
16
|
-
const logoutPath = `${baseURL.endsWith("/") ? baseURL : baseURL + "/"}${process.env.AUTH_LOGIN_URL || "api/auth/logout"}`
|
|
17
|
-
if (status === 401 && (request || {}).path !== logoutPath) {
|
|
18
|
-
store.dispatch('mapo/user/logout')
|
|
19
|
-
}
|
|
20
|
-
if (status == 403) {
|
|
21
|
-
store.dispatch("mapo/app/showSnackMessage", { message: "Permission denied..", color: "error" })
|
|
22
|
-
}
|
|
23
|
-
return Promise.reject(error)
|
|
24
|
-
})
|
|
25
|
-
}
|
|
26
|
-
}
|
package/interceptor/index.js
DELETED
package/middleware/auth.js
DELETED
|
@@ -1,10 +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, app }) {
|
|
4
|
-
const { router: { options } } = app;
|
|
5
|
-
const isLoggedIn = store.getters['mapo/user/isLoggedIn']
|
|
6
|
-
if (!isLoggedIn) {
|
|
7
|
-
query['redirect'] = route.path
|
|
8
|
-
return redirect(`${options.base || '/'}login`, query)
|
|
9
|
-
}
|
|
10
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import Middleware from "../../middleware"; // little hack to inject middleware in root project :P
|
|
2
|
-
|
|
3
|
-
Middleware.permissions =
|
|
4
|
-
Middleware.permissions ||
|
|
5
|
-
async function ({ route, error, store }) {
|
|
6
|
-
route.meta = Array.isArray(route.meta) ? route.meta[0] : route.meta;
|
|
7
|
-
if (!route.meta.permissions) return;
|
|
8
|
-
const userInfo = store.getters["mapo/user/info"];
|
|
9
|
-
if (userInfo.is_superuser) {
|
|
10
|
-
await store.dispatch("mapo/user/addPagePermission", {
|
|
11
|
-
key: route.name,
|
|
12
|
-
value: ["add", "change", "delete", "view"],
|
|
13
|
-
});
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
const { model } = route.meta.permissions;
|
|
17
|
-
const userPermission = (
|
|
18
|
-
userInfo.all_permissions ||
|
|
19
|
-
userInfo.user_permissions ||
|
|
20
|
-
[]
|
|
21
|
-
)
|
|
22
|
-
.filter((perm) => perm.codename.endsWith(model))
|
|
23
|
-
.map(({ codename }) => codename.replace(`_${model}`, ""));
|
|
24
|
-
if (!userPermission.includes("view")) {
|
|
25
|
-
return error({
|
|
26
|
-
statusCode: 404,
|
|
27
|
-
message: "This page could not be found",
|
|
28
|
-
});
|
|
29
|
-
} else {
|
|
30
|
-
await store.dispatch("mapo/user/addPagePermission", {
|
|
31
|
-
key: route.name,
|
|
32
|
-
value: userPermission,
|
|
33
|
-
});
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
};
|
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,44 +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
|
-
this.addTemplate({
|
|
37
|
-
src: path.resolve(__dirname, './templates/options.js'),
|
|
38
|
-
fileName: path.join(namespace, 'options.js'),
|
|
39
|
-
options: moduleOptions
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
module.exports.meta = require('./package.json')
|
package/templates/options.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default <%= serializeFunction(options) %>;
|