From ae7efd3e48f49203c3538d7e5cd8d0068e10a132 Mon Sep 17 00:00:00 2001 From: Karel Hala <khala@redhat.com> Date: Sun, 5 Apr 2020 19:28:48 +0200 Subject: [PATCH] Add JSdoc to custom functions --- src/react-auth0-spa.js | 11 ++++++++--- src/utils/api.js | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/react-auth0-spa.js b/src/react-auth0-spa.js index 7db3540..e6c8786 100644 --- a/src/react-auth0-spa.js +++ b/src/react-auth0-spa.js @@ -65,6 +65,13 @@ export const Auth0Provider = ({ children, onRedirectCallback = DEFAULT_REDIRECT_ setUser(user); }; + /** + * Update user data in Auth0 application. + * @param {*} userData data to be updated. + * @example + * updateProfile({ user_metadata: { name: "something" } }) // updates user metadata + * @see https://auth0.com/docs/api/management/v2#!/Users/patch_users_by_id + */ const updateProfile = async (userData) => { if (isAuthenticated) { const [{ _id, ...body }] = await Api.get("/rest/auth"); @@ -75,15 +82,13 @@ export const Auth0Provider = ({ children, onRedirectCallback = DEFAULT_REDIRECT_ body: JSON.stringify(body) }) ).json(); - const rawUserData = await ( + return ( await fetch(`https://${domain}/api/v2/users/${user.sub}`, { method: "PATCH", headers: { Authorization: `Bearer ${access_token}`, "Content-Type": "application/json" }, body: JSON.stringify(userData) }) ).json(); - setUser(rawUserData); - return rawUserData; } }; diff --git a/src/utils/api.js b/src/utils/api.js index 1ee8a7c..c15144a 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -4,6 +4,15 @@ import get from "lodash/get"; import config from "../auth_config.json"; import keys from "../api_keys.json"; +/** + * Update or add data at given path in custom data. + * @param {string} path what should be updated, requires at least team.dataset. + * @param {object} payload data payload that'll be saved on path. + * @param {add|replace} method either add or replace. + * @example: + * API.updateData("professors.example", {some: "value"}) // merging with previous data + * API.updateData("professors.example", {different: "value"}, "replace") // rewriting data + */ const updateData = (api) => (path, payload, method = "add") => { if (!path) { throw new Error("Error, path must be specified when calling updateData endpoint"); @@ -30,8 +39,12 @@ const updateData = (api) => (path, payload, method = "add") => { }); }; +/** + * Retrieve custom data for specific team. + * @param {string} teamName team to read data from. + */ const getCustomData = (api) => (teamName = "professors") => { - return api.get(`/rest/customdata?q={ "team": "${teamName}" }`); + return api.get(`/rest/customdata?q={"team": "${teamName}"}`); }; const instances = Object.entries(keys).reduce( -- GitLab