motidata
Version:
Data retrieval library for services (e.g. App) accessing MotiMate's main Api
32 lines • 1.1 kB
JavaScript
/**
* Collection of {@link SimpleResponseDTO} Helpers
*/
export const SimpleResponseHelpers = {
/**
* Due to limitations of NextJs Server Actions, [this can not be a class](https://react.dev/reference/rsc/use-server#serializable-parameters-and-return-values).
* @throws any `Response.json()` related error
*/
async transformToSimpleResponse(fetchResponse) {
return {
ok: fetchResponse.ok,
statusCode: fetchResponse.status,
data: (await this._extractDataBasedOnContentType(fetchResponse)),
};
},
/**
* @private
*/
async _extractDataBasedOnContentType(fetchResponse) {
const CONTENT_TYPE = fetchResponse.headers.get("Content-Type");
if (CONTENT_TYPE === null) {
return undefined;
}
else if (CONTENT_TYPE.includes("application/json")) {
return await fetchResponse.json();
}
else if (CONTENT_TYPE.includes("text/plain")) {
return await fetchResponse.text();
}
},
};
//# sourceMappingURL=SimpleResponseDTO.js.map