@standardserver/fetch 0.0.15 → 0.0.19
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 +140 -0
- package/dist/index.mjs +2 -3
- package/package.json +5 -4
package/README.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# @standardserver/fetch
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
<a href="https://codecov.io/gh/middleapi/standardserver">
|
|
5
|
+
<img alt="codecov" src="https://codecov.io/gh/middleapi/standardserver/branch/main/graph/badge.svg">
|
|
6
|
+
</a>
|
|
7
|
+
<a href="https://www.npmjs.com/package/@standardserver/fetch">
|
|
8
|
+
<img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40standardserver%2Ffetch?logo=npm" />
|
|
9
|
+
</a>
|
|
10
|
+
<a href="https://github.com/middleapi/standardserver/blob/main/LICENSE">
|
|
11
|
+
<img alt="MIT License" src="https://img.shields.io/github/license/middleapi/standardserver?logo=open-source-initiative" />
|
|
12
|
+
</a>
|
|
13
|
+
<a href="https://discord.gg/TXEbwRBvQn">
|
|
14
|
+
<img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
|
|
15
|
+
</a>
|
|
16
|
+
<a href="https://deepwiki.com/middleapi/standardserver">
|
|
17
|
+
<img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki">
|
|
18
|
+
</a>
|
|
19
|
+
</div>
|
|
20
|
+
|
|
21
|
+
`@standardserver/fetch` adapts the Fetch API to the transport-agnostic request and response model defined by Standard Server.
|
|
22
|
+
|
|
23
|
+
Standard Server provides a unified interface for client-server communication across HTTP and message-based transports. It lets you write handlers and clients against the same request, response, body, and streaming primitives whether the underlying transport is Fetch, Node.js HTTP, or a peer-style message channel.
|
|
24
|
+
|
|
25
|
+
This package is the Fetch API adapter for that model. It converts between native `Request`, `Response`, `Headers`, and stream values and the corresponding Standard Server shapes from `@standardserver/core`.
|
|
26
|
+
|
|
27
|
+
## Entry Points
|
|
28
|
+
|
|
29
|
+
The package exports a single entry point:
|
|
30
|
+
|
|
31
|
+
| Export | Purpose |
|
|
32
|
+
| ----------------------- | ----------------------------------------------------------------------- |
|
|
33
|
+
| `@standardserver/fetch` | Fetch adapter helpers for requests, responses, bodies, headers, and SSE |
|
|
34
|
+
|
|
35
|
+
## Package overview
|
|
36
|
+
|
|
37
|
+
The main entry point exposes three groups of helpers:
|
|
38
|
+
|
|
39
|
+
| Group | Exports | Purpose |
|
|
40
|
+
| ----------------------- | --------------------------------------------------------------------------- | ----------------------------------------------------- |
|
|
41
|
+
| Request and response | `toStandardLazyRequest()`, `toStandardLazyResponse()`, `toFetchResponse()` | Convert between Fetch API objects and Standard Server |
|
|
42
|
+
| Body and stream helpers | `toStandardBody()`, `toFetchBody()`, `toEventIterator()`, `toEventStream()` | Parse and serialize body values, including SSE |
|
|
43
|
+
| Header and URL helpers | `toStandardHeaders()`, `toFetchHeaders()`, `toStandardUrl()` | Normalize Fetch headers and URLs for Standard Server |
|
|
44
|
+
|
|
45
|
+
Use these helpers when you want Standard Server handlers to run in Fetch-based runtimes such as browsers, Cloudflare Workers, Bun, Deno, service workers, or server frameworks that expose the standard Fetch API.
|
|
46
|
+
|
|
47
|
+
## Server-side request handling
|
|
48
|
+
|
|
49
|
+
Use `toStandardLazyRequest()` to convert an incoming Fetch `Request` into a `StandardLazyRequest`.
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import type { StandardLazyRequest, StandardResponse } from '@standardserver/core'
|
|
53
|
+
import { toFetchResponse, toStandardLazyRequest } from '@standardserver/fetch'
|
|
54
|
+
|
|
55
|
+
async function handle(request: StandardLazyRequest): Promise<StandardResponse> {
|
|
56
|
+
const body = await request.resolveBody()
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
status: 200,
|
|
60
|
+
headers: { 'content-type': 'application/json' },
|
|
61
|
+
body: {
|
|
62
|
+
ok: true,
|
|
63
|
+
method: request.method,
|
|
64
|
+
url: request.url,
|
|
65
|
+
received: body,
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export async function fetchHandler(request: Request): Promise<Response> {
|
|
71
|
+
const standardRequest = toStandardLazyRequest(request)
|
|
72
|
+
const standardResponse = await handle(standardRequest)
|
|
73
|
+
return toFetchResponse(standardResponse, {/** options */})
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
> [!TIP]
|
|
78
|
+
> When sending requests or responses, you can pass additional options such as event-stream keep-alive.
|
|
79
|
+
|
|
80
|
+
## Client-side response handling
|
|
81
|
+
|
|
82
|
+
Use `toStandardLazyResponse()` when you receive a Fetch `Response` but want to work with the Standard Server response contract.
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { toFetchBody, toFetchHeaders, toStandardLazyResponse } from '@standardserver/fetch'
|
|
86
|
+
|
|
87
|
+
const standardRequest = {
|
|
88
|
+
method: 'POST',
|
|
89
|
+
url: '/echo',
|
|
90
|
+
headers: { 'content-type': 'application/json' },
|
|
91
|
+
body: { message: 'hello' },
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const [body, headers] = toFetchBody(standardRequest.body, standardRequest.headers, {/** options */})
|
|
95
|
+
const response = await fetch(standardRequest.url, {
|
|
96
|
+
method: standardRequest.method,
|
|
97
|
+
headers: toFetchHeaders(headers),
|
|
98
|
+
body,
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
const standardResponse = toStandardLazyResponse(response)
|
|
102
|
+
const payload = await standardResponse.resolveBody()
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
> [!TIP]
|
|
106
|
+
> When sending requests or responses, you can pass additional options such as event-stream keep-alive.
|
|
107
|
+
|
|
108
|
+
## Resolving Body
|
|
109
|
+
|
|
110
|
+
`resolveBody(hint?)` determines how to parse the body using the following priority:
|
|
111
|
+
|
|
112
|
+
1. If the `standard-server` header is present, use it as the `StandardBodyHint`.
|
|
113
|
+
2. Otherwise, if `hint?` is provided, use it as the `StandardBodyHint`.
|
|
114
|
+
3. Otherwise, if `content-type` is one of the common types, parse accordingly.
|
|
115
|
+
4. Otherwise, if `content-length` exists, treat the body as `file`; if not, treat it as `octet-stream`.
|
|
116
|
+
|
|
117
|
+
For efficient communication, set the `standard-server` header to explicitly hint the body type, especially for file or binary streaming. For example, if you upload a file with a common `content-type` such as `application/json` but omit the `standard-server` header, the server may interpret it as JSON and parse it unexpectedly.
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
const response = await fetch('/upload', {
|
|
121
|
+
method: 'POST',
|
|
122
|
+
headers: {
|
|
123
|
+
'content-type': 'application/json',
|
|
124
|
+
'standard-server': 'file', // <- hint the body type to avoid misinterpretation
|
|
125
|
+
},
|
|
126
|
+
body: new Blob(['{"message": "Hello, world!"}'], { type: 'application/json' }),
|
|
127
|
+
})
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Learn more
|
|
131
|
+
|
|
132
|
+
For the higher-level project overview, see the root [Standard Server README](../../README.md).
|
|
133
|
+
|
|
134
|
+
## Sponsors
|
|
135
|
+
|
|
136
|
+
<p align="center">
|
|
137
|
+
<a href="https://cdn.jsdelivr.net/gh/middleapi/static/sponsors.svg">
|
|
138
|
+
<img src='https://cdn.jsdelivr.net/gh/middleapi/static/sponsors.svg' alt="Sponsors"/>
|
|
139
|
+
</a>
|
|
140
|
+
</p>
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { getFilenameFromContentDisposition, generateContentDisposition } from '@standardserver/core';
|
|
1
|
+
import { EventStreamDecoderStream, withEventIteratorEventMeta, EventIteratorErrorEvent, encodeEventStreamMessage, unwrapEventIteratorEvent, getEventIteratorEventMeta, getFilenameFromContentDisposition, generateContentDisposition } from '@standardserver/core';
|
|
2
2
|
import { AsyncIteratorClass, parseEmptyableJSON, isTypescriptObject, stringifyJSON, AbortError, isAsyncIteratorObject } from '@standardserver/shared';
|
|
3
|
-
import { EventStreamDecoderStream, withEventIteratorEventMeta, EventIteratorErrorEvent, encodeEventStreamMessage, unwrapEventIteratorEvent, getEventIteratorEventMeta } from '@standardserver/core/event-stream';
|
|
4
3
|
|
|
5
4
|
function toEventIterator(stream) {
|
|
6
5
|
const eventStream = stream?.pipeThrough(new TextDecoderStream()).pipeThrough(new EventStreamDecoderStream());
|
|
@@ -182,7 +181,7 @@ function toFetchBody(body, headers, options = {}) {
|
|
|
182
181
|
if (Number.isNaN(body.size)) {
|
|
183
182
|
return [body.stream(), headers];
|
|
184
183
|
}
|
|
185
|
-
headers["content-length"]
|
|
184
|
+
headers["content-length"] = body.size.toString();
|
|
186
185
|
return [body, headers];
|
|
187
186
|
}
|
|
188
187
|
if (body instanceof FormData) {
|
package/package.json
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@standardserver/fetch",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.19",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://standardserver.dev",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/
|
|
9
|
+
"url": "git+https://github.com/middleapi/standardserver.git",
|
|
10
10
|
"directory": "packages/fetch"
|
|
11
11
|
},
|
|
12
12
|
"sideEffects": false,
|
|
13
13
|
"exports": {
|
|
14
|
+
"./package.json": "./package.json",
|
|
14
15
|
".": {
|
|
15
16
|
"types": "./dist/index.d.mts",
|
|
16
17
|
"import": "./dist/index.mjs",
|
|
@@ -21,8 +22,8 @@
|
|
|
21
22
|
"dist"
|
|
22
23
|
],
|
|
23
24
|
"dependencies": {
|
|
24
|
-
"@standardserver/core": "0.0.
|
|
25
|
-
"@standardserver/shared": "0.0.
|
|
25
|
+
"@standardserver/core": "0.0.19",
|
|
26
|
+
"@standardserver/shared": "0.0.19"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"@hono/node-server": "^2.0.1"
|