From f06742fcd2f95623b0a7ae1807d986db68afe934 Mon Sep 17 00:00:00 2001 From: Arnau Sanchez Date: Fri, 9 Apr 2021 14:15:31 +0200 Subject: [PATCH 1/2] [data store] validate response of update operations --- package.json | 2 +- src/api/dataStore.ts | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 64a352b..71f5b13 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@eyeseetea/d2-api", "description": "Typed wrapper over DHIS2 API", - "version": "1.8.0", + "version": "1.8.0-beta.3", "license": "GPL-3.0", "author": "EyeSeeTea team", "repository": { diff --git a/src/api/dataStore.ts b/src/api/dataStore.ts index caa3aaf..24cba6a 100644 --- a/src/api/dataStore.ts +++ b/src/api/dataStore.ts @@ -1,6 +1,9 @@ -import { D2ApiResponse } from "./common"; +import { D2ApiResponse, HttpResponse } from "./common"; +import { HttpResponse as HttpClientResponse } from "../repositories/HttpClientRepository"; import { D2ApiGeneric } from "./d2Api"; +type UpdateResponse = HttpResponse; + export class DataStore { private endpoint: string; @@ -49,16 +52,20 @@ export class DataStore { const config = { url: `/${this.endpoint}/${namespace}/${key}`, data: value }; return d2Api - .request({ + .request({ method: "put", ...config, validateStatus: validate404, }) + .map(validateResponse) .flatMap(response => { if (response.status === 404) { - return d2Api.request({ method: "post", ...config }); + return d2Api + .request({ method: "post", ...config }) + .map(validateResponse); } else { - return D2ApiResponse.build({ response: Promise.resolve(response) }); + const voidResponse = { ...response, data: undefined }; + return D2ApiResponse.build({ response: Promise.resolve(voidResponse) }); } }); } @@ -67,12 +74,12 @@ export class DataStore { const { d2Api, namespace } = this; return d2Api - .request({ + .request({ method: "delete", url: `/${this.endpoint}/${namespace}/${key}`, validateStatus: validate404, }) - .map(response => (response.status === 404 ? false : true)); + .map(response => (response.status === 404 ? false : response.data.status === "OK")); } } @@ -80,6 +87,15 @@ function validate404(status: number): boolean { return (status >= 200 && status < 300) || status === 404; } +function validateResponse(response: HttpClientResponse>): undefined { + const { data } = response; + if (data.status === "OK") { + return; + } else { + throw new Error(data.message || "Invalid response from server"); + } +} + export type DataStoreType = "global" | "user"; export interface DataStoreKeyMetadata { From b0484dd07102a8f6e67dc032bfc27654a97921a6 Mon Sep 17 00:00:00 2001 From: Arnau Sanchez Date: Fri, 9 Apr 2021 15:45:13 +0200 Subject: [PATCH 2/2] Check response status 200 on dataStore --- package.json | 2 +- src/api/dataStore.ts | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 71f5b13..9daf6fb 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@eyeseetea/d2-api", "description": "Typed wrapper over DHIS2 API", - "version": "1.8.0-beta.3", + "version": "1.8.0-beta.4", "license": "GPL-3.0", "author": "EyeSeeTea team", "repository": { diff --git a/src/api/dataStore.ts b/src/api/dataStore.ts index 24cba6a..f05de19 100644 --- a/src/api/dataStore.ts +++ b/src/api/dataStore.ts @@ -57,14 +57,13 @@ export class DataStore { ...config, validateStatus: validate404, }) - .map(validateResponse) .flatMap(response => { if (response.status === 404) { return d2Api .request({ method: "post", ...config }) .map(validateResponse); } else { - const voidResponse = { ...response, data: undefined }; + const voidResponse = { ...response, data: validateResponse(response) }; return D2ApiResponse.build({ response: Promise.resolve(voidResponse) }); } }); @@ -89,7 +88,7 @@ function validate404(status: number): boolean { function validateResponse(response: HttpClientResponse>): undefined { const { data } = response; - if (data.status === "OK") { + if (response.status === 200 && data.status === "OK") { return; } else { throw new Error(data.message || "Invalid response from server");