Skip to content

Commit 66cce46

Browse files
committed
docs
1 parent 4b516eb commit 66cce46

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+4566
-2
lines changed

docs/.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
node_modules
2+
3+
.DS_Store
4+
.cache
5+
.vercel
6+
.output
7+
.nitro
8+
/build/
9+
/api/
10+
/server/build
11+
/public/build
12+
/test-results/
13+
/playwright-report/
14+
/blob-report/
15+
/playwright/.cache/
16+
.tanstack
17+
18+
src/routeTree.gen.ts
19+
.source

docs/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# docs
2+
3+
This is a Tanstack Start application generated with
4+
[Create Fumadocs](https://github.com/fuma-nama/fumadocs).
5+
6+
Run development server:
7+
8+
```bash
9+
npm run dev
10+
# or
11+
pnpm dev
12+
# or
13+
yarn dev
14+
```

docs/bun.lock

Lines changed: 1054 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
title: Audit Logs
3+
description: Query and manage audit logs
4+
icon: FileSearch
5+
---
6+
7+
# Audit Logs
8+
9+
Methods for querying and managing audit logs (requires `auditLogs: true` in configuration).
10+
11+
## getLogs()
12+
13+
Query audit logs.
14+
15+
```typescript
16+
// Get logs for a specific key
17+
const logs = await keys.getLogs({
18+
keyId: 'key_123',
19+
limit: 100,
20+
})
21+
22+
// Get logs by action
23+
const revokedLogs = await keys.getLogs({
24+
action: 'revoked',
25+
startDate: '2025-01-01',
26+
})
27+
28+
// Get logs for an owner
29+
const ownerLogs = await keys.getLogs({
30+
ownerId: 'user_123',
31+
limit: 50,
32+
offset: 0,
33+
})
34+
```
35+
36+
**Returns**: `Promise<AuditLog[]>`
37+
38+
### AuditLogQuery Options
39+
40+
```typescript
41+
{
42+
keyId?: string // Filter by key ID
43+
ownerId?: string // Filter by owner ID
44+
action?: AuditAction // Filter by action
45+
startDate?: string // Filter by start date (ISO timestamp)
46+
endDate?: string // Filter by end date (ISO timestamp)
47+
limit?: number // Maximum results (default: 100)
48+
offset?: number // Pagination offset (default: 0)
49+
}
50+
```
51+
52+
## countLogs()
53+
54+
Count audit logs matching a query.
55+
56+
```typescript
57+
const count = await keys.countLogs({
58+
action: 'created',
59+
startDate: '2025-01-01',
60+
})
61+
```
62+
63+
**Returns**: `Promise<number>`
64+
65+
## deleteLogs()
66+
67+
Delete audit logs matching a query.
68+
69+
```typescript
70+
// Delete old logs
71+
const deleted = await keys.deleteLogs({
72+
endDate: '2024-01-01',
73+
})
74+
```
75+
76+
**Returns**: `Promise<number>` (number of logs deleted)
77+
78+
## clearLogs()
79+
80+
Clear all logs for a specific key.
81+
82+
```typescript
83+
await keys.clearLogs('key_123')
84+
```
85+
86+
**Returns**: `Promise<number>` (number of logs deleted)
87+
88+
## getLogStats()
89+
90+
Get statistics about audit logs for an owner.
91+
92+
```typescript
93+
const stats = await keys.getLogStats('user_123')
94+
95+
console.log(stats.total) // Total number of logs
96+
console.log(stats.byAction.created) // Number of created actions
97+
console.log(stats.byAction.revoked) // Number of revoked actions
98+
console.log(stats.lastActivity) // Last activity timestamp
99+
```
100+
101+
**Returns**: `Promise<AuditLogStats>`
102+
103+
### AuditLogStats Structure
104+
105+
```typescript
106+
{
107+
total: number
108+
byAction: {
109+
created?: number
110+
revoked?: number
111+
rotated?: number
112+
enabled?: number
113+
disabled?: number
114+
}
115+
lastActivity: string | null
116+
}
117+
```
118+
119+
## Audit Actions
120+
121+
- `'created'` - Key was created
122+
- `'revoked'` - Key was revoked
123+
- `'rotated'` - Key was rotated
124+
- `'enabled'` - Key was enabled
125+
- `'disabled'` - Key was disabled
126+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
title: createKeys()
3+
description: Create a new API key manager instance
4+
icon: KeyRound
5+
---
6+
7+
# createKeys()
8+
9+
Creates a new API key manager instance.
10+
11+
## Signature
12+
13+
```typescript
14+
function createKeys(config?: ConfigInput): ApiKeyManager
15+
```
16+
17+
## Parameters
18+
19+
- **`config`** (ConfigInput, optional): Configuration options
20+
21+
## Returns
22+
23+
An `ApiKeyManager` instance for creating and verifying keys.
24+
25+
## Examples
26+
27+
### Basic Usage
28+
29+
```typescript
30+
import { createKeys } from 'keypal'
31+
32+
const keys = createKeys({
33+
prefix: 'sk_',
34+
})
35+
```
36+
37+
### With Redis Storage
38+
39+
```typescript
40+
import Redis from 'ioredis'
41+
42+
const redis = new Redis()
43+
44+
const keys = createKeys({
45+
prefix: 'sk_live_',
46+
storage: 'redis',
47+
redis,
48+
cache: true,
49+
cacheTtl: 300,
50+
})
51+
```
52+
53+
### With Custom Storage
54+
55+
```typescript
56+
import { createKeys, type Storage } from 'keypal'
57+
58+
const customStorage: Storage = {
59+
save: async (record) => { /* ... */ },
60+
findByHash: async (keyHash) => { /* ... */ },
61+
// ... other methods
62+
}
63+
64+
const keys = createKeys({
65+
storage: customStorage,
66+
})
67+
```
68+
69+
## Configuration Options
70+
71+
See the [Configuration](/docs/getting-started/configuration) guide for all available options.
72+
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
title: Key Management
3+
description: Create, list, find, and manage API keys
4+
icon: KeyRound
5+
---
6+
7+
# Key Management
8+
9+
Methods for creating, listing, finding, and managing API keys.
10+
11+
## create()
12+
13+
Create a new API key.
14+
15+
```typescript
16+
const { key, record } = await keys.create({
17+
ownerId: 'user_123',
18+
name: 'Production Key',
19+
description: 'Key for production API access',
20+
scopes: ['read', 'write'],
21+
tags: ['production', 'api'],
22+
resources: {
23+
'project:123': ['read', 'write'],
24+
},
25+
expiresAt: '2025-12-31',
26+
enabled: true,
27+
})
28+
```
29+
30+
**Returns**: `{ key: string, record: ApiKeyRecord }`
31+
32+
> **Important**: The `key` is only returned once during creation. Store it securely!
33+
34+
## list()
35+
36+
List all keys for an owner.
37+
38+
```typescript
39+
const userKeys = await keys.list('user_123')
40+
```
41+
42+
**Returns**: `Promise<ApiKeyRecord[]>`
43+
44+
## findById()
45+
46+
Find a key by its ID.
47+
48+
```typescript
49+
const keyRecord = await keys.findById('key_123')
50+
```
51+
52+
**Returns**: `Promise<ApiKeyRecord | null>`
53+
54+
## findByHash()
55+
56+
Find a key by its hash.
57+
58+
```typescript
59+
const keyByHash = await keys.findByHash('hashed_key_value')
60+
```
61+
62+
**Returns**: `Promise<ApiKeyRecord | null>`
63+
64+
## findByTag()
65+
66+
Find keys with a specific tag.
67+
68+
```typescript
69+
const taggedKeys = await keys.findByTag('production')
70+
const userTaggedKeys = await keys.findByTag('production', 'user_123')
71+
```
72+
73+
**Returns**: `Promise<ApiKeyRecord[]>`
74+
75+
## findByTags()
76+
77+
Find keys with any of the specified tags.
78+
79+
```typescript
80+
const taggedKeys = await keys.findByTags(['production', 'staging'])
81+
const userTaggedKeys = await keys.findByTags(['production', 'staging'], 'user_123')
82+
```
83+
84+
**Returns**: `Promise<ApiKeyRecord[]>`
85+
86+
## revoke()
87+
88+
Revoke (soft delete) a key.
89+
90+
```typescript
91+
await keys.revoke('key_123', {
92+
userId: 'admin_456',
93+
metadata: { reason: 'Security breach' },
94+
})
95+
```
96+
97+
**Returns**: `Promise<void>`
98+
99+
## revokeAll()
100+
101+
Revoke all keys for an owner.
102+
103+
```typescript
104+
await keys.revokeAll('user_123')
105+
```
106+
107+
**Returns**: `Promise<void>`
108+
109+
## enable()
110+
111+
Enable a disabled key.
112+
113+
```typescript
114+
await keys.enable('key_123', {
115+
userId: 'admin_456',
116+
})
117+
```
118+
119+
**Returns**: `Promise<void>`
120+
121+
## disable()
122+
123+
Disable a key.
124+
125+
```typescript
126+
await keys.disable('key_123', {
127+
userId: 'admin_456',
128+
})
129+
```
130+
131+
**Returns**: `Promise<void>`
132+
133+
## rotate()
134+
135+
Rotate a key (create new, mark old as revoked).
136+
137+
```typescript
138+
const { key: newKey, record: newRecord, oldRecord } = await keys.rotate('key_123', {
139+
name: 'Updated Key',
140+
scopes: ['read', 'write', 'admin'],
141+
})
142+
```
143+
144+
**Returns**: `Promise<{ key: string, record: ApiKeyRecord, oldRecord: ApiKeyRecord }>`
145+
146+
## updateLastUsed()
147+
148+
Manually update the last used timestamp.
149+
150+
```typescript
151+
await keys.updateLastUsed('key_123')
152+
```
153+
154+
**Returns**: `Promise<void>`
155+
156+
> **Note**: This is automatically called when `autoTrackUsage` is enabled and `skipTracking` is not set.
157+

0 commit comments

Comments
 (0)