Skip to content

Commit

Permalink
page: fix inconsistent behaviour in getRedirectTarget(), other refact…
Browse files Browse the repository at this point in the history
…oring

Fixes #75
  • Loading branch information
siddharthvp committed Sep 22, 2024
1 parent 59d29b4 commit 58dc00d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 47 deletions.
64 changes: 20 additions & 44 deletions src/page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { MwnError } from './error';

import type { Mwn, MwnTitle, EditTransform } from './bot';
import type { Mwn, MwnTitle, EditTransform, ApiQueryResponse } from './bot';
import type {
ApiDeleteParams,
ApiEditPageParams,
Expand All @@ -19,7 +18,6 @@ export interface MwnPageStatic {
}

export interface MwnPage extends MwnTitle {
data: any;
getTalkPage(): MwnPage;
getSubjectPage(): MwnPage;
/**
Expand Down Expand Up @@ -166,8 +164,6 @@ export interface MwnPage extends MwnTitle {

export default function (bot: Mwn): MwnPageStatic {
class Page extends bot.Title implements MwnPage {
data: any;

constructor(title: MwnTitle | string, namespace?: number) {
// bot property is set by mwn#Page() method
if (typeof title === 'string') {
Expand All @@ -177,7 +173,6 @@ export default function (bot: Mwn): MwnPageStatic {
} else {
throw new Error('unknown constructor type for mwn Page');
}
this.data = {};
}

/**
Expand Down Expand Up @@ -311,16 +306,15 @@ export default function (bot: Mwn): MwnPageStatic {
/** @inheritDoc */
subpages(options?: ApiQueryAllPagesParams): Promise<string[]> {
return bot
.request({
action: 'query',
.query({
list: 'allpages',
apprefix: this.title + '/',
apnamespace: this.namespace,
aplimit: 'max',
...options,
})
.then((data) => {
return data.query.allpages.map((pg: ApiPage) => pg.title);
return data.query.allpages.map((pg) => pg.title);

Check failure on line 317 in src/page.ts

View workflow job for this annotation

GitHub Actions / build

Parameter 'pg' implicitly has an 'any' type.
});
}

Expand All @@ -333,45 +327,30 @@ export default function (bot: Mwn): MwnPageStatic {

/** @inheritDoc */
getRedirectTarget(): Promise<string> {
if (this.data.text) {
let target = /^\s*#redirect \[\[(.*?)\]\]/.exec(this.data.text);
if (!target) {
return Promise.resolve(this.toText());
}
return Promise.resolve(new bot.Title(target[1]).toText());
}
return bot
.request({
action: 'query',
.query({
titles: this.toString(),
redirects: '1',
redirects: true,
})
.then((data) => {
let page = data.query.pages[0];
if (page.missing) {
return Promise.reject(new MwnError.MissingPage());
}
return page.title;
this.throwIfPageMissing(data);

Check failure on line 336 in src/page.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type 'ApiResponse' is not assignable to parameter of type 'ApiQueryResponse'.
return data.query.pages[0].title;
});
}

/** @inheritDoc */
getCreator(): Promise<string> {
return bot
.request({
action: 'query',
.query({
titles: this.toString(),
prop: 'revisions',
rvprop: 'user',
rvlimit: 1,
rvdir: 'newer',
})
.then((data) => {
let page = data.query.pages[0];
if (page.missing) {
return Promise.reject(new MwnError.MissingPage());
}
return page.revisions[0].user;
this.throwIfPageMissing(data);

Check failure on line 352 in src/page.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type 'ApiResponse' is not assignable to parameter of type 'ApiQueryResponse'.
return data.query.pages[0].revisions[0].user;
});
}

Expand All @@ -396,19 +375,14 @@ export default function (bot: Mwn): MwnPageStatic {

/** @inheritDoc */
getDescription(customOptions: WikibaseClientApiDescriptionParams) {
// ApiParams
return bot
.request({
action: 'query',
.query({
prop: 'description',
titles: this.toString(),
...customOptions,
})
.then((data) => {
let page = data.query.pages[0];
if (page.missing) {
return Promise.reject(new MwnError.MissingPage());
}
this.throwIfPageMissing(data);

Check failure on line 385 in src/page.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type 'ApiResponse' is not assignable to parameter of type 'ApiQueryResponse'.
return data.query.pages[0].description;
});
}
Expand All @@ -420,19 +394,15 @@ export default function (bot: Mwn): MwnPageStatic {
customOptions?: ApiQueryRevisionsParams
): Promise<ApiRevision[]> {
return bot
.request({
action: 'query',
.query({
prop: 'revisions',
titles: this.toString(),
rvprop: props || 'ids|timestamp|flags|comment|user',
rvlimit: limit || 50,
...customOptions,
})
.then((data) => {
let page = data.query.pages[0];
if (page.missing) {
return Promise.reject(new MwnError.MissingPage());
}
this.throwIfPageMissing(data);

Check failure on line 405 in src/page.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type 'ApiResponse' is not assignable to parameter of type 'ApiQueryResponse'.
return data.query.pages[0].revisions;
});
}
Expand Down Expand Up @@ -507,6 +477,12 @@ export default function (bot: Mwn): MwnPageStatic {
}
}

private throwIfPageMissing(data: ApiQueryResponse) {
if (data.query.pages[0].missing || data.query.pages[0].invalid) {
throw new MwnError.MissingPage();
}
}

/** @inheritDoc */
async pageViews(options: PageViewOptions = {}): Promise<PageViewData[]> {
let project = bot.options.apiUrl.match(/.*\/(.*?)\.(?:org|com|net)/)?.[1];
Expand Down
13 changes: 10 additions & 3 deletions tests/page.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const { bot, expect, teardown } = require('./base/test_wiki');
const { MwnError } = require('../build/error');

describe('Page', async function () {
this.timeout(10000);
Expand Down Expand Up @@ -40,8 +41,7 @@ describe('Page', async function () {
return page.categories().then((cats) => {
expect(cats).to.be.instanceOf(Array);
expect(cats.length).to.be.gte(1); // check it on testwiki, could change
expect(cats[0].category).to.be.a('string');
expect(cats[0].sortkey).to.be.a('string');
expect(cats[0]).to.be.a('string');
});
});

Expand Down Expand Up @@ -90,10 +90,17 @@ describe('Page', async function () {
return page.links().then((links) => {
expect(links).to.be.instanceOf(Array);
expect(links.length).to.be.gte(1);
expect(links[0].title).to.be.a('string');
expect(links[0]).to.be.a('string');
});
});

it('links on non-existing page', async function () {
await expect(new bot.Page('1239012390e32413').links())
.to.be.eventually.rejectedWith(MwnError)
.that.has.property('code')
.which.equals('missingtitle');
});

it('backlinks', function () {
return page.backlinks().then((backlinks) => {
expect(backlinks).to.be.instanceOf(Array);
Expand Down

0 comments on commit 58dc00d

Please sign in to comment.