What it does
permly works in plain JavaScript without TypeScript. You lose type safety and autocomplete, but permly functions work the same way. All APIs are available; only compile-time checks are missing.
When to use it
Use JavaScript when:
- You don't use TypeScript — legacy projects, simple apps
- You prefer dynamic typing — flexibility matters more than safety
- You want minimal setup — no build tools required
TypeScript is recommended for large projects, but JavaScript is fully supported.
How it works
- Import permly the same way as TypeScript
- Call the same functions —
setupPermissions(),.can(), etc. - Check permissions as strings
- No autocomplete or compile-time checks — typos return false at runtime
Examples
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// Check permissions the same way as TypeScript
const allowed = await perms.user(42).can("posts.edit");
if (allowed) {
console.log("User can edit");
} else {
console.log("Access denied");
}
No type hints
Without TypeScript, you don't get autocomplete or type errors:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// Typo: "posts.edt" instead of "posts.edit"
// No error—it just returns false at runtime
const allowed = await perms.user(42).can("posts.edt");
console.log(allowed); // false (permission doesn't exist)
Document permissions with comments
Without TypeScript types, document permissions in comments:
import { perms, setupPermissions } from "./permly.js";
// Available permissions:
// - posts.create, posts.edit, posts.delete
// - comments.create, comments.delete
// - admin.settings
await setupPermissions();
// Check if user can create posts
const allowed = await perms.user(42).can("posts.create");
console.log(allowed);
Create a permissions constant
Define permissions as a constant for reuse. Split into two files:
// Define permissions as constants for reuse and avoid typos:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
const PERMISSIONS = {
POST_CREATE: "posts.create",
POST_EDIT: "posts.edit",
POST_DELETE: "posts.delete",
COMMENT_CREATE: "comments.create",
COMMENT_DELETE: "comments.delete",
ADMIN_SETTINGS: "admin.settings",
};
const allowed = await perms.user(42).can(PERMISSIONS.POST_EDIT);
console.log(allowed); // false
In Express routes
Use permly the same way in Express:
import express from "express";
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
const app = express();
// Minimal auth middleware (reads user ID from header)
app.use((req, res, next) => {
const userId = req.headers["x-user-id"];
if (userId) req.user = { id: parseInt(userId) };
next();
});
// Check permissions in route handlers
app.post("/posts/:id", async (req, res) => {
const userId = req.user?.id;
if (!userId) {
return res.status(401).json({ error: "Not authenticated" });
}
try {
const allowed = await perms.user(userId).can("posts.edit");
if (!allowed) {
return res.status(403).json({ error: "Access denied" });
}
res.json({ ok: true });
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
});
Run-time validation
Add manual validation to catch permission typos:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// Define permissions locally
const PERMISSIONS = {
POST_CREATE: "posts.create",
POST_EDIT: "posts.edit",
POST_DELETE: "posts.delete",
COMMENT_CREATE: "comments.create",
COMMENT_DELETE: "comments.delete",
ADMIN_SETTINGS: "admin.settings",
};
const VALID_PERMISSIONS = Object.values(PERMISSIONS);
async function checkAccess(userId, permission) {
// Validate permission name
if (!VALID_PERMISSIONS.includes(permission)) {
throw new Error(`Invalid permission: ${permission}`);
}
return await perms.user(userId).can(permission);
}
const allowed = await checkAccess(42, "posts.edit");
console.log(allowed);
// This throws an error
// await checkAccess(42, "posts.edt"); // Error: Invalid permission
Upgrade to TypeScript
When you're ready, migrate to TypeScript for autocomplete and type safety:
- Rename
.jsfiles to.ts - Add type annotations
- Export
Permissiontype - Import it in other files
See TypeScript Autocomplete for the setup.
Common mistakes
Typos in permission strings — No compile-time check. Use a PERMISSIONS constant to avoid typos.
Forgetting to await — Permission checks are async. Always await.
Not documenting permissions — Without types, document available permissions in comments.
Assuming undefined permissions fail gracefully — They do, but silently. If a permission doesn't exist, the check returns false.
Related pages
- TypeScript Autocomplete — Type safety and autocomplete
- Strict Mode — Type checking for permissions
- Permissions — Core concepts