Commit bb3d23cb authored by Diegodlh's avatar Diegodlh
Browse files

Use mediawiki endpoint and save as Citation object

parent 1b034f3c
import { fetchSimpleCitation } from "./citoid";
import { fetchCitation } from "./citoid";
import * as nodeFetch from "node-fetch";
import { pages } from "./webpage/samplePages";
......@@ -16,8 +16,8 @@ describe("Simple Citoid citation", () => {
sampleUrl,
JSON.stringify(pages[sampleUrl].citoid)
);
return fetchSimpleCitation(sampleUrl).then((citation) => {
expect(citation).toEqual({
return fetchCitation(sampleUrl).then((citation) => {
expect(citation.simple).toEqual({
itemType: "webpage",
title: "Sample article",
tags: ["first tag", "second tag"],
......@@ -34,7 +34,7 @@ describe("Error responses", () => {
it("handles a successful non-JSON response", () => {
mockNodeFetch.__addCitoidResponse(url, "unexpected response format");
return expect(fetchSimpleCitation(url)).rejects.toThrow(
return expect(fetchCitation(url)).rejects.toThrow(
"Unknown Citoid response format"
);
});
......@@ -42,7 +42,7 @@ describe("Error responses", () => {
it("handles a successful unknown JSON response", () => {
const wrongCitation = { invalidKey: "invalidValue" };
mockNodeFetch.__addCitoidResponse(url, JSON.stringify([wrongCitation]));
return expect(fetchSimpleCitation(url)).rejects.toThrow(
return expect(fetchCitation(url)).rejects.toThrow(
"Unknown Citoid response format"
);
});
......@@ -55,8 +55,8 @@ it("accepts basic Citoid citation with required fields only", async () => {
url: "https://example.com/",
};
mockNodeFetch.__setDefaultResponse(JSON.stringify([basicCitation]));
const citation = await fetchSimpleCitation("https://example.com/");
expect(citation).toEqual({
const citation = await fetchCitation("https://example.com/");
expect(citation.simple).toEqual({
itemType: "journalArticle",
title: "sample title",
url: "https://example.com/",
......
import fetch, { Headers } from "node-fetch";
import { HTTPResponseError } from "./errors";
import { CITOID_API_ENDPOINT as API_ENDPOINT } from "./config";
import {
CitoidCitation,
MediaWikiBaseFieldCitation,
SimpleCitoidCitation,
isCitoidCitation,
} from "./citation/citationTypes";
import { CitoidCitation, isCitoidCitation } from "./citation/citationTypes";
import { Citation } from "./citation/citation";
// type CitoidRequestFormat = "mediawiki" | "mediawiki-basefields" | "zotero";
......@@ -73,17 +68,14 @@ function translateUrl(
});
}
export function fetchSimpleCitation(
export function fetchCitation(
url: string,
language?: string
): Promise<SimpleCitoidCitation> {
): Promise<Citation> {
return new Promise((resolve, reject) => {
translateUrl(url, language)
.then((citation) => {
const simpleCitation = new Citation(
citation as MediaWikiBaseFieldCitation
).simple;
resolve(simpleCitation);
resolve(new Citation(citation));
})
.catch((reason) => {
reject(reason);
......
import { FieldName } from "./translationField";
export const CITOID_API_ENDPOINT =
"https://en.wikipedia.org/api/rest_v1/data/citation/mediawiki-basefields";
"https://en.wikipedia.org/api/rest_v1/data/citation/mediawiki";
// todo: should this be used directly by the translation template constructor
// instead of passing it as an argument?
......
......@@ -104,7 +104,7 @@ export class Domain {
let baseCitation: MediaWikiBaseFieldCitation | undefined;
if (options.fillWithCitoid) {
// baseCitation = (await target.cache.citoid.getData()).citation
// baseCitation = (await target.cache.citoid.getData()).citation.simple
}
// compose the final outputs
......
......@@ -76,7 +76,7 @@ export class CitoidSelection extends Selection {
target.cache.citoid
.getData(false)
.then((data) => {
let selection = data.citation[field];
let selection = data.citation.simple[field];
if (!(selection instanceof Array)) {
if (selection === undefined) {
selection = [];
......
......@@ -43,7 +43,7 @@ describe("Citoid Cache", () => {
expect(cache.url).toBe(url);
return cache.fetchData().then((data) => {
expect(data.citation).toEqual({
expect(data.citation.simple).toEqual({
itemType: "webpage",
title: "Sample article",
tags: ["first tag", "second tag"],
......
// node-fetch 3 does not support jest yet
// https://github.com/node-fetch/node-fetch/issues/1265
import fetch from "node-fetch";
import { fetchSimpleCitation } from "../citoid";
import { SimpleCitoidCitation } from "../citation/citationTypes";
import { fetchCitation } from "../citoid";
import { JSDOM } from "jsdom";
import { HTTPResponseError } from "../errors";
import { Citation } from "../citation/citation";
abstract class ResponseCache {
url: string;
......@@ -113,7 +113,7 @@ function cleanDom(window: JSDOM["window"]): Document {
}
interface CitoidCacheData {
citation: SimpleCitoidCitation;
citation: Citation;
}
class CitoidCache extends ResponseCache {
......@@ -128,7 +128,7 @@ class CitoidCache extends ResponseCache {
fetchData(): Promise<CitoidCacheData> {
return new Promise<CitoidCacheData>((resolve, reject) => {
fetchSimpleCitation(this.url)
fetchCitation(this.url)
.then((citation) => {
const data: CitoidCacheData = {
citation: citation,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment