exguard-backend 1.0.7 → 1.0.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exguard-backend",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -125,12 +125,12 @@ import { Guard } from 'exguard-backend';
125
125
  apiUrl: process.env.EXGUARD_API_URL || 'http://localhost:3000',
126
126
  cache: {
127
127
  enabled: process.env.EXGUARD_CACHE_ENABLED !== 'false',
128
- ttl: parseInt(process.env.EXGUARD_CACHE_TTL) || 300000, // 5 minutes
128
+ ttl: parseInt(process.env.EXGUARD_CACHE_TTL || '300000'), // 5 minutes
129
129
  },
130
130
  realtime: {
131
131
  enabled: process.env.EXGUARD_REALTIME_ENABLED === 'true',
132
- url: process.env.EXGUARD_REALTIME_URL,
133
- token: process.env.EXGUARD_SERVICE_TOKEN,
132
+ url: process.env.EXGUARD_REALTIME_URL || undefined,
133
+ token: process.env.EXGUARD_SERVICE_TOKEN || undefined,
134
134
  },
135
135
  }),
136
136
  },
@@ -160,7 +160,7 @@ import { Guard, GuardContext } from 'exguard-backend';
160
160
 
161
161
  @Injectable()
162
162
  export class ExGuardNestGuard implements CanActivate {
163
- constructor(protected exGuard: Guard) {}
163
+ constructor(public exGuard: Guard) {}
164
164
 
165
165
  async canActivate(context: ExecutionContext): Promise<boolean> {
166
166
  const request = context.switchToHttp().getRequest();
@@ -185,11 +185,11 @@ export class ExGuardNestGuard implements CanActivate {
185
185
  return true;
186
186
  }
187
187
 
188
- protected async checkPermissions(context: GuardContext) {
188
+ public async checkPermissions(context: GuardContext) {
189
189
  return this.exGuard.authenticate(context);
190
190
  }
191
191
 
192
- protected extractToken(request: any): string | null {
192
+ public extractToken(request: any): string | null {
193
193
  const authHeader = request.headers?.authorization;
194
194
  if (authHeader?.startsWith('Bearer ')) {
195
195
  return authHeader.substring(7);
@@ -200,14 +200,14 @@ export class ExGuardNestGuard implements CanActivate {
200
200
 
201
201
  @Injectable()
202
202
  export class ExGuardPermissionGuard extends ExGuardNestGuard {
203
- protected async checkPermissions(context: GuardContext) {
203
+ public async checkPermissions(context: GuardContext) {
204
204
  return this.exGuard.requirePermissions(context, ['read']);
205
205
  }
206
206
  }
207
207
 
208
208
  @Injectable()
209
209
  export class ExGuardRoleGuard extends ExGuardNestGuard {
210
- protected async checkPermissions(context: GuardContext) {
210
+ public async checkPermissions(context: GuardContext) {
211
211
  return this.exGuard.requireRoles(context, ['Admin']);
212
212
  }
213
213
  }
@@ -215,7 +215,7 @@ export class ExGuardRoleGuard extends ExGuardNestGuard {
215
215
  // Factory functions for dynamic guards
216
216
  export function createPermissionGuard(permissions: string[], requireAll = false) {
217
217
  return class extends ExGuardNestGuard {
218
- protected async checkPermissions(context: GuardContext) {
218
+ public async checkPermissions(context: GuardContext) {
219
219
  return this.exGuard.requirePermissions(context, permissions, { requireAll });
220
220
  }
221
221
  };
@@ -223,7 +223,7 @@ export function createPermissionGuard(permissions: string[], requireAll = false)
223
223
 
224
224
  export function createRoleGuard(roles: string[], requireAll = false) {
225
225
  return class extends ExGuardNestGuard {
226
- protected async checkPermissions(context: GuardContext) {
226
+ public async checkPermissions(context: GuardContext) {
227
227
  return this.exGuard.requireRoles(context, roles, { requireAll });
228
228
  }
229
229
  };
@@ -231,7 +231,7 @@ export function createRoleGuard(roles: string[], requireAll = false) {
231
231
 
232
232
  export function createModuleGuard(modules: string[], requireAll = false) {
233
233
  return class extends ExGuardNestGuard {
234
- protected async checkPermissions(context: GuardContext) {
234
+ public async checkPermissions(context: GuardContext) {
235
235
  return this.exGuard.requireModules(context, modules, { requireAll });
236
236
  }
237
237
  };
@@ -421,26 +421,50 @@ function updateAppModule() {
421
421
  return;
422
422
  }
423
423
 
424
- // Add import at the top
425
- if (!appModuleContent.includes('import { ExGuardModule }')) {
424
+ // Add import at the top - find the last import statement
425
+ const importRegex = /import\s+.*\s+from\s+['"][^'"]*['"];?\s*/g;
426
+ const imports = appModuleContent.match(importRegex);
427
+
428
+ if (imports && imports.length > 0) {
429
+ // Add after the last import
430
+ const lastImport = imports[imports.length - 1];
431
+ const lastImportIndex = appModuleContent.lastIndexOf(lastImport);
432
+ const afterLastImport = appModuleContent.substring(lastImportIndex + lastImport.length);
433
+
434
+ appModuleContent = appModuleContent.substring(0, lastImportIndex + lastImport.length) +
435
+ `import { ExGuardModule } from './exguard/exguard.module';\n` +
436
+ afterLastImport;
437
+ } else {
438
+ // Add at the very top
426
439
  appModuleContent = `import { ExGuardModule } from './exguard/exguard.module';\n${appModuleContent}`;
427
440
  }
428
441
 
429
- // Add to imports array - more robust regex
430
- const moduleMatch = appModuleContent.match(/@Module\s*\(\s*\{([^}]*)\}/s);
431
- if (moduleMatch) {
432
- const moduleContent = moduleMatch[1];
442
+ // Find and update the @Module decorator
443
+ const moduleDecoratorMatch = appModuleContent.match(/@Module\s*\(\s*\{([^}]*)\}/s);
444
+ if (moduleDecoratorMatch) {
445
+ const moduleContent = moduleDecoratorMatch[1];
433
446
 
434
447
  // Check if imports array exists
435
- const importsMatch = moduleContent.match(/imports:\s*\[([^\]]*)\]/);
436
- if (importsMatch) {
437
- const importsContent = importsMatch[1];
448
+ const importsArrayMatch = moduleContent.match(/imports:\s*\[([^\]]*)\]/s);
449
+ if (importsArrayMatch) {
450
+ // Add to existing imports array
451
+ const importsContent = importsArrayMatch[1];
438
452
  const newImportsContent = importsContent.trim() + ',\n ExGuardModule';
439
- appModuleContent = appModuleContent.replace(importsMatch[0], `imports: [${newImportsContent}]`);
453
+ appModuleContent = appModuleContent.replace(
454
+ importsArrayMatch[0],
455
+ `imports: [${newImportsContent}]`
456
+ );
440
457
  } else {
441
- // Add imports array
442
- const newModuleContent = moduleContent.replace(/@Module\s*\(\s*\{/, `@Module({\n imports: [ExGuardModule],`);
443
- appModuleContent = appModuleContent.replace(moduleMatch[0], newModuleContent);
458
+ // Add imports array after @Module({
459
+ const moduleStartMatch = appModuleContent.match(/@Module\s*\(\s*\{/);
460
+ if (moduleStartMatch) {
461
+ const moduleStartIndex = appModuleContent.indexOf(moduleStartMatch[0]);
462
+ const afterModuleStart = appModuleContent.substring(moduleStartIndex + moduleStartMatch[0].length);
463
+
464
+ appModuleContent = appModuleContent.substring(0, moduleStartIndex + moduleStartMatch[0].length) +
465
+ `\n imports: [ExGuardModule],` +
466
+ afterModuleStart;
467
+ }
444
468
  }
445
469
  }
446
470