Skip to content

Commit

Permalink
Add improved errors, support for immediate requests
Browse files Browse the repository at this point in the history
  • Loading branch information
auralia committed Nov 16, 2016
1 parent 900fe19 commit 3b8da39
Show file tree
Hide file tree
Showing 13 changed files with 566 additions and 69 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.1.8 ##

* Improve errors
* Add support for immediate requests

## 0.1.7 ##

* Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion docs/assets/js/search.js

Large diffs are not rendered by default.

399 changes: 399 additions & 0 deletions docs/classes/apierror.html

Large diffs are not rendered by default.

124 changes: 73 additions & 51 deletions docs/classes/nsapi.html

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions docs/enums/telegramtype.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ <h3>Recruitment</h3>
<li class=" tsd-kind-enum">
<a href="worldassemblycouncil.html" class="tsd-kind-icon">World<wbr>Assembly<wbr>Council</a>
</li>
<li class=" tsd-kind-class">
<a href="../classes/apierror.html" class="tsd-kind-icon">Api<wbr>Error</a>
</li>
<li class=" tsd-kind-class">
<a href="../classes/nsapi.html" class="tsd-kind-icon">Ns<wbr>Api</a>
</li>
Expand Down
3 changes: 3 additions & 0 deletions docs/enums/worldassemblycouncil.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ <h3>Security<wbr>Council</h3>
</li>
</ul>
<ul class="after-current">
<li class=" tsd-kind-class">
<a href="../classes/apierror.html" class="tsd-kind-icon">Api<wbr>Error</a>
</li>
<li class=" tsd-kind-class">
<a href="../classes/nsapi.html" class="tsd-kind-icon">Ns<wbr>Api</a>
</li>
Expand Down
6 changes: 5 additions & 1 deletion docs/globals.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ <h3>Enumerations</h3>
<section class="tsd-index-section ">
<h3>Classes</h3>
<ul class="tsd-index-list">
<li class="tsd-kind-class"><a href="classes/apierror.html" class="tsd-kind-icon">Api<wbr>Error</a></li>
<li class="tsd-kind-class"><a href="classes/nsapi.html" class="tsd-kind-icon">Ns<wbr>Api</a></li>
</ul>
</section>
Expand Down Expand Up @@ -119,7 +120,7 @@ <h3>API_<wbr>VERSION</h3>
<section class="tsd-panel tsd-member tsd-kind-variable">
<a name="version" class="tsd-anchor"></a>
<h3>VERSION</h3>
<div class="tsd-signature tsd-kind-icon">VERSION<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol"> =&nbsp;&quot;0.1.7&quot;</span></div>
<div class="tsd-signature tsd-kind-icon">VERSION<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol"> =&nbsp;&quot;0.1.8&quot;</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in api.ts:52</li>
Expand Down Expand Up @@ -159,6 +160,9 @@ <h3>xml<wbr>Parser</h3>
<li class=" tsd-kind-enum">
<a href="enums/worldassemblycouncil.html" class="tsd-kind-icon">World<wbr>Assembly<wbr>Council</a>
</li>
<li class=" tsd-kind-class">
<a href="classes/apierror.html" class="tsd-kind-icon">Api<wbr>Error</a>
</li>
<li class=" tsd-kind-class">
<a href="classes/nsapi.html" class="tsd-kind-icon">Ns<wbr>Api</a>
</li>
Expand Down
3 changes: 3 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ <h2 id="license">License</h2>
<li class=" tsd-kind-enum">
<a href="enums/worldassemblycouncil.html" class="tsd-kind-icon">World<wbr>Assembly<wbr>Council</a>
</li>
<li class=" tsd-kind-class">
<a href="classes/apierror.html" class="tsd-kind-icon">Api<wbr>Error</a>
</li>
<li class=" tsd-kind-class">
<a href="classes/nsapi.html" class="tsd-kind-icon">Ns<wbr>Api</a>
</li>
Expand Down
9 changes: 6 additions & 3 deletions docs/interfaces/apirequest.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ <h3>func</h3>
<div class="tsd-signature tsd-kind-icon">func<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">function</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in api.ts:140</li>
<li>Defined in api.ts:172</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -134,7 +134,7 @@ <h3>reject</h3>
<div class="tsd-signature tsd-kind-icon">reject<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">function</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in api.ts:144</li>
<li>Defined in api.ts:176</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -170,7 +170,7 @@ <h3>tg</h3>
<div class="tsd-signature tsd-kind-icon">tg<span class="tsd-signature-symbol">:</span> <a href="../enums/telegramtype.html" class="tsd-signature-type">TelegramType</a><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">undefined</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in api.ts:136</li>
<li>Defined in api.ts:168</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -198,6 +198,9 @@ <h3>tg</h3>
<li class=" tsd-kind-enum">
<a href="../enums/worldassemblycouncil.html" class="tsd-kind-icon">World<wbr>Assembly<wbr>Council</a>
</li>
<li class=" tsd-kind-class">
<a href="../classes/apierror.html" class="tsd-kind-icon">Api<wbr>Error</a>
</li>
<li class=" tsd-kind-class">
<a href="../classes/nsapi.html" class="tsd-kind-icon">Ns<wbr>Api</a>
</li>
Expand Down
3 changes: 3 additions & 0 deletions docs/interfaces/cache.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ <h5>time<span class="tsd-signature-symbol">: </span><span class="tsd-signature-t
<li class=" tsd-kind-enum">
<a href="../enums/worldassemblycouncil.html" class="tsd-kind-icon">World<wbr>Assembly<wbr>Council</a>
</li>
<li class=" tsd-kind-class">
<a href="../classes/apierror.html" class="tsd-kind-icon">Api<wbr>Error</a>
</li>
<li class=" tsd-kind-class">
<a href="../classes/nsapi.html" class="tsd-kind-icon">Ns<wbr>Api</a>
</li>
Expand Down
3 changes: 3 additions & 0 deletions docs/interfaces/privateshardsauth.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ <h3><span class="tsd-flag ts-flagOptional">Optional</span> update<wbr>Pin</h3>
<li class=" tsd-kind-enum">
<a href="../enums/worldassemblycouncil.html" class="tsd-kind-icon">World<wbr>Assembly<wbr>Council</a>
</li>
<li class=" tsd-kind-class">
<a href="../classes/apierror.html" class="tsd-kind-icon">Api<wbr>Error</a>
</li>
<li class=" tsd-kind-class">
<a href="../classes/nsapi.html" class="tsd-kind-icon">Ns<wbr>Api</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nsapi",
"version": "0.1.7",
"version": "0.1.8",
"description": "Provides access to the NationStates API",
"keywords": [
"NationStates",
Expand Down
73 changes: 61 additions & 12 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const xmlParser = new xml2js.Parser(
/**
* The current version of nsapi.
*/
export const VERSION = "0.1.7";
export const VERSION = "0.1.8";

/**
* The API version specified in API requests.
Expand Down Expand Up @@ -114,6 +114,38 @@ export interface PrivateShardsAuth {
updateAutologin?: boolean;
}

/**
* Error thrown during API requests.
*/
export class ApiError extends Error {
/**
* The message associated with the error.
*/
public message: string;
/**
* The HTTP response code returned by the API.
*/
public responseCode?: number;
/**
* The HTTP response text returned by the API.
*/
public responseText?: string;

/**
* Initializes a new instance of the ApiError class.
*
* @param message The message associated with the error.
* @param responseCode The HTTP response code returned by the API.
* @param responseText The HTTP response text returned by the API.
*/
constructor(message: string, responseCode?: number, responseText?: string) {
super(message);
this.message = message;
this.responseCode = responseCode;
this.responseText = responseText;
}
}

/**
* Represents a request cache.
*/
Expand Down Expand Up @@ -186,14 +218,21 @@ export class NsApi {
* true.
* @param requestCacheValiditySecs The number of seconds that a request
* should stay cached. Defaults to 900.
* @param allowImmediateRequests Allows API requests immediately after the
* API is initialized without delay.
* @param allowImmediateTgRequests Allows telegram requests immediately
* after the API is initialized without
* delay.
*/
constructor(userAgent: string,
delay: boolean = true,
apiDelayMillis: number = 600,
nonRecruitTgDelayMillis: number = 60000,
recruitTgDelayMillis: number = 180000,
requestCacheEnabled: boolean = true,
requestCacheValiditySecs: number = 900)
requestCacheValiditySecs: number = 900,
allowImmediateRequests: boolean = true,
allowImmediateTgRequests: boolean = true)
{
this.userAgent = userAgent;
this.delay = delay;
Expand All @@ -202,8 +241,16 @@ export class NsApi {
this.recruitTgDelayMillis = recruitTgDelayMillis;

this._reqQueue = [];
this._reqLast = Date.now();
this._tgReqLast = Date.now();
if (allowImmediateRequests) {
this._reqLast = Date.now() - this.apiDelayMillis;
} else {
this._reqLast = Date.now();
}
if (allowImmediateTgRequests) {
this._tgReqLast = Date.now() - this.recruitTgDelayMillis;
} else {
this._tgReqLast = Date.now();
}
this._reqInProgress = false;
if (this.delay) {
this._reqInterval = setInterval(() => {
Expand Down Expand Up @@ -586,9 +633,9 @@ export class NsApi {
if (!(typeof data === "string"
&& data.trim().toLowerCase() === "queued"))
{
throw new Error("telegram API response did not"
+ " consist of the string"
+ " 'queued'");
throw new ApiError(
"telegram API response did not consist of"
+ " the string 'queued'", 200, data);
}
});
});
Expand Down Expand Up @@ -630,9 +677,9 @@ export class NsApi {
{
return false;
} else {
throw new Error("authentication API response did"
+ " not consist of the string"
+ " '1' or '0'");
throw new ApiError(
"authentication API response did not consist"
+ " of the string '1' or '0'", 200, data);
}
});
});
Expand Down Expand Up @@ -786,9 +833,11 @@ export class NsApi {

resolve(clone(data));
} else {
reject(new Error(
reject(new ApiError(
`API returned HTTP response code`
+ ` ${res.statusCode}`));
+ ` ${res.statusCode}`,
res.statusCode,
data));
}
});
}
Expand Down

0 comments on commit 3b8da39

Please sign in to comment.