nationstates.js
Version:
A wrapper to interact with the NationStates API.
149 lines • 5.64 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Dispatch = void 0;
const client_1 = require("../client");
const request_builder_1 = require("../request_builder/request_builder");
const private_request_builder_1 = require("../private_request_builder/private_request_builder");
const fetch = require('node-fetch');
/**
* A class to handle creating, editing, and deleting dispatches in more high-level functions.
* @example const methods = new Dispatch(api, 'nation', 'password', Mode.add);
* @param { client } client The client object to enforce rate limiting and user agents.
* @param { string } nation Your nation name without prefixes. Used for authenticating with the NationStates client.
* @param { string } password Your password. Used for authenticating with the NationStates client.
* @param { Mode } action Enumerator to specify the action to be taken.
*/
class Dispatch extends request_builder_1.RequestBuilder {
constructor(client, nation, password, action) {
super(client);
this.client = client instanceof client_1.API ? new client_1.Client(client.userAgent, client.rateLimit) : client;
this.nation = nation;
this.password = password;
this.action = action;
}
/**
* Set the dispatch mode. It can be either:
* - 'add'
* - 'remove'
* - 'edit'<br><br>
* See NationStates client documentation for more information.
* @param method
*/
addAction(method) {
if (typeof method !== 'string')
throw new Error('Action must be a string.');
method = method.toLowerCase().trim();
if (method !== 'add' && method !== 'remove' && method !== 'edit') {
throw new Error('Action must be either "add", "remove", or "edit".');
}
this._urlObj.searchParams.append('dispatch', method);
}
/**
* Add title to the dispatch.
* @param text
*/
title(text) {
if (typeof text !== 'string')
throw new Error('The title must be a string.');
this._urlObj.searchParams.append('title', text);
return this;
}
/**
* Add text to the dispatch.
* @param text
*/
text(text) {
if (typeof text !== 'string')
throw new Error('The text must be a string.');
this._urlObj.searchParams.append('text', text);
return this;
}
/**
* Set the category of the dispatch.
* @param category
*/
category(category) {
if (typeof (category) !== 'number') {
throw new Error('The category must be a number. See NationStates client documentation.');
}
this._urlObj.searchParams.append('category', category.toString());
return this;
}
/**
* Set the category of the dispatch.
* @param subcategory
*/
subcategory(subcategory) {
if (typeof (subcategory) !== 'number')
throw new Error('The category must be a number. See NationStates client documentation.');
this._urlObj.searchParams.append('subcategory', subcategory.toString());
return this;
}
/**
* Set the dispatch ID when editing or a removing a dispatch.
* @param id
*/
dispatchID(id) {
if (typeof (id) !== 'number')
throw new Error('The dispatch ID must be a number.');
if (this._urlObj.searchParams.get('dispatch') === 'add') {
throw new Error('The dispatch ID is only set when editing or removing dispatches..');
}
this._urlObj.searchParams.append('dispatchid', id.toString());
return this;
}
/**
* Obtain the x-pin of a nation.
* @private
*/
async authenticate() {
const privateRequest = new private_request_builder_1.PrivateRequestBuilder(this.client);
await privateRequest.authenticate(this.nation, this.password);
return privateRequest._authentication._xPin;
}
/**
* Sends command asynchronously according to specifications with mode=prepare and mode=execute.
* To check for success, access the returned response attribute.
*/
async execute() {
/*----- 1. Retrieve the x-pin -----*/
const xPin = await this.authenticate();
/*----- 2. Prepare Request & Get Token -----*/
this.addNation(this.nation).addCustomParam('c', 'dispatch');
this.addAction(this.action);
this._urlObj.searchParams.append('mode', 'prepare');
await this.execRateLimit();
try {
const res = await fetch(this.href, {
headers: {
'User-Agent': this.client.userAgent,
'X-Pin': xPin
}
});
await this.logRequest(res);
}
catch (err) {
throw new Error(`Error sending request: ${err}`);
}
let token = (await this.toJS()).js['success']; // Convert request to JS and extract success token.
/*----- 3. Execute Request -----*/
this._urlObj.searchParams.set('mode', 'execute');
this._urlObj.searchParams.append('token', token);
await this.execRateLimit();
try {
const res = await fetch(this.href, {
headers: {
'User-Agent': this.client.userAgent,
'X-Pin': xPin
}
});
await this.logRequest(res);
}
catch (err) {
throw new Error(`Error sending request: ${err}`);
}
return this;
}
}
exports.Dispatch = Dispatch;
//# sourceMappingURL=dispatch.js.map