Skip to content

Commit 1153c93

Browse files
committed
Improve naming
1 parent be6c644 commit 1153c93

7 files changed

Lines changed: 61 additions & 43 deletions

File tree

JavaScript/1-closure.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
'use strict';
22

3-
const withContext = (context) => (accountId) => {
4-
const { console, rbac, user } = context;
5-
console.log(`User ${user.name} requesting balance for ${accountId}`);
6-
if (!rbac.check(user.role, 'read:balance')) {
7-
console.error('Access denied: insufficient permissions');
8-
return null;
9-
}
10-
const balance = 15420.5;
11-
console.log('Access granted');
12-
return balance;
3+
const withContext = (context) => {
4+
const { console, accessPolicy, user } = context;
5+
const getBalance = (accountId) => {
6+
console.log(`User ${user.name} requesting balance for ${accountId}`);
7+
if (!accessPolicy.check(user.role, 'read:balance')) {
8+
console.error('Access denied: insufficient permissions');
9+
return null;
10+
}
11+
const balance = 15420.5;
12+
console.log('Access granted');
13+
return balance;
14+
};
15+
return getBalance;
1316
};
1417

1518
// Usage
1619

17-
const rbac = {
20+
const accessPolicy = {
1821
permissions: {
1922
admin: ['read:balance', 'read:transactions', 'write:transactions'],
2023
user: ['read:balance'],
2124
guest: [],
2225
},
23-
check: (role, permission) => rbac.permissions[role]?.includes(permission),
26+
check: (role, permission) =>
27+
accessPolicy.permissions[role]?.includes(permission),
2428
};
2529

26-
const context = { console, rbac, user: { name: 'Marcus', role: 'admin' } };
30+
const context = {
31+
console,
32+
accessPolicy,
33+
user: { name: 'Marcus', role: 'admin' },
34+
};
2735
const getBalance = withContext(context);
2836
const balance = getBalance('Account-123');
29-
console.log(`Access granted: balance = $${balance}`);
37+
console.log(`Balance = $${balance}`);

JavaScript/2-oop.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class AccountService {
66
}
77

88
getBalance(accountId) {
9-
const { console, rbac, user } = this.context;
9+
const { console, accessPolicy, user } = this.context;
1010
console.log(`User ${user.name} requesting balance for ${accountId}`);
11-
if (!rbac.check(user.role, 'read:balance')) {
11+
if (!accessPolicy.check(user.role, 'read:balance')) {
1212
console.error('Access denied: insufficient permissions');
1313
return null;
1414
}
@@ -20,7 +20,7 @@ class AccountService {
2020

2121
// Usage
2222

23-
class RBAC {
23+
class AccessPolicy {
2424
constructor() {
2525
this.permissions = {
2626
admin: ['read:balance', 'read:transactions', 'write:transactions'],
@@ -41,10 +41,10 @@ class User {
4141
}
4242
}
4343

44-
const rbac = new RBAC();
44+
const accessPolicy = new AccessPolicy();
4545
const user = new User('Marcus', 'admin');
46-
const context = { console, rbac, user };
46+
const context = { console, accessPolicy, user };
4747

4848
const accountService = new AccountService(context);
4949
const balance = accountService.getBalance('Account-123');
50-
console.log(`Access granted: balance = $${balance}`);
50+
console.log(`Balance = $${balance}`);

JavaScript/3-factory.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
'use strict';
22

33
const createAccountService = (context) => {
4-
const { console, rbac, user } = context;
4+
const { console, accessPolicy, user } = context;
55

66
const getBalance = (accountId) => {
77
console.log(`User ${user.name} requesting balance for ${accountId}`);
8-
if (!rbac.check(user.role, 'read:balance')) {
8+
if (!accessPolicy.check(user.role, 'read:balance')) {
99
console.error('Access denied: insufficient permissions');
1010
return null;
1111
}
1212
return 15420.5;
1313
};
1414

1515
const getTransactions = (accountId) => {
16-
if (!rbac.check(user.role, 'read:transactions')) {
16+
if (!accessPolicy.check(user.role, 'read:transactions')) {
1717
console.error('Access denied: insufficient permissions');
1818
return null;
1919
}
@@ -26,16 +26,21 @@ const createAccountService = (context) => {
2626

2727
// Usage
2828

29-
const rbac = {
29+
const accessPolicy = {
3030
permissions: {
3131
admin: ['read:balance', 'read:transactions', 'write:transactions'],
3232
user: ['read:balance'],
3333
guest: [],
3434
},
35-
check: (role, permission) => rbac.permissions[role]?.includes(permission),
35+
check: (role, permission) =>
36+
accessPolicy.permissions[role]?.includes(permission),
3637
};
3738

38-
const context = { console, rbac, user: { name: 'Marcus', role: 'admin' } };
39+
const context = {
40+
console,
41+
accessPolicy,
42+
user: { name: 'Marcus', role: 'admin' },
43+
};
3944

4045
const accountService = createAccountService(context);
4146

JavaScript/4-pipeline.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,22 @@ const auth = (context) => {
3535
return Promise.resolve({ ...context, user });
3636
};
3737

38-
const rbac = (context) => {
38+
const accessPolicy = (context) => {
3939
const permissions = {
4040
admin: ['read:balance', 'read:transactions'],
4141
user: ['read:balance'],
4242
guest: [],
4343
};
4444
const check = (role, permission) => permissions[role]?.includes(permission);
45-
return Promise.resolve({ ...context, rbac: { check, permissions } });
45+
return Promise.resolve({
46+
...context,
47+
accessPolicy: { check, permissions },
48+
});
4649
};
4750

4851
const getBalance = (context) => {
49-
const { console, rbac, user, requestId } = context;
50-
if (!rbac.check(user.role, 'read:balance')) {
52+
const { console, accessPolicy, user, requestId } = context;
53+
if (!accessPolicy.check(user.role, 'read:balance')) {
5154
console.error(`[${requestId}] Access denied for ${user.name}`);
5255
return Promise.resolve({ ...context, status: 403, body: null });
5356
}
@@ -58,7 +61,7 @@ const getBalance = (context) => {
5861

5962
// Usage
6063

61-
const execute = pipeline(tracing, auth, rbac, getBalance);
64+
const execute = pipeline(tracing, auth, accessPolicy, getBalance);
6265

6366
const context = {
6467
console,

JavaScript/5-async.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const asyncLocalStorage = new AsyncLocalStorage();
66
const getBalance = async (accountId) => {
77
const context = asyncLocalStorage.getStore();
88
if (!context) throw new Error('No context');
9-
const { console, rbac, user, requestId } = context;
9+
const { console, accessPolicy, user, requestId } = context;
1010
console.log(`[${requestId}] User ${user.name} balance for ${accountId}`);
11-
if (!rbac.check(user.role, 'read:balance')) {
11+
if (!accessPolicy.check(user.role, 'read:balance')) {
1212
console.error(`[${requestId}] Access denied for ${user.name}`);
1313
return null;
1414
}
@@ -18,18 +18,19 @@ const getBalance = async (accountId) => {
1818

1919
// Usage
2020

21-
const rbac = {
21+
const accessPolicy = {
2222
permissions: {
2323
admin: ['read:balance'],
2424
user: ['read:balance'],
2525
guest: [],
2626
},
27-
check: (role, permission) => rbac.permissions[role]?.includes(permission),
27+
check: (role, permission) =>
28+
accessPolicy.permissions[role]?.includes(permission),
2829
};
2930

3031
const context = {
3132
console,
32-
rbac,
33+
accessPolicy,
3334
user: { name: 'Marcus', role: 'admin' },
3435
requestId: 'req-001',
3536
};

JavaScript/6-immutable.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const withUser = (base, user) => createContext(base, { user });
77
const withRequest = (base, requestId) => createContext(base, { requestId });
88

99
const getBalance = (context, accountId) => {
10-
const { console, rbac, user, requestId } = context;
10+
const { console, accessPolicy, user, requestId } = context;
1111
if (!user) {
1212
console.error('No user in context');
1313
return null;
1414
}
1515
console.log(`Request ID: ${requestId}`);
1616
console.log(`User ${user.name} requesting balance for account ${accountId}`);
17-
if (!rbac.check(user.role, 'read:balance')) {
17+
if (!accessPolicy.check(user.role, 'read:balance')) {
1818
console.error(`Access denied for ${user.name}`);
1919
return null;
2020
}
@@ -23,16 +23,17 @@ const getBalance = (context, accountId) => {
2323

2424
// Usage
2525

26-
const rbac = {
26+
const accessPolicy = {
2727
permissions: {
2828
admin: ['read:balance'],
2929
user: ['read:balance'],
3030
guest: [],
3131
},
32-
check: (role, permission) => rbac.permissions[role]?.includes(permission),
32+
check: (role, permission) =>
33+
accessPolicy.permissions[role]?.includes(permission),
3334
};
3435

35-
const context1 = createContext({ console, rbac });
36+
const context1 = createContext({ console, accessPolicy });
3637
console.log('Context 1:', getBalance(context1, 'ACC-002'), '\n');
3738

3839
const context2 = withUser(context1, { name: 'Marcus', role: 'admin' });

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Context used to provide an efficient way of sharing data between different compo
55
## Examples index
66

77
- [1-closure.js](JavaScript/1-closure.js) — Context with closure
8-
- Partially applied function with bound context; simple RBAC and user auth
8+
- Partially applied function with bound context; simple AccessPolicy and user auth
99
- [2-oop.js](JavaScript/2-oop.js) — Context in OOP
10-
- Context passed to constructor; RBAC, User, and AccountService as classes
10+
- Context passed to constructor; AccessPolicy, User, and AccountService as classes
1111
- [3-factory.js](JavaScript/3-factory.js) — Context factory
1212
- Factory creates services with shared context; dependency injection
1313
- [4-pipeline.js](JavaScript/4-pipeline.js) — Context in middleware pipeline

0 commit comments

Comments
 (0)