@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
@@ -0,0 +1,138 @@
1
+ import { normalizeEndpoint } from "@mapomodule/utils";
2
+ import { applyMultipartPolicy } from "./multipart";
3
+ import { useMapoFetch } from "../utils/useMapoFetch";
4
+ import { MultipartPolicyEnum } from "../types";
5
+ import type {
6
+ PaginatedResponse,
7
+ ResourceMetadata,
8
+ CrudConfig,
9
+ CrudOptions,
10
+ CrudRepository,
11
+ } from "../types";
12
+
13
+ /**
14
+ * Creates a typed CRUD repository bound to a resource endpoint.
15
+ *
16
+ * The returned repository exposes common operations (`list`, `detail`, `create`,
17
+ * `update`, `partialUpdate`, `delete`) plus utility actions (`updateOrCreate`,
18
+ * `updateOrder`). Requests are executed through the shared Mapo fetch client.
19
+ *
20
+ * Configuration precedence:
21
+ * - `higherConf` defines repository-level default request options.
22
+ * - Per-call config arguments override repository-level defaults.
23
+ *
24
+ * Multipart handling:
25
+ * - Write methods pass payloads through `applyMultipartPolicy`, using
26
+ * `MultipartPolicyEnum.Auto` unless overridden in `CrudOptions`.
27
+ *
28
+ * @typeParam T Entity type handled by the repository.
29
+ * @param endpoint Resource endpoint (normalized to a trailing-slash base path).
30
+ * @param higherConf Optional repository-level fetch configuration defaults.
31
+ * @returns A typed `CrudRepository<T>` for the specified endpoint.
32
+ */
33
+ export function useCrud<T>(
34
+ endpoint: string,
35
+ higherConf?: CrudConfig,
36
+ ): CrudRepository<T> {
37
+ const base = normalizeEndpoint(endpoint);
38
+
39
+ /**
40
+ * Merges higher-level CRUD config with per-call config.
41
+ *
42
+ * Per-call options take precedence over defaults provided at repository
43
+ * creation time.
44
+ */
45
+ function merged(local?: CrudConfig): Record<string, unknown> {
46
+ return { ...higherConf, ...local };
47
+ }
48
+
49
+ /**
50
+ * Executes a typed request through the shared Mapo fetch client.
51
+ *
52
+ * @param path Target URL path.
53
+ * @param options Request options passed to `$mapoFetch`.
54
+ * @returns A promise resolving to the typed response payload.
55
+ */
56
+ function fetch<R>(
57
+ path: string,
58
+ options: Record<string, unknown>,
59
+ ): Promise<R> {
60
+ return useMapoFetch().fetch(path, options as never) as Promise<R>;
61
+ }
62
+
63
+ return {
64
+ list(params, config) {
65
+ return fetch<PaginatedResponse<T>>(base, {
66
+ method: "GET",
67
+ params,
68
+ ...merged(config),
69
+ });
70
+ },
71
+
72
+ detail(id, config) {
73
+ return fetch<T>(`${base}${id}/`, { method: "GET", ...merged(config) });
74
+ },
75
+
76
+ create(data, config, opts: CrudOptions = {}) {
77
+ const body = applyMultipartPolicy(
78
+ data as Record<string, unknown>,
79
+ opts.multipart ?? MultipartPolicyEnum.Auto,
80
+ );
81
+ return fetch<T>(base, { method: "POST", body, ...merged(config) });
82
+ },
83
+
84
+ update(id, data, config, opts: CrudOptions = {}) {
85
+ const body = applyMultipartPolicy(
86
+ data as Record<string, unknown>,
87
+ opts.multipart ?? MultipartPolicyEnum.Auto,
88
+ );
89
+ return fetch<T>(`${base}${id}/`, {
90
+ method: "PUT",
91
+ body,
92
+ ...merged(config),
93
+ });
94
+ },
95
+
96
+ partialUpdate(id, diff, config, opts: CrudOptions = {}) {
97
+ const body = applyMultipartPolicy(
98
+ diff as Record<string, unknown>,
99
+ opts.multipart ?? MultipartPolicyEnum.Auto,
100
+ );
101
+ return fetch<T>(`${base}${id}/`, {
102
+ method: "PATCH",
103
+ body,
104
+ ...merged(config),
105
+ });
106
+ },
107
+
108
+ delete(id, config) {
109
+ return fetch<void>(`${base}${id}/`, {
110
+ method: "DELETE",
111
+ ...merged(config),
112
+ });
113
+ },
114
+
115
+ options(config) {
116
+ return fetch<ResourceMetadata>(base, {
117
+ method: "OPTIONS",
118
+ ...merged(config),
119
+ });
120
+ },
121
+
122
+ updateOrCreate(data, config, opts) {
123
+ if (data.id) {
124
+ const { id, ...rest } = data;
125
+ return this.update(id, rest as Partial<T>, config, opts);
126
+ }
127
+ return this.create(data as Partial<T>, config, opts);
128
+ },
129
+
130
+ updateOrder(startId, endId, config) {
131
+ return fetch<void>(`${base}update_order/`, {
132
+ method: "POST",
133
+ body: { startId, endId },
134
+ ...merged(config),
135
+ });
136
+ },
137
+ };
138
+ }
@@ -0,0 +1,78 @@
1
+ import type { MultipartPolicy } from "../types";
2
+ import { MultipartPolicyEnum } from "../types";
3
+
4
+ /**
5
+ * Flattened file entry extracted from a nested payload.
6
+ */
7
+ interface FileEntry {
8
+ /** Dot-notation path where the file/blob was found in the original payload. */
9
+ path: string;
10
+ /** File-like value to append to FormData. */
11
+ file: File | Blob;
12
+ }
13
+
14
+ /**
15
+ * Recursively collects all `File`/`Blob` values from a nested payload.
16
+ *
17
+ * @param obj Value to inspect.
18
+ * @param path Current dot-notation traversal path.
19
+ * @returns A flat list of file entries with their original paths.
20
+ */
21
+ function collectFiles(obj: unknown, path = ""): FileEntry[] {
22
+ if (obj instanceof File || obj instanceof Blob) {
23
+ return [{ path, file: obj }];
24
+ }
25
+ if (Array.isArray(obj)) {
26
+ return obj.flatMap((item, i) =>
27
+ collectFiles(item, path ? `${path}.${i}` : String(i)),
28
+ );
29
+ }
30
+ if (obj !== null && typeof obj === "object") {
31
+ return Object.entries(obj as Record<string, unknown>).flatMap(
32
+ ([key, val]) => collectFiles(val, path ? `${path}.${key}` : key),
33
+ );
34
+ }
35
+ return [];
36
+ }
37
+
38
+ /**
39
+ * Converts a payload into multipart `FormData`.
40
+ *
41
+ * The full payload is serialized under the `data` field, while every extracted
42
+ * `File`/`Blob` is appended as a dedicated FormData field at its dot-path key.
43
+ *
44
+ * @param obj Source payload object.
45
+ * @returns Multipart form data representation of `obj`.
46
+ */
47
+ function toFormData(obj: Record<string, unknown>): FormData {
48
+ const files = collectFiles(obj);
49
+ const fd = new FormData();
50
+ // JSON.stringify naturally drops File/Blob — used as the plain-data field
51
+ fd.append("data", JSON.stringify(obj));
52
+ for (const { path, file } of files) {
53
+ fd.append(path, file);
54
+ }
55
+ return fd;
56
+ }
57
+
58
+ /**
59
+ * Applies the configured multipart policy to a request payload.
60
+ *
61
+ * Policy behavior:
62
+ * - `Disable`: always return the original object payload.
63
+ * - `Force`: always return a `FormData` payload.
64
+ * - `Auto`: return `FormData` only when at least one `File`/`Blob` is present.
65
+ *
66
+ * @param payload Request payload object.
67
+ * @param policy Multipart transformation policy.
68
+ * @returns Either the original payload or a `FormData` instance.
69
+ */
70
+ export function applyMultipartPolicy(
71
+ payload: Record<string, unknown>,
72
+ policy: MultipartPolicy,
73
+ ): FormData | Record<string, unknown> {
74
+ if (policy === MultipartPolicyEnum.Disable) return payload;
75
+ if (policy === MultipartPolicyEnum.Force) return toFormData(payload);
76
+ // auto: transform only if File/Blob are present
77
+ return collectFiles(payload).length > 0 ? toFormData(payload) : payload;
78
+ }
@@ -0,0 +1,48 @@
1
+ import { useAuthStore } from "@mapomodule/store/runtime/stores/auth";
2
+ import type { RouteMeta } from "vue-router";
3
+
4
+ /** Two-format permission spec: Django model shorthand or explicit codename list. */
5
+ export type RoutePermissions = { model: string } | string[];
6
+
7
+ /**
8
+ * Returns whether the current user can access a route based on its meta.
9
+ * Mirrors the logic of the `auth`, `permissions`, and `roles` middlewares
10
+ * so the sidebar and any UI that gates visibility stays in sync with navigation guards.
11
+ */
12
+ export function useCanAccessRoute(meta: RouteMeta): boolean {
13
+ const auth = useAuthStore();
14
+
15
+ const raw = meta.middleware;
16
+ const middleware = Array.isArray(raw) ? raw : raw ? [raw] : [];
17
+
18
+ // auth middleware: must be logged in
19
+ if (middleware.includes("auth") && !auth.isAuthenticated) return false;
20
+
21
+ // roles middleware
22
+ if (middleware.includes("roles")) {
23
+ const required = meta.roles as string[] | undefined;
24
+ if (required?.length && !auth.info?.is_superuser) {
25
+ const userGroups = auth.info?.groups ?? [];
26
+ if (!required.some((r: string) => userGroups.includes(r))) return false;
27
+ }
28
+ }
29
+
30
+ // permissions middleware
31
+ if (middleware.includes("permissions")) {
32
+ const perms = meta.permissions as RoutePermissions | undefined;
33
+ if (perms && !auth.info?.is_superuser) {
34
+ if (Array.isArray(perms)) {
35
+ if (!perms.every((p) => auth.rawPermissions.includes(p))) return false;
36
+ } else {
37
+ const hasView = auth.rawPermissions.some(
38
+ (p) =>
39
+ p === `view_${perms.model}` ||
40
+ (p.endsWith(`_${perms.model}`) && p.startsWith("view")),
41
+ );
42
+ if (!hasView) return false;
43
+ }
44
+ }
45
+ }
46
+
47
+ return true;
48
+ }
@@ -0,0 +1,91 @@
1
+ import { useNuxtApp, navigateTo, useCookie, useRuntimeConfig } from "nuxt/app";
2
+ import { useAuthStore } from "@mapomodule/store/runtime/stores/auth";
3
+ import { CoreCookieEnum } from "../types";
4
+ import type { Credentials, MapoCoreRuntimeConfig } from "../types";
5
+
6
+ /**
7
+ * Optional endpoint overrides for auth operations.
8
+ */
9
+ type AuthOverrides = {
10
+ /** Login endpoint used to create the server session. */
11
+ authLoginUrl?: string;
12
+ /** Endpoint used to fetch the current authenticated user profile. */
13
+ userInfoApi?: string;
14
+ /** Logout endpoint used to invalidate the server session. */
15
+ logoutUrl?: string;
16
+ };
17
+
18
+ /**
19
+ * Provides authentication helpers built on top of the shared Mapo fetch client.
20
+ *
21
+ * Endpoints are resolved from runtime config and can be selectively overridden
22
+ * through `options`.
23
+ *
24
+ * @param options Optional endpoint overrides for login, logout, and user fetch.
25
+ * @returns Auth helpers: `login`, `logout`, and `fetchUser`.
26
+ */
27
+ export function useMapoAuth(options?: AuthOverrides) {
28
+ const rc = useRuntimeConfig().public.mapoCore as MapoCoreRuntimeConfig;
29
+ const authLoginUrl = options?.authLoginUrl ?? rc.authLoginUrl;
30
+ const userInfoApi = options?.userInfoApi ?? rc.userInfoApi;
31
+ const logoutUrl = options?.logoutUrl ?? rc.logoutUrl;
32
+ const loginUrl = rc.loginUrl;
33
+
34
+ const { $mapoFetch } = useNuxtApp() as ReturnType<typeof useNuxtApp> & {
35
+ $mapoFetch: typeof $fetch;
36
+ };
37
+ const authStore = useAuthStore();
38
+
39
+ /**
40
+ * Fetches the current user from `userInfoApi` and hydrates the auth store.
41
+ */
42
+ async function fetchUser(): Promise<void> {
43
+ const user = await $mapoFetch<
44
+ import("@mapomodule/store/runtime/types").MapoUser
45
+ >(userInfoApi, { method: "GET" });
46
+ authStore.setUser(user);
47
+ }
48
+
49
+ /**
50
+ * Performs login and then confirms the session by fetching the current user.
51
+ *
52
+ * The backend is expected to set an HttpOnly session cookie on successful
53
+ * login. `fetchUser` is used immediately after to hydrate store state.
54
+ *
55
+ * @param credentials User credentials payload for the login endpoint.
56
+ */
57
+ async function login(credentials: Credentials): Promise<void> {
58
+ // The backend sets the session cookie (HttpOnly) on a successful login.
59
+ // We confirm the session by fetching the user — if it succeeds, the
60
+ // store is hydrated and isAuthenticated becomes true.
61
+ await $mapoFetch(authLoginUrl, { method: "POST", body: credentials });
62
+ await fetchUser();
63
+ }
64
+
65
+ /**
66
+ * Logs out the current user and always performs local cleanup.
67
+ *
68
+ * Cleanup includes clearing the session cookie mirror, resetting auth store
69
+ * state, removing persisted Mapo draft keys from localStorage, and navigating
70
+ * to the configured login page.
71
+ */
72
+ async function logout(): Promise<void> {
73
+ try {
74
+ await $mapoFetch(logoutUrl, { method: "GET" });
75
+ } finally {
76
+ useCookie(CoreCookieEnum.Session).value = null;
77
+ authStore.reset();
78
+ if (typeof localStorage !== "undefined") {
79
+ const toRemove: string[] = [];
80
+ for (let i = 0; i < localStorage.length; i++) {
81
+ const k = localStorage.key(i);
82
+ if (k?.startsWith("mapo:draft:")) toRemove.push(k);
83
+ }
84
+ toRemove.forEach((k) => localStorage.removeItem(k));
85
+ }
86
+ await navigateTo(loginUrl);
87
+ }
88
+ }
89
+
90
+ return { login, logout, fetchUser };
91
+ }
@@ -0,0 +1,61 @@
1
+ import { useSnackStore } from "@mapomodule/store/runtime/stores/snack";
2
+ import { useConfirmStore } from "@mapomodule/store/runtime/stores/confirm";
3
+ import { useMapoAuth } from "../auth/useMapoAuth";
4
+ import { useCrud } from "../api/crud";
5
+ import type { MapoOptions } from "../../types";
6
+ import type { CrudRepository } from "../types";
7
+
8
+ /**
9
+ * API layer exposed by {@link useMapo}.
10
+ */
11
+ export interface MapoApiLayer {
12
+ /**
13
+ * Creates a CRUD repository bound to the provided endpoint.
14
+ *
15
+ * @param endpoint API endpoint for the resource collection.
16
+ * @returns A typed repository with list/detail/create/update/delete helpers.
17
+ */
18
+ crud<T>(endpoint: string): CrudRepository<T>;
19
+ }
20
+
21
+ /**
22
+ * High-level facade that groups the most common Mapo runtime capabilities.
23
+ *
24
+ * This keeps feature access ergonomic in components by exposing API, auth,
25
+ * feedback stores, and resolved options from a single composable call.
26
+ */
27
+ export interface MapoFacade {
28
+ /** Resource-oriented API helpers. */
29
+ $api: MapoApiLayer;
30
+ /** Authentication helpers (`login`, `logout`, `fetchUser`). */
31
+ $auth: ReturnType<typeof useMapoAuth>;
32
+ /** Global snackbar store for user feedback messages. */
33
+ $snack: ReturnType<typeof useSnackStore>;
34
+ /** Global confirm dialog store for destructive/critical actions. */
35
+ $confirm: ReturnType<typeof useConfirmStore>;
36
+ /** Effective options passed to the facade factory. */
37
+ $options: MapoOptions;
38
+ }
39
+
40
+ /**
41
+ * Creates the Mapo facade used across admin pages and composables.
42
+ *
43
+ * The facade is a convenience aggregator over lower-level composables/stores:
44
+ * - `$api.crud<T>(endpoint)` delegates to `useCrud<T>(endpoint)`.
45
+ * - `$auth` delegates to `useMapoAuth(options)` (same overrides).
46
+ * - `$snack` and `$confirm` expose the shared UI feedback stores.
47
+ *
48
+ * @param options Optional runtime overrides forwarded to the auth layer.
49
+ * @returns A `MapoFacade` with grouped API/auth/feedback capabilities.
50
+ */
51
+ export function useMapo(options: MapoOptions = {}): MapoFacade {
52
+ return {
53
+ $api: {
54
+ crud: <T>(endpoint: string): CrudRepository<T> => useCrud<T>(endpoint),
55
+ },
56
+ $auth: useMapoAuth(options),
57
+ $snack: useSnackStore(),
58
+ $confirm: useConfirmStore(),
59
+ $options: options,
60
+ };
61
+ }
@@ -0,0 +1,24 @@
1
+ import {
2
+ defineNuxtRouteMiddleware,
3
+ navigateTo,
4
+ useRuntimeConfig,
5
+ } from "nuxt/app";
6
+ import { useAuthStore } from "@mapomodule/store/runtime/stores/auth";
7
+
8
+ /**
9
+ * Route middleware that protects authenticated pages.
10
+ *
11
+ * If the user is not authenticated, it redirects to the configured login page
12
+ * and appends the current route (`to.fullPath`) as a `redirect` query param,
13
+ * so the app can return the user to the original destination after login.
14
+ */
15
+ export default defineNuxtRouteMiddleware((to) => {
16
+ const loginUrl = (useRuntimeConfig().public.mapoCore as { loginUrl: string })
17
+ .loginUrl;
18
+ const auth = useAuthStore();
19
+ if (!auth.isAuthenticated) {
20
+ return navigateTo(
21
+ `${loginUrl}?redirect=${encodeURIComponent(to.fullPath)}`,
22
+ );
23
+ }
24
+ });
@@ -0,0 +1,52 @@
1
+ import { defineNuxtRouteMiddleware, createError } from "nuxt/app";
2
+ import { useAuthStore } from "@mapomodule/store/runtime/stores/auth";
3
+
4
+ type RoutePermissions = { model: string } | string[];
5
+
6
+ /**
7
+ * Route middleware that enforces page-level permissions from `to.meta.permissions`.
8
+ *
9
+ * Supported formats:
10
+ * - `string[]`: all listed raw permission codenames are required.
11
+ * - `{ model: string }`: permissions are derived from codenames ending with
12
+ * `_<model>` (for example, `view_article`, `change_article`).
13
+ *
14
+ * Behavior:
15
+ * - If no permissions are declared, access is allowed.
16
+ * - Superusers always pass; for model-based permissions, full CRUD actions are
17
+ * exposed in `auth.pagePermissions[routeName]`.
18
+ * - For regular users, access is denied with HTTP 403 when required permissions
19
+ * are missing (or `view` is missing for model-based permissions).
20
+ * - For allowed model-based routes, resolved actions are stored in
21
+ * `auth.pagePermissions[routeName]`.
22
+ */
23
+ export default defineNuxtRouteMiddleware((to) => {
24
+ const auth = useAuthStore();
25
+ const perms = to.meta.permissions as RoutePermissions | undefined;
26
+ if (!perms) return;
27
+ if (auth.info?.is_superuser) {
28
+ if (!Array.isArray(perms)) {
29
+ auth.pagePermissions[String(to.name)] = [
30
+ "view",
31
+ "add",
32
+ "change",
33
+ "delete",
34
+ ];
35
+ }
36
+ return;
37
+ }
38
+
39
+ if (Array.isArray(perms)) {
40
+ if (!perms.every((p) => auth.rawPermissions.includes(p)))
41
+ throw createError({ statusCode: 403, message: "Forbidden" });
42
+ } else {
43
+ const actions = auth.rawPermissions
44
+ .filter((p) => p.endsWith(`_${perms.model}`))
45
+ .map((p) => p.replace(`_${perms.model}`, ""));
46
+
47
+ if (!actions.includes("view"))
48
+ throw createError({ statusCode: 403, message: "Forbidden" });
49
+
50
+ auth.pagePermissions[String(to.name)] = actions;
51
+ }
52
+ });
@@ -0,0 +1,23 @@
1
+ import { defineNuxtRouteMiddleware, createError } from "nuxt/app";
2
+ import { useAuthStore } from "@mapomodule/store/runtime/stores/auth";
3
+
4
+ /**
5
+ * Route middleware that enforces role-based access from `to.meta.roles`.
6
+ *
7
+ * Access rules:
8
+ * - If no roles are configured on the route, access is allowed.
9
+ * - Superusers always bypass role checks.
10
+ * - For other users, at least one required role must match a user group.
11
+ * - If no match is found, navigation is blocked with HTTP 403.
12
+ */
13
+ export default defineNuxtRouteMiddleware((to) => {
14
+ const auth = useAuthStore();
15
+ const required = to.meta.roles as string[] | undefined;
16
+ if (!required?.length) return;
17
+ if (auth.info?.is_superuser) return;
18
+ const userGroups = auth.info?.groups ?? [];
19
+ const hasRole = required.some((r) => userGroups.includes(r));
20
+ if (!hasRole) {
21
+ throw createError({ statusCode: 403, message: "Forbidden" });
22
+ }
23
+ });
@@ -0,0 +1,23 @@
1
+ declare module "#app" {
2
+ interface PageMeta {
3
+ /**
4
+ * Permissions required to access this route. Checked by the 'permissions' middleware and sidebar visibility.
5
+ * - `{ model: string }` — derives `view_<model>` from a Django model name and populates `authStore.pagePermissions`
6
+ * - `string[]` — raw Django permission codenames (e.g. `['view_article', 'change_article']`)
7
+ */
8
+ permissions?: { model: string } | string[];
9
+ /** Required role/group names. Checked by the 'roles' middleware. */
10
+ roles?: string[];
11
+ }
12
+ }
13
+
14
+ declare module "vue-router" {
15
+ interface RouteMeta {
16
+ permissions?: { model: string } | string[];
17
+ roles?: string[];
18
+ /** Route middleware names applied to this route. Read by useCanAccessRoute for sidebar visibility. */
19
+ middleware?: string | string[];
20
+ }
21
+ }
22
+
23
+ export {};
@@ -0,0 +1,92 @@
1
+ import { ref, computed } from "vue";
2
+ import {
3
+ defineNuxtPlugin,
4
+ navigateTo,
5
+ useRequestHeaders,
6
+ useRoute,
7
+ useRuntimeConfig,
8
+ } from "nuxt/app";
9
+ import { useAuthStore } from "@mapomodule/store/runtime/stores/auth";
10
+ import { useSnackStore } from "@mapomodule/store/runtime/stores/snack";
11
+ import type { MapoCoreRuntimeConfig } from "../types";
12
+
13
+ /**
14
+ * Registers the global Mapo fetch client and loading state.
15
+ *
16
+ * The plugin provides:
17
+ * - `$mapoFetch`: a shared `$fetch` instance with centralized request/response hooks.
18
+ * - `$mapoFetchLoading`: a computed boolean based on a global pending-request counter.
19
+ *
20
+ * Error handling behavior:
21
+ * - `401` (except logout calls): reset auth state and, on client, redirect to login
22
+ * with the current route encoded in a `redirect` query parameter.
23
+ * - `403`: show a global "Permission denied" error snackbar.
24
+ */
25
+ export default defineNuxtPlugin({
26
+ name: "mapo-core:fetch",
27
+ enforce: "pre",
28
+ setup() {
29
+ const rc = useRuntimeConfig().public.mapoCore as MapoCoreRuntimeConfig;
30
+ const logoutUrl = rc.logoutUrl;
31
+ const loginUrl = rc.loginUrl;
32
+
33
+ // Track whether a 401 redirect is already in flight to prevent duplicate redirects.
34
+ let redirectInFlight = false;
35
+
36
+ const handle = (status: number, requestUrl: string) => {
37
+ if (
38
+ status === 401 &&
39
+ !requestUrl.includes(logoutUrl) &&
40
+ !redirectInFlight
41
+ ) {
42
+ redirectInFlight = true;
43
+ const auth = useAuthStore();
44
+ auth.reset();
45
+ if (import.meta.client) {
46
+ const route = useRoute();
47
+ navigateTo(
48
+ `${loginUrl}?redirect=${encodeURIComponent(route.fullPath)}`,
49
+ );
50
+ }
51
+ }
52
+ if (status === 403) {
53
+ useSnackStore().show("Permission denied", "error");
54
+ }
55
+ };
56
+
57
+ // Global loading counter — incremented on every request, decremented once on every
58
+ // response (success or error) or request error. Exposed as $mapoFetchLoading so any
59
+ // component can show a spinner without wrapping every fetch call manually.
60
+ const pending = ref(0);
61
+ const mapoFetchLoading = computed(() => pending.value > 0);
62
+
63
+ const forwardedCookie = import.meta.server
64
+ ? useRequestHeaders(["cookie"]).cookie
65
+ : undefined;
66
+
67
+ const mapoFetch = $fetch.create({
68
+ onRequest({ options }) {
69
+ pending.value++;
70
+ if (forwardedCookie) {
71
+ const headers = new Headers(
72
+ options.headers as HeadersInit | undefined,
73
+ );
74
+ if (!headers.has("cookie")) headers.set("cookie", forwardedCookie);
75
+ options.headers = headers;
76
+ }
77
+ },
78
+ onResponse({ response, request }) {
79
+ pending.value--;
80
+ handle(response.status, String(request));
81
+ },
82
+ onRequestError() {
83
+ // Decrement counter on network errors (no response received).
84
+ pending.value--;
85
+ },
86
+ });
87
+
88
+ return {
89
+ provide: { mapoFetch, mapoFetchLoading },
90
+ };
91
+ },
92
+ });