Skip to content

Commit

Permalink
improve typings
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Jun 19, 2021
1 parent fac01f1 commit 5e900f6
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 64 deletions.
63 changes: 63 additions & 0 deletions src/api_response_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
displaytitle: string;
iwlinks: Array<{
prefix: string;
url: string;
title: string;
}>;
properties: {
notoc: '';
noindex: '';
[name: string]: string;
};
}

export type ApiSearchResult = {
ns: number;
title: string;
Expand Down
86 changes: 34 additions & 52 deletions src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,27 +23,9 @@ export interface MwnPage extends MwnTitle {
getTalkPage(): MwnPage;
getSubjectPage(): MwnPage;
text(): Promise<string>;
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<ApiParseResponse['categories']>;
templates(): Promise<ApiParseResponse['templates']>;
links(): Promise<ApiParseResponse['links']>;
backlinks(): Promise<string[]>;
transclusions(): Promise<string[]>;
images(): Promise<string[]>;
Expand Down Expand Up @@ -163,7 +118,7 @@ export default function (bot: mwn): MwnPageStatic {
* @returns {Promise<Object[]>} Resolved with array of objects like
* { sortkey: '...', category: '...', hidden: true }
*/
categories(): Promise<{ sortkey: string; category: string; hidden: boolean }[]> {
categories(): Promise<ApiParseResponse['categories']> {
return bot
.request({
action: 'parse',
Expand All @@ -178,7 +133,7 @@ export default function (bot: mwn): MwnPageStatic {
* @returns {Promise<Object[]>} Resolved with array of objects like
* { ns: 10, title: 'Template:Cite web', exists: true }
*/
templates(): Promise<{ ns: number; title: string; exists: boolean }[]> {
templates(): Promise<ApiParseResponse['templates']> {
return bot
.request({
action: 'parse',
Expand All @@ -193,7 +148,7 @@ export default function (bot: mwn): MwnPageStatic {
* @returns {Promise<Object[]>} Resolved with array of objects like
* { ns: 0, title: 'Main Page', exists: true }
*/
links(): Promise<{ ns: number; title: string; exists: boolean }[]> {
links(): Promise<ApiParseResponse['links']> {
return bot
.request({
action: 'parse',
Expand Down Expand Up @@ -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;
}>;
}
33 changes: 21 additions & 12 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,12 +32,12 @@ export interface MwnUser {
contribsGen(options?: ApiQueryUserContribsParams): AsyncGenerator<UserContribution>;
logs(options?: ApiQueryLogEventsParams): Promise<LogEvent[]>;
logsGen(options?: ApiQueryLogEventsParams): AsyncGenerator<LogEvent>;
info(props?: ApiQueryUsersParams['usprop']): Promise<any>;
globalinfo(props?: ApiQueryGlobalUserInfoParams['guiprop']): Promise<any>;
sendMessage(header: string, message: string): Promise<any>;
email(subject: string, message: string, options?: ApiEmailUserParams): Promise<any>;
block(options: ApiBlockParams): Promise<any>;
unblock(options: ApiUnblockParams): Promise<any>;
info(props?: ApiQueryUsersParams['usprop']): Promise<ApiQueryUsersResponse>;
globalinfo(props?: ApiQueryGlobalUserInfoParams['guiprop']): Promise<ApiQueryGlobalUserInfoResponse>;
sendMessage(header: string, message: string): Promise<ApiEditResponse>;
email(subject: string, message: string, options?: ApiEmailUserParams): Promise<ApiEmailUserResponse>;
block(options: ApiBlockParams): Promise<ApiBlockResponse>;
unblock(options: ApiUnblockParams): Promise<ApiUnblockResponse>;
}

export default function (bot: mwn) {
Expand Down Expand Up @@ -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<any> {
globalinfo(props?: ApiQueryGlobalUserInfoParams['guiprop']): Promise<ApiQueryGlobalUserInfoResponse> {
return bot
.request({
action: 'query',
Expand All @@ -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<ApiEditResponse> {
return this.talkpage.newSection(header, message);
}

/**
* Send the user an email
*/
email(subject: string, message: string, options?: ApiEmailUserParams): Promise<any> {
email(subject: string, message: string, options?: ApiEmailUserParams): Promise<ApiEmailUserResponse> {
return bot
.request({
action: 'emailuser',
Expand Down Expand Up @@ -197,7 +206,7 @@ export default function (bot: mwn) {
* Block the user
* @param {Object} options - additional API options
*/
block(options: ApiBlockParams): Promise<any> {
block(options: ApiBlockParams): Promise<ApiBlockResponse> {
return bot
.request({
action: 'block',
Expand All @@ -212,7 +221,7 @@ export default function (bot: mwn) {
* Unblock the user
* @param {Object} options - additional API options
*/
unblock(options: ApiUnblockParams): Promise<any> {
unblock(options: ApiUnblockParams): Promise<ApiUnblockResponse> {
return bot
.request({
action: 'unblock',
Expand Down

0 comments on commit 5e900f6

Please sign in to comment.