adminizer 4.3.0-build.133 → 4.3.0-build.135

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.
@@ -194,7 +194,8 @@ export interface AdminpanelConfig {
194
194
  };
195
195
  cors?: {
196
196
  enabled: boolean;
197
- origin?: string[];
197
+ origin: string[] | string;
198
+ path: string;
198
199
  credentials?: boolean;
199
200
  methods?: string[];
200
201
  allowedHeaders?: string[];
package/lib/Adminizer.js CHANGED
@@ -31,6 +31,7 @@ import { CatalogHandler } from "./catalog/CatalogHandler";
31
31
  import { v4 as uuid } from "uuid";
32
32
  import { bindNotifications } from "../system/bindNotifications";
33
33
  import { MediaManagerHandler } from "./media-manager/MediaManagerHandler";
34
+ import { bindCors } from "../system/bindCors";
34
35
  export class Adminizer {
35
36
  // Preconfigures
36
37
  /**
@@ -177,47 +178,8 @@ export class Adminizer {
177
178
  set: configForms.set ?? defaultForms.set
178
179
  }
179
180
  };
180
- // Middleware для всех API маршрутов
181
- const defaultOrigin = process.env.FRONTEND_URL || 'http://localhost:8080';
182
- if (config?.cors?.enabled) {
183
- const corsConfig = config.cors;
184
- // Поддерживаем массив разрешенных origin
185
- const allowedOrigins = Array.isArray(corsConfig.origin)
186
- ? corsConfig.origin
187
- : [corsConfig.origin || defaultOrigin];
188
- this.app.all(`${this.config.routePrefix}/api/*`, (req, res, next) => {
189
- const requestOrigin = req.headers.origin;
190
- // Проверяем разрешен ли origin
191
- const isOriginAllowed = !requestOrigin || allowedOrigins.includes(requestOrigin);
192
- if (requestOrigin && !isOriginAllowed) {
193
- console.log(`❌ CORS: Blocked request from ${requestOrigin}`);
194
- if (req.method === 'OPTIONS') {
195
- // Для preflight - 200 без CORS headers
196
- return res.status(200).end();
197
- }
198
- else {
199
- // Для основных запросов - ошибка
200
- return res.status(403).json({
201
- error: 'CORS policy: Origin not allowed'
202
- });
203
- }
204
- }
205
- // Запрос с разрешенного origin или без Origin
206
- if (isOriginAllowed) {
207
- // Для CORS запросов возвращаем тот же origin (или первый из списка)
208
- const allowOrigin = requestOrigin || allowedOrigins[0];
209
- res.header('Access-Control-Allow-Origin', allowOrigin);
210
- res.header('Access-Control-Allow-Credentials', corsConfig.credentials !== false ? 'true' : 'false');
211
- res.header('Access-Control-Allow-Methods', corsConfig.methods?.join(',') || 'GET,POST,PUT,DELETE,OPTIONS');
212
- res.header('Access-Control-Allow-Headers', corsConfig.allowedHeaders?.join(',') || 'Content-Type,Authorization,X-Requested-With,X-CSRF-Token,x-xsrf-token');
213
- }
214
- if (req.method === 'OPTIONS') {
215
- return res.status(200).end();
216
- }
217
- next();
218
- });
219
- console.log('✅ API CORS middleware enabled. Allowed origins:', allowedOrigins);
220
- }
181
+ // Bind cors
182
+ bindCors(this);
221
183
  // set cookie parser
222
184
  this.app.use(cookieParser());
223
185
  // Set vite middleware
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "adminizer",
3
3
  "type": "module",
4
- "version": "4.3.0-build.133",
4
+ "version": "4.3.0-build.135",
5
5
  "main": "index.js",
6
6
  "exports": {
7
7
  ".": "./index.js",
package/system/Router.js CHANGED
@@ -67,7 +67,9 @@ export default class Router {
67
67
  * Edit form
68
68
  * */
69
69
  adminizer.app.all(`${adminizer.config.routePrefix}/form/:slug`, adminizer.policyManager.bindPolicies(policies, _form));
70
- // Create a base entity route
70
+ /**
71
+ * Create a base entity route
72
+ */
71
73
  let baseRoute = `${adminizer.config.routePrefix}/:entityType(form|model)/:entityName`;
72
74
  /**
73
75
  * Catalog
@@ -0,0 +1,2 @@
1
+ import { Adminizer } from "../lib/Adminizer";
2
+ export declare function bindCors(adminizer: Adminizer): void;
@@ -0,0 +1,41 @@
1
+ export function bindCors(adminizer) {
2
+ if (adminizer.config?.cors?.enabled) {
3
+ const corsConfig = adminizer.config.cors;
4
+ // Поддерживаем массив разрешенных origin
5
+ const allowedOrigins = Array.isArray(corsConfig.origin)
6
+ ? corsConfig.origin
7
+ : [corsConfig.origin];
8
+ adminizer.app.all(`${adminizer.config.routePrefix}/${corsConfig.path}`, (req, res, next) => {
9
+ const requestOrigin = req.headers.origin;
10
+ // Проверяем разрешен ли origin
11
+ const isOriginAllowed = !requestOrigin || allowedOrigins.includes(requestOrigin);
12
+ if (requestOrigin && !isOriginAllowed) {
13
+ console.log(`❌ CORS: Blocked request from ${requestOrigin}`);
14
+ if (req.method === 'OPTIONS') {
15
+ // Для preflight - 200 без CORS headers
16
+ return res.status(200).end();
17
+ }
18
+ else {
19
+ // Для основных запросов - ошибка
20
+ return res.status(403).json({
21
+ error: 'CORS policy: Origin not allowed'
22
+ });
23
+ }
24
+ }
25
+ // Запрос с разрешенного origin или без Origin
26
+ if (isOriginAllowed) {
27
+ // Для CORS запросов возвращаем тот же origin (или первый из списка)
28
+ const allowOrigin = requestOrigin || allowedOrigins[0];
29
+ res.header('Access-Control-Allow-Origin', allowOrigin);
30
+ res.header('Access-Control-Allow-Credentials', corsConfig.credentials !== false ? 'true' : 'false');
31
+ res.header('Access-Control-Allow-Methods', corsConfig.methods?.join(',') || 'GET,POST,PUT,DELETE,OPTIONS');
32
+ res.header('Access-Control-Allow-Headers', corsConfig.allowedHeaders?.join(',') || 'Content-Type,Authorization,X-Requested-With,X-CSRF-Token,x-xsrf-token');
33
+ }
34
+ if (req.method === 'OPTIONS') {
35
+ return res.status(200).end();
36
+ }
37
+ next();
38
+ });
39
+ console.log('✅ API CORS middleware enabled. Allowed origins:', allowedOrigins);
40
+ }
41
+ }
@@ -143,6 +143,8 @@ let adminpanelConfig = {
143
143
  },
144
144
  cors: {
145
145
  enabled: false,
146
+ origin: 'http://localhost:8080',
147
+ path: 'api/*'
146
148
  },
147
149
  mediamanager: {
148
150
  fileStoragePath: '.tmp/public',