Commit bce4c251 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate host-config module to TS

parent 9110ab0b
...@@ -5,17 +5,13 @@ import { ...@@ -5,17 +5,13 @@ import {
toObject, toObject,
toString, toString,
} from '../../shared/type-coercions'; } from '../../shared/type-coercions';
import type { ConfigFromAnnotator } from '../../types/config';
/** @typedef {import('../../types/config').ConfigFromAnnotator} ConfigFromAnnotator */
/** /**
* Return the app configuration specified by the frame embedding the Hypothesis * Return the app configuration specified by the frame embedding the Hypothesis
* client. * client.
*
* @param {Window} window
* @return {ConfigFromAnnotator}
*/ */
export function hostPageConfig(window) { export function hostPageConfig(window: Window): ConfigFromAnnotator {
const config = parseConfigFragment(window.location.href); const config = parseConfigFragment(window.location.href);
// Known configuration parameters which we will import from the host page. // Known configuration parameters which we will import from the host page.
...@@ -70,7 +66,7 @@ export function hostPageConfig(window) { ...@@ -70,7 +66,7 @@ export function hostPageConfig(window) {
// into the config and this ensures they safely work downstream even if they // into the config and this ensures they safely work downstream even if they
// are incorrect. // are incorrect.
// //
// Currently we are only handling the following config values do to the fact // Currently, we are only handling the following config values due to the fact
// that via3 will soon discontinue passing boolean types or integer types. // that via3 will soon discontinue passing boolean types or integer types.
// - requestConfigFromFrame // - requestConfigFromFrame
// - openSidebar // - openSidebar
...@@ -79,20 +75,18 @@ export function hostPageConfig(window) { ...@@ -79,20 +75,18 @@ export function hostPageConfig(window) {
// even validate all such config values. // even validate all such config values.
// See https://github.com/hypothesis/client/issues/1968 // See https://github.com/hypothesis/client/issues/1968
/** @type {Record<string, (value: unknown) => unknown>} */ const coercions: Record<string, (value: unknown) => unknown> = {
const coercions = {
openSidebar: toBoolean, openSidebar: toBoolean,
/** @param {unknown} value */
requestConfigFromFrame: value => { requestConfigFromFrame: value => {
if (typeof value === 'string') { if (typeof value === 'string') {
// Legacy `requestConfigFromFrame` value which holds only the origin. // Legacy `requestConfigFromFrame` value which holds only the origin.
return value; return value;
} }
const objectVal = const objectVal = toObject(value) as {
/** @type {{ origin: unknown, ancestorLevel: unknown }} */ ( origin: unknown;
toObject(value) ancestorLevel: unknown;
); };
return { return {
origin: toString(objectVal.origin), origin: toString(objectVal.origin),
ancestorLevel: toInteger(objectVal.ancestorLevel), ancestorLevel: toInteger(objectVal.ancestorLevel),
...@@ -100,9 +94,8 @@ export function hostPageConfig(window) { ...@@ -100,9 +94,8 @@ export function hostPageConfig(window) {
}, },
}; };
/** @type {Record<string, unknown>} */ const result: Record<string, unknown> = {};
const result = {}; for (const [key, value] of Object.entries(config)) {
for (let [key, value] of Object.entries(config)) {
if (!paramWhiteList.includes(key)) { if (!paramWhiteList.includes(key)) {
continue; continue;
} }
......
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