@temboplus/afloat 0.1.12 → 0.1.13
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/README.md +120 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ This JavaScript/TypeScript package provides a central hub for shared utilities,
|
|
|
8
8
|
|
|
9
9
|
* **Abstracted Server Communication**
|
|
10
10
|
* Simplifies front-end development by abstracting all interactions with the server behind model-specific repositories
|
|
11
|
-
*
|
|
11
|
+
* Consuming projects only need to interact with these repositories, decoupling them from the underlying API implementation
|
|
12
12
|
|
|
13
13
|
* **Shared Utilities**
|
|
14
14
|
* Provides a collection of reusable helper functions for common tasks across Afloat projects, such as error handling
|
|
@@ -18,4 +18,122 @@ This JavaScript/TypeScript package provides a central hub for shared utilities,
|
|
|
18
18
|
|
|
19
19
|
* **Enhanced Maintainability**
|
|
20
20
|
* Centralizes common logic, making it easier to maintain and update across all consuming projects
|
|
21
|
-
* Reduces code duplication and improves consistency
|
|
21
|
+
* Reduces code duplication and improves consistency
|
|
22
|
+
|
|
23
|
+
* **Cross-Environment Compatibility**
|
|
24
|
+
* Works seamlessly in both client-side and server-side environments
|
|
25
|
+
* Supports both synchronous and asynchronous initialization patterns
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
### Authentication Setup
|
|
30
|
+
|
|
31
|
+
#### Client-Side Usage
|
|
32
|
+
|
|
33
|
+
In client-side applications, authentication is initialized synchronously:
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { AfloatAuth } from "@temboplus/afloat";
|
|
37
|
+
|
|
38
|
+
// Initialize client auth (typically in your app entry point)
|
|
39
|
+
const auth = AfloatAuth.instance;
|
|
40
|
+
|
|
41
|
+
// Check if user is authenticated
|
|
42
|
+
console.log("User authenticated:", !!auth.currentUser);
|
|
43
|
+
|
|
44
|
+
// Access current user
|
|
45
|
+
const user = auth.currentUser;
|
|
46
|
+
if (user) {
|
|
47
|
+
console.log(`Logged in as: ${user.email}`);
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
#### Server-Side Usage
|
|
52
|
+
|
|
53
|
+
In server-side environments, authentication requires asynchronous initialization:
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { AfloatAuth } from "@temboplus/afloat";
|
|
57
|
+
|
|
58
|
+
// In a server route handler or similar context
|
|
59
|
+
async function handleRequest(req, res) {
|
|
60
|
+
try {
|
|
61
|
+
// Extract token from request
|
|
62
|
+
const token = req.headers.authorization?.replace('Bearer ', '');
|
|
63
|
+
|
|
64
|
+
if (!token) {
|
|
65
|
+
return res.status(401).json({ error: 'Unauthorized' });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Initialize server-side auth
|
|
69
|
+
const auth = await AfloatAuth.initializeServer(token);
|
|
70
|
+
|
|
71
|
+
// Now you can use auth for permission checks
|
|
72
|
+
const isAdmin = auth.checkPermission(Permissions.Payout.View);
|
|
73
|
+
|
|
74
|
+
// Continue with your handler logic...
|
|
75
|
+
} catch (error) {
|
|
76
|
+
console.error('Authentication error:', error);
|
|
77
|
+
return res.status(500).json({ error: 'Authentication failed' });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Using Repositories
|
|
83
|
+
|
|
84
|
+
Repositories provide a consistent interface for data operations across environments.
|
|
85
|
+
|
|
86
|
+
#### Client-Side Repository Usage
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
import { WalletRepo } from "@temboplus/afloat";
|
|
90
|
+
|
|
91
|
+
// Create repository - auth is automatically handled
|
|
92
|
+
const walletRepo = new WalletRepo();
|
|
93
|
+
|
|
94
|
+
// Use repository methods
|
|
95
|
+
async function displayBalance() {
|
|
96
|
+
try {
|
|
97
|
+
const balance = await walletRepo.getBalance();
|
|
98
|
+
console.log(`Current balance: ${balance}`);
|
|
99
|
+
} catch (error) {
|
|
100
|
+
console.error('Error fetching balance:', error);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### Server-Side Repository Usage
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { AfloatAuth, WalletRepo } from "@temboplus/afloat";
|
|
109
|
+
|
|
110
|
+
async function processServerRequest(token) {
|
|
111
|
+
// Initialize auth for this request
|
|
112
|
+
const auth = await AfloatAuth.initializeServer(token);
|
|
113
|
+
|
|
114
|
+
// Create repository with explicit auth instance
|
|
115
|
+
const walletRepo = new WalletRepo({ auth });
|
|
116
|
+
|
|
117
|
+
// Use repository methods
|
|
118
|
+
const balance = await walletRepo.getBalance();
|
|
119
|
+
const wallets = await walletRepo.getWallets();
|
|
120
|
+
|
|
121
|
+
return { balance, wallets };
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Best Practices
|
|
126
|
+
|
|
127
|
+
1. **Client-Side Applications**
|
|
128
|
+
- Initialize `AfloatAuth.instance` early in your application lifecycle
|
|
129
|
+
- Create repositories without explicit auth parameters
|
|
130
|
+
- Handle permission errors appropriately in your UI
|
|
131
|
+
|
|
132
|
+
2. **Server-Side Applications**
|
|
133
|
+
- Always use `await AfloatAuth.initializeServer(token)` for each request
|
|
134
|
+
- Pass the auth instance explicitly to repositories
|
|
135
|
+
- Implement proper error handling for authentication failures
|
|
136
|
+
|
|
137
|
+
3. **Testing**
|
|
138
|
+
- Use the `AuthContext` to inject mock auth instances during testing
|
|
139
|
+
- Reset `AuthContext.current` after each test to prevent test pollution
|
package/package.json
CHANGED