Skip to content

Commit cb9dd8b

Browse files
committed
unexport makeResponse and getAcceptableMediaType
1 parent 5ef6d02 commit cb9dd8b

File tree

1 file changed

+34
-52
lines changed

1 file changed

+34
-52
lines changed

src/handler.ts

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,37 @@ export function createHandler<
437437
];
438438
}
439439

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

673-
/**
674-
* Request's Media-Type that the server accepts.
675-
*
676-
* @category Server
677-
*/
678-
export type AcceptableMediaType =
703+
/** Request's Media-Type that the server accepted. */
704+
type AcceptableMediaType =
679705
| 'application/graphql-response+json'
680706
| 'application/json';
681707

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-
724708
/**
725709
* Creates an appropriate GraphQL over HTTP response following the provided arguments.
726710
*
@@ -731,10 +715,8 @@ export function getAcceptableMediaType(
731715
*
732716
* If the first argument is an `Error`, the operation will be treated as a bad request responding with `400: Bad Request` and the
733717
* error will be present in the `ExecutionResult` style.
734-
*
735-
* @category Server
736718
*/
737-
export function makeResponse(
719+
function makeResponse(
738720
resultOrErrors:
739721
| Readonly<ExecutionResult>
740722
| Readonly<GraphQLError[]>

0 commit comments

Comments
 (0)