adminizer 4.3.0-build.126 → 4.3.0-build.127

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.
@@ -191,6 +191,13 @@ export interface AdminpanelConfig {
191
191
  notifications?: {
192
192
  enabled: boolean;
193
193
  };
194
+ cors?: {
195
+ enabled: boolean;
196
+ origin?: string;
197
+ credentials?: boolean;
198
+ methods?: string[];
199
+ allowedHeaders?: string[];
200
+ };
194
201
  }
195
202
  export interface ModelConfig {
196
203
  adapter?: string;
package/lib/Adminizer.js CHANGED
@@ -150,16 +150,9 @@ export class Adminizer {
150
150
  chalk.blue('\nAccess your app at http://localhost:3000'));
151
151
  }
152
152
  async init(config) {
153
- // set cookie parser
154
- this.app.use(cookieParser());
155
153
  if (!config || Object.keys(config).length === 0) {
156
154
  Adminizer.log.warn(`Adminizer init > Adminizer config is emtpy`);
157
155
  }
158
- // Set vite middleware
159
- const isViteDev = process.env.VITE_ENV === "dev";
160
- if (isViteDev)
161
- await this.viteMiddleware();
162
- this.emitter.emit('adminizer:init');
163
156
  if (this.config && Object.keys(this.config).length > 0) {
164
157
  throw new Error("Config has already been initialized");
165
158
  }
@@ -184,6 +177,31 @@ export class Adminizer {
184
177
  set: configForms.set ?? defaultForms.set
185
178
  }
186
179
  };
180
+ // Middleware для всех API маршрутов
181
+ const allowedOrigin = process.env.FRONTEND_URL || 'http://localhost:8080';
182
+ if (config?.cors?.enabled) {
183
+ const corsConfig = config.cors;
184
+ this.app.all(`${this.config.routePrefix}/api/*`, (req, res, next) => {
185
+ // Устанавливаем CORS заголовки
186
+ res.header('Access-Control-Allow-Origin', corsConfig.origin || allowedOrigin);
187
+ res.header('Access-Control-Allow-Credentials', corsConfig.credentials !== false ? 'true' : 'false');
188
+ res.header('Access-Control-Allow-Methods', corsConfig.methods?.join(',') || 'GET,POST,PUT,DELETE,OPTIONS');
189
+ res.header('Access-Control-Allow-Headers', corsConfig.allowedHeaders?.join(',') || 'Content-Type,Authorization,X-Requested-With,X-CSRF-Token,x-xsrf-token');
190
+ // Если это OPTIONS запрос - сразу отвечаем
191
+ if (req.method === 'OPTIONS') {
192
+ return res.status(200).end();
193
+ }
194
+ next();
195
+ });
196
+ console.log('✅ API CORS middleware enabled');
197
+ }
198
+ // set cookie parser
199
+ this.app.use(cookieParser());
200
+ // Set vite middleware
201
+ const isViteDev = process.env.VITE_ENV === "dev";
202
+ if (isViteDev)
203
+ await this.viteMiddleware();
204
+ this.emitter.emit('adminizer:init');
187
205
  this.modelHandler = new ModelHandler();
188
206
  // TODO: 'hot reload' unbind models & unbind forms
189
207
  await bindModels(this);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "adminizer",
3
3
  "type": "module",
4
- "version": "4.3.0-build.126",
4
+ "version": "4.3.0-build.127",
5
5
  "main": "index.js",
6
6
  "exports": {
7
7
  ".": "./index.js",
@@ -102,7 +102,7 @@ export function bindInertia(adminizer) {
102
102
  enabled: true,
103
103
  cookieName: 'XSRF-TOKEN',
104
104
  headerName: 'x-xsrf-token'
105
- },
105
+ }
106
106
  }));
107
107
  adminizer.app.use((req, _, next) => {
108
108
  checkAuth(req, adminizer);
@@ -141,6 +141,9 @@ let adminpanelConfig = {
141
141
  notifications: {
142
142
  enabled: false
143
143
  },
144
+ cors: {
145
+ enabled: false,
146
+ },
144
147
  mediamanager: {
145
148
  fileStoragePath: '.tmp/public',
146
149
  }