Commit 1997770f authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate build-settings module to TS

parent 6ed0156a
import type {
ConfigFromHost,
ConfigFromAnnotator,
ConfigFromEmbedder,
ConfigFromSidebar,
RPCSettings,
SidebarSettings,
} from '../../types/config';
import * as postMessageJsonRpc from '../util/postmessage-json-rpc'; import * as postMessageJsonRpc from '../util/postmessage-json-rpc';
import { getApiUrl } from './get-api-url'; import { getApiUrl } from './get-api-url';
import { hostPageConfig } from './host-config'; import { hostPageConfig } from './host-config';
/**
* @typedef {import('../../types/config').ConfigFromHost} ConfigFromHost
* @typedef {import('../../types/config').ConfigFromAnnotator} ConfigFromAnnotator
* @typedef {import('../../types/config').ConfigFromEmbedder} ConfigFromEmbedder
* @typedef {import('../../types/config').ConfigFromSidebar} ConfigFromSidebar
* @typedef {import('../../types/config').RPCSettings} RPCSettings
* @typedef {import('../../types/config').SidebarSettings} SidebarSettings
* @typedef {import('../../types/config').Service['groups']} ServiceGroups
*/
/** /**
* Ascend `levels` from `window_` to find the designated embedder frame. * Ascend `levels` from `window_` to find the designated embedder frame.
* *
* @param {number} levels - Number of ancestors levels to ascend * @param levels - Number of ancestors levels to ascend
* @param {Window=} window_
* @return {Window}
*/ */
function getEmbedderFrame(levels, window_ = window) { function getEmbedderFrame(levels: number, window_: Window = window): Window {
let ancestorWindow = window_; let ancestorWindow = window_;
for (let i = 0; i < levels; i++) { for (let i = 0; i < levels; i++) {
if (ancestorWindow === ancestorWindow.top) { if (ancestorWindow === ancestorWindow.top) {
...@@ -40,14 +36,11 @@ function getEmbedderFrame(levels, window_ = window) { ...@@ -40,14 +36,11 @@ function getEmbedderFrame(levels, window_ = window) {
* This function (maybe) mutates values in the provided `configFromHost`'s * This function (maybe) mutates values in the provided `configFromHost`'s
* ServiceGroups: `$rpc:requestGroups` values are replaced with * ServiceGroups: `$rpc:requestGroups` values are replaced with
* `Promise<string[]>`. * `Promise<string[]>`.
*
* @see ServiceGroups
*
* @param {ConfigFromHost} configFromHost
* @param {RPCSettings} rpcSettings
* @return {ConfigFromHost}
*/ */
function fetchServiceGroups(configFromHost, rpcSettings) { function fetchServiceGroups(
configFromHost: ConfigFromHost,
rpcSettings: RPCSettings
): ConfigFromHost {
const services = configFromHost.services; const services = configFromHost.services;
if (!Array.isArray(services)) { if (!Array.isArray(services)) {
return configFromHost; return configFromHost;
...@@ -56,19 +49,17 @@ function fetchServiceGroups(configFromHost, rpcSettings) { ...@@ -56,19 +49,17 @@ function fetchServiceGroups(configFromHost, rpcSettings) {
if (service.groups === '$rpc:requestGroups') { if (service.groups === '$rpc:requestGroups') {
// The `groups` need to be fetched from a secondary RPC call and // The `groups` need to be fetched from a secondary RPC call and
// there should be no timeout as it may be waiting for user input. // there should be no timeout as it may be waiting for user input.
service.groups = /** @type {Promise<string[]>} */ ( service.groups = postMessageJsonRpc
postMessageJsonRpc .call<string[]>(
.call( rpcSettings.targetFrame,
rpcSettings.targetFrame, rpcSettings.origin,
rpcSettings.origin, 'requestGroups',
'requestGroups', [index],
[index], 0 // no timeout
0 // no timeout )
) .catch(() => {
.catch(() => { throw new Error('Unable to fetch groups');
throw new Error('Unable to fetch groups'); });
})
);
} }
}); });
return configFromHost; return configFromHost;
...@@ -76,12 +67,11 @@ function fetchServiceGroups(configFromHost, rpcSettings) { ...@@ -76,12 +67,11 @@ function fetchServiceGroups(configFromHost, rpcSettings) {
/** /**
* Derive RPC settings from the provided `ConfigFromAnnotator`, if any are present. * Derive RPC settings from the provided `ConfigFromAnnotator`, if any are present.
*
* @param {ConfigFromAnnotator} configFromAnnotator
* @param {Window} window_
* @return {import('../../types/config').RPCSettings|null}
*/ */
function buildRPCSettings(configFromAnnotator, window_) { function buildRPCSettings(
configFromAnnotator: ConfigFromAnnotator,
window_: Window
): RPCSettings | null {
const rpcConfig = configFromAnnotator.requestConfigFromFrame; const rpcConfig = configFromAnnotator.requestConfigFromFrame;
if (!rpcConfig) { if (!rpcConfig) {
return null; return null;
...@@ -101,20 +91,17 @@ function buildRPCSettings(configFromAnnotator, window_) { ...@@ -101,20 +91,17 @@ function buildRPCSettings(configFromAnnotator, window_) {
/** /**
* Retrieve host configuration from embedder frame * Retrieve host configuration from embedder frame
*
* @param {ConfigFromAnnotator} configFromAnnotator
* @param {RPCSettings} rpcSettings
* @return {Promise<ConfigFromEmbedder>}
*/ */
async function getEmbedderConfig(configFromAnnotator, rpcSettings) { async function getEmbedderConfig(
const configFromEmbedder = /** @type {ConfigFromEmbedder} */ ( configFromAnnotator: ConfigFromAnnotator,
await postMessageJsonRpc.call( rpcSettings: RPCSettings
rpcSettings.targetFrame, ): Promise<ConfigFromEmbedder> {
rpcSettings.origin, const configFromEmbedder = await postMessageJsonRpc.call<ConfigFromEmbedder>(
'requestConfig', rpcSettings.targetFrame,
[], rpcSettings.origin,
3000 'requestConfig',
) [],
3000
); );
// In cases where host configuration is requested from the embedder frame // In cases where host configuration is requested from the embedder frame
...@@ -138,11 +125,12 @@ async function getEmbedderConfig(configFromAnnotator, rpcSettings) { ...@@ -138,11 +125,12 @@ async function getEmbedderConfig(configFromAnnotator, rpcSettings) {
* @see {ConfigFromEmbedder} * @see {ConfigFromEmbedder}
* @see {ConfigFromHost} * @see {ConfigFromHost}
* *
* @param {ConfigFromSidebar} configFromSidebar * @return The merged settings
* @param {Window} window_ - Test seam
* @return {Promise<SidebarSettings>} - The merged settings
*/ */
export async function buildSettings(configFromSidebar, window_ = window) { export async function buildSettings(
configFromSidebar: ConfigFromSidebar,
window_: Window = window
): Promise<SidebarSettings> {
const configFromAnnotator = hostPageConfig(window); const configFromAnnotator = hostPageConfig(window);
const rpcSettings = buildRPCSettings(configFromAnnotator, window_); const rpcSettings = buildRPCSettings(configFromAnnotator, window_);
...@@ -160,8 +148,7 @@ export async function buildSettings(configFromSidebar, window_ = window) { ...@@ -160,8 +148,7 @@ export async function buildSettings(configFromSidebar, window_ = window) {
configFromHost = configFromAnnotator; configFromHost = configFromAnnotator;
} }
/** @type {SidebarSettings} */ const sidebarSettings: SidebarSettings = {
const sidebarSettings = {
...configFromSidebar, ...configFromSidebar,
...configFromHost, ...configFromHost,
}; };
......
...@@ -15,13 +15,14 @@ function createTimeout(delay, message) { ...@@ -15,13 +15,14 @@ function createTimeout(delay, message) {
/** /**
* Make a JSON-RPC call to a server in another frame using `postMessage`. * Make a JSON-RPC call to a server in another frame using `postMessage`.
* *
* @template T
* @param {Window} frame - Frame to send call to * @param {Window} frame - Frame to send call to
* @param {string} origin - Origin filter for `window.postMessage` call * @param {string} origin - Origin filter for `window.postMessage` call
* @param {string} method - Name of the JSON-RPC method * @param {string} method - Name of the JSON-RPC method
* @param {unknown[]} params - Parameters of the JSON-RPC method * @param {unknown[]} params - Parameters of the JSON-RPC method
* @param {number} [timeout] - Maximum time to wait in ms * @param {number} [timeout] - Maximum time to wait in ms
* @param {Window} [window_] - Test seam. * @param {Window} [window_] - Test seam.
* @return {Promise<unknown>} - A Promise for the response to the call * @return {Promise<T>} - A Promise for the response to the call
*/ */
export function call( export function call(
frame, frame,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment