Skip to content

Commit 2482614

Browse files
committed
docs(readme): enhance API reference and consolidate module examples 📖
1 parent 0825ee1 commit 2482614

File tree

1 file changed

+93
-27
lines changed

1 file changed

+93
-27
lines changed

README.md

Lines changed: 93 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,16 @@ npm install @neabyte/secure-jwt
3131

3232
## 🚀 Usage
3333

34-
### CommonJS
34+
### JavaScript (CommonJS & ESM)
3535

3636
```javascript
37+
// CommonJS
3738
const SecureJWT = require('@neabyte/secure-jwt').default
3839

39-
const jwt = new SecureJWT({
40-
secret: 'your-secret-key-here',
41-
expireIn: '1h',
42-
version: '1.0.0',
43-
cached: 1000
44-
})
45-
46-
// Encode any data type
47-
const data = { userId: 123, role: 'admin' }
48-
const token = jwt.sign(data)
49-
const isValid = jwt.verify(token)
50-
const decoded = jwt.decode(token)
51-
52-
// Also works with strings, numbers, arrays, etc.
53-
const stringToken = jwt.sign('Hello World!')
54-
const numberToken = jwt.sign(42)
55-
const arrayToken = jwt.sign([1, 2, 3])
56-
```
57-
58-
### ES Modules (ESM)
59-
60-
```javascript
40+
// ES Modules (ESM)
6141
import SecureJWT from '@neabyte/secure-jwt'
6242

43+
// Usage (same for both)
6344
const jwt = new SecureJWT({
6445
secret: 'your-secret-key-here',
6546
expireIn: '1h',
@@ -168,16 +149,101 @@ new SecureJWT(options: JWTOptions)
168149
### Methods
169150

170151
#### `sign(data: unknown): string`
171-
Creates encrypted JWT token from data.
152+
Creates an encrypted JWT token from any data type.
153+
154+
**Parameters:**
155+
- `data: unknown` - The data to encrypt (objects, strings, numbers, arrays, etc.)
156+
157+
**Returns:**
158+
- `string` - Encrypted JWT token
159+
160+
**Example:**
161+
```javascript
162+
const token = jwt.sign({ userId: 123, role: 'admin' })
163+
const stringToken = jwt.sign('Hello World!')
164+
const numberToken = jwt.sign(42)
165+
```
166+
167+
**Throws:**
168+
- `PayloadTooLargeError` - If payload exceeds 8KB limit
169+
- `EncryptionError` - If encryption fails
170+
171+
---
172172

173173
#### `verify(token: string): boolean`
174-
Validates token, returns true/false.
174+
Validates token integrity and expiration.
175+
176+
**Parameters:**
177+
- `token: string` - The JWT token to validate
178+
179+
**Returns:**
180+
- `boolean` - `true` if valid, `false` if invalid or expired
181+
182+
**Example:**
183+
```javascript
184+
const isValid = jwt.verify(token)
185+
if (isValid) {
186+
console.log('Token is valid')
187+
} else {
188+
console.log('Token is invalid or expired')
189+
}
190+
```
191+
192+
---
175193

176194
#### `verifyStrict(token: string): void`
177-
Validates token, throws specific errors.
195+
Validates token and throws specific errors for detailed handling.
196+
197+
**Parameters:**
198+
- `token: string` - The JWT token to validate
199+
200+
**Returns:**
201+
- `void` - Throws error if invalid
202+
203+
**Example:**
204+
```javascript
205+
try {
206+
jwt.verifyStrict(token)
207+
console.log('Token is valid')
208+
} catch (error) {
209+
console.log('Token error:', error.message)
210+
}
211+
```
212+
213+
**Throws:**
214+
- `TokenExpiredError` - If token has expired
215+
- `TokenInvalidError` - If token format is invalid
216+
- `VersionMismatchError` - If token version doesn't match
217+
- `DecryptionError` - If decryption fails
218+
219+
---
178220

179221
#### `decode(token: string): unknown`
180-
Extracts data from token, throws on error.
222+
Extracts and decrypts data from token.
223+
224+
**Parameters:**
225+
- `token: string` - The JWT token to decode
226+
227+
**Returns:**
228+
- `unknown` - The original data that was encrypted
229+
230+
**Example:**
231+
```javascript
232+
try {
233+
const data = jwt.decode(token)
234+
console.log('Decoded data:', data)
235+
} catch (error) {
236+
console.log('Decode error:', error.message)
237+
}
238+
```
239+
240+
**Throws:**
241+
- `TokenExpiredError` - If token has expired
242+
- `TokenInvalidError` - If token format is invalid
243+
- `VersionMismatchError` - If token version doesn't match
244+
- `DecryptionError` - If decryption fails
245+
246+
---
181247

182248
## ❌ Error Handling
183249

0 commit comments

Comments
 (0)