๐ Migration Guide: v2.x to v3.0.0 โ
This guide helps you migrate from Fire Shield v2.x to v3.0.0.
โ What's New in v3.0.0 โ
New Features โ
- ๐งช Plugin System - Extensible architecture for custom logic
- ๐ฆ Platform Independence - Removed fs module dependency
Breaking Changes โ
| Feature | v2.x | v3.0.0 | Action Required |
|---|---|---|---|
RBAC.fromFile() | Available | โ REMOVED | Use loader packages |
RBAC.fromFileSync() | Available | โ REMOVED | Use loader packages |
RBAC.fromJSONConfig() | Available | โ Still works | No action |
RBAC.validateConfig() | Available | โ Still works | No action |
| Plugin System | Not available | โ NEW | Optional - see plugin guide |
๐ฆ Migration Paths โ
1. Node.js Backend โ
Before (v2.x) โ
import { RBAC } from '@fire-shield/core';
// Load from file (NOT AVAILABLE IN v3.0.0)
const rbac = await RBAC.fromFile('./rbac.config.json');After (v3.0.0) - Use fromJSONConfig โ
import { RBAC } from '@fire-shield/core';
import { readFileSync } from 'fs';
// Load from file manually
const json = JSON.parse(readFileSync('./rbac.config.json', 'utf-8'));
const config = JSON.parse(json);
// Create RBAC from JSON config
const rbac = new RBAC({
preset: config
});
// Or use static method
const rbac2 = RBAC.fromJSONConfig(JSON.stringify(config));
// Everything works to same!
rbac.hasPermission(user, 'posts:write');2. Browser / SSR / Edge โ
Before (v2.x) โ
import { RBAC } from '@fire-shield/core';
// In browser, we couldn't use fromFile anyway
// We used fromJSONConfig or manual setup
const json = require('./rbac.config.json');
const rbac = new RBAC({ preset: json });After (v3.0.0) - No Changes Needed! โ
import { RBAC } from '@fire-shield/core';
// Browser/SSR/Edge - No changes needed!
const json = require('./rbac.config.json');
const rbac = RBAC.fromJSONConfig(json);
// Works exactly to same
rbac.hasPermission(user, 'posts:read');3. React / Vue / Angular / Other Frameworks โ
Framework Adapters โ
All framework adapters (React, Vue, Next.js, Nuxt, Angular, Svelte, Express, Fastify, Hono, etc.) are NOT AFFECTED by the v3.0.0 breaking changes.
They use the core RBAC class internally, and the only breaking change was the removal of fromFile() and fromFileSync(). Since framework adapters don't use these methods (they typically use fromJSONConfig() or manual setup), you don't need to change any adapter code.
// React - NO CHANGES NEEDED
import { RBAC } from '@fire-shield/core';
import { useRBAC } from '@fire-shield/react';
const rbac = new RBAC(); // Works same as before
const { can } = useRBAC(rbac);<!-- Vue - NO CHANGES NEEDED -->
<script setup>
import { RBAC } from '@fire-shield/core';
import { can } from '@fire-shield/vue';
const rbac = new RBAC(); // Works same as before
</script>// Express - NO CHANGES NEEDED
import { createExpressRBAC } from '@fire-shield/express';
import { RBAC } from '@fire-shield/core';
const rbac = new RBAC(); // Works same as before
const rbacMiddleware = createExpressRBAC(rbac, {
getUser: (req) => req.user
});๐ง Step-by-Step Migration โ
Step 1: Identify Your Usage โ
Check if you use:
RBAC.fromFile()orRBAC.fromFileSync()- โ NEEDS MIGRATIONRBAC.fromJSONConfig()- โ NO MIGRATION NEEDED- Manual setup with
new RBAC({ config: ... })- โ NO MIGRATION NEEDED
Step 2: Choose Migration Path โ
Path A: Use fromJSONConfig (Recommended for Now) โ
Best for: All platforms (Node.js, Browser, Edge, SSR)
Code:
import { RBAC } from '@fire-shield/core';
import { readFileSync } from 'fs'; // Node.js only
// Node.js
import { readFileSync } from 'fs';
const json = JSON.parse(readFileSync('./rbac.config.json', 'utf-8'));
const rbac = RBAC.fromJSONConfig(JSON.stringify(json));
// Browser/Edge
const json = require('./rbac.config.json');
const rbac = RBAC.fromJSONConfig(json);Pros:
- โ Works on all platforms
- โ No external dependencies
- โ No package installation needed
- โ Future-proof - won't break in v4.0.0
Cons:
- โ ๏ธ Manual file reading in Node.js
- โ ๏ธ Need to parse JSON yourself
Path B: Use Loader Packages (Recommended for Node.js) โ
Best for: Node.js backends only
Code: (Coming in a future release)
import { NodeLoader } from '@fire-shield/node-loader';
// Load from file
const rbac = await NodeLoader.load('./rbac.config.json');
// Load from directory
const rbac = await NodeLoader.loadFromDirectory('./configs');
// Load with validation
const rbac = await NodeLoader.load('./rbac.config.json', {
validate: true
});Pros:
- โ Clean API
- โ Built-in validation
- โ Error handling
- โ Supports multiple formats
Cons:
- โ ๏ธ Requires package installation
- โ ๏ธ Node.js only
Step 3: Update Your Code โ
Example 1: Express Server (Node.js) โ
Before (v2.x):
import express from 'express';
import { RBAC } from '@fire-shield/core';
const rbac = await RBAC.fromFile('./rbac.config.json');
const app = express();
app.use('/admin',
rbacMiddleware.requirePermission('admin:access')
);After (v3.0.0):
import express from 'express';
import { readFileSync } from 'fs';
import { RBAC } from '@fire-shield/core';
import { createExpressRBAC } from '@fire-shield/express';
// Load config manually
const json = JSON.parse(readFileSync('./rbac.config.json', 'utf-8'));
const rbac = new RBAC({ preset: json });
// Or use fromJSONConfig
const rbac2 = RBAC.fromJSONConfig(JSON.stringify(json));
const app = express();
app.use('/admin',
rbacMiddleware.requirePermission('admin:access')
);Example 2: Next.js (SSR) โ
Before (v2.x):
import { RBAC } from '@fire-shield/core';
// In server-side code
const rbac = await RBAC.fromFile('./rbac.config.json');
export default function RootLayout({ children }) {
return (
<RBACProvider rbac={rbac}>
{children}
</RBACProvider>
);
}After (v3.0.0) - No Changes Needed!
import { RBAC } from '@fire-shield/core';
// In server-side code - fromFile was never used in SSR anyway
const json = require('./rbac.config.json');
const rbac = new RBAC({ preset: json });
export default function RootLayout({ children }) {
return (
<RBACProvider rbac={rbac}>
{children}
</RBACProvider>
);
}Example 3: React App (Browser) โ
Before (v2.x):
import { RBAC } from '@fire-shield/core';
// In browser, fromFile doesn't work
const json = require('./rbac.config.json');
const rbac = new RBAC({ preset: json });After (v3.0.0) - Same!
import { RBAC } from '@fire-shield/core';
// In browser, nothing changes!
const json = require('./rbac.config.json');
const rbac = RBAC.fromJSONConfig(json);๐งช Plugin System Migration (Optional) โ
What is the Plugin System? โ
The plugin system in v3.0.0 allows you to extend RBAC functionality with custom logic. You can create plugins for:
- Database audit logging
- Analytics tracking
- Rate limiting
- External validation
- And much more!
Do You Need to Migrate? โ
NO! The plugin system is OPTIONAL. You don't need to use it if you don't want to.
Your existing code will work exactly the same without plugins.
How to Use Plugins (Optional) โ
If you want to use the new plugin system:
import { RBAC, RBACPlugin } from '@fire-shield/core';
// Create a custom plugin
class AuditDatabasePlugin implements RBACPlugin {
name = 'database-audit';
constructor(private db: any) {}
async onPermissionCheck(event) {
// Log to database
await this.db.insert({
userId: event.userId,
permission: event.permission,
allowed: event.allowed,
timestamp: event.timestamp
});
}
}
// Create RBAC instance
const rbac = new RBAC();
// Register plugin
await rbac.registerPlugin(new AuditDatabasePlugin(database));
// Now all permission checks are logged to database!
rbac.hasPermission(user, 'posts:write');Learn More: Plugin System Guide
โ Verification Checklist โ
After migrating, verify:
- [ ] All tests pass
- [ ] Permission checks work as expected
- [ ] No TypeScript errors
- [ ] No runtime errors
- [ ] Config loads correctly
- [ ] Role hierarchy works
- [ ] Wildcard permissions work
- [ ] Deny permissions work
- [ ] Audit logging works (if used)
- [ ] Framework adapters work (if used)
๐ Common Issues and Solutions โ
Issue 1: "RBAC.fromFile is not a function" โ
Cause: You're trying to use RBAC.fromFile() which was removed in v3.0.0.
Solution: Use RBAC.fromJSONConfig() instead:
// โ DON'T
const rbac = await RBAC.fromFile('./config.json');
// โ
DO
const json = require('./config.json');
const rbac = RBAC.fromJSONConfig(json);Issue 2: "fs module not found" (in Browser) โ
Cause: You're importing fs in browser code.
Solution: Don't use fs in browser code. Use require() or fetch():
// โ DON'T (Browser)
import { readFileSync } from 'fs';
// โ
DO (Browser)
const json = require('./config.json');Issue 3: "Cannot find module '@fire-shield/node-loader'" โ
Cause: Loader packages are not yet available.
Solution: Use RBAC.fromJSONConfig() for now (Option A), or wait for loader packages in a future release.
๐ Additional Resources โ
- Plugin System Guide - Learn how to create custom plugins
- API Reference - Complete API documentation
- Getting Started - Setup guide and basic usage
- Roadmap - Future plans and release notes
๐ก Tips โ
1. Use Presets for Cleaner Code โ
// Before
const rbac = new RBAC();
rbac.createRole('admin', ['*']);
rbac.createRole('editor', ['posts:read', 'posts:write']);
// ... more setup
// After - Use preset
const preset = {
name: 'my-app',
permissions: [
{ name: 'admin', bit: 1 },
{ name: 'posts:read', bit: 2 },
{ name: 'posts:write', bit: 4 }
],
roles: [
{
name: 'admin',
permissions: ['admin', 'posts:read', 'posts:write'],
level: 10
},
{
name: 'editor',
permissions: ['posts:read', 'posts:write'],
level: 5
}
]
};
const rbac = new RBAC({ preset });2. Use Environment Variables for Config Paths โ
// Node.js
const configPath = process.env.RBAC_CONFIG_PATH || './rbac.config.json';
const json = readFileSync(configPath, 'utf-8');
const rbac = RBAC.fromJSONConfig(json);
// Browser
const configUrl = process.env.RBAC_CONFIG_URL || '/rbac.config.json';
const response = await fetch(configUrl);
const json = await response.json();
const rbac = RBAC.fromJSONConfig(json);3. Wrap Loading in Try-Catch โ
let rbac: RBAC;
try {
const json = require('./rbac.config.json');
rbac = RBAC.fromJSONConfig(json);
} catch (error) {
console.error('Failed to load RBAC config:', error);
// Use fallback config
rbac = new RBAC({
preset: fallbackConfig
});
}๐ฏ Quick Reference โ
| Feature | v2.x | v3.0.0 | Migration |
|---|---|---|---|
RBAC.fromFile() | โ | โ Removed | Use fromJSONConfig() |
RBAC.fromFileSync() | โ | โ Removed | Use fromJSONConfig() |
RBAC.fromJSONConfig() | โ | โ | โ No change |
RBAC.validateConfig() | โ | โ | โ No change |
| Plugin System | โ | โ | Optional |
| Core functionality | โ | โ | โ No change |
| Framework adapters | โ | โ | โ No change |
โ You're Ready! โ
After following this migration guide:
- โ Your code works with v3.0.0
- โ You have access to the new plugin system (optional)
- โ Your app is platform-independent (works in Node.js, Browser, Edge)
- โ You're ready for future v3.x releases
Need Help?
- Plugin System Guide - Learn about plugins
- GitHub Issues - Report bugs
- GitHub Discussions - Ask questions
Happy migrating! ๐
