-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #450 from live-codes/types
improve loading types
- Loading branch information
Showing
3 changed files
with
32 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
/* eslint-disable import/no-internal-modules */ | ||
import { isBare } from '../compiler/import-map'; | ||
import type { Types } from '../models'; | ||
import { allowedOrigin } from './allowed-origin'; | ||
|
||
export const typesService = { | ||
getTypeUrls: async (types: string[]) => { | ||
let fetchedTypes: Types = {}; | ||
if (types.length > 0 && allowedOrigin()) { | ||
try { | ||
const api = 'https://api.livecodes.io/types'; | ||
const res = await fetch(api, { | ||
method: 'POST', | ||
mode: 'cors', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ types }), | ||
}); | ||
fetchedTypes = await res.json(); | ||
} catch { | ||
// | ||
} | ||
} | ||
const fetchedTypes: Types = {}; | ||
await Promise.all( | ||
types.map(async (type) => { | ||
if (!isBare(type)) return; | ||
try { | ||
const mod = type.includes(':') ? type.split(':')[1] : type; | ||
const res = await fetch('https://esm.sh/' + mod); | ||
if (!res.ok) return; | ||
const typesUrl = res.headers.get('X-Typescript-Types'); | ||
if (!typesUrl) return; | ||
fetchedTypes[type] = typesUrl; | ||
} catch { | ||
// ignore | ||
} | ||
}), | ||
); | ||
return fetchedTypes; | ||
}, | ||
}; |