Last active
February 11, 2025 16:10
-
-
Save rofe/3424497182d8c0843ddf1212a3e460d6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 2025 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 */ | |
/** | |
* The AEM Sidekick Client | |
*/ | |
export default class AEMSidekickClient { | |
#id; | |
/** | |
* Sends a message to the sidekick extension | |
* @param {string} action The action name | |
* @param {Object} [options] The message options | |
* @param {Function} [callback] The callback function | |
* @return {Promise<*>} The API response | |
*/ | |
async #sendMessage(action, options = {}, callback = () => { }) { | |
chrome.runtime.sendMessage( | |
// @ts-ignore | |
this.#id, | |
{ | |
...options, | |
action, | |
}, | |
// @ts-ignore | |
callback, | |
); | |
} | |
/** | |
* Creates a new instance of {@code SidekickAPI} | |
* @param {string} [id] The extension ID | |
*/ | |
constructor(id) { | |
this.#id = id || 'igkmdomcgoebiipaifhmpfjhbjccggml'; | |
} | |
/** | |
* Returns the names of the organizations the user is currently authenticated for. | |
* @returns {Promise<string[]>} The organizations | |
*/ | |
async getAuthInfo() { | |
try { | |
return new Promise((resolve) => { | |
this.#sendMessage('getAuthInfo', {}, (res) => resolve(res)); | |
}); | |
} catch (e) { | |
console.error(e); | |
} | |
return null; | |
} | |
/** | |
* Returns the configured sites. | |
* @returns {Promise<Object[]>} The organizations | |
*/ | |
async getSites() { | |
try { | |
return new Promise((resolve) => { | |
this.#sendMessage('getSites', {}, (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