Skip to content

Commit a638cb4

Browse files
authored
fix(handler): Don't export makeResponse, getAcceptableMediaType or isResponse (#98)
1 parent c08d4f4 commit a638cb4

File tree

2 files changed

+36
-135
lines changed

2 files changed

+36
-135
lines changed

docs/modules/handler.md

-77
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
### Type Aliases
1414

15-
- [AcceptableMediaType](handler.md#acceptablemediatype)
1615
- [FormatError](handler.md#formaterror)
1716
- [Handler](handler.md#handler)
1817
- [OperationArgs](handler.md#operationargs)
@@ -25,20 +24,9 @@
2524
### Functions
2625

2726
- [createHandler](handler.md#createhandler)
28-
- [getAcceptableMediaType](handler.md#getacceptablemediatype)
29-
- [isResponse](handler.md#isresponse)
30-
- [makeResponse](handler.md#makeresponse)
3127

3228
## Server
3329

34-
### AcceptableMediaType
35-
36-
Ƭ **AcceptableMediaType**: ``"application/graphql-response+json"`` \| ``"application/json"``
37-
38-
Request's Media-Type that the server accepts.
39-
40-
___
41-
4230
### FormatError
4331

4432
Ƭ **FormatError**: (`err`: `Readonly`<`GraphQLError` \| `Error`\>) => `GraphQLError` \| `Error`
@@ -227,68 +215,3 @@ console.log('Listening to port 4000');
227215
#### Returns
228216

229217
[`Handler`](handler.md#handler)<`RequestRaw`, `RequestContext`\>
230-
231-
___
232-
233-
### getAcceptableMediaType
234-
235-
**getAcceptableMediaType**(`acceptHeader`): [`AcceptableMediaType`](handler.md#acceptablemediatype) \| ``null``
236-
237-
Inspects the request and detects the appropriate/acceptable Media-Type
238-
looking at the `Accept` header while complying with the GraphQL over HTTP spec.
239-
240-
#### Parameters
241-
242-
| Name | Type |
243-
| :------ | :------ |
244-
| `acceptHeader` | `undefined` \| ``null`` \| `string` |
245-
246-
#### Returns
247-
248-
[`AcceptableMediaType`](handler.md#acceptablemediatype) \| ``null``
249-
250-
___
251-
252-
### isResponse
253-
254-
**isResponse**(`val`): val is Response
255-
256-
Checks whether the passed value is the `graphql-http` server agnostic response.
257-
258-
#### Parameters
259-
260-
| Name | Type |
261-
| :------ | :------ |
262-
| `val` | `unknown` |
263-
264-
#### Returns
265-
266-
val is Response
267-
268-
___
269-
270-
### makeResponse
271-
272-
**makeResponse**(`resultOrErrors`, `acceptedMediaType`, `formatError`): [`Response`](handler.md#response)
273-
274-
Creates an appropriate GraphQL over HTTP response following the provided arguments.
275-
276-
If the first argument is an `ExecutionResult`, the operation will be treated as "successful".
277-
278-
If the first argument is (an array of) `GraphQLError`, or an `ExecutionResult` without the `data` field, it will be treated
279-
the response will be constructed with the help of `acceptedMediaType` complying with the GraphQL over HTTP spec.
280-
281-
If the first argument is an `Error`, the operation will be treated as a bad request responding with `400: Bad Request` and the
282-
error will be present in the `ExecutionResult` style.
283-
284-
#### Parameters
285-
286-
| Name | Type |
287-
| :------ | :------ |
288-
| `resultOrErrors` | readonly `GraphQLError`[] \| `Readonly`<`ExecutionResult`<`ObjMap`<`unknown`\>, `ObjMap`<`unknown`\>\>\> \| `Readonly`<`GraphQLError`\> \| `Readonly`<`Error`\> |
289-
| `acceptedMediaType` | [`AcceptableMediaType`](handler.md#acceptablemediatype) |
290-
| `formatError` | [`FormatError`](handler.md#formaterror) |
291-
292-
#### Returns
293-
294-
[`Response`](handler.md#response)

src/handler.ts

+36-58
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,8 @@ export interface ResponseInit {
123123
*/
124124
export type Response = readonly [body: ResponseBody | null, init: ResponseInit];
125125

126-
/**
127-
* Checks whether the passed value is the `graphql-http` server agnostic response.
128-
*
129-
* @category Server
130-
*/
131-
export function isResponse(val: unknown): val is Response {
126+
/** Checks whether the passed value is the `graphql-http` server agnostic response. */
127+
function isResponse(val: unknown): val is Response {
132128
// TODO: make sure the contents of init match ResponseInit
133129
return (
134130
Array.isArray(val) &&
@@ -437,7 +433,37 @@ export function createHandler<
437433
];
438434
}
439435

440-
const acceptedMediaType = getAcceptableMediaType(getHeader(req, 'accept'));
436+
let acceptedMediaType: AcceptableMediaType | null = null;
437+
const accepts = (getHeader(req, 'accept') || '*/*')
438+
.replace(/\s/g, '')
439+
.toLowerCase()
440+
.split(',');
441+
for (const accept of accepts) {
442+
// accept-charset became obsolete, shouldnt be used (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Charset)
443+
// TODO: handle the weight parameter "q"
444+
const [mediaType, ...params] = accept.split(';');
445+
const charset =
446+
params?.find((param) => param.includes('charset=')) || 'charset=utf8'; // utf-8 is assumed when not specified;
447+
448+
if (
449+
mediaType === 'application/graphql-response+json' &&
450+
charset === 'charset=utf8'
451+
) {
452+
acceptedMediaType = 'application/graphql-response+json';
453+
break;
454+
}
455+
456+
// application/json should be the default until watershed
457+
if (
458+
(mediaType === 'application/json' ||
459+
mediaType === 'application/*' ||
460+
mediaType === '*/*') &&
461+
charset === 'charset=utf8'
462+
) {
463+
acceptedMediaType = 'application/json';
464+
break;
465+
}
466+
}
441467
if (!acceptedMediaType) {
442468
return [
443469
null,
@@ -670,57 +696,11 @@ export function createHandler<
670696
};
671697
}
672698

673-
/**
674-
* Request's Media-Type that the server accepts.
675-
*
676-
* @category Server
677-
*/
678-
export type AcceptableMediaType =
699+
/** Request's Media-Type that the server accepted. */
700+
type AcceptableMediaType =
679701
| 'application/graphql-response+json'
680702
| 'application/json';
681703

682-
/**
683-
* Inspects the request and detects the appropriate/acceptable Media-Type
684-
* looking at the `Accept` header while complying with the GraphQL over HTTP spec.
685-
*
686-
* @category Server
687-
*/
688-
export function getAcceptableMediaType(
689-
acceptHeader: string | null | undefined,
690-
): AcceptableMediaType | null {
691-
let acceptedMediaType: AcceptableMediaType | null = null;
692-
const accepts = (acceptHeader || '*/*')
693-
.replace(/\s/g, '')
694-
.toLowerCase()
695-
.split(',');
696-
for (const accept of accepts) {
697-
// accept-charset became obsolete, shouldnt be used (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Charset)
698-
// TODO: handle the weight parameter "q"
699-
const [mediaType, ...params] = accept.split(';');
700-
const charset =
701-
params?.find((param) => param.includes('charset=')) || 'charset=utf8'; // utf-8 is assumed when not specified;
702-
703-
if (
704-
mediaType === 'application/graphql-response+json' &&
705-
charset === 'charset=utf8'
706-
) {
707-
acceptedMediaType = 'application/graphql-response+json';
708-
break;
709-
}
710-
711-
if (
712-
(mediaType === 'application/json' ||
713-
mediaType === 'application/*' ||
714-
mediaType === '*/*') &&
715-
charset === 'charset=utf8'
716-
) {
717-
acceptedMediaType = 'application/json';
718-
break;
719-
}
720-
}
721-
return acceptedMediaType;
722-
}
723-
724704
/**
725705
* Creates an appropriate GraphQL over HTTP response following the provided arguments.
726706
*
@@ -731,10 +711,8 @@ export function getAcceptableMediaType(
731711
*
732712
* If the first argument is an `Error`, the operation will be treated as a bad request responding with `400: Bad Request` and the
733713
* error will be present in the `ExecutionResult` style.
734-
*
735-
* @category Server
736714
*/
737-
export function makeResponse(
715+
function makeResponse(
738716
resultOrErrors:
739717
| Readonly<ExecutionResult>
740718
| Readonly<GraphQLError[]>

0 commit comments

Comments
 (0)