Skip to content

Commit d459991

Browse files
committed
feat(handler): Headers can be native to fetch
1 parent 49a68b7 commit d459991

File tree

5 files changed

+43
-65
lines changed

5 files changed

+43
-65
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,10 @@ await serve(
224224
return new Response(null, { status: 404, statusText: 'Not Found' });
225225
}
226226

227-
const headers: Record<string, string> = {};
228-
req.headers.forEach((value, key) => (headers[key] = value));
229227
const [body, init] = await handler({
230228
url: req.url,
231229
method: req.method,
232-
headers,
230+
headers: req.headers,
233231
body: () => req.text(),
234232
raw: req,
235233
});

docs/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ graphql-http
1717
- [ClientOptions](interfaces/ClientOptions.md)
1818
- [HandlerOptions](interfaces/HandlerOptions.md)
1919
- [Request](interfaces/Request.md)
20-
- [RequestHeaders](interfaces/RequestHeaders.md)
2120
- [RequestParams](interfaces/RequestParams.md)
2221
- [ResponseInit](interfaces/ResponseInit.md)
2322
- [ResponseLike](interfaces/ResponseLike.md)
@@ -32,6 +31,7 @@ graphql-http
3231
- [AuditResult](README.md#auditresult)
3332
- [ExecutionContext](README.md#executioncontext)
3433
- [Handler](README.md#handler)
34+
- [RequestHeaders](README.md#requestheaders)
3535
- [Response](README.md#response)
3636
- [ResponseBody](README.md#responsebody)
3737
- [ResponseHeaders](README.md#responseheaders)
@@ -228,6 +228,14 @@ considered internal errors and you should take care of them accordingly.
228228

229229
___
230230

231+
### RequestHeaders
232+
233+
Ƭ **RequestHeaders**: { `[key: string]`: `string` \| `string`[] \| `undefined`; `set-cookie?`: `string` \| `string`[] } \| { `get`: (`key`: `string`) => `string` \| ``null`` }
234+
235+
The incoming request headers the implementing server should provide.
236+
237+
___
238+
231239
### createHandler
232240

233241
**createHandler**<`RawRequest`, `Context`\>(`options`): [`Handler`](README.md#handler)<`RawRequest`, `Context`\>

docs/interfaces/Request.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ ___
4646

4747
### headers
4848

49-
`Readonly` **headers**: [`RequestHeaders`](RequestHeaders.md)
49+
`Readonly` **headers**: [`RequestHeaders`](../README.md#requestheaders)
5050

5151
___
5252

docs/interfaces/RequestHeaders.md

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/handler.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ import {
2929
*
3030
* @category Common
3131
*/
32-
export interface RequestHeaders {
33-
accept?: string | undefined;
34-
allow?: string | undefined;
35-
'content-type'?: string | undefined;
36-
/**
37-
* Always an array in Node. Duplicates are added to it.
38-
* Not necessarily true for other environments, make sure
39-
* to check the type during runtime.
40-
*/
41-
'set-cookie'?: string | string[] | undefined;
42-
[key: string]: string | string[] | undefined;
43-
}
32+
export type RequestHeaders =
33+
| {
34+
/**
35+
* Always an array in Node. Duplicates are added to it.
36+
* Not necessarily true for other environments.
37+
*/
38+
'set-cookie'?: string | string[] | undefined;
39+
[key: string]: string | string[] | undefined;
40+
}
41+
| {
42+
get: (key: string) => string | null;
43+
};
4444

4545
/**
4646
* Server agnostic request interface containing the raw request
@@ -374,7 +374,7 @@ export function createHandler<RawRequest = unknown, Context = unknown>(
374374
];
375375
}
376376

377-
const acceptedMediaType = getAcceptableMediaType(req.headers.accept);
377+
const acceptedMediaType = getAcceptableMediaType(getHeader(req, 'accept'));
378378
if (!acceptedMediaType) {
379379
return [
380380
null,
@@ -394,7 +394,7 @@ export function createHandler<RawRequest = unknown, Context = unknown>(
394394
const [
395395
mediaType,
396396
charset = 'charset=utf-8', // utf-8 is assumed when not specified. this parameter is either "charset" or "boundary" (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length)
397-
] = (req.headers['content-type'] || '')
397+
] = (getHeader(req, 'content-type') || '')
398398
.replace(/\s/g, '')
399399
.toLowerCase()
400400
.split(';');
@@ -691,3 +691,21 @@ export function makeResponse(
691691
},
692692
];
693693
}
694+
695+
function getHeader(
696+
req: Request<unknown, unknown>,
697+
key: 'set-cookie',
698+
): string[] | null;
699+
function getHeader(
700+
req: Request<unknown, unknown>,
701+
key: 'accept' | 'allow' | 'content-type' | string,
702+
): string | null;
703+
function getHeader(
704+
req: Request<unknown, unknown>,
705+
key: string,
706+
): string | string[] | null {
707+
if (typeof req.headers.get === 'function') {
708+
return req.headers.get(key);
709+
}
710+
return Object(req.headers)[key];
711+
}

0 commit comments

Comments
 (0)