From ca3baeaf264b84e7b1bce659dd55947b821a02ee Mon Sep 17 00:00:00 2001 From: Nelson Martell Date: Wed, 8 Nov 2023 12:50:14 +0000 Subject: [PATCH] feat: allow JSON:API media type with params https://jsonapi.org/format/#media-type-parameters --- src/middlewares/json-api-express.ts | 2 +- src/middlewares/json-api-koa.ts | 2 +- src/middlewares/json-api-vercel.ts | 2 +- tests/test-suite/acceptance/httpStrictMode.test.ts | 12 ++++++++++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/middlewares/json-api-express.ts b/src/middlewares/json-api-express.ts index 01a9b89..ecb64dd 100644 --- a/src/middlewares/json-api-express.ts +++ b/src/middlewares/json-api-express.ts @@ -28,7 +28,7 @@ export default function jsonApiExpress( return next(); } - if (req.headers["content-type"] !== "application/vnd.api+json") { + if (!req.headers["content-type"] || !req.headers["content-type"].startsWith("application/vnd.api+json")) { res .status(400) .json(convertErrorToHttpResponse(jsonApiErrors.BadRequest("Content-Type must be application/vnd.api+json"))); diff --git a/src/middlewares/json-api-koa.ts b/src/middlewares/json-api-koa.ts index c0212b9..4a0ffbd 100644 --- a/src/middlewares/json-api-koa.ts +++ b/src/middlewares/json-api-koa.ts @@ -28,7 +28,7 @@ export default function jsonApiKoa( return next(); } - if (ctx.headers["content-type"] !== "application/vnd.api+json") { + if (!ctx.headers["content-type"] || !ctx.headers["content-type"].startsWith("application/vnd.api+json")) { ctx.status = 400; ctx.body = convertErrorToHttpResponse(jsonApiErrors.BadRequest("Content-Type must be application/vnd.api+json")); } diff --git a/src/middlewares/json-api-vercel.ts b/src/middlewares/json-api-vercel.ts index 77799bb..a380a91 100644 --- a/src/middlewares/json-api-vercel.ts +++ b/src/middlewares/json-api-vercel.ts @@ -25,7 +25,7 @@ const checkStrictMode = async ( return; } - if (req.headers["content-type"] !== "application/vnd.api+json") { + if (!req.headers["content-type"] || !req.headers["content-type"].startsWith("application/vnd.api+json")) { res.status(400); res.send(convertErrorToHttpResponse(jsonApiErrors.BadRequest("Content-Type must be application/vnd.api+json"))); } else { diff --git a/tests/test-suite/acceptance/httpStrictMode.test.ts b/tests/test-suite/acceptance/httpStrictMode.test.ts index eeba85a..a9a4856 100644 --- a/tests/test-suite/acceptance/httpStrictMode.test.ts +++ b/tests/test-suite/acceptance/httpStrictMode.test.ts @@ -22,6 +22,18 @@ describe.each(transportLayers)("Transport Layer: %s", (transportLayer) => { expect(result.body.data.attributes).toHaveProperty("randomNumber"); expect(result.body.data.attributes.randomNumber).toBeGreaterThan(0); }); + + it("Random endpoint with correct content-type with params", async () => { + // Add JSON:API media type with params (ext) + request.set("Content-Type", 'application/vnd.api+json; ext="https://jsonapi.org/ext/version"'); + const result = await request.get("/random/number"); + + expect(result.status).toEqual(200); + expect(result.body.data.attributes).toHaveProperty("randomNumber"); + expect(result.body.data.attributes.randomNumber).toBeGreaterThan(0); + }); + + // TODO: Check valid JSON:API media type params (https://jsonapi.org/format/#media-type-parameter-rules) }); }); });