包详细信息

bungie-net-core

owens1127812MIT3.0.3

An easy way to interact with the Bungie.net API

Destiny 2, Bungie, TypeScript, REST API

自述文件

bungie-net-core

This is a Typescript wrapper for the Bungie API. It is mostly for personal use, but if you find any bugs please report them.

Installation

npm i bungie-net-core

Example Usage

This http method is used for makingrequests to the Bungie Platform API

import { BungieHttpProtocol } from 'bungie-net-core';
import { getProfile } from 'bungie-net-core/services/Destiny2';

const bungiePlatformHttp: BungieHttpProtocol = async config => {
  const headers = new Headers({
    'X-API-KEY': process.env.BUNGIE_API_KEY!
  });

  let body;
  if (config.contentType === 'application/json') {
    headers.set('Content-Type', config.contentType);
    body = JSON.stringify(config.body);
  }

  // implement how you like
  // headers.set('Authorization', `Bearer ${access_token}`);

  const url = config.baseUrl + (config.searchParams ? '?' + config.searchParams.toString() : '');

  const payload = {
    method: config.method,
    body,
    headers
  };

  const res = await fetch(url, payload);
  if (!res.ok) {
    // Your choice here
    throw new Error(res.statusText);
  }

  if (res.headers.get('Content-Type')?.includes('application/json')) {
    return await res.json();
  } else {
    // Your choice here
    throw new Error('Response was not JSON');
  }
};

getProfile(bungiePlatformHttp, {
  components: ['CharacterInventories'],
  destinyMembershipId: '4611741274194011',
  membershipType: 3
});