@mapomodule/core 1.0.0 → 2.0.0-beta.10

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.
Files changed (96) hide show
  1. package/README.md +56 -0
  2. package/dist/index.d.mts +14 -0
  3. package/dist/index.d.ts +6 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.mjs +4 -0
  6. package/dist/module.d.mts +11 -0
  7. package/dist/module.d.ts +10 -0
  8. package/dist/module.d.ts.map +1 -0
  9. package/dist/module.json +9 -0
  10. package/dist/module.mjs +78 -0
  11. package/dist/runtime/__tests__/plugins/00.fetch.real.spec.d.ts +2 -0
  12. package/dist/runtime/__tests__/plugins/00.fetch.real.spec.d.ts.map +1 -0
  13. package/dist/runtime/__tests__/plugins/00.fetch.spec.d.ts +2 -0
  14. package/dist/runtime/__tests__/plugins/00.fetch.spec.d.ts.map +1 -0
  15. package/dist/runtime/__tests__/plugins/01.init.server.spec.d.ts +2 -0
  16. package/dist/runtime/__tests__/plugins/01.init.server.spec.d.ts.map +1 -0
  17. package/dist/runtime/__tests__/utils/fetchError.spec.d.ts +2 -0
  18. package/dist/runtime/__tests__/utils/fetchError.spec.d.ts.map +1 -0
  19. package/dist/runtime/api/crud.d.ts +23 -0
  20. package/dist/runtime/api/crud.d.ts.map +1 -0
  21. package/dist/runtime/api/crud.js +80 -0
  22. package/dist/runtime/api/multipart.d.ts +15 -0
  23. package/dist/runtime/api/multipart.d.ts.map +1 -0
  24. package/dist/runtime/api/multipart.js +31 -0
  25. package/dist/runtime/auth/useCanAccessRoute.d.ts +12 -0
  26. package/dist/runtime/auth/useCanAccessRoute.d.ts.map +1 -0
  27. package/dist/runtime/auth/useCanAccessRoute.js +28 -0
  28. package/dist/runtime/auth/useMapoAuth.d.ts +28 -0
  29. package/dist/runtime/auth/useMapoAuth.d.ts.map +1 -0
  30. package/dist/runtime/auth/useMapoAuth.js +38 -0
  31. package/dist/runtime/composables/useMapo.d.ts +48 -0
  32. package/dist/runtime/composables/useMapo.d.ts.map +1 -0
  33. package/dist/runtime/composables/useMapo.js +15 -0
  34. package/dist/runtime/middleware/auth.d.ts +10 -0
  35. package/dist/runtime/middleware/auth.d.ts.map +1 -0
  36. package/dist/runtime/middleware/auth.js +15 -0
  37. package/dist/runtime/middleware/permissions.d.ts +20 -0
  38. package/dist/runtime/middleware/permissions.d.ts.map +1 -0
  39. package/dist/runtime/middleware/permissions.js +27 -0
  40. package/dist/runtime/middleware/roles.d.ts +12 -0
  41. package/dist/runtime/middleware/roles.d.ts.map +1 -0
  42. package/dist/runtime/middleware/roles.js +13 -0
  43. package/dist/runtime/page-meta.nuxt.d.ts +23 -0
  44. package/dist/runtime/plugins/00.fetch.d.ts +21 -0
  45. package/dist/runtime/plugins/00.fetch.d.ts.map +1 -0
  46. package/dist/runtime/plugins/00.fetch.js +61 -0
  47. package/dist/runtime/plugins/01.init.server.d.ts +15 -0
  48. package/dist/runtime/plugins/01.init.server.d.ts.map +1 -0
  49. package/dist/runtime/plugins/01.init.server.js +35 -0
  50. package/dist/runtime/types.d.ts +75 -0
  51. package/dist/runtime/types.d.ts.map +1 -0
  52. package/dist/runtime/types.js +10 -0
  53. package/dist/runtime/types.nuxt.d.ts +18 -0
  54. package/dist/runtime/utils/fetchError.d.ts +29 -0
  55. package/dist/runtime/utils/fetchError.d.ts.map +1 -0
  56. package/dist/runtime/utils/fetchError.js +12 -0
  57. package/dist/runtime/utils/useMapoFetch.d.ts +30 -0
  58. package/dist/runtime/utils/useMapoFetch.d.ts.map +1 -0
  59. package/dist/runtime/utils/useMapoFetch.js +8 -0
  60. package/dist/types.d.mts +13 -0
  61. package/dist/types.d.ts +13 -0
  62. package/dist/types.d.ts.map +1 -0
  63. package/package.json +58 -15
  64. package/src/index.ts +9 -0
  65. package/src/module.ts +114 -0
  66. package/src/runtime/__tests__/plugins/00.fetch.real.spec.ts +124 -0
  67. package/src/runtime/__tests__/plugins/00.fetch.spec.ts +146 -0
  68. package/src/runtime/__tests__/plugins/01.init.server.spec.ts +94 -0
  69. package/src/runtime/__tests__/utils/fetchError.spec.ts +97 -0
  70. package/src/runtime/api/crud.ts +138 -0
  71. package/src/runtime/api/multipart.ts +78 -0
  72. package/src/runtime/auth/useCanAccessRoute.ts +48 -0
  73. package/src/runtime/auth/useMapoAuth.ts +91 -0
  74. package/src/runtime/composables/useMapo.ts +61 -0
  75. package/src/runtime/middleware/auth.ts +24 -0
  76. package/src/runtime/middleware/permissions.ts +52 -0
  77. package/src/runtime/middleware/roles.ts +23 -0
  78. package/src/runtime/page-meta.nuxt.d.ts +23 -0
  79. package/src/runtime/plugins/00.fetch.ts +92 -0
  80. package/src/runtime/plugins/01.init.server.ts +66 -0
  81. package/src/runtime/types.nuxt.d.ts +18 -0
  82. package/src/runtime/types.ts +102 -0
  83. package/src/runtime/utils/fetchError.ts +50 -0
  84. package/src/runtime/utils/useMapoFetch.ts +41 -0
  85. package/src/types.ts +13 -0
  86. package/CHANGELOG.md +0 -248
  87. package/api/crud.js +0 -141
  88. package/api/multipart.js +0 -41
  89. package/index.js +0 -134
  90. package/interceptor/auth.js +0 -26
  91. package/interceptor/index.js +0 -5
  92. package/middleware/auth.js +0 -10
  93. package/middleware/permissions.js +0 -36
  94. package/middleware/roles.js +0 -10
  95. package/module.js +0 -44
  96. package/templates/options.js +0 -1
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
- */
@@ -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
- }
@@ -1,5 +0,0 @@
1
- const AuthInterceptor = require('./auth')
2
-
3
- module.exports = (ctx) => {
4
- AuthInterceptor(ctx)
5
- }
@@ -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
- };
@@ -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')
@@ -1 +0,0 @@
1
- export default <%= serializeFunction(options) %>;