From 672f41f64619169d5ef821d33fbb70772a216816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20D?= <15271+edas@users.noreply.github.com> Date: Sun, 17 Feb 2019 00:35:17 +0100 Subject: [PATCH] feat: Inject cozy-client - stage 11 - cozyFetchJSON simple wrapper around internal `fetchJSON()` maintaining compatibility with the old cozy-client-js --- src/lib/stack-client.js | 28 ++++++++++- .../stack-client.cozyfetchjson.spec.js | 49 +++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 test/lib/stack-client/stack-client.cozyfetchjson.spec.js diff --git a/src/lib/stack-client.js b/src/lib/stack-client.js index cbe7ab576..890d5b852 100644 --- a/src/lib/stack-client.js +++ b/src/lib/stack-client.js @@ -97,7 +97,7 @@ const updateAccessToken = function(token) { * * @function * @private - * @returns {Promise} the full raw JSON playload + * @returns {Promise} the full raw JSON payload */ const fetchJSON = function(method, path, body, options={}) { @@ -302,6 +302,29 @@ const getContext = function() { return fetchJSON('GET', '/settings/context') } +/** + * Fetch a resource on the cozy stack + * with a prototype compatible with the legacy cozy-client-js + * + * @function + * @param {object} cozy - cozy-client-js + * @param {string} method - HTTP method + * @param {string} path + * @param {object} body + * @returns {Promise} + */ +const cozyFetchJSON = function(cozy, method, path, body) { + return fetchJSON(method, path, body).then( + json => { + const responseData = Object.assign({}, json.data) + if (responseData.id) { + responseData._id = responseData.id + } + return responseData + } + ) +} + /** * Initializes the functions to call the cozy stack * @@ -333,7 +356,8 @@ export default { iconProps: getAppIconProps, cozyURL: getCozyURLOrigin }, - updateAccessToken, + updateAccessToken, + cozyFetchJSON, logout, init  } \ No newline at end of file diff --git a/test/lib/stack-client/stack-client.cozyfetchjson.spec.js b/test/lib/stack-client/stack-client.cozyfetchjson.spec.js new file mode 100644 index 000000000..ba81c2f9d --- /dev/null +++ b/test/lib/stack-client/stack-client.cozyfetchjson.spec.js @@ -0,0 +1,49 @@ +import stack from 'lib/stack-client' + +describe("stack client", () => { + + describe("cozyFetchJSON", () => { + + let json = jest.fn().mockReturnValue({ + data: { id: "myid" } + }) + let fetch = jest.fn().mockResolvedValue({ + status:200, + headers: { + get: (header) => 'application/json' + }, + json + }) + let cozyClient = { + getStackClient: () => { + return { + token: { token: "mytoken"}, + uri: "https://test.mycozy.cloud", + fetch + } + } + } + let params = { + cozyClient, + onCreateApp: function() {}, + onDeleteApp: function() {}, + } + + beforeAll(async () => { + await stack.init(params) + }) + + it("should transform `id` in `_id`", async () => { + const data = await stack.cozyFetchJSON('_', 'GET', '/path') + expect( data._id ).toBe('myid') + }) + + it("should call fetch from cozy-client", async () => { + fetch.mockClear() + const data = await stack.cozyFetchJSON('_', 'GET', '/path') + expect( fetch ).toHaveBeenCalled() + }) + + }) + +}) \ No newline at end of file