umami-api-js 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -5
- package/dist/index.d.ts +8 -4
- package/dist/index.js +27 -15
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -82,13 +82,14 @@ const api = new umami.API("<api_endpoint>", "<username>", "<password>", {
|
|
|
82
82
|
|
|
83
83
|
### Tokens
|
|
84
84
|
|
|
85
|
-
An [`access_token`](https://umami-api-js.taevas.xyz/classes/API.html#access_token) is required to access the API
|
|
85
|
+
An [`access_token`](https://umami-api-js.taevas.xyz/classes/API.html#access_token) is required to access the API. When you first create your [`api`](https://umami-api-js.taevas.xyz/classes/API.html) object, that token is automatically set before any request is made, so you don't have to worry about that!
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
Unlike many other web applications, [Umami doesn't expire tokens after a certain amount of time](https://github.com/umami-software/umami/discussions/1170), meaning in theory that you would only ever need to request a new token if your credentials change. After changing the [username](https://umami-api-js.taevas.xyz/classes/API.html#username) or [password](https://umami-api-js.taevas.xyz/classes/API.html#password), you may manually call [`setNewToken()`](https://umami-api-js.taevas.xyz/classes/API.html#setnewtoken), which will replace your previous token with a new one!
|
|
88
88
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
-
|
|
89
|
+
This package was built with the expectation that tokens would eventually expire, so you've got some configuration options you can play around with:
|
|
90
|
+
|
|
91
|
+
- Keep the [`set_token_on_expires`](https://umami-api-js.taevas.xyz/classes/API.html#set_token_on_expires) option to true if you'd like the object to call `setNewToken()` automatically as soon as the token expires
|
|
92
|
+
- By default, the [`set_token_on_401`](https://umami-api-js.taevas.xyz/classes/API.html#set_token_on_401) option is set to false, which (as its name indicates) would do that upon encountering a 401
|
|
92
93
|
- When that happens, if [`retry_on_new_token`](https://umami-api-js.taevas.xyz/classes/API.html#retry_on_new_token) is set to true as it is by default, it will retry the request it has encountered a 401 on, with the new token! (note that loops are prevented, it won't retry or get another token if the same request with the new token gets a 401)
|
|
93
94
|
|
|
94
95
|
At any point in time, you can see when the current `access_token` is set to expire through the [`expires`](https://umami-api-js.taevas.xyz/classes/API.html#expires) property of the API.
|
package/dist/index.d.ts
CHANGED
|
@@ -125,9 +125,13 @@ export declare class API {
|
|
|
125
125
|
get token_type(): string;
|
|
126
126
|
set token_type(token: string);
|
|
127
127
|
private _expires;
|
|
128
|
-
/**
|
|
129
|
-
|
|
130
|
-
|
|
128
|
+
/**
|
|
129
|
+
* The expiration date of your token
|
|
130
|
+
* @remarks Umami v3.0.3 behaviour is to NOT expire tokens a given amount of time after creation,
|
|
131
|
+
* see https://github.com/umami-software/umami/discussions/1170
|
|
132
|
+
*/
|
|
133
|
+
get expires(): Date | null;
|
|
134
|
+
set expires(date: Date | null);
|
|
131
135
|
private _server;
|
|
132
136
|
/** The base URL where requests should land, **should include the `/api` portion if applicable** */
|
|
133
137
|
get server(): string;
|
|
@@ -171,7 +175,7 @@ export declare class API {
|
|
|
171
175
|
get set_token_on_creation(): boolean;
|
|
172
176
|
set set_token_on_creation(bool: boolean);
|
|
173
177
|
private _set_token_on_401;
|
|
174
|
-
/** If true, upon failing a request due to a 401, it will call {@link API.setNewToken} (defaults to **
|
|
178
|
+
/** If true, upon failing a request due to a 401, it will call {@link API.setNewToken} (defaults to **false**) */
|
|
175
179
|
get set_token_on_401(): boolean;
|
|
176
180
|
set set_token_on_401(bool: boolean);
|
|
177
181
|
private _set_token_on_expires;
|
package/dist/index.js
CHANGED
|
@@ -62,14 +62,17 @@ export class API {
|
|
|
62
62
|
// Very likely a clone created while the original didn't have a valid token
|
|
63
63
|
this.set_token_on_expires = false; // In which case allow the clone to get its own token, but not renewing it automatically
|
|
64
64
|
} // And if the clone keeps getting used, `set_token_on_401` being true should cover that
|
|
65
|
-
|
|
66
|
-
if (this.set_token_on_creation &&
|
|
67
|
-
typeof server_or_settings === "string" &&
|
|
68
|
-
username &&
|
|
69
|
-
password) {
|
|
70
|
-
this.server = server_or_settings;
|
|
65
|
+
if (username) {
|
|
71
66
|
this.username = username;
|
|
67
|
+
}
|
|
68
|
+
if (password) {
|
|
72
69
|
this.password = password;
|
|
70
|
+
}
|
|
71
|
+
if (typeof server_or_settings === "string") {
|
|
72
|
+
this.server = server_or_settings;
|
|
73
|
+
}
|
|
74
|
+
/** We want to set a new token instantly if account credentials have been provided */
|
|
75
|
+
if (this.set_token_on_creation && username && password) {
|
|
73
76
|
this.setNewToken();
|
|
74
77
|
}
|
|
75
78
|
}
|
|
@@ -110,8 +113,12 @@ export class API {
|
|
|
110
113
|
set token_type(token) {
|
|
111
114
|
this._token_type = token;
|
|
112
115
|
}
|
|
113
|
-
_expires =
|
|
114
|
-
/**
|
|
116
|
+
_expires = null;
|
|
117
|
+
/**
|
|
118
|
+
* The expiration date of your token
|
|
119
|
+
* @remarks Umami v3.0.3 behaviour is to NOT expire tokens a given amount of time after creation,
|
|
120
|
+
* see https://github.com/umami-software/umami/discussions/1170
|
|
121
|
+
*/
|
|
115
122
|
get expires() {
|
|
116
123
|
return this._expires;
|
|
117
124
|
}
|
|
@@ -179,7 +186,9 @@ export class API {
|
|
|
179
186
|
/** Has {@link API.setNewToken} been called and not yet returned anything? */
|
|
180
187
|
is_setting_token = false;
|
|
181
188
|
/** If {@link API.setNewToken} has been called, you can wait for it to be done through this promise */
|
|
182
|
-
token_promise = new Promise((r) =>
|
|
189
|
+
token_promise = new Promise((r) => {
|
|
190
|
+
r();
|
|
191
|
+
}); // `{r()}` over `r` prevents Node.js weirdness when awaited
|
|
183
192
|
/**
|
|
184
193
|
* This contacts the server in order to get and set a new {@link API.token}!
|
|
185
194
|
* @remarks The API object requires a {@link API.username} and a {@link API.password} to successfully get any token
|
|
@@ -204,9 +213,10 @@ export class API {
|
|
|
204
213
|
// Note: `json` currently only has `token` & `user`
|
|
205
214
|
this.token = json.token;
|
|
206
215
|
this.user = json.user;
|
|
207
|
-
|
|
208
|
-
expiration_date
|
|
209
|
-
|
|
216
|
+
// If Umami ever sets expiration dates on tokens, the code to let the API object know should be here
|
|
217
|
+
// const expiration_date = new Date();
|
|
218
|
+
// expiration_date.setDate(expiration_date.getDate() + 1); // Assume 24 hours
|
|
219
|
+
// this.expires = expiration_date;
|
|
210
220
|
resolve();
|
|
211
221
|
});
|
|
212
222
|
})
|
|
@@ -226,8 +236,8 @@ export class API {
|
|
|
226
236
|
set set_token_on_creation(bool) {
|
|
227
237
|
this._set_token_on_creation = bool;
|
|
228
238
|
}
|
|
229
|
-
_set_token_on_401 =
|
|
230
|
-
/** If true, upon failing a request due to a 401, it will call {@link API.setNewToken} (defaults to **
|
|
239
|
+
_set_token_on_401 = false;
|
|
240
|
+
/** If true, upon failing a request due to a 401, it will call {@link API.setNewToken} (defaults to **false**) */
|
|
231
241
|
get set_token_on_401() {
|
|
232
242
|
return this._set_token_on_401;
|
|
233
243
|
}
|
|
@@ -386,7 +396,9 @@ export class API {
|
|
|
386
396
|
let error_object;
|
|
387
397
|
let error_message = "no error message available";
|
|
388
398
|
if (!is_token_related)
|
|
389
|
-
await this.token_promise.catch(() => (this.token_promise = new Promise((r) =>
|
|
399
|
+
await this.token_promise.catch(() => (this.token_promise = new Promise((r) => {
|
|
400
|
+
r();
|
|
401
|
+
})));
|
|
390
402
|
for (const [p, v] of Object.entries(parameters)) {
|
|
391
403
|
// Convert Dates to ms
|
|
392
404
|
if (typeof v === "object" && !Array.isArray(v) && v !== null) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "umami-api-js",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Package to easily access the Umami api!",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -32,12 +32,12 @@
|
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/chai": "^5.2.3",
|
|
34
34
|
"@types/node": "^24.9.2",
|
|
35
|
-
"ajv": "^8.
|
|
35
|
+
"ajv": "^8.18.0",
|
|
36
36
|
"chai": "^6.2.2",
|
|
37
37
|
"dotenv": "^17.2.3",
|
|
38
38
|
"prettier": "3.8.1",
|
|
39
39
|
"ts-json-schema-generator": "^2.4.0",
|
|
40
|
-
"typedoc": "^0.28.
|
|
40
|
+
"typedoc": "^0.28.17",
|
|
41
41
|
"typescript": "^5.9.3"
|
|
42
42
|
}
|
|
43
43
|
}
|