diff --git a/src/model.ts b/src/model.ts index a36cca9..635e895 100644 --- a/src/model.ts +++ b/src/model.ts @@ -988,7 +988,7 @@ export class SpraypaintBase { throw err } - if (response.status === 202) { + if (response.status === 202 || response.status === 204) { return await this._handleAcceptedResponse(response, this.onDeferredUpdate) } diff --git a/src/request.ts b/src/request.ts index cc35599..907e34e 100644 --- a/src/request.ts +++ b/src/request.ts @@ -118,7 +118,7 @@ export class Request { response: Response, requestOptions: RequestInit ) { - let wasDelete = + const wasDelete = requestOptions.method === "DELETE" && [204, 200].indexOf(response.status) > -1 if (wasDelete) return @@ -127,7 +127,8 @@ export class Request { try { json = await response.clone().json() } catch (e) { - if (response.status === 202) return + const isEmptyResponse = [202, 204].indexOf(response.status) > -1 + if (isEmptyResponse) return throw new ResponseError(response, "invalid json", e) } diff --git a/test/integration/persistence.test.ts b/test/integration/persistence.test.ts index 8257f94..8985142 100644 --- a/test/integration/persistence.test.ts +++ b/test/integration/persistence.test.ts @@ -322,6 +322,34 @@ describe("Model persistence", () => { expect(job.klass).to.eq(BackgroundJob) }) }) + + describe("when the server returns 204 (no content)", () => { + const lastName = "Richards" + beforeEach(() => { + fetchMock.restore() + + fetchMock.mock("http://example.com/api/v1/people", { + status: 204, + body: null + }) + }) + + afterEach(resetMocks) + + it("doesn't blow up when posting", async () => { + instance.isPersisted = false + instance.lastName = lastName + await instance.save() + expect(instance.lastName).to.eq(lastName) + }) + + it("doesn't blow up when patching", async () => { + instance.isPersisted = true + instance.lastName = lastName + await instance.save() + expect(instance.lastName).to.eq(lastName) + }) + }) }) describe("#destroy", () => { @@ -347,15 +375,12 @@ describe("Model persistence", () => { beforeEach(() => { fetchMock.restore() - fetchMock.mock({ - matcher: "http://example.com/api/v1/people/1", - response: new Response({ status: 204 } as any) + fetchMock.mock("http://example.com/api/v1/people/1", { + status: 204 }) }) - afterEach(() => { - resetMocks() - }) + afterEach(resetMocks) it("does not blow up", async () => { expect(instance.isPersisted).to.eq(true) diff --git a/test/unit/associations.test.ts b/test/unit/associations.test.ts index 6603d06..a95c0e5 100644 --- a/test/unit/associations.test.ts +++ b/test/unit/associations.test.ts @@ -55,7 +55,6 @@ describe("Associations", () => { const jsonapiTypeAssoc = assoc({ type: "type_strings" }) expect(jsonapiTypeAssoc.jsonapiType).to.eq("type_strings") - it }) it("accepts a bare spraypaint type string", () => {