@firtoz/hono-fetcher 1.0.0 → 2.0.0
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 +62 -10
- package/package.json +9 -8
- package/src/honoDirectFetcher.ts +10 -0
- package/src/honoDoFetcher.ts +1 -1
- package/src/index.ts +15 -11
package/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# @firtoz/hono-fetcher
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@firtoz/hono-fetcher)
|
|
4
|
+
[](https://www.npmjs.com/package/@firtoz/hono-fetcher)
|
|
5
|
+
[](https://github.com/firtoz/fullstack-toolkit/blob/main/LICENSE)
|
|
6
|
+
|
|
3
7
|
Type-safe Hono API client with full TypeScript inference for routes, params, and payloads.
|
|
4
8
|
|
|
5
9
|
## Features
|
|
@@ -25,12 +29,14 @@ This package requires the following peer dependencies:
|
|
|
25
29
|
bun add hono
|
|
26
30
|
```
|
|
27
31
|
|
|
28
|
-
For Durable Object support:
|
|
32
|
+
For Durable Object support, use `wrangler types` to generate accurate types:
|
|
29
33
|
|
|
30
34
|
```bash
|
|
31
|
-
|
|
35
|
+
wrangler types
|
|
32
36
|
```
|
|
33
37
|
|
|
38
|
+
This generates `worker-configuration.d.ts` with types for your specific environment bindings.
|
|
39
|
+
|
|
34
40
|
## Quick Start
|
|
35
41
|
|
|
36
42
|
### Basic Usage
|
|
@@ -70,11 +76,30 @@ await api.post({
|
|
|
70
76
|
|
|
71
77
|
### Remote API Usage
|
|
72
78
|
|
|
79
|
+
For remote APIs, you have two options:
|
|
80
|
+
|
|
81
|
+
#### Option 1: Using `honoDirectFetcher` (Recommended)
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { honoDirectFetcher } from '@firtoz/hono-fetcher';
|
|
85
|
+
import type { AppType } from './backend/app'; // Your backend app type
|
|
86
|
+
|
|
87
|
+
// Simply pass the base URL
|
|
88
|
+
const api = honoDirectFetcher<AppType>('https://api.example.com');
|
|
89
|
+
|
|
90
|
+
// Use it immediately
|
|
91
|
+
const response = await api.get({
|
|
92
|
+
url: '/users/:id',
|
|
93
|
+
params: { id: '123' }
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### Option 2: Using `honoFetcher` with Custom Fetch
|
|
98
|
+
|
|
73
99
|
```typescript
|
|
74
100
|
import { honoFetcher } from '@firtoz/hono-fetcher';
|
|
75
101
|
|
|
76
|
-
// For
|
|
77
|
-
// (Usually exported from your backend)
|
|
102
|
+
// For more control over the fetch behavior
|
|
78
103
|
const api = honoFetcher<typeof app>((url, init) => {
|
|
79
104
|
return fetch(`https://api.example.com${url}`, init);
|
|
80
105
|
});
|
|
@@ -83,7 +108,7 @@ const api = honoFetcher<typeof app>((url, init) => {
|
|
|
83
108
|
### Durable Objects
|
|
84
109
|
|
|
85
110
|
```typescript
|
|
86
|
-
import { honoDoFetcher, honoDoFetcherWithName } from '@firtoz/hono-fetcher
|
|
111
|
+
import { honoDoFetcher, honoDoFetcherWithName } from '@firtoz/hono-fetcher';
|
|
87
112
|
import { DurableObject } from 'cloudflare:workers';
|
|
88
113
|
import { Hono } from 'hono';
|
|
89
114
|
|
|
@@ -106,11 +131,11 @@ export class ChatRoomDO extends DurableObject {
|
|
|
106
131
|
// In your worker
|
|
107
132
|
export default {
|
|
108
133
|
async fetch(request: Request, env: Env): Promise<Response> {
|
|
109
|
-
// Option 1: From a stub
|
|
110
|
-
const stub = env.CHAT_ROOM.
|
|
134
|
+
// Option 1: From a stub (using new getByName API)
|
|
135
|
+
const stub = env.CHAT_ROOM.getByName('room-1');
|
|
111
136
|
const api = honoDoFetcher(stub);
|
|
112
137
|
|
|
113
|
-
// Option 2: Directly with name
|
|
138
|
+
// Option 2: Directly with name (recommended)
|
|
114
139
|
const api2 = honoDoFetcherWithName(env.CHAT_ROOM, 'room-1');
|
|
115
140
|
|
|
116
141
|
// Use it!
|
|
@@ -140,6 +165,33 @@ A typed fetcher with methods for each HTTP verb: `get`, `post`, `put`, `delete`,
|
|
|
140
165
|
const api = honoFetcher<typeof app>(app.request);
|
|
141
166
|
```
|
|
142
167
|
|
|
168
|
+
### `honoDirectFetcher<T>(baseUrl)`
|
|
169
|
+
|
|
170
|
+
Convenience wrapper around `honoFetcher` for remote APIs. Automatically prepends the base URL to all requests.
|
|
171
|
+
|
|
172
|
+
#### Parameters
|
|
173
|
+
|
|
174
|
+
- `baseUrl: string` - The base URL of your API (e.g., `'https://api.example.com'`)
|
|
175
|
+
|
|
176
|
+
#### Returns
|
|
177
|
+
|
|
178
|
+
A typed fetcher with methods for each HTTP verb: `get`, `post`, `put`, `delete`, `patch`
|
|
179
|
+
|
|
180
|
+
#### Example
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
import { honoDirectFetcher } from '@firtoz/hono-fetcher';
|
|
184
|
+
import type { AppType } from './backend/app';
|
|
185
|
+
|
|
186
|
+
const api = honoDirectFetcher<AppType>('https://api.example.com');
|
|
187
|
+
|
|
188
|
+
// Make requests
|
|
189
|
+
const response = await api.get({
|
|
190
|
+
url: '/users/:id',
|
|
191
|
+
params: { id: '123' }
|
|
192
|
+
});
|
|
193
|
+
```
|
|
194
|
+
|
|
143
195
|
### Method Signature
|
|
144
196
|
|
|
145
197
|
All methods follow this signature:
|
|
@@ -253,7 +305,7 @@ await api.get({
|
|
|
253
305
|
Creates a typed fetcher for a Durable Object stub.
|
|
254
306
|
|
|
255
307
|
```typescript
|
|
256
|
-
const stub = env.MY_DO.
|
|
308
|
+
const stub = env.MY_DO.getByName('example');
|
|
257
309
|
const api = honoDoFetcher(stub);
|
|
258
310
|
|
|
259
311
|
await api.get({ url: '/status' });
|
|
@@ -318,7 +370,7 @@ type Params = ParsePathParams<'/users/:id/posts/:postId'>;
|
|
|
318
370
|
Type for Durable Objects that expose a Hono app.
|
|
319
371
|
|
|
320
372
|
```typescript
|
|
321
|
-
import type { DOWithHonoApp } from '@firtoz/hono-fetcher
|
|
373
|
+
import type { DOWithHonoApp } from '@firtoz/hono-fetcher';
|
|
322
374
|
|
|
323
375
|
export class MyDO extends DurableObject implements DOWithHonoApp {
|
|
324
376
|
app = new Hono()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@firtoz/hono-fetcher",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Type-safe Hono API client with full TypeScript inference for routes, params, and payloads",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"module": "./src/index.ts",
|
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
],
|
|
25
25
|
"scripts": {
|
|
26
26
|
"typecheck": "tsc --noEmit",
|
|
27
|
-
"lint": "biome
|
|
27
|
+
"lint": "biome check --write src",
|
|
28
|
+
"lint:ci": "biome ci src",
|
|
28
29
|
"format": "biome format src --write",
|
|
29
30
|
"test": "bun test",
|
|
30
31
|
"test:watch": "bun test --watch"
|
|
@@ -51,8 +52,8 @@
|
|
|
51
52
|
"url": "https://github.com/firtoz/fullstack-toolkit/issues"
|
|
52
53
|
},
|
|
53
54
|
"peerDependencies": {
|
|
54
|
-
"@cloudflare/workers-types": "^4.
|
|
55
|
-
"hono": "^4.9.
|
|
55
|
+
"@cloudflare/workers-types": "^4.20251008.0",
|
|
56
|
+
"hono": "^4.9.10"
|
|
56
57
|
},
|
|
57
58
|
"engines": {
|
|
58
59
|
"node": ">=18.0.0"
|
|
@@ -61,9 +62,9 @@
|
|
|
61
62
|
"access": "public"
|
|
62
63
|
},
|
|
63
64
|
"devDependencies": {
|
|
64
|
-
"@hono/node-server": "^1.
|
|
65
|
-
"@hono/zod-validator": "^0.4
|
|
66
|
-
"bun-types": "^1.
|
|
67
|
-
"zod": "^
|
|
65
|
+
"@hono/node-server": "^1.19.5",
|
|
66
|
+
"@hono/zod-validator": "^0.7.4",
|
|
67
|
+
"bun-types": "^1.3.0",
|
|
68
|
+
"zod": "^4.1.12"
|
|
68
69
|
}
|
|
69
70
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Hono } from "hono";
|
|
2
|
+
import { honoFetcher, type TypedHonoFetcher } from "./honoFetcher";
|
|
3
|
+
|
|
4
|
+
export const honoDirectFetcher = <T extends Hono>(
|
|
5
|
+
baseUrl: string,
|
|
6
|
+
): TypedHonoFetcher<T> => {
|
|
7
|
+
return honoFetcher<T>((request, init) => {
|
|
8
|
+
return fetch(`${baseUrl}${request}`, init) as ReturnType<T["request"]>;
|
|
9
|
+
});
|
|
10
|
+
};
|
package/src/honoDoFetcher.ts
CHANGED
|
@@ -44,7 +44,7 @@ export const honoDoFetcherWithName = <
|
|
|
44
44
|
namespace: DurableObjectNamespace<T>,
|
|
45
45
|
name: string,
|
|
46
46
|
): TypedDoFetcher<DurableObjectStub<T>> => {
|
|
47
|
-
return honoDoFetcher(namespace.
|
|
47
|
+
return honoDoFetcher(namespace.getByName(name));
|
|
48
48
|
};
|
|
49
49
|
|
|
50
50
|
export const honoDoFetcherWithId = <
|
package/src/index.ts
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
|
+
// Convenience wrapper for direct HTTP fetching
|
|
2
|
+
export { honoDirectFetcher } from "./honoDirectFetcher";
|
|
3
|
+
// Durable Object integration
|
|
4
|
+
export {
|
|
5
|
+
type DOSchemaKeys,
|
|
6
|
+
type DOSchemaMap,
|
|
7
|
+
type DOStubSchema,
|
|
8
|
+
type DOWithHonoApp,
|
|
9
|
+
honoDoFetcher,
|
|
10
|
+
honoDoFetcherWithId,
|
|
11
|
+
honoDoFetcherWithName,
|
|
12
|
+
type TypedDoFetcher,
|
|
13
|
+
} from "./honoDoFetcher";
|
|
14
|
+
// Core fetcher functionality
|
|
1
15
|
export {
|
|
2
|
-
honoFetcher,
|
|
3
16
|
type BaseTypedHonoFetcher,
|
|
4
17
|
type HonoSchemaKeys,
|
|
5
18
|
type HttpMethod,
|
|
19
|
+
honoFetcher,
|
|
6
20
|
type JsonResponse,
|
|
7
21
|
type ParsePathParams,
|
|
8
22
|
type TypedHonoFetcher,
|
|
9
23
|
} from "./honoFetcher";
|
|
10
|
-
export {
|
|
11
|
-
honoDoFetcher,
|
|
12
|
-
honoDoFetcherWithId,
|
|
13
|
-
honoDoFetcherWithName,
|
|
14
|
-
type DOSchemaKeys,
|
|
15
|
-
type DOSchemaMap,
|
|
16
|
-
type DOStubSchema,
|
|
17
|
-
type DOWithHonoApp,
|
|
18
|
-
type TypedDoFetcher,
|
|
19
|
-
} from "./honoDoFetcher";
|