Skip to content

1681-engine / index / ApiClient

Class: ApiClient ​

Defined in: src/net/ApiClient.ts:95

A small JSON REST client: a bearer token, JSON in and out, a timeout on every call, and a session kept alive — a 401 is refreshed once (refresher) and the request sent again. A game keeps its endpoints and types in a subclass or beside it; nothing here knows any route.

Constructors ​

Constructor ​

ts
new ApiClient(options): ApiClient;

Defined in: src/net/ApiClient.ts:102

Parameters ​

ParameterType
optionsApiClientOptions

Returns ​

ApiClient

Properties ​

onUnauthorized ​

ts
onUnauthorized: (() => void) | null = null;

Defined in: src/net/ApiClient.ts:100

Called when a 401 ends the session: no refresher, a refused refresh, or a retry refused again.


refresher ​

ts
refresher: 
  | (() => Promise<RefreshResult>)
  | null = null;

Defined in: src/net/ApiClient.ts:98

A new access token for a 401 (RefreshResult); unset, a 401 is simply thrown.

Accessors ​

accessToken ​

Get Signature ​

ts
get accessToken(): string | null;

Defined in: src/net/ApiClient.ts:104

Returns ​

string | null

Methods ​

request() ​

ts
request<T>(
   method, 
   path, 
   body?, 
   options?, 
   retried?
): Promise<T>;

Defined in: src/net/ApiClient.ts:113

Sends a request and returns its parsed JSON body (undefined for an empty or unreadable one).

Type Parameters ​

Type Parameter
T

Parameters ​

ParameterTypeDefault value
methodApiMethodundefined
pathstringundefined
body?unknownundefined
options?ApiRequestOptions{}
retried?booleanfalse

Returns ​

Promise<T>


setToken() ​

ts
setToken(token): void;

Defined in: src/net/ApiClient.ts:108

Parameters ​

ParameterType
tokenstring | null

Returns ​

void

Released under the MIT License.