From b8fad00cb24a6991103105c8615443b3fe9297a5 Mon Sep 17 00:00:00 2001 From: sunafterrainwm Date: Wed, 22 Mar 2023 19:01:57 +0800 Subject: [PATCH] fix typescript errors and stop making date set* methods chainable Since MwnDate extends Date, and Date set* methods return numbers rather than `this`, revert to keeping that same behaviour as there's no way to represent this in types. --- src/api_response_types.ts | 4 ++++ src/bot.ts | 14 ++++++++------ src/date.ts | 15 ++------------- src/page.ts | 12 +++++++++--- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/api_response_types.ts b/src/api_response_types.ts index 3a75bca..ee09fd6 100644 --- a/src/api_response_types.ts +++ b/src/api_response_types.ts @@ -3,6 +3,10 @@ export interface ApiResponse { [prop: string]: any; } +export interface ApiResponseSubType { + [prop: string]: any; +} + export type ApiEditResponse = { // fix result: string; diff --git a/src/bot.ts b/src/bot.ts index e9ef84f..95746dc 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -81,6 +81,7 @@ import { ApiRevision, ApiRollbackResponse, ApiSearchResult, + ApiResponseSubType, ApiUndeleteResponse, ApiUploadResponse, } from './api_response_types'; @@ -134,7 +135,7 @@ export type ApiParams = { | Date | File | { - stream: ReadableStream; + stream: NodeJS.ReadableStream; name: string; }; }; @@ -232,7 +233,7 @@ export class mwn { * Cookie jar for the bot instance - holds session and login cookies * @type {tough.CookieJar} */ - cookieJar = new tough.CookieJar(); + cookieJar: tough.CookieJar = new tough.CookieJar(); static requestDefaults: RawRequestParams = { headers: { @@ -766,7 +767,8 @@ export class mwn { action: 'paraminfo', modules: action, }).then((response) => { - return response.paraminfo.modules[0].parameters.find((p) => p.name === 'token').tokentype; + return response.paraminfo.modules[0].parameters.find((p: ApiResponseSubType) => p.name === 'token') + .tokentype; }); } @@ -822,7 +824,7 @@ export class mwn { ...options, }).then((data) => { let result: Record = {}; - data.query.allmessages.forEach((obj) => { + data.query.allmessages.forEach((obj: ApiResponseSubType) => { if (!obj.missing) { result[obj.name] = obj.content; } @@ -1578,7 +1580,7 @@ export class mwn { throw new Error(`massQuery: batch field in query must be an array`); } const limit = batchSize || (this.hasApiHighLimit ? 500 : 50); - const batches = arrayChunk(batchValues, limit); + const batches = arrayChunk(batchValues, limit); const numBatches = batches.length; for (let i = 0; i < numBatches; i++) { @@ -1870,7 +1872,7 @@ export class mwn { }, responseType: 'json', }).then((oresResponse) => { - Object.assign(response, Object.values(oresResponse.data)[0].scores); + Object.assign(response, Object.values(oresResponse.data)[0].scores); }); }, 0, diff --git a/src/date.ts b/src/date.ts index a1fb035..2465ad0 100644 --- a/src/date.ts +++ b/src/date.ts @@ -119,6 +119,8 @@ export interface MwnDateStatic { export default function (bot: mwn) { class XDate extends Date implements MwnDate { + constructor(...args: ConstructorParameters); + constructor(timestamp: string); constructor(...args: any[]) { if (args.length === 1 && typeof args[0] === 'string') { // parse MediaWiki format: YYYYMMDDHHmmss @@ -372,19 +374,6 @@ export default function (bot: mwn) { } } - // Tweak set* methods (setHours, setUTCMinutes, etc) so that they - // return the modified XDate object rather than the seconds-since-epoch - // representation which is what JS Date() gives - Object.getOwnPropertyNames(Date.prototype) - .filter((f) => f.startsWith('set')) - .forEach((func) => { - let proxy = XDate.prototype[func]; - XDate.prototype[func] = function (...args) { - proxy.call(this, ...args); - return this; - }; - }); - return XDate as MwnDateStatic; } diff --git a/src/page.ts b/src/page.ts index 63530c9..beac289 100644 --- a/src/page.ts +++ b/src/page.ts @@ -12,7 +12,7 @@ import type { ApiUndeleteParams, WikibaseClientApiDescriptionParams, } from './api_params'; -import { ApiPage, ApiParseResponse, ApiRevision, LogEvent } from './api_response_types'; +import { ApiPage, ApiParseResponse, ApiRevision, ApiResponseSubType, LogEvent } from './api_response_types'; export interface MwnPageStatic { new (title: MwnTitle | string, namespace?: number): MwnPage; @@ -535,7 +535,13 @@ export default function (bot: mwn): MwnPageStatic { date.setUTCDate(1); date.setUTCMonth(date.getUTCMonth() - 1); start = start || date; - end = end || new bot.date().setUTCDate(1); + end = + end || + (function () { + let d = new bot.date(); + d.setUTCDate(1); + return d; + })(); } let startString = new bot.date(start).format('YYYYMMDD'), @@ -578,7 +584,7 @@ export default function (bot: mwn): MwnPageStatic { throw new Error(err && err.response && err.response.data && err.response.data.Error); } - const tokens = Object.values(json.revisions[0])[0].tokens; + const tokens = Object.values(json.revisions[0])[0].tokens; let data: AuthorshipData = { totalBytes: 0,