fandom.js 1.0.0 → 1.1.0
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/LICENSE +23 -0
- package/README.md +73 -1
- package/dist/client/client.d.ts +69 -0
- package/dist/client/client.d.ts.map +1 -0
- package/dist/client/client.js +74 -0
- package/dist/enums/index.d.ts +7 -0
- package/dist/enums/index.d.ts.map +1 -0
- package/dist/enums/index.js +6 -0
- package/dist/enums/log-type.d.ts +20 -0
- package/dist/enums/log-type.d.ts.map +1 -0
- package/dist/enums/log-type.js +20 -0
- package/dist/enums/namespace.d.ts +25 -0
- package/dist/enums/namespace.d.ts.map +1 -0
- package/dist/enums/namespace.js +25 -0
- package/dist/enums/protection-level.d.ts +9 -0
- package/dist/enums/protection-level.d.ts.map +1 -0
- package/dist/enums/protection-level.js +9 -0
- package/dist/enums/rc-show.d.ts +16 -0
- package/dist/enums/rc-show.d.ts.map +1 -0
- package/dist/enums/rc-show.js +16 -0
- package/dist/enums/rc-type.d.ts +11 -0
- package/dist/enums/rc-type.d.ts.map +1 -0
- package/dist/enums/rc-type.js +11 -0
- package/dist/enums/token-type.d.ts +13 -0
- package/dist/enums/token-type.d.ts.map +1 -0
- package/dist/enums/token-type.js +13 -0
- package/dist/errors/api-error.d.ts +6 -0
- package/dist/errors/api-error.d.ts.map +1 -0
- package/dist/errors/api-error.js +8 -0
- package/dist/events/events.d.ts +14 -0
- package/dist/events/events.d.ts.map +1 -0
- package/dist/events/events.js +3 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/managers/base-manager.d.ts +16 -0
- package/dist/managers/base-manager.d.ts.map +1 -0
- package/dist/managers/base-manager.js +12 -0
- package/dist/managers/category-manager.d.ts +15 -0
- package/dist/managers/category-manager.d.ts.map +1 -0
- package/dist/managers/category-manager.js +27 -0
- package/dist/managers/event-manager.d.ts +48 -0
- package/dist/managers/event-manager.d.ts.map +1 -0
- package/dist/managers/event-manager.js +110 -0
- package/dist/managers/meta-manager.d.ts +14 -0
- package/dist/managers/meta-manager.d.ts.map +1 -0
- package/dist/managers/meta-manager.js +20 -0
- package/dist/managers/page-manager.d.ts +26 -0
- package/dist/managers/page-manager.d.ts.map +1 -0
- package/dist/managers/page-manager.js +86 -0
- package/dist/managers/revision-manager.d.ts +32 -0
- package/dist/managers/revision-manager.d.ts.map +1 -0
- package/dist/managers/revision-manager.js +69 -0
- package/dist/managers/search-manager.d.ts +14 -0
- package/dist/managers/search-manager.d.ts.map +1 -0
- package/dist/managers/search-manager.js +24 -0
- package/dist/managers/user-manager.d.ts +34 -0
- package/dist/managers/user-manager.d.ts.map +1 -0
- package/dist/managers/user-manager.js +66 -0
- package/dist/package-constants.d.ts +5 -0
- package/dist/package-constants.d.ts.map +1 -0
- package/dist/package-constants.js +4 -0
- package/dist/request/rate-limiter.d.ts +13 -0
- package/dist/request/rate-limiter.d.ts.map +1 -0
- package/dist/request/rate-limiter.js +28 -0
- package/dist/request/request-manager.d.ts +29 -0
- package/dist/request/request-manager.d.ts.map +1 -0
- package/dist/request/request-manager.js +106 -0
- package/dist/structures/base-structure.d.ts +24 -0
- package/dist/structures/base-structure.d.ts.map +1 -0
- package/dist/structures/base-structure.js +13 -0
- package/dist/structures/page-structure.d.ts +81 -0
- package/dist/structures/page-structure.d.ts.map +1 -0
- package/dist/structures/page-structure.js +164 -0
- package/dist/structures/revision-structure.d.ts +46 -0
- package/dist/structures/revision-structure.d.ts.map +1 -0
- package/dist/structures/revision-structure.js +41 -0
- package/dist/structures/user-structure.d.ts +45 -0
- package/dist/structures/user-structure.d.ts.map +1 -0
- package/dist/structures/user-structure.js +52 -0
- package/dist/types/api.d.ts +14 -0
- package/dist/types/api.d.ts.map +1 -0
- package/dist/types/api.js +1 -0
- package/dist/types/client.d.ts +39 -0
- package/dist/types/client.d.ts.map +1 -0
- package/dist/types/client.js +1 -0
- package/dist/types/events.d.ts +141 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +1 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +7 -0
- package/dist/types/page.d.ts +105 -0
- package/dist/types/page.d.ts.map +1 -0
- package/dist/types/page.js +1 -0
- package/dist/types/revision.d.ts +83 -0
- package/dist/types/revision.d.ts.map +1 -0
- package/dist/types/revision.js +1 -0
- package/dist/types/site.d.ts +290 -0
- package/dist/types/site.d.ts.map +1 -0
- package/dist/types/site.js +1 -0
- package/dist/types/user.d.ts +100 -0
- package/dist/types/user.d.ts.map +1 -0
- package/dist/types/user.js +1 -0
- package/package.json +54 -8
- package/src/index.js +0 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { QueryParams, APIResponse } from "../types/index.js";
|
|
2
|
+
import { TokenType } from "../enums/index.js";
|
|
3
|
+
export interface RequestManagerOptions {
|
|
4
|
+
baseURL: string;
|
|
5
|
+
apiPath?: string;
|
|
6
|
+
userAgent?: string;
|
|
7
|
+
maxRetries?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare class RequestManager {
|
|
10
|
+
private baseURL;
|
|
11
|
+
private apiPath;
|
|
12
|
+
private userAgent?;
|
|
13
|
+
private maxRetries;
|
|
14
|
+
private limiter;
|
|
15
|
+
private tokenCache;
|
|
16
|
+
private cookies;
|
|
17
|
+
constructor(opt: RequestManagerOptions);
|
|
18
|
+
private buildURL;
|
|
19
|
+
get<T = APIResponse>(params: QueryParams, options?: {
|
|
20
|
+
apiPath?: string;
|
|
21
|
+
}): Promise<T>;
|
|
22
|
+
post<T = APIResponse>(params: QueryParams, options?: {
|
|
23
|
+
apiPath?: string;
|
|
24
|
+
tokenType?: TokenType;
|
|
25
|
+
}): Promise<T>;
|
|
26
|
+
fetchToken(type: TokenType): Promise<string>;
|
|
27
|
+
private request;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=request-manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-manager.d.ts","sourceRoot":"","sources":["../../src/request/request-manager.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAElE,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAG9C,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,qBAAa,cAAc;IACzB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,SAAS,CAAC,CAAS;IAC3B,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,OAAO,CAAc;IAC7B,OAAO,CAAC,UAAU,CAAqC;IACvD,OAAO,CAAC,OAAO,CAAgB;gBAEnB,GAAG,EAAE,qBAAqB;IAQtC,OAAO,CAAC,QAAQ;IAaH,GAAG,CAAC,CAAC,GAAG,WAAW,EAC9B,MAAM,EAAE,WAAW,EACnB,OAAO,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,GAC7B,OAAO,CAAC,CAAC,CAAC;IAKA,IAAI,CAAC,CAAC,GAAG,WAAW,EAC/B,MAAM,EAAE,WAAW,EACnB,OAAO,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,SAAS,CAAA;KAAE,GACpD,OAAO,CAAC,CAAC,CAAC;IAyBA,UAAU,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;YAmB3C,OAAO;CAwCtB"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import fetch from "cross-fetch";
|
|
2
|
+
import { RateLimiter } from "./rate-limiter.js";
|
|
3
|
+
import { APIError } from "../errors/api-error.js";
|
|
4
|
+
export class RequestManager {
|
|
5
|
+
constructor(opt) {
|
|
6
|
+
this.tokenCache = new Map();
|
|
7
|
+
this.cookies = [];
|
|
8
|
+
this.baseURL = opt.baseURL;
|
|
9
|
+
this.apiPath = opt.apiPath ?? "/api.php";
|
|
10
|
+
this.userAgent = opt.userAgent;
|
|
11
|
+
this.maxRetries = opt.maxRetries ?? 3;
|
|
12
|
+
this.limiter = new RateLimiter({ capacity: 5, refillPerSecond: 1 });
|
|
13
|
+
}
|
|
14
|
+
buildURL(params, apiPath) {
|
|
15
|
+
const url = new URL(apiPath ?? this.apiPath, this.baseURL);
|
|
16
|
+
Object.entries(params).forEach(([k, v]) => {
|
|
17
|
+
if (v === undefined)
|
|
18
|
+
return;
|
|
19
|
+
if (Array.isArray(v)) {
|
|
20
|
+
v.forEach((item) => url.searchParams.append(k, String(item)));
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
url.searchParams.set(k, String(v));
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
return url.toString();
|
|
27
|
+
}
|
|
28
|
+
async get(params, options) {
|
|
29
|
+
const url = this.buildURL(params, options?.apiPath);
|
|
30
|
+
return this.request(url, { method: "GET" });
|
|
31
|
+
}
|
|
32
|
+
async post(params, options) {
|
|
33
|
+
const body = new URLSearchParams();
|
|
34
|
+
if (options?.tokenType) {
|
|
35
|
+
const token = await this.fetchToken(options.tokenType);
|
|
36
|
+
body.append("token", token);
|
|
37
|
+
}
|
|
38
|
+
Object.entries(params).forEach(([k, v]) => {
|
|
39
|
+
if (v === undefined)
|
|
40
|
+
return;
|
|
41
|
+
if (Array.isArray(v)) {
|
|
42
|
+
v.forEach((item) => body.append(k, String(item)));
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
body.append(k, String(v));
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
const url = this.buildURL({}, options?.apiPath);
|
|
49
|
+
return this.request(url, {
|
|
50
|
+
method: "POST",
|
|
51
|
+
body,
|
|
52
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
async fetchToken(type) {
|
|
56
|
+
if (this.tokenCache.has(type))
|
|
57
|
+
return this.tokenCache.get(type);
|
|
58
|
+
const res = await this.get({
|
|
59
|
+
action: "query",
|
|
60
|
+
meta: "tokens",
|
|
61
|
+
type: type,
|
|
62
|
+
format: "json",
|
|
63
|
+
});
|
|
64
|
+
const token = res?.query?.tokens?.[`${type}token`];
|
|
65
|
+
if (!token)
|
|
66
|
+
throw new Error(`Failed to fetch token of type ${type}`);
|
|
67
|
+
this.tokenCache.set(type, token);
|
|
68
|
+
return token;
|
|
69
|
+
}
|
|
70
|
+
async request(url, init, attempt = 0) {
|
|
71
|
+
await this.limiter.removeTokens(1);
|
|
72
|
+
const headers = new Headers(init.headers);
|
|
73
|
+
if (this.userAgent)
|
|
74
|
+
headers.set("User-Agent", this.userAgent);
|
|
75
|
+
if (this.cookies.length > 0)
|
|
76
|
+
headers.set("Cookie", this.cookies.join("; "));
|
|
77
|
+
try {
|
|
78
|
+
const res = await fetch(url, { ...init, headers });
|
|
79
|
+
const setCookie = res.headers.get("set-cookie");
|
|
80
|
+
if (setCookie) {
|
|
81
|
+
this.cookies.push(setCookie);
|
|
82
|
+
}
|
|
83
|
+
if (!res.ok) {
|
|
84
|
+
if (attempt < this.maxRetries) {
|
|
85
|
+
const backoff = 2 ** attempt * 200;
|
|
86
|
+
await new Promise((r) => setTimeout(r, backoff));
|
|
87
|
+
return this.request(url, init, attempt + 1);
|
|
88
|
+
}
|
|
89
|
+
throw new Error(`HTTP ${res.status} ${res.statusText}`);
|
|
90
|
+
}
|
|
91
|
+
const data = await res.json();
|
|
92
|
+
if (data.error) {
|
|
93
|
+
throw new APIError(data.error.code, data.error.info);
|
|
94
|
+
}
|
|
95
|
+
return data;
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
if (attempt < this.maxRetries) {
|
|
99
|
+
const backoff = 2 ** attempt * 200;
|
|
100
|
+
await new Promise((r) => setTimeout(r, backoff));
|
|
101
|
+
return this.request(url, init, attempt + 1);
|
|
102
|
+
}
|
|
103
|
+
throw err;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Client } from "../client/client.js";
|
|
2
|
+
/**
|
|
3
|
+
* The base structure class that all other structures extend.
|
|
4
|
+
* @template T - The type of data this structure holds.
|
|
5
|
+
*/
|
|
6
|
+
export declare abstract class BaseStructure<T = unknown> {
|
|
7
|
+
/**
|
|
8
|
+
* The client that instantiated this structure.
|
|
9
|
+
*/
|
|
10
|
+
client: Client;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new instance of the BaseStructure.
|
|
13
|
+
* @param client - The client instance.
|
|
14
|
+
*/
|
|
15
|
+
constructor(client: Client);
|
|
16
|
+
/**
|
|
17
|
+
* Patches the structure with new data.
|
|
18
|
+
* @param data - The data to patch with.
|
|
19
|
+
* @returns The updated structure.
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
abstract _patch(data: T): this;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=base-structure.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-structure.d.ts","sourceRoot":"","sources":["../../src/structures/base-structure.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAElD;;;GAGG;AACH,8BAAsB,aAAa,CAAC,CAAC,GAAG,OAAO;IAC7C;;OAEG;IACI,MAAM,EAAE,MAAM,CAAC;IAEtB;;;OAGG;gBACS,MAAM,EAAE,MAAM;IAI1B;;;;;OAKG;aACa,MAAM,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI;CACtC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The base structure class that all other structures extend.
|
|
3
|
+
* @template T - The type of data this structure holds.
|
|
4
|
+
*/
|
|
5
|
+
export class BaseStructure {
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new instance of the BaseStructure.
|
|
8
|
+
* @param client - The client instance.
|
|
9
|
+
*/
|
|
10
|
+
constructor(client) {
|
|
11
|
+
this.client = client;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { BaseStructure } from "./base-structure.js";
|
|
2
|
+
import type { Client } from "../client/client.js";
|
|
3
|
+
import { ProtectionLevel } from "../enums/index.js";
|
|
4
|
+
import { Revision } from "./revision-structure.js";
|
|
5
|
+
import type { APIPage } from "../types/index.js";
|
|
6
|
+
/**
|
|
7
|
+
* Represents a wiki page.
|
|
8
|
+
*/
|
|
9
|
+
export declare class Page extends BaseStructure<Partial<APIPage>> {
|
|
10
|
+
/**
|
|
11
|
+
* The ID of the page.
|
|
12
|
+
*/
|
|
13
|
+
pageid: number;
|
|
14
|
+
/**
|
|
15
|
+
* The namespace ID of the page.
|
|
16
|
+
*/
|
|
17
|
+
ns: number;
|
|
18
|
+
/**
|
|
19
|
+
* The title of the page.
|
|
20
|
+
*/
|
|
21
|
+
title: string;
|
|
22
|
+
/**
|
|
23
|
+
* The extract (summary) of the page.
|
|
24
|
+
*/
|
|
25
|
+
extract?: string;
|
|
26
|
+
/**
|
|
27
|
+
* The content of the page.
|
|
28
|
+
*/
|
|
29
|
+
content?: string;
|
|
30
|
+
/**
|
|
31
|
+
* The categories the page belongs to.
|
|
32
|
+
*/
|
|
33
|
+
categories: string[];
|
|
34
|
+
/**
|
|
35
|
+
* Creates a new instance of the Page.
|
|
36
|
+
* @param client - The client instance.
|
|
37
|
+
* @param data - The data to initialize the page with.
|
|
38
|
+
*/
|
|
39
|
+
constructor(client: Client, data: Partial<APIPage>);
|
|
40
|
+
/**
|
|
41
|
+
* Patches the page with new data.
|
|
42
|
+
* @param data - The data to patch with.
|
|
43
|
+
* @returns The updated page.
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
_patch(data: Partial<APIPage>): this;
|
|
47
|
+
/**
|
|
48
|
+
* Edits the page content.
|
|
49
|
+
* @param text - The new content of the page.
|
|
50
|
+
* @param summary - The edit summary.
|
|
51
|
+
* @returns A Promise that resolves to the updated Page.
|
|
52
|
+
*/
|
|
53
|
+
edit(text: string, summary?: string): Promise<Page>;
|
|
54
|
+
/**
|
|
55
|
+
* Deletes the page.
|
|
56
|
+
* @param reason - The reason for deletion.
|
|
57
|
+
* @returns A Promise that resolves when the page is deleted.
|
|
58
|
+
*/
|
|
59
|
+
delete(reason?: string): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Protects the page.
|
|
62
|
+
* @param level - The protection level.
|
|
63
|
+
* @param expiry - The expiry time for the protection.
|
|
64
|
+
* @param reason - The reason for protection.
|
|
65
|
+
* @returns A Promise that resolves when the page is protected.
|
|
66
|
+
*/
|
|
67
|
+
protect(level: ProtectionLevel, expiry: string, reason?: string): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Fetches the revision history of the page.
|
|
70
|
+
* @returns A Promise that resolves to an array of Revision objects.
|
|
71
|
+
*/
|
|
72
|
+
fetchHistory(): Promise<Revision[]>;
|
|
73
|
+
/**
|
|
74
|
+
* Reverts the page to a specific revision.
|
|
75
|
+
* @param revid - The revision ID to revert to.
|
|
76
|
+
* @param summary - The edit summary.
|
|
77
|
+
* @returns A Promise that resolves to the updated Page.
|
|
78
|
+
*/
|
|
79
|
+
revertTo(revid: number, summary?: string): Promise<Page>;
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=page-structure.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page-structure.d.ts","sourceRoot":"","sources":["../../src/structures/page-structure.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAa,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,KAAK,EACV,OAAO,EAIR,MAAM,mBAAmB,CAAC;AAE3B;;GAEG;AACH,qBAAa,IAAK,SAAQ,aAAa,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACvD;;OAEG;IACI,MAAM,EAAG,MAAM,CAAC;IACvB;;OAEG;IACI,EAAE,EAAG,MAAM,CAAC;IACnB;;OAEG;IACI,KAAK,EAAG,MAAM,CAAC;IACtB;;OAEG;IACI,OAAO,CAAC,EAAE,MAAM,CAAC;IACxB;;OAEG;IACI,OAAO,CAAC,EAAE,MAAM,CAAC;IACxB;;OAEG;IACI,UAAU,EAAE,MAAM,EAAE,CAAM;IAEjC;;;;OAIG;gBACS,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC;IAKlD;;;;;OAKG;IACI,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI;IAyB3C;;;;;OAKG;IACU,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAehE;;;;OAIG;IACU,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAanD;;;;;;OAMG;IACU,OAAO,CAClB,KAAK,EAAE,eAAe,EACtB,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,IAAI,CAAC;IAehB;;;OAGG;IACU,YAAY,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;IAuBhD;;;;;OAKG;IACU,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CA+BtE"}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { BaseStructure } from "./base-structure.js";
|
|
2
|
+
import { TokenType } from "../enums/index.js";
|
|
3
|
+
import { Revision } from "./revision-structure.js";
|
|
4
|
+
/**
|
|
5
|
+
* Represents a wiki page.
|
|
6
|
+
*/
|
|
7
|
+
export class Page extends BaseStructure {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new instance of the Page.
|
|
10
|
+
* @param client - The client instance.
|
|
11
|
+
* @param data - The data to initialize the page with.
|
|
12
|
+
*/
|
|
13
|
+
constructor(client, data) {
|
|
14
|
+
super(client);
|
|
15
|
+
/**
|
|
16
|
+
* The categories the page belongs to.
|
|
17
|
+
*/
|
|
18
|
+
this.categories = [];
|
|
19
|
+
this._patch(data);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Patches the page with new data.
|
|
23
|
+
* @param data - The data to patch with.
|
|
24
|
+
* @returns The updated page.
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
_patch(data) {
|
|
28
|
+
if (data.pageid !== undefined)
|
|
29
|
+
this.pageid = data.pageid;
|
|
30
|
+
if (data.ns !== undefined)
|
|
31
|
+
this.ns = data.ns;
|
|
32
|
+
if (data.title !== undefined)
|
|
33
|
+
this.title = data.title;
|
|
34
|
+
if (data.extract !== undefined)
|
|
35
|
+
this.extract = data.extract;
|
|
36
|
+
if (data.revisions &&
|
|
37
|
+
Array.isArray(data.revisions) &&
|
|
38
|
+
data.revisions.length > 0) {
|
|
39
|
+
const rev = data.revisions[0];
|
|
40
|
+
if (rev["*"])
|
|
41
|
+
this.content = rev["*"];
|
|
42
|
+
else if (rev.slots) {
|
|
43
|
+
const main = rev.slots.main || rev.slots.Main;
|
|
44
|
+
if (main && main["*"])
|
|
45
|
+
this.content = main["*"];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (data.categories && Array.isArray(data.categories)) {
|
|
49
|
+
this.categories = data.categories.map((c) => c.title);
|
|
50
|
+
}
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Edits the page content.
|
|
55
|
+
* @param text - The new content of the page.
|
|
56
|
+
* @param summary - The edit summary.
|
|
57
|
+
* @returns A Promise that resolves to the updated Page.
|
|
58
|
+
*/
|
|
59
|
+
async edit(text, summary) {
|
|
60
|
+
const params = {
|
|
61
|
+
action: "edit",
|
|
62
|
+
title: this.title,
|
|
63
|
+
text: text,
|
|
64
|
+
format: "json",
|
|
65
|
+
};
|
|
66
|
+
if (summary)
|
|
67
|
+
params.summary = summary;
|
|
68
|
+
await this.client.requestManager.post(params, {
|
|
69
|
+
tokenType: TokenType.Csrf,
|
|
70
|
+
});
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Deletes the page.
|
|
75
|
+
* @param reason - The reason for deletion.
|
|
76
|
+
* @returns A Promise that resolves when the page is deleted.
|
|
77
|
+
*/
|
|
78
|
+
async delete(reason) {
|
|
79
|
+
const params = {
|
|
80
|
+
action: "delete",
|
|
81
|
+
title: this.title,
|
|
82
|
+
format: "json",
|
|
83
|
+
};
|
|
84
|
+
if (reason)
|
|
85
|
+
params.reason = reason;
|
|
86
|
+
await this.client.requestManager.post(params, {
|
|
87
|
+
tokenType: TokenType.Csrf,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Protects the page.
|
|
92
|
+
* @param level - The protection level.
|
|
93
|
+
* @param expiry - The expiry time for the protection.
|
|
94
|
+
* @param reason - The reason for protection.
|
|
95
|
+
* @returns A Promise that resolves when the page is protected.
|
|
96
|
+
*/
|
|
97
|
+
async protect(level, expiry, reason) {
|
|
98
|
+
const params = {
|
|
99
|
+
action: "protect",
|
|
100
|
+
title: this.title,
|
|
101
|
+
protections: `edit=${level}|move=${level}`,
|
|
102
|
+
expiry: expiry,
|
|
103
|
+
format: "json",
|
|
104
|
+
};
|
|
105
|
+
if (reason)
|
|
106
|
+
params.reason = reason;
|
|
107
|
+
await this.client.requestManager.post(params, {
|
|
108
|
+
tokenType: TokenType.Csrf,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Fetches the revision history of the page.
|
|
113
|
+
* @returns A Promise that resolves to an array of Revision objects.
|
|
114
|
+
*/
|
|
115
|
+
async fetchHistory() {
|
|
116
|
+
const res = await this.client.requestManager.get({
|
|
117
|
+
action: "query",
|
|
118
|
+
prop: "revisions",
|
|
119
|
+
titles: this.title,
|
|
120
|
+
rvprop: "ids|timestamp|user|content",
|
|
121
|
+
rvlimit: 50,
|
|
122
|
+
format: "json",
|
|
123
|
+
});
|
|
124
|
+
const pages = res?.query?.pages || {};
|
|
125
|
+
const pageId = Object.keys(pages)[0];
|
|
126
|
+
const page = pages[pageId];
|
|
127
|
+
if (!page || !page.revisions)
|
|
128
|
+
return [];
|
|
129
|
+
return page.revisions.map((rev) => new Revision(this.client, rev));
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Reverts the page to a specific revision.
|
|
133
|
+
* @param revid - The revision ID to revert to.
|
|
134
|
+
* @param summary - The edit summary.
|
|
135
|
+
* @returns A Promise that resolves to the updated Page.
|
|
136
|
+
*/
|
|
137
|
+
async revertTo(revid, summary) {
|
|
138
|
+
const res = await this.client.requestManager.get({
|
|
139
|
+
action: "query",
|
|
140
|
+
prop: "info",
|
|
141
|
+
titles: this.title,
|
|
142
|
+
format: "json",
|
|
143
|
+
});
|
|
144
|
+
const pages = res?.query?.pages || {};
|
|
145
|
+
const pageId = Object.keys(pages)[0];
|
|
146
|
+
const page = pages[pageId];
|
|
147
|
+
const currentRevid = page.lastrevid;
|
|
148
|
+
if (currentRevid === revid)
|
|
149
|
+
return this;
|
|
150
|
+
const params = {
|
|
151
|
+
action: "edit",
|
|
152
|
+
title: this.title,
|
|
153
|
+
undo: currentRevid,
|
|
154
|
+
undoafter: revid,
|
|
155
|
+
format: "json",
|
|
156
|
+
};
|
|
157
|
+
if (summary)
|
|
158
|
+
params.summary = summary;
|
|
159
|
+
await this.client.requestManager.post(params, {
|
|
160
|
+
tokenType: TokenType.Csrf,
|
|
161
|
+
});
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { BaseStructure } from "./base-structure.js";
|
|
2
|
+
import type { Client } from "../client/client.js";
|
|
3
|
+
import type { APIRevision } from "../types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Represents a revision of a page.
|
|
6
|
+
*/
|
|
7
|
+
export declare class Revision extends BaseStructure<Partial<APIRevision>> {
|
|
8
|
+
/**
|
|
9
|
+
* The ID of the revision.
|
|
10
|
+
*/
|
|
11
|
+
revid: number;
|
|
12
|
+
/**
|
|
13
|
+
* The timestamp of the revision.
|
|
14
|
+
*/
|
|
15
|
+
timestamp: Date;
|
|
16
|
+
/**
|
|
17
|
+
* The user who created the revision.
|
|
18
|
+
*/
|
|
19
|
+
user: string;
|
|
20
|
+
/**
|
|
21
|
+
* The content of the revision.
|
|
22
|
+
*/
|
|
23
|
+
content?: string;
|
|
24
|
+
/**
|
|
25
|
+
* The comment associated with the revision.
|
|
26
|
+
*/
|
|
27
|
+
comment?: string;
|
|
28
|
+
/**
|
|
29
|
+
* The title of the page this revision belongs to.
|
|
30
|
+
*/
|
|
31
|
+
title?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new instance of the Revision.
|
|
34
|
+
* @param client - The client instance.
|
|
35
|
+
* @param data - The data to initialize the revision with.
|
|
36
|
+
*/
|
|
37
|
+
constructor(client: Client, data: Partial<APIRevision>);
|
|
38
|
+
/**
|
|
39
|
+
* Patches the revision with new data.
|
|
40
|
+
* @param data - The data to patch with.
|
|
41
|
+
* @returns The updated revision.
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
_patch(data: Partial<APIRevision>): this;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=revision-structure.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"revision-structure.d.ts","sourceRoot":"","sources":["../../src/structures/revision-structure.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD;;GAEG;AACH,qBAAa,QAAS,SAAQ,aAAa,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IAC/D;;OAEG;IACI,KAAK,EAAG,MAAM,CAAC;IACtB;;OAEG;IACI,SAAS,EAAG,IAAI,CAAC;IACxB;;OAEG;IACI,IAAI,EAAG,MAAM,CAAC;IACrB;;OAEG;IACI,OAAO,CAAC,EAAE,MAAM,CAAC;IACxB;;OAEG;IACI,OAAO,CAAC,EAAE,MAAM,CAAC;IACxB;;OAEG;IACI,KAAK,CAAC,EAAE,MAAM,CAAC;IAEtB;;;;OAIG;gBACS,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,WAAW,CAAC;IAKtD;;;;;OAKG;IACI,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,WAAW,CAAC,GAAG,IAAI;CAchD"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { BaseStructure } from "./base-structure.js";
|
|
2
|
+
/**
|
|
3
|
+
* Represents a revision of a page.
|
|
4
|
+
*/
|
|
5
|
+
export class Revision extends BaseStructure {
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new instance of the Revision.
|
|
8
|
+
* @param client - The client instance.
|
|
9
|
+
* @param data - The data to initialize the revision with.
|
|
10
|
+
*/
|
|
11
|
+
constructor(client, data) {
|
|
12
|
+
super(client);
|
|
13
|
+
this._patch(data);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Patches the revision with new data.
|
|
17
|
+
* @param data - The data to patch with.
|
|
18
|
+
* @returns The updated revision.
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
_patch(data) {
|
|
22
|
+
if (data.revid !== undefined)
|
|
23
|
+
this.revid = data.revid;
|
|
24
|
+
if (data.timestamp !== undefined)
|
|
25
|
+
this.timestamp = new Date(data.timestamp);
|
|
26
|
+
if (data.user !== undefined)
|
|
27
|
+
this.user = data.user;
|
|
28
|
+
if (data.comment !== undefined)
|
|
29
|
+
this.comment = data.comment;
|
|
30
|
+
if (data.title)
|
|
31
|
+
this.title = data.title;
|
|
32
|
+
if (data["*"])
|
|
33
|
+
this.content = data["*"];
|
|
34
|
+
else if (data.slots) {
|
|
35
|
+
const main = data.slots.main || data.slots.Main;
|
|
36
|
+
if (main && main["*"])
|
|
37
|
+
this.content = main["*"];
|
|
38
|
+
}
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { BaseStructure } from "./base-structure.js";
|
|
2
|
+
import type { Client } from "../client/client.js";
|
|
3
|
+
import type { APIUser } from "../types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Represents a wiki user.
|
|
6
|
+
*/
|
|
7
|
+
export declare class User extends BaseStructure<Partial<APIUser>> {
|
|
8
|
+
/**
|
|
9
|
+
* The ID of the user.
|
|
10
|
+
*/
|
|
11
|
+
id: number;
|
|
12
|
+
/**
|
|
13
|
+
* The name of the user.
|
|
14
|
+
*/
|
|
15
|
+
name: string;
|
|
16
|
+
/**
|
|
17
|
+
* The registration date of the user.
|
|
18
|
+
*/
|
|
19
|
+
registrationDate?: Date;
|
|
20
|
+
/**
|
|
21
|
+
* The groups the user belongs to.
|
|
22
|
+
*/
|
|
23
|
+
groups: string[];
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new instance of the User.
|
|
26
|
+
* @param client - The client instance.
|
|
27
|
+
* @param data - The data to initialize the user with.
|
|
28
|
+
*/
|
|
29
|
+
constructor(client: Client, data: Partial<APIUser>);
|
|
30
|
+
/**
|
|
31
|
+
* Patches the user with new data.
|
|
32
|
+
* @param data - The data to patch with.
|
|
33
|
+
* @returns The updated user.
|
|
34
|
+
* @internal
|
|
35
|
+
*/
|
|
36
|
+
_patch(data: Partial<APIUser>): this;
|
|
37
|
+
/**
|
|
38
|
+
* Blocks the user.
|
|
39
|
+
* @param expiry - The expiry time for the block.
|
|
40
|
+
* @param reason - The reason for the block.
|
|
41
|
+
* @returns A Promise that resolves when the user is blocked.
|
|
42
|
+
*/
|
|
43
|
+
block(expiry: string, reason: string): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=user-structure.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user-structure.d.ts","sourceRoot":"","sources":["../../src/structures/user-structure.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAEjD;;GAEG;AACH,qBAAa,IAAK,SAAQ,aAAa,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACvD;;OAEG;IACI,EAAE,EAAG,MAAM,CAAC;IACnB;;OAEG;IACI,IAAI,EAAG,MAAM,CAAC;IACrB;;OAEG;IACI,gBAAgB,CAAC,EAAE,IAAI,CAAC;IAC/B;;OAEG;IACI,MAAM,EAAE,MAAM,EAAE,CAAM;IAE7B;;;;OAIG;gBACS,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC;IAKlD;;;;;OAKG;IACI,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI;IAQ3C;;;;;OAKG;IACU,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAYlE"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { BaseStructure } from "./base-structure.js";
|
|
2
|
+
import { TokenType } from "../enums/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Represents a wiki user.
|
|
5
|
+
*/
|
|
6
|
+
export class User extends BaseStructure {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new instance of the User.
|
|
9
|
+
* @param client - The client instance.
|
|
10
|
+
* @param data - The data to initialize the user with.
|
|
11
|
+
*/
|
|
12
|
+
constructor(client, data) {
|
|
13
|
+
super(client);
|
|
14
|
+
/**
|
|
15
|
+
* The groups the user belongs to.
|
|
16
|
+
*/
|
|
17
|
+
this.groups = [];
|
|
18
|
+
this._patch(data);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Patches the user with new data.
|
|
22
|
+
* @param data - The data to patch with.
|
|
23
|
+
* @returns The updated user.
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
_patch(data) {
|
|
27
|
+
if (data.userid !== undefined)
|
|
28
|
+
this.id = data.userid;
|
|
29
|
+
if (data.name !== undefined)
|
|
30
|
+
this.name = data.name;
|
|
31
|
+
if (data.registration)
|
|
32
|
+
this.registrationDate = new Date(data.registration);
|
|
33
|
+
if (data.groups)
|
|
34
|
+
this.groups = data.groups;
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Blocks the user.
|
|
39
|
+
* @param expiry - The expiry time for the block.
|
|
40
|
+
* @param reason - The reason for the block.
|
|
41
|
+
* @returns A Promise that resolves when the user is blocked.
|
|
42
|
+
*/
|
|
43
|
+
async block(expiry, reason) {
|
|
44
|
+
await this.client.requestManager.post({
|
|
45
|
+
action: "block",
|
|
46
|
+
user: this.name,
|
|
47
|
+
expiry: expiry,
|
|
48
|
+
reason: reason,
|
|
49
|
+
format: "json",
|
|
50
|
+
}, { tokenType: TokenType.Csrf });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface QueryParams {
|
|
2
|
+
[key: string]: string | number | boolean | (string | number | boolean)[] | undefined;
|
|
3
|
+
}
|
|
4
|
+
export interface APIResponse<T = unknown> {
|
|
5
|
+
batchcomplete?: string;
|
|
6
|
+
continue?: Record<string, string>;
|
|
7
|
+
query?: T;
|
|
8
|
+
error?: {
|
|
9
|
+
code: string;
|
|
10
|
+
info: string;
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/types/api.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,WAAW;IAC1B,CAAC,GAAG,EAAE,MAAM,GACR,MAAM,GACN,MAAM,GACN,OAAO,GACP,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,EAAE,GAC7B,SAAS,CAAC;CACf;AAED,MAAM,WAAW,WAAW,CAAC,CAAC,GAAG,OAAO;IACtC,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAClC,KAAK,CAAC,EAAE,CAAC,CAAC;IACV,KAAK,CAAC,EAAE;QACN,IAAI,EAAE,MAAM,CAAC;QACb,IAAI,EAAE,MAAM,CAAC;QACb,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;KACxB,CAAC;CACH"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|