Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix typescript error and audit #25

Merged
merged 1 commit into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -81,6 +81,7 @@ import {
ApiRevision,
ApiRollbackResponse,
ApiSearchResult,
ApiResponseSubType,
ApiUndeleteResponse,
ApiUploadResponse,
} from './api_response_types';
Expand Down Expand Up @@ -134,7 +135,7 @@ export type ApiParams = {
| Date
| File
| {
stream: ReadableStream;
stream: NodeJS.ReadableStream;
name: string;
};
};
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
});
}

Expand Down Expand Up @@ -822,7 +824,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 @@ -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(<string[]>batchValues, limit);
const numBatches = batches.length;

for (let i = 0; i < numBatches; i++) {
Expand Down Expand Up @@ -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<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