bungie-net-core
Version:
An easy way to interact with the Bungie.net API
46 lines (45 loc) • 1.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.authorize = authorize;
exports.createOAuthURL = createOAuthURL;
exports.refreshAuthorization = refreshAuthorization;
function createOAuthURL(query) {
const url = new URL('https://www.bungie.net/en/OAuth/Authorize');
if (!query.client_id) throw Error('Missing client_id in query');
url.searchParams.set('client_id', query.client_id);
url.searchParams.set('response_type', 'code');
if (query.state !== undefined) url.searchParams.set('state', query.state);
if (query.reauth !== undefined) url.searchParams.set('reauth', query.reauth.toString());
if (query.redirect_uri !== undefined) url.searchParams.set('redirect_uri', query.redirect_uri);
return url;
}
async function authorize(code, credentials, http) {
return await http({
baseUrl: 'https://www.bungie.net/platform/app/oauth/token/',
method: 'POST',
body: new URLSearchParams({
grant_type: 'authorization_code',
code,
client_id: credentials.client_id,
client_secret: credentials.client_secret
}),
contentType: 'application/x-www-form-urlencoded',
searchParams: undefined
});
}
async function refreshAuthorization(token, credentials, http) {
return await http({
baseUrl: 'https://www.bungie.net/platform/app/oauth/token/',
method: 'POST',
body: new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: token,
client_id: credentials.client_id,
client_secret: credentials.client_secret
}),
contentType: 'application/x-www-form-urlencoded',
searchParams: undefined
});
}