Skip to content

Commit

Permalink
fix typescript error
Browse files Browse the repository at this point in the history
  • Loading branch information
sunafterrainwm committed Nov 27, 2022
1 parent 4142b2a commit 690cf30
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
4 changes: 4 additions & 0 deletions src/api_response_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export interface ApiResponse {
[prop: string]: any;
}

export interface ApiResponseSubType {
[prop: string]: any;
}

export type ApiEditResponse = {
// fix
result: string;
Expand Down
14 changes: 8 additions & 6 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import {
ApiRevision,
ApiRollbackResponse,
ApiSearchResult,
ApiResponseSubType,
ApiUndeleteResponse,
ApiUploadResponse,
} from './api_response_types';
Expand Down Expand Up @@ -135,7 +136,7 @@ export type ApiParams = {
| Date
| File
| {
stream: ReadableStream;
stream: NodeJS.ReadableStream;
name: string;
};
};
Expand Down Expand Up @@ -233,7 +234,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: {
Expand Down Expand Up @@ -773,7 +774,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;
});
}

Expand Down Expand Up @@ -829,7 +831,7 @@ export class mwn {
...options,
}).then((data) => {
let result: Record<string, string> = {};
data.query.allmessages.forEach((obj) => {
data.query.allmessages.forEach((obj: ApiResponseSubType) => {
if (!obj.missing) {
result[obj.name] = obj.content;
}
Expand Down Expand Up @@ -1585,7 +1587,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(<string[]>batchValues, limit);
const numBatches = batches.length;

for (let i = 0; i < numBatches; i++) {
Expand Down Expand Up @@ -1877,7 +1879,7 @@ export class mwn {
},
responseType: 'json',
}).then((oresResponse) => {
Object.assign(response, Object.values(oresResponse.data)[0].scores);
Object.assign(response, Object.values<ApiResponseSubType>(oresResponse.data)[0].scores);
});
},
0,
Expand Down
15 changes: 2 additions & 13 deletions src/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ export interface MwnDateStatic {

export default function (bot: mwn) {
class XDate extends Date implements MwnDate {
constructor(...args: ConstructorParameters<DateConstructor>);
constructor(timestamp: string);
constructor(...args: any[]) {
if (args.length === 1 && typeof args[0] === 'string') {
// parse MediaWiki format: YYYYMMDDHHmmss
Expand Down Expand Up @@ -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;
}

Expand Down
12 changes: 9 additions & 3 deletions src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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<ApiResponseSubType>(json.revisions[0])[0].tokens;

let data: AuthorshipData = {
totalBytes: 0,
Expand Down

0 comments on commit 690cf30

Please sign in to comment.