Skip to content

Commit

Permalink
Merge pull request #371 from nelson6e65/fix/strict-mode
Browse files Browse the repository at this point in the history
feat: allow JSON:API media type with params
  • Loading branch information
joelalejandro authored Dec 12, 2023
2 parents e1dd5c6 + ca3baea commit 4214634
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/middlewares/json-api-express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/json-api-koa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/json-api-vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions tests/test-suite/acceptance/httpStrictMode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
});
});

0 comments on commit 4214634

Please sign in to comment.