Coder Social home page Coder Social logo

bungie-api-ts's Introduction

Bungie API TypeScript support

This project implements TypeScript definitions and API helpers for the Bungie.net API. It's meant for use in Destiny Item Manager, but should be general enough to use in any project. The code is completely generated from Bungie's documentation - I considered using something like Swagger Codegen, but instead opted for a custom generator so we could make the result as nice as possible.

Ports

Feel free to fork this and use it to generate for your favorite language!

Install

yarn add bungie-api-ts

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 { DestinyInventoryComponent, DestinyInventoryItemDefinition } from 'bungie-api-ts/destiny2';

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, HttpClientConfig } from 'bungie-api-ts/destiny2';

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

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

Imports

It is possible to import all services from bungie-api-ts directly, but it's better to import the specific service and pick out what you want:

// good
import { getProfile, HttpClientConfig } from 'bungie-api-ts/destiny2';
getProfile(...);

// works, but not as good
import { Destiny2 } from 'bungie-api-ts';
Destiny2.getProfile(...);

Manifest Helpers

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

import { getDestinyManifestSlice } from 'bungie-api-ts/destiny2';

async function $http(config: HttpClientConfig) {
  // 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

# setup
yarn && yarn submodule
# run
yarn start

Updating API sources

Run the update API sources GitHub Action and it should create a new PR for the updated sources.

Publishing

Update the version in package.json, and when the PR merges to master, a GitHub workflow will automatically publish to NPM. Don't forget to run yarn start and commit all changed files!

bungie-api-ts's People

Contributors

48klocs avatar archived-2f4dd0 avatar bhollis avatar delphiactual avatar dependabot[bot] avatar lauriys avatar nev-r avatar robojumper avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bungie-api-ts's Issues

Can't use with isolated modules

I'm new to typescript, so I don't fully understand this compilation error:

Type error: Cannot access ambient const enums when the '--isolatedModules' flag is provided.

  83 |             const playerResp = await searchDestinyPlayer($http, {
  84 |                 displayName: encodeURIComponent(name),
> 85 |                 membershipType: BungieMembershipType.All,
     |                                 ^
  86 |             })
  87 |             if (!playerResp.Response.length) {
  88 |                 console.log("Couldn't find player (retry?)")

but it led me here:
https://ncjamieson.com/dont-export-const-enums/

getMembersOfGroup does not pass all parameters to endpoint

Specifically: currentpage is a required property in GetMembersOfGroupParams but is not passed to the endpoint:

function getMembersOfGroup(http, params) {
    return http({
        method: 'GET',
        url: "https://www.bungie.net/Platform/GroupV2/" + params.groupId + "/Members/",
        params: {
            memberType: params.memberType,
            nameSearch: params.nameSearch
        }
    });
}

`bungie-api-ts/common` is not exposed in the generated package.json's `exports` field

I’m trying to use BungieMembershipType from bungie-api-ts/common and webpack 5 is yelling at me:

Module not found: Error: Package path ./common is not exported from package ./node_modules/bungie-api-ts (see exports field in ./node_modules/bungie-api-ts/package.json)

At the moment I’m digging around to see if I can override the exports field with a plugin. Worst case scenario: I’ll hard-code the enum value. Not the end of the world, but it’s not Technically Correct™.

(Personal opinion: Given that 1. this module only exports ESM code and 2. the structure of the generated code perfectly matches what the exports field contains, the exports field can [again IMO] probably be omitted. One less thing to maintain!)

Thank you for this fantastic package 🙏

Error require() of ES Module when importing functions

Hello, I'm trying to get the manifest with the example in the readme but when I do the imports, I get error for require() of ES Module.

To reproduce it:

import { getDestinyManifest, getDestinyManifestSlice } from 'bungie-api-ts/destiny2';
console.log(getDestinyManifest);

the console.log it's for reproduce the problem

Error:

Error [ERR_REQUIRE_ESM]: require() of ES Module /destiny2-api/node_modules/bungie-api-ts/destiny2/index.js from /destiny2-api/src/manifest/index.ts not supported.
Instead change the require of index.js in /destiny2-api/src/manifest/index.ts to a dynamic import() which is available in all CommonJS modules.
    at Object.nodeDevHook [as .js] (/destiny2-api/node_modules/ts-node-dev/lib/hook.js:63:13)
    at Object.<anonymous> (/destiny2-api/src/manifest/index.ts:42:18)
    at Module._compile (/destiny2-api/node_modules/source-map-support/source-map-support.js:568:25)
    at Module.m._compile (/private/var/folders/1m/j_r9mhw56_g26n_8dxw1dkdc0000gn/T/ts-node-dev-hook-8312512297435408.js:69:33)
    at require.extensions.<computed> (/private/var/folders/1m/j_r9mhw56_g26n_8dxw1dkdc0000gn/T/ts-node-dev-hook-8312512297435408.js:71:20)
    at Object.nodeDevHook [as .ts] (/destiny2-api/node_modules/ts-node-dev/lib/hook.js:63:13)
    at Object.<anonymous> (/destiny2-api/src/prueba/prueba.router.ts:47:34)
    at Module._compile (/destiny2-api/node_modules/source-map-support/source-map-support.js:568:25)
    at Module.m._compile (/private/var/folders/1m/j_r9mhw56_g26n_8dxw1dkdc0000gn/T/ts-node-dev-hook-8312512297435408.js:69:33)
    at require.extensions.<computed> (/private/var/folders/1m/j_r9mhw56_g26n_8dxw1dkdc0000gn/T/ts-node-dev-hook-8312512297435408.js:71:20)
    at Object.nodeDevHook [as .ts] (/destiny2-api/node_modules/ts-node-dev/lib/hook.js:63:13)
    at Object.<anonymous> (/destiny2-api/src/index.ts:29:23)
    at Module._compile (/destiny2-api/node_modules/source-map-support/source-map-support.js:568:25)
    at Module.m._compile (/private/var/folders/1m/j_r9mhw56_g26n_8dxw1dkdc0000gn/T/ts-node-dev-hook-8312512297435408.js:69:33)
    at require.extensions.<computed> (/private/var/folders/1m/j_r9mhw56_g26n_8dxw1dkdc0000gn/T/ts-node-dev-hook-8312512297435408.js:71:20)
    at Object.nodeDevHook [as .ts] (/destiny2-api/node_modules/ts-node-dev/lib/hook.js:63:13)
    at Object.<anonymous> (/destiny2-api/node_modules/ts-node-dev/lib/wrap.js:104:1)
    at Module._compile (/destiny2-api/node_modules/source-map-support/source-map-support.js:568:25)
[ERROR] 19:27:02 Error: require() of ES Module /destiny2-api/node_modules/bungie-api-ts/destiny2/index.js from /destiny2-api/src/manifest/index.ts not supported.

If I import types and use them there is no problem, the problem is when I import any function like getDestinyManifest.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.