UNPKG

quria

Version:

A user-friendly Destiny 2 API Wrapper written with TypeScript and approved by -Axis Minds- Oryx.

96 lines (95 loc) 3.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkRunningEnvironment = exports.generateOptions = exports.parseUserAgent = exports.parseAuthenticationHeaders = exports.formatQueryStrings = void 0; const _1 = require("./"); const http_request_1 = require("./http-request"); function formatQueryStrings(uri, queryObject) { // Querystring example : { "components": components } const allKeys = Object.keys(queryObject || {}); const allValues = Object.values(queryObject || {}); let formattedQuerystring = ""; allValues.forEach((item, index) => { if (item !== null && item !== undefined) { const startingChar = index === 0 ? "?" : "&"; const formattedItem = typeof item === "object" ? item.join(",") : item; formattedQuerystring += `${startingChar}${allKeys[index]}=${formattedItem}`; } }); return `${uri}${formattedQuerystring}`; } exports.formatQueryStrings = formatQueryStrings; function parseAuthenticationHeaders(headers, tokens) { let newObject = {}; if (tokens && tokens.access_token) newObject = { Authorization: `Bearer ${tokens.access_token}` }; return { ...headers, ...newObject }; } exports.parseAuthenticationHeaders = parseAuthenticationHeaders; function parseUserAgent(userAgent) { let appId; let appName = "Quria Wrapper"; let appVersion = "2.3.0"; let contacts = ""; if (userAgent) { // If the user specify a string User Agent, pass it if (typeof userAgent === "string") { return userAgent; } // Parse user agent from object if ("APP_NAME" in userAgent) { appName = userAgent.APP_NAME; appVersion = userAgent.APP_VERSION.toString(); if ("CONTACT_WEBSITE" in userAgent) { contacts += `${userAgent.CONTACT_WEBSITE}`; if ("CONTACT_MAIL" in userAgent) { contacts += `;${userAgent.CONTACT_MAIL}`; } } } if ("APP_ID" in userAgent) { appId = userAgent?.APP_ID?.toString(); } } // Build user agent let final = `${appName}/${appVersion}`; // Add AppId to User Agent if (appId) { final += ` AppId/${appId}`; } // Add contacts if (contacts) { final += ` (+${contacts})`; } return final; } exports.parseUserAgent = parseUserAgent; function generateOptions(changes) { const host = changes.HOST || "https://www.bungie.net"; _1.Controller.setRequestHandler(changes.FETCHER || http_request_1.httpRequest); const options = { host, urls: { api: `${host}/Platform`, authorization: `${host}/en/OAuth/Authorize`, token: `${host}/Platform/App/OAuth/Token`, }, app: { client_id: changes.CLIENT_ID, client_secret: changes.CLIENT_SECRET || undefined, redirect_uri: changes.REDIRECT_URI || undefined, }, headers: { "X-API-Key": changes.API_KEY, }, }; // Add user agent if running on server if (checkRunningEnvironment() === "node") { options.headers["User-Agent"] = parseUserAgent(changes.USER_AGENT); } return options; } exports.generateOptions = generateOptions; function checkRunningEnvironment() { return typeof process !== "undefined" && process.versions != null && process.versions.node != null ? "node" : "browser"; } exports.checkRunningEnvironment = checkRunningEnvironment;