wikibase-edit
Version:
Edit Wikibase from NodeJS
62 lines • 2.69 kB
JavaScript
import config from 'config';
import { yellow } from 'tiny-chalk';
import { WBK } from 'wikibase-sdk';
import { newError } from '../../../src/lib/error.js';
import { customFetch } from '../../../src/lib/request/fetch.js';
import { resolveTitle } from '../../../src/lib/resolve_title.js';
const { instance } = config;
const wbk = WBK({ instance });
async function getRevisions({ id, customInstance, limit, props }) {
customInstance = customInstance || instance;
const title = await resolveTitle(id, `${customInstance}/w/api.php`);
const customWbk = WBK({ instance: customInstance });
const url = customWbk.getRevisions({ ids: title, limit, prop: props });
const { query } = await customFetch(url).then(res => res.json());
const page = Object.values(query.pages)[0];
if (!(typeof page === 'object' && 'revisions' in page)) {
throw newError('revisions not found', 400, { id, url, page });
}
return page.revisions;
}
export async function getLastRevision(id, customInstance) {
const revisions = await getRevisions({ id, customInstance, limit: 1, props: ['comment', 'tags'] });
return revisions[0];
}
export const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
export async function getEntity(id) {
const url = wbk.getEntities({ ids: id });
const { entities } = await customFetch(url).then(res => res.json());
return entities[id];
}
export async function getEntityHistory(id, customInstance) {
// @ts-expect-error
const revisions = await getRevisions({ id, customInstance });
// @ts-expect-error
return revisions.sort(chronologically);
}
export async function getLastEditSummary(_id) {
const id = typeof _id === 'string' ? _id : _id.entity.id;
const revision = await getLastRevision(id);
return revision.comment;
}
// A function to quickly fail when a test gets an undesired positive answer
export const undesiredRes = done => res => {
console.warn(yellow('undesired positive res:'), res);
done(new Error('.then function was expected not to be called'));
};
// Same but for async/await tests that don't use done
export function shouldNotBeCalled(res) {
console.warn(yellow('undesired positive res:'), res);
const err = new Error('function was expected not to be called');
err.name = 'shouldNotBeCalled';
err.context = { res };
throw err;
}
export function rethrowShouldNotBeCalledErrors(err) {
if (err.name === 'shouldNotBeCalled')
throw err;
}
// See /wiki/Special:BotPasswords
export const isBotPassword = password => password.match(/^\w+@[a-z0-9]{32}$/);
const chronologically = (a, b) => a.revid - b.revid;
//# sourceMappingURL=utils.js.map