Skip to content

Commit

Permalink
fix(jsonapi): add type and id in create and update payload if applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
chfoidl committed Jun 5, 2023
1 parent 5b6629f commit 6928e40
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-pianos-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@drupal-kit/jsonapi": patch
---

Add type and id in create and update payload if applicable
12 changes: 12 additions & 0 deletions packages/jsonapi/src/DrupalkitJsonApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ export const DrupalkitJsonApi = (

const url = buildJsonApiUrl(path, options);

// Set the type if not already set.
if (!parameters.payload.type) {
parameters.payload.type = type;
}

const result = await drupalkit.request<Response<TResourceObject>>(url, {
method: "POST",
headers: defaultHeaders,
Expand Down Expand Up @@ -222,6 +227,13 @@ export const DrupalkitJsonApi = (

const url = buildJsonApiUrl(path, options);

parameters.payload.id = parameters.uuid;

// Set the type if not already set.
if (!parameters.payload.type) {
parameters.payload.type = type;
}

const result = await drupalkit.request<Response<TResourceObject>>(url, {
method: "PATCH",
headers: defaultHeaders,
Expand Down
1 change: 1 addition & 0 deletions packages/jsonapi/src/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ type ResourceCreatePayload<
* Extract the update payload type from a resource object.
*/
type ResourceUpdatePayload<R extends JsonApiResource> = object & {
id?: R["id"];
type?: R["type"];
attributes?: Partial<R["attributes"]>;
relationships?: Partial<R["relationships"]>;
Expand Down
29 changes: 20 additions & 9 deletions packages/jsonapi/tests/resources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,18 +360,22 @@ test.serial("Handle network error", async (t) => {
test.serial("Create new resource", async (t) => {
const drupalkit = createDrupalkit();

t.plan(3);

server.use(
rest.post("*/jsonapi/node/article", (_req, res, ctx) =>
res(
rest.post("*/jsonapi/node/article", async (req, res, ctx) => {
const payload = await req.json();
t.is(payload.type, "node--article");

return res(
ctx.set("Content-Type", "application/vnd.api+json"),
ctx.json(JsonApiArticleDetail),
),
),
);
}),
);

const result = await drupalkit.jsonApi.resource("node--article", "create", {
payload: {
type: "node--article",
attributes: {
title: "New Article",
},
Expand Down Expand Up @@ -435,13 +439,20 @@ test.serial("Update resource", async (t) => {
const drupalkit = createDrupalkit();
const uuid = "5f5f5f5f-5f5f-5f5f-5f5f-5f5f5f5f5f5f";

t.plan(4);

server.use(
rest.patch("*/jsonapi/node/article/" + uuid, (_req, res, ctx) =>
res(
rest.patch("*/jsonapi/node/article/" + uuid, async (req, res, ctx) => {
const payload = await req.json();

t.is(payload.type, "node--article");
t.is(payload.id, uuid);

return res(
ctx.set("Content-Type", "application/vnd.api+json"),
ctx.json(JsonApiArticleDetail),
),
),
);
}),
);

const result = await drupalkit.jsonApi.resource("node--article", "update", {
Expand Down

0 comments on commit 6928e40

Please sign in to comment.