Skip to content

Commit f13d8ab

Browse files
committed
Add jsconfig setup for typescript checking.
1 parent 40b7569 commit f13d8ab

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

index.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ customElements.define(
44
connectedCallback() {
55
let shadow = this.attachShadow({ mode: "closed" });
66
const urlAttr = this.getAttribute("url");
7-
let urlToEmbed;
87
if (urlAttr) {
98
renderOembed(shadow, urlAttr, {
109
maxwidth: this.getAttribute("maxwidth"),
@@ -13,30 +12,36 @@ customElements.define(
1312
} else {
1413
const discoverUrl = this.getAttribute("discover-url");
1514
if (discoverUrl) {
16-
const discoveredUrl = getDiscoverUrl(discoverUrl, function(
17-
discoveredUrl
18-
) {
19-
renderOembed(shadow, discoveredUrl);
15+
getDiscoverUrl(discoverUrl, function(discoveredUrl) {
16+
renderOembed(shadow, discoveredUrl, null);
2017
});
2118
}
2219
}
2320
}
2421
}
2522
);
2623

24+
/**
25+
*
26+
* @param {ShadowRoot} shadow
27+
* @param {string} urlToEmbed
28+
* @param {{maxwidth: string?; maxheight: string?}?} options
29+
*/
2730
function renderOembed(shadow, urlToEmbed, options) {
28-
let apiUrl = new URL(`https://cors-anywhere.herokuapp.com/${urlToEmbed}`);
31+
let apiUrlBuilder = new URL(
32+
`https://cors-anywhere.herokuapp.com/${urlToEmbed}`
33+
);
2934
if (options && options.maxwidth) {
30-
apiUrl.searchParams.set("maxwidth", options.maxwidth);
35+
apiUrlBuilder.searchParams.set("maxwidth", options.maxwidth);
3136
}
3237
if (options && options.maxheight) {
33-
apiUrl.searchParams.set("maxheight", options.maxheight);
38+
apiUrlBuilder.searchParams.set("maxheight", options.maxheight);
3439
}
35-
apiUrl = apiUrl.toString();
36-
httpGetAsync(apiUrl, rawResponse => {
37-
response = JSON.parse(rawResponse);
40+
const apiUrl = apiUrlBuilder.toString();
41+
httpGetAsync(apiUrl, (/** @type {Object} */ rawResponse) => {
42+
const response = JSON.parse(rawResponse);
3843

39-
let iframe;
44+
/** @type {HTMLIFrameElement} */ let iframe;
4045
switch (response.type) {
4146
case "rich":
4247
iframe = createIframe(response);
@@ -101,19 +106,25 @@ function renderOembed(shadow, urlToEmbed, options) {
101106
});
102107
}
103108

109+
/**
110+
* @param {{ height: number?; width: number?; html: string; }} response
111+
*/
104112
function createIframe(response) {
105113
let iframe = document.createElement("iframe");
106114
iframe.setAttribute("border", "0");
107115
iframe.setAttribute("frameborder", "0");
108-
iframe.setAttribute("height", (response.height || 500) + 20);
109-
iframe.setAttribute("width", (response.width || 500) + 20);
116+
iframe.setAttribute("height", ((response.height || 500) + 20).toString());
117+
iframe.setAttribute("width", ((response.width || 500) + 20).toString());
110118
iframe.srcdoc = response.html;
111119
return iframe;
112120
}
113121

122+
/**
123+
* @param {string} url
124+
* @param {{ (discoveredUrl: string): void;}} callback
125+
*/
114126
function getDiscoverUrl(url, callback) {
115-
let apiUrl = new URL(`https://cors-anywhere.herokuapp.com/${url}`);
116-
apiUrl = apiUrl.toString();
127+
let apiUrl = new URL(`https://cors-anywhere.herokuapp.com/${url}`).toString();
117128
httpGetAsync(apiUrl, function(response) {
118129
let dom = document.createElement("html");
119130
dom.innerHTML = response;
@@ -123,6 +134,10 @@ function getDiscoverUrl(url, callback) {
123134
});
124135
}
125136

137+
/**
138+
* @param {string} theUrl
139+
* @param {{ (rawResponse: string): void }} callback
140+
*/
126141
function httpGetAsync(theUrl, callback) {
127142
var xmlHttp = new XMLHttpRequest();
128143
xmlHttp.onreadystatechange = function() {

jsconfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"noImplicitAny": true,
5+
"removeComments": true,
6+
"strictNullChecks": true,
7+
"sourceMap": false,
8+
"resolveJsonModule": true,
9+
"lib": ["es5", "es2015.promise", "dom", "es2015"],
10+
"checkJs": true
11+
}
12+
}

0 commit comments

Comments
 (0)