bungie-api-typedef

TypeScript/JavaScript mappings for the Bungie.net API


Keywords
Destiny, Bungie, Bungie.net, Bungie API, TypeScript, NodeJS
License
MIT
Install
npm install bungie-api-typedef@1.0.1

Documentation

Bungie API TypeScript support

This project implements TypeScript definitions and API helpers for the Bungie.net API. The original design was for use in Destiny Item Manager, but I wanted to make some changes. The code is completely generated from Bungie's documentation - The lovely developers over at DIM produved much of the code here, but I made some modifications. Their project can be found here: destiny-api-ts

Install

npm i bungie-api-typedef

Interfaces and Enums

All the interface type definitions and enums are for type info only - everything will compile out. Only the API helpers produce real JavaScript output. You can import types from each service defined on Bungie.net:

Import

import { DestinyInventoryComponent, DestinyInventoryItemDefinition } from 'bungie-api-ts/schemas';

require

const { DestinyInventoryComponent, DestinyInventoryItemDefinition } = require('bungie-api-ts/schemas');

There are definitions for every type defined in the Bungie.net services. See their documentation for a list - the interface names are the last part of the full name (for example, Destiny.Definitions.DestinyVendorActionDefinition becomes DestinyVendorActionDefinition). There are a few exceptions, like SingleComponentResponseOfDestinyInventoryComponent, which have been mapped into nicer forms like SingleComponentResponse<DestinyInventoryComponent>, and the server responses, which are now ServerResponse<T> instead of something like DestinyCharacterResponse.

API Helpers

In addition to the types, there are also simple helper functions for each API endpoint. They define the inputs and outputs to that endpoint, and will call a user-provided function with HTTP request info that you can then use to make an HTTP request. This pattern was used so the API helpers could provide full type information. These helpers are not a full API client - they assist in building one. An example:

import { getProfile } from 'bungie-api-typedef/endpoints/Destiny2';

async function $http(config) {
  // fill in the API key, handle OAuth, etc., then make an HTTP request using the config.
  return fetch(config.url, ...);
}

const profileInfo = await getProfile($http, {
  components: [DestinyComponentType.Profiles, DestinyComponentType.Characters],
  destinyMembershipId: 12345,
  membershipType: BungieMembershipType.TigerPsn
});

Imports

It is possible to import all services from bungie-api-typedef in a few ways, but it's better to import the specific service and pick out what you want:

// good
import { getCharacter } from 'bungie-api-typedef/endpoints/Destiny2';

const charInfo = getCharacter(...);

// works as well, but not as clean
import { Destiny2 } from 'bungie-api-typedef/endpoints';
const charInfo = Destiny2.getCharacter(...);

// not ideal
import { Endpoints } from 'bungie-api-typedef';
const charInfo = Endpoints.Destiny2.getCharacter(...);

// you do you
import * as BungieNet from 'bungie-api-typedef';
const charInfo = BungieNet.Endpoints.Destiny2.getCharacter(...);

Manifest Helpers

The manifest import also contains helpers for typing and downloading the Destiny manifest:

import { getDestinyManifestSlice } from 'bungie-api-typedef/manifest';

async function $http(config) {
  // fill in the API key, handle OAuth, etc., then make an HTTP request using the config.
  return fetch(config.url, ...);
}

const destinyManifest = await getDestinyManifest($http);
const manifestTables = getDestinyManifestSlice($http, {
  destinyManifest,
  tableNames: ['DestinyInventoryItemDefinition', 'DestinySocketDefinition'],
  language: 'en',
});

// manifestTables is an object with properties DestinyInventoryItemDefinition and DestinySocketDefinition

Build from Git Repository

# setup
npm i
npm run submodule
# build the library
npm run build