Last active
October 1, 2024 18:19
-
-
Save rofe/3424497182d8c0843ddf1212a3e460d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2024 Adobe. All rights reserved. | |
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. You may obtain a copy | |
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software distributed under | |
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | |
* OF ANY KIND, either express or implied. See the License for the specific language | |
* governing permissions and limitations under the License. | |
*/ | |
/* eslint-disable no-console */ | |
/** | |
* Returns the browser's runtime API. | |
* @private | |
* @returns {Object} The runtime | |
*/ | |
function getRuntime() { | |
const c = window.chrome || window.browser; | |
if (!c || !c.runtime) { | |
throw new Error('no browser runtime found'); | |
} | |
return c.runtime; | |
} | |
/** | |
* Sends a message to the sidekick extension | |
* @private | |
* @param {string} action The action name | |
* @param {Object} [options] The message options | |
* @param {function} [callback] The callback function | |
* @return {Promise<*>} The API response | |
*/ | |
async function msg(action, options = {}, callback = () => {}) { | |
getRuntime().sendMessage( | |
window.SIDEKICK_ID || 'igkmdomcgoebiipaifhmpfjhbjccggml', | |
{ | |
...options, | |
action, | |
}, | |
callback, | |
); | |
} | |
/** | |
* The Sidekick API | |
*/ | |
export default class SidekickAPI { | |
#cfg; | |
/** | |
* Creates a new instance of {@code SidekickAPI} | |
* @param {Object} [cfg] The configuration | |
* @param {string} [cfg.org] The organization | |
* @param {string} [cfg.site] The site | |
*/ | |
constructor(cfg) { | |
this.#cfg = cfg || {}; | |
} | |
/** | |
* Returns the names of the organizations the user is currently authenticated for. | |
* @returns {Promise<string[]>} The organizations | |
*/ | |
async getAuthInfo() { | |
try { | |
return new Promise((resolve) => { | |
msg('getAuthInfo', {}, (res) => resolve(res)); | |
}); | |
} catch (e) { | |
console.error(e); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment