From db7af63c2df7997b25589a821c51a6967822de5a Mon Sep 17 00:00:00 2001 From: Matheus Paiva Date: Sun, 2 Dec 2018 22:40:03 +0000 Subject: [PATCH] feat: Add service to connect to web api --- package-lock.json | 10 ++++++++++ package.json | 4 ++++ src/rosetta-service.js | 20 ++++++++++++++++++++ src/utils/is-plain-object.js | 3 +++ src/utils/nestify.js | 24 ++++++++++++++++++++++++ 5 files changed, 61 insertions(+) create mode 100644 src/rosetta-service.js create mode 100644 src/utils/is-plain-object.js create mode 100644 src/utils/nestify.js diff --git a/package-lock.json b/package-lock.json index 2b4b6a3..cc0748e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2332,6 +2332,11 @@ "integrity": "sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA==", "dev": true }, + "ky": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/ky/-/ky-0.5.1.tgz", + "integrity": "sha512-1ZGATX+y8kQIMg8jzTQFboqN6XUSdYTtJbVK8h8mXy1HTp04iq78plsDoKkHPnAb1lnaN5P7TDZ95aMB+Mr1zA==" + }, "lcid": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz", @@ -4369,6 +4374,11 @@ } } }, + "whatwg-fetch": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz", + "integrity": "sha512-9GSJUgz1D4MfyKU7KRqwOjXCXTqWdFNvEr7eUBYchQiVc744mqK/MzXPNR2WsPkmkOa4ywfg8C2n8h+13Bey1Q==" + }, "which": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", diff --git a/package.json b/package.json index e1c00cb..3d4df5b 100644 --- a/package.json +++ b/package.json @@ -32,5 +32,9 @@ "onchange": "^5.1.3", "webpack": "^4.26.1", "webpack-cli": "^3.1.2" + }, + "dependencies": { + "ky": "^0.5.1", + "whatwg-fetch": "^3.0.0" } } diff --git a/src/rosetta-service.js b/src/rosetta-service.js new file mode 100644 index 0000000..8e07019 --- /dev/null +++ b/src/rosetta-service.js @@ -0,0 +1,20 @@ +// This is a polyfill to use window.fetch on older browsers +import 'whatwg-fetch'; +import ky from 'ky'; +import nestify from './utils/nestify'; + +const rosettaService = (id = '', { prefixUrl = '' }) => { + const api = ky.extend({ prefixUrl }); + + return { + getNames: (culture = [], names = []) => api.get(`${id}/names`, { + searchParams: { + names: names.toString(), + culture: culture.toString(), + }, + }).then(data => nestify(data)), + getCultures: () => api.get(`${id}/cultures`), + }; +}; + +module.exports = rosettaService; diff --git a/src/utils/is-plain-object.js b/src/utils/is-plain-object.js new file mode 100644 index 0000000..feff41d --- /dev/null +++ b/src/utils/is-plain-object.js @@ -0,0 +1,3 @@ +export default t => !!t + && (typeof t === 'object') + && Object.prototype.toString.call(t) === '[object Object]'; diff --git a/src/utils/nestify.js b/src/utils/nestify.js new file mode 100644 index 0000000..0b410b8 --- /dev/null +++ b/src/utils/nestify.js @@ -0,0 +1,24 @@ +import isObject from './is-plain-object'; + +const nestify = (obj, context = {}) => Object.keys(obj) + .reduce((baseObj, key) => { + const [thisKey, ...otherKeys] = key.split('.'); + const keysToBeNested = otherKeys.join('.'); + const originalValue = obj[key]; + const shouldReturnSame = !keysToBeNested && !isObject(originalValue); + + const objToNest = keysToBeNested + ? { [keysToBeNested]: originalValue } + : originalValue; + + const nestifiedObj = shouldReturnSame + ? { [key]: originalValue } + : { [thisKey]: nestify(objToNest, baseObj[thisKey]) }; + + return { + ...baseObj, + ...nestifiedObj, + }; + }, context); + +export default nestify;