nationstates.js
Version:
A wrapper to interact with the NationStates API.
78 lines • 2.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrivateRequestBuilder = void 0;
const request_builder_1 = require("../request_builder/request_builder");
const fetch = require('node-fetch');
/**
* Extends the RequestBuilder to allow authenticating with the NS client and accessing private shards.
* No support for accessing private commands. Only shards.
* @example const request = new PrivateRequestBuilder(api).authenticate('Testlandia', 'password');
*/
class PrivateRequestBuilder extends request_builder_1.RequestBuilder {
constructor(api) {
super(api);
this._authentication = {
status: false,
};
}
/**
* Returns the authentication information as an object.
*/
get authInfo() {
return this._authentication;
}
/**
* Retrieves the X-Pin value from NationStates and saves all information to the _authentication object.
* Must be run before sending any requests.
* @example const request = new PrivateRequestBuilder(api).authenticate('Testlandia', 'password');
* @param {string} nation The nation to retrieve the X-Pin for.
* @param {string} password The password for the nation.
*/
async authenticate(nation, password) {
this._authentication._nation = nation;
this._authentication._xPassword = password;
const req = new PrivateRequestBuilder(this.client).addNation(nation).addShards('unread');
await this.execRateLimit();
try {
let res = await fetch(req.href, {
headers: {
'User-Agent': this.client.userAgent,
'X-Password': password
}
});
await this.logRequest(res);
this._authentication._xPin = res.headers.get('x-pin');
this._authentication.status = true;
}
catch (err) {
throw new Error(err);
}
return this;
}
/**
* Executes the request and saves the response to the RequestBuilder object.
* Retrieve after awaiting it via .response, .body, or convert it to a JS object with convertToJSON();
* Polymorph of RequestBuilder.
*/
async execute() {
if (!this._authentication.status) {
throw new Error('You must first authenticate! Run authenticate() on your private request before sending it.');
}
await this.execRateLimit();
try {
const res = await fetch(this.href, {
headers: {
'User-Agent': this.client.userAgent,
'X-Pin': this._authentication._xPin,
}
});
await this.logRequest(res);
}
catch (err) {
throw new Error(`Error sending request: ${err}`);
}
return this;
}
}
exports.PrivateRequestBuilder = PrivateRequestBuilder;
//# sourceMappingURL=private_request_builder.js.map