Skip to content

Commit

Permalink
🚸 compatible with the legacy importHTML api
Browse files Browse the repository at this point in the history
  • Loading branch information
kuitos committed Dec 3, 2019
1 parent 5f943d1 commit d799090
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export type Entry = string | { styles?: string[], scripts?: string[], html?: str

export function execScripts<T>(entry: string | null, scripts: string[], proxy: Window, opts?: ExecScriptsOpts): Promise<T>;

export default function importHTML(url: string, opts?: ImportEntryOpts): Promise<IImportResult>;
export default function importHTML(url: string, opts?: ImportEntryOpts | Function): Promise<IImportResult>;

export function importEntry(entry: Entry, opts?: ImportEntryOpts): Promise<IImportResult>;
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,19 @@ export function execScripts(entry, scripts, proxy = window, opts = {}) {
}

export default function importHTML(url, opts = {}) {
const { fetch = defaultFetch, getDomain = defaultGetDomain, getTemplate = defaultGetTemplate } = opts;
let fetch = defaultFetch;
let getDomain = defaultGetDomain;
let getTemplate = defaultGetTemplate;

// compatible with the legacy importHTML api
if (typeof opts === 'function') {
fetch = opts;
} else {
fetch = opts.fetch || defaultFetch;
getDomain = opts.getDomain || defaultGetDomain;
getTemplate = opts.getTemplate || defaultGetTemplate;
}

return embedHTMLCache[url] || (embedHTMLCache[url] = fetch(url)
.then(response => response.text())
.then(html => {
Expand Down

0 comments on commit d799090

Please sign in to comment.