@d2api/manifest-web
v3.2.1
Published
manages the d2 manifest in browsers, using IndexedDB
Downloads
99
Readme
@d2api/manifest-web
a simple singleton with functions to download, ✨CACHE✨, and access the d2 manifest, with proper component types
this package downloads the manifest and provides functions to retrieve its data. it caches the fetched tables in IndexedDB, for faster loads later.
these functions will return accurately typed entries from bungie-api-ts.
so your tsc
or IDE of choice should know that a DestinyInventoryItem
has an itemCategoryHashes
property, and it's an array of numbers.
2.0.0 update: most functions are renamed for clarity and to clash less with api calls
2.2.0 update: def function return types were modified, to more correctly refer to valid bungie-api-ts paths
3.0.0 update: bundling cjs/esm separately in hopes of better compatibility. say hi if there are any problems.
3.2.0 update: fixed some issues with bundling and peerDeps causing inappropriate "manifest not loaded" error.loadDefs({ fallBackToCache: true })
now allows using stored local defs if bungie servers can't be contacted.
// [...] setup here
const myItem = getInventoryItemDef(460688465);
myItem.icon;
// TypeScript error: Property 'icon' does not exist on type 'DestinyInventoryItemDefinition'.ts(2339)
functions/exports
setup
setApiKey('apikey')
you should add an api key or you are really likely to have CORS problems.
setLanguage('en')
sets the language of the manifest to download. doing this alone doesn't trigger a fresh download, you need to loadDefs()
afterward.
verbose()
run this if you love console logs. run empty to turn on verbosity, or with a boolean for on/off.
includeTables(['InventoryItem', 'Activity'])
specify which definitions tables should be downloaded and available. you can leave this empty to fetch everything, but do you really need several MB worth of UnlockValue
s?
see loadDefs
below for an extra note.
excludeTables(['UnlockValues', 'InventoryItemLite'])
if you realllly want everything, you can specify which definitions tables should be excluded, and download everything else.
prefer using includeTables
, but if you're downloading everything, this is a good place to put InventoryItemLite
since you'll already have InventoryItem
loadDefs()
loads the newest manifest according to what version the API advertises. attempts to use the cached copy if it's up to date. returns a promise that resolves once loading is complete.
THIS CAN BE RE-RUN
- to download additional data after adding more components with
includeTables
- to check for manifest version updates
- to switch to a new language
lookups
getActivityDef('2656947700')
performs a lookup of a known activity hash. this example returns the type DestinyActivityDefinition | undefined
, because the hash might not be valid.
optional chaining will serve you well here, or you can live dangerously with a non-null assertion!
getDef('Activity', 2656947700)
a more generic def getter, which accepts table names as string literals. this returns the same data as the above example.
getInventoryItemDef(2575506895)
/ getDamageTypeDef(3373582085)
same as getActivity, but for other tables/data types. there's 50+ of these functions, not going to list them all here, that's what intellisense is for.
getAllDefs('Activity')
/ getAllActivityDefs()
returns an array of all the values in a manifest component, such as, in this example, a list of every DestinyActivityDefinition
getComponent('InventoryItem')
like getAllDefs
but returns the whole lookup table object, keyed by hashes
allManifest
an object containing all manifest components which have been fetched
misc exports
loadedVersion
the manifest version currently loaded in memory. the version that you'll get information from if you getDef()
an entry
defLanguages
a list of available manifest languages
examples
basic usage
import { verbose, setApiKey, loadDefs, getInventoryItemDef, getAllDefs, includeTables } from '@d2api/manifest-web';
verbose(); // make the client chatty. if you want.
setApiKey('hahahoho');
includeTables(['InventoryItem', 'DamageType']);
(async () => {
// checks the API for the current version.
// loads our cached copy if it's up to date, or downloads a new one from bungie
await loadDefs();
// if you made it to this comment, the manifest is ready for use!
const kindledOrchid = getInventoryItemDef(2575506895);
console.log(kindledOrchid?.displayProperties.description);
// -> "Find the beauty in the flame."
console.log(getAllDefs('DamageType').length);
// -> 6
})();