astro-tokenkit 1.0.0 → 1.0.2
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 +138 -0
- package/dist/index.d.ts +1 -1
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# Astro TokenKit
|
|
2
|
+
|
|
3
|
+
A powerful, type-safe API client for Astro with automatic token rotation, session management, and seamless context integration.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **🚀 Built for Astro:** Deep integration with Astro's middleware and context.
|
|
8
|
+
- **🔄 Automatic Token Rotation:** Handles access and refresh tokens automatically behind the scenes.
|
|
9
|
+
- **🔒 Secure by Default:** Uses HttpOnly cookies for token storage.
|
|
10
|
+
- **🧩 Flexible Context:** Supports both internal `AsyncLocalStorage` and external context management.
|
|
11
|
+
- **🛠 Type-Safe:** Built with TypeScript for a first-class developer experience.
|
|
12
|
+
- **📡 Powerful Interceptors:** Easily add custom logic for requests, responses, and errors.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pnpm add astro-tokenkit
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
### 1. Create your API Client
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
// src/lib/api.ts
|
|
26
|
+
import { createClient } from 'astro-tokenkit';
|
|
27
|
+
|
|
28
|
+
export const api = createClient({
|
|
29
|
+
baseURL: 'https://api.yourserver.com',
|
|
30
|
+
auth: {
|
|
31
|
+
login: '/auth/login',
|
|
32
|
+
refresh: '/auth/refresh',
|
|
33
|
+
logout: '/auth/logout',
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 2. Add the Integration
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
// astro.config.mjs
|
|
42
|
+
import { defineConfig } from 'astro/config';
|
|
43
|
+
import { tokenKit } from 'astro-tokenkit';
|
|
44
|
+
import { api } from './src/lib/api';
|
|
45
|
+
|
|
46
|
+
export default defineConfig({
|
|
47
|
+
integrations: [tokenKit(api)],
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 3. Setup Middleware
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
// src/middleware.ts
|
|
55
|
+
import { defineMiddleware } from 'astro-tokenkit';
|
|
56
|
+
import { api } from './lib/api';
|
|
57
|
+
|
|
58
|
+
export const onRequest = defineMiddleware(api);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### 4. Use in Pages
|
|
62
|
+
|
|
63
|
+
```astro
|
|
64
|
+
---
|
|
65
|
+
// src/pages/profile.astro
|
|
66
|
+
import { api } from '../lib/api';
|
|
67
|
+
|
|
68
|
+
// No need to pass context, it's handled by middleware!
|
|
69
|
+
const user = await api.get('/me');
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
<h1>Welcome, {user.name}</h1>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Configuration
|
|
76
|
+
|
|
77
|
+
### Client Configuration
|
|
78
|
+
|
|
79
|
+
| Property | Type | Description |
|
|
80
|
+
| :--- | :--- | :--- |
|
|
81
|
+
| `baseURL` | `string` | **Required.** Base URL for all requests. |
|
|
82
|
+
| `auth` | `AuthConfig` | Optional authentication configuration. |
|
|
83
|
+
| `headers` | `Record<string, string>` | Default headers for all requests. |
|
|
84
|
+
| `timeout` | `number` | Request timeout in milliseconds (default: 30000). |
|
|
85
|
+
| `retry` | `RetryConfig` | Retry strategy for failed requests. |
|
|
86
|
+
| `interceptors`| `InterceptorsConfig` | Request/Response/Error interceptors. |
|
|
87
|
+
| `context` | `AsyncLocalStorage` | External AsyncLocalStorage instance. |
|
|
88
|
+
| `getContextStore`| `() => TokenKitContext`| Custom method to retrieve the context store. |
|
|
89
|
+
|
|
90
|
+
### Auth Configuration
|
|
91
|
+
|
|
92
|
+
| Property | Type | Description |
|
|
93
|
+
| :--- | :--- | :--- |
|
|
94
|
+
| `login` | `string` | Endpoint path for login (POST). |
|
|
95
|
+
| `refresh` | `string` | Endpoint path for token refresh (POST). |
|
|
96
|
+
| `logout` | `string` | Endpoint path for logout (POST). |
|
|
97
|
+
| `fields` | `FieldMapping` | Custom mapping for token fields in API responses. |
|
|
98
|
+
| `cookies` | `CookieConfig` | Configuration for auth cookies. |
|
|
99
|
+
| `policy` | `RefreshPolicy` | Strategy for when to trigger token refresh. |
|
|
100
|
+
|
|
101
|
+
## Advanced Usage
|
|
102
|
+
|
|
103
|
+
### Manual Context
|
|
104
|
+
|
|
105
|
+
If you prefer not to use middleware, you can pass the Astro context explicitly to any request:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
const data = await api.get('/data', { ctx: Astro });
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Interceptors
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
const api = createClient({
|
|
115
|
+
baseURL: '...',
|
|
116
|
+
interceptors: {
|
|
117
|
+
request: [
|
|
118
|
+
(config, ctx) => {
|
|
119
|
+
config.headers = { ...config.headers, 'X-Custom': 'Value' };
|
|
120
|
+
return config;
|
|
121
|
+
}
|
|
122
|
+
]
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Login and Logout
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
// In an API route or server-side component
|
|
131
|
+
await api.login({ username, password });
|
|
132
|
+
|
|
133
|
+
await api.logout();
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## License
|
|
137
|
+
|
|
138
|
+
MIT © [oamm](https://github.com/oamm)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { createClient, APIClient } from './client/client';
|
|
2
2
|
export { tokenKit, defineMiddleware } from './integration';
|
|
3
3
|
export { createMiddleware } from './middleware';
|
|
4
|
-
export type { ClientConfig, AuthConfig, RefreshPolicy, CookieConfig, RetryConfig, RequestOptions, RequestConfig, APIResponse, Session, TokenBundle, FieldMapping, RequestInterceptor, ResponseInterceptor, ErrorInterceptor, } from './types';
|
|
4
|
+
export type { ClientConfig, AuthConfig, RefreshPolicy, CookieConfig, RetryConfig, RequestOptions, RequestConfig, APIResponse, Session, TokenBundle, FieldMapping, RequestInterceptor, ResponseInterceptor, ErrorInterceptor, TokenKitContext } from './types';
|
|
5
5
|
export { APIError, AuthError, NetworkError, TimeoutError, } from './types';
|
|
6
6
|
export { parseTime, formatTime } from './utils/time';
|
package/package.json
CHANGED