diff --git a/src/api_response_types.ts b/src/api_response_types.ts index a81a9ff..c3a6983 100644 --- a/src/api_response_types.ts +++ b/src/api_response_types.ts @@ -15,6 +15,69 @@ export type ApiEditResponse = { newtimestamp: string; }; +export type ApiBlockResponse = ApiResponse; + +export type ApiUnblockResponse = ApiResponse; + +export type ApiEmailUserResponse = ApiResponse; + +export type ApiQueryUsersResponse = ApiResponse; + +export type ApiQueryGlobalUserInfoResponse = ApiResponse; + +export interface ApiParseResponse { + title: string; + pageid: number; + revid: number; + text: string; + langlinks: Array<{ + lang: string; // language code + url: string; + langname: string; + autonym: string; + title: string; + }>; + categories: Array<{ + sortkey: string; + category: string; + hidden?: true; + }>; + links: Array<{ + ns: number; + title: string; + exists: boolean; + }>; + templates: Array<{ + ns: number; + title: string; + exists: boolean; + }>; + images: string[]; + externallinks: string[]; + sections: Array<{ + toclevel: number; + level: string; // int + line: string; + number: string; // int + index: string; // int + fromtitle: string; + byteoffset: number; + anchor: string; + }>; + parsewarnings: Array; + displaytitle: string; + iwlinks: Array<{ + prefix: string; + url: string; + title: string; + }>; + properties: { + notoc: ''; + noindex: ''; + [name: string]: string; + }; +} + export type ApiSearchResult = { ns: number; title: string; diff --git a/src/page.ts b/src/page.ts index 8f2a061..bfd3e0a 100644 --- a/src/page.ts +++ b/src/page.ts @@ -12,34 +12,7 @@ import type { ApiUndeleteParams, WikibaseClientApiDescriptionParams, } from './api_params'; -import { ApiPage, ApiRevision, LogEvent } from './api_response_types'; - -export interface PageViewOptions { - access?: 'all-access' | 'desktop' | 'mobile-app' | 'mobile-web'; - agent?: 'all-agents' | 'user' | 'spider' | 'automated'; - granularity?: 'daily' | 'monthly'; - start?: Date; - end?: Date; -} -export interface PageViewData { - project: string; - article: string; - granularity: string; - timestamp: string; - access: string; - agent: string; - views: number; -} - -export interface AuthorshipData { - totalBytes: number; - users: Array<{ - id: number; - name: string; - bytes: number; - percent: number; - }>; -} +import { ApiPage, ApiParseResponse, ApiRevision, LogEvent } from './api_response_types'; export interface MwnPageStatic { new (title: MwnTitle | string, namespace?: number): MwnPage; @@ -50,27 +23,9 @@ export interface MwnPage extends MwnTitle { getTalkPage(): MwnPage; getSubjectPage(): MwnPage; text(): Promise; - categories(): Promise< - { - sortkey: string; - category: string; - hidden: boolean; - }[] - >; - templates(): Promise< - { - ns: number; - title: string; - exists: boolean; - }[] - >; - links(): Promise< - { - ns: number; - title: string; - exists: boolean; - }[] - >; + categories(): Promise; + templates(): Promise; + links(): Promise; backlinks(): Promise; transclusions(): Promise; images(): Promise; @@ -163,7 +118,7 @@ export default function (bot: mwn): MwnPageStatic { * @returns {Promise} Resolved with array of objects like * { sortkey: '...', category: '...', hidden: true } */ - categories(): Promise<{ sortkey: string; category: string; hidden: boolean }[]> { + categories(): Promise { return bot .request({ action: 'parse', @@ -178,7 +133,7 @@ export default function (bot: mwn): MwnPageStatic { * @returns {Promise} Resolved with array of objects like * { ns: 10, title: 'Template:Cite web', exists: true } */ - templates(): Promise<{ ns: number; title: string; exists: boolean }[]> { + templates(): Promise { return bot .request({ action: 'parse', @@ -193,7 +148,7 @@ export default function (bot: mwn): MwnPageStatic { * @returns {Promise} Resolved with array of objects like * { ns: 0, title: 'Main Page', exists: true } */ - links(): Promise<{ ns: number; title: string; exists: boolean }[]> { + links(): Promise { return bot .request({ action: 'parse', @@ -686,3 +641,30 @@ export default function (bot: mwn): MwnPageStatic { return Page as MwnPageStatic; } + +export interface PageViewOptions { + access?: 'all-access' | 'desktop' | 'mobile-app' | 'mobile-web'; + agent?: 'all-agents' | 'user' | 'spider' | 'automated'; + granularity?: 'daily' | 'monthly'; + start?: Date; + end?: Date; +} +export interface PageViewData { + project: string; + article: string; + granularity: string; + timestamp: string; + access: string; + agent: string; + views: number; +} + +export interface AuthorshipData { + totalBytes: number; + users: Array<{ + id: number; + name: string; + bytes: number; + percent: number; + }>; +} diff --git a/src/user.ts b/src/user.ts index 7f63cef..d65414d 100644 --- a/src/user.ts +++ b/src/user.ts @@ -9,7 +9,16 @@ import type { ApiUnblockParams, } from './api_params'; import { rejectWithError } from './error'; -import { LogEvent, UserContribution } from './api_response_types'; +import { + ApiBlockResponse, + ApiEditResponse, + ApiEmailUserResponse, + ApiQueryGlobalUserInfoResponse, + ApiQueryUsersResponse, + ApiUnblockResponse, + LogEvent, + UserContribution, +} from './api_response_types'; export interface MwnUserStatic { new (username: string): MwnUser; @@ -23,12 +32,12 @@ export interface MwnUser { contribsGen(options?: ApiQueryUserContribsParams): AsyncGenerator; logs(options?: ApiQueryLogEventsParams): Promise; logsGen(options?: ApiQueryLogEventsParams): AsyncGenerator; - info(props?: ApiQueryUsersParams['usprop']): Promise; - globalinfo(props?: ApiQueryGlobalUserInfoParams['guiprop']): Promise; - sendMessage(header: string, message: string): Promise; - email(subject: string, message: string, options?: ApiEmailUserParams): Promise; - block(options: ApiBlockParams): Promise; - unblock(options: ApiUnblockParams): Promise; + info(props?: ApiQueryUsersParams['usprop']): Promise; + globalinfo(props?: ApiQueryGlobalUserInfoParams['guiprop']): Promise; + sendMessage(header: string, message: string): Promise; + email(subject: string, message: string, options?: ApiEmailUserParams): Promise; + block(options: ApiBlockParams): Promise; + unblock(options: ApiUnblockParams): Promise; } export default function (bot: mwn) { @@ -142,7 +151,7 @@ export default function (bot: mwn) { * Get global user info for wikis with CentralAuth * @param {string|string[]} props */ - globalinfo(props?: ApiQueryGlobalUserInfoParams['guiprop']): Promise { + globalinfo(props?: ApiQueryGlobalUserInfoParams['guiprop']): Promise { return bot .request({ action: 'query', @@ -161,14 +170,14 @@ export default function (bot: mwn) { * @param {string} message * @returns {Promise} */ - sendMessage(header: string, message: string) { + sendMessage(header: string, message: string): Promise { return this.talkpage.newSection(header, message); } /** * Send the user an email */ - email(subject: string, message: string, options?: ApiEmailUserParams): Promise { + email(subject: string, message: string, options?: ApiEmailUserParams): Promise { return bot .request({ action: 'emailuser', @@ -197,7 +206,7 @@ export default function (bot: mwn) { * Block the user * @param {Object} options - additional API options */ - block(options: ApiBlockParams): Promise { + block(options: ApiBlockParams): Promise { return bot .request({ action: 'block', @@ -212,7 +221,7 @@ export default function (bot: mwn) { * Unblock the user * @param {Object} options - additional API options */ - unblock(options: ApiUnblockParams): Promise { + unblock(options: ApiUnblockParams): Promise { return bot .request({ action: 'unblock',