@minesa-org/mini-interaction 0.4.6 → 0.4.8
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.
|
@@ -12,7 +12,9 @@ export declare class DiscordRestClient {
|
|
|
12
12
|
private readonly baseUrl;
|
|
13
13
|
private readonly maxRetries;
|
|
14
14
|
constructor(options: DiscordRestClientOptions);
|
|
15
|
-
request<T>(path: string, init?: RequestInit
|
|
15
|
+
request<T>(path: string, init?: RequestInit & {
|
|
16
|
+
authenticated?: boolean;
|
|
17
|
+
}): Promise<T>;
|
|
16
18
|
createFollowup(interactionToken: string, body: unknown): Promise<unknown>;
|
|
17
19
|
editOriginal(interactionToken: string, body: unknown): Promise<unknown>;
|
|
18
20
|
}
|
|
@@ -12,13 +12,14 @@ export class DiscordRestClient {
|
|
|
12
12
|
}
|
|
13
13
|
async request(path, init = {}) {
|
|
14
14
|
let lastError;
|
|
15
|
+
const { authenticated = true, ...requestInit } = init;
|
|
15
16
|
for (let attempt = 0; attempt <= this.maxRetries; attempt += 1) {
|
|
16
17
|
const response = await this.fetchImpl(`${this.baseUrl}${path}`, {
|
|
17
|
-
...
|
|
18
|
+
...requestInit,
|
|
18
19
|
headers: {
|
|
19
|
-
Authorization: `Bot ${this.options.token}
|
|
20
|
+
...(authenticated ? { Authorization: `Bot ${this.options.token}` } : {}),
|
|
20
21
|
'Content-Type': 'application/json',
|
|
21
|
-
...(
|
|
22
|
+
...(requestInit.headers ?? {}),
|
|
22
23
|
},
|
|
23
24
|
});
|
|
24
25
|
if (response.status === 429) {
|
|
@@ -35,7 +36,7 @@ export class DiscordRestClient {
|
|
|
35
36
|
await sleep(150 * (attempt + 1));
|
|
36
37
|
continue;
|
|
37
38
|
}
|
|
38
|
-
lastError = new Error(`[DiscordRestClient] ${
|
|
39
|
+
lastError = new Error(`[DiscordRestClient] ${requestInit.method ?? 'GET'} ${path} failed: ${response.status}`);
|
|
39
40
|
break;
|
|
40
41
|
}
|
|
41
42
|
throw lastError instanceof Error ? lastError : new Error('[DiscordRestClient] unknown request failure');
|
|
@@ -44,12 +45,14 @@ export class DiscordRestClient {
|
|
|
44
45
|
return this.request(`/webhooks/${this.options.applicationId}/${interactionToken}`, {
|
|
45
46
|
method: 'POST',
|
|
46
47
|
body: JSON.stringify(body),
|
|
48
|
+
authenticated: false,
|
|
47
49
|
});
|
|
48
50
|
}
|
|
49
51
|
editOriginal(interactionToken, body) {
|
|
50
52
|
return this.request(`/webhooks/${this.options.applicationId}/${interactionToken}/messages/@original`, {
|
|
51
53
|
method: 'PATCH',
|
|
52
54
|
body: JSON.stringify(body),
|
|
55
|
+
authenticated: false,
|
|
53
56
|
});
|
|
54
57
|
}
|
|
55
58
|
}
|
|
@@ -44,6 +44,10 @@ export class MiniDatabase {
|
|
|
44
44
|
this.config = config;
|
|
45
45
|
this.schema = schema;
|
|
46
46
|
}
|
|
47
|
+
sanitiseStoredData(data) {
|
|
48
|
+
const { _id, createdAt, updatedAt, ...rest } = data;
|
|
49
|
+
return rest;
|
|
50
|
+
}
|
|
47
51
|
/**
|
|
48
52
|
* Initializes the database connection.
|
|
49
53
|
*/
|
|
@@ -125,9 +129,10 @@ export class MiniDatabase {
|
|
|
125
129
|
const dataWithDefaults = this.schema
|
|
126
130
|
? this.schema.applyDefaults(data)
|
|
127
131
|
: data;
|
|
132
|
+
const sanitizedData = this.sanitiseStoredData(dataWithDefaults);
|
|
128
133
|
await collection.updateOne({ _id: key }, {
|
|
129
134
|
$set: {
|
|
130
|
-
...
|
|
135
|
+
...sanitizedData,
|
|
131
136
|
_id: key,
|
|
132
137
|
updatedAt: new Date(),
|
|
133
138
|
},
|
|
@@ -164,9 +169,10 @@ export class MiniDatabase {
|
|
|
164
169
|
const dataWithDefaults = this.schema
|
|
165
170
|
? this.schema.applyDefaults(merged)
|
|
166
171
|
: merged;
|
|
172
|
+
const sanitizedData = this.sanitiseStoredData(dataWithDefaults);
|
|
167
173
|
await collection.updateOne({ _id: key }, {
|
|
168
174
|
$set: {
|
|
169
|
-
...
|
|
175
|
+
...sanitizedData,
|
|
170
176
|
updatedAt: new Date(),
|
|
171
177
|
},
|
|
172
178
|
$setOnInsert: {
|
package/package.json
CHANGED