mirror of
https://gitlab.com/Alamantus/Readlebee.git
synced 2025-05-13 21:51:18 +02:00
Add inventaireDomain to server config
For if someone wants to self-host Inventaire as well as Readlebee
This commit is contained in:
parent
c80e5e63ff
commit
a9d40193f0
3 changed files with 17 additions and 15 deletions
|
@ -1,5 +1,6 @@
|
||||||
{
|
{
|
||||||
"port": 3000,
|
"port": 3000,
|
||||||
"jwtSecretKey": "SomethingAtLeast32CharactersLong!",
|
"jwtSecretKey": "SomethingAtLeast32CharactersLong!",
|
||||||
"tokenExpireDays": 7
|
"tokenExpireDays": 7,
|
||||||
|
"inventaireDomain": "https://inventaire.io"
|
||||||
}
|
}
|
|
@ -1,7 +1,8 @@
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
|
|
||||||
class SearchController {
|
class SearchController {
|
||||||
constructor(searchTerm, language = 'en') {
|
constructor(inventaireDomain, searchTerm, language = 'en') {
|
||||||
|
this.inventaire = inventaireDomain;
|
||||||
this.term = searchTerm;
|
this.term = searchTerm;
|
||||||
this.lang = language;
|
this.lang = language;
|
||||||
}
|
}
|
||||||
|
@ -12,7 +13,7 @@ class SearchController {
|
||||||
|
|
||||||
searchInventaire() {
|
searchInventaire() {
|
||||||
if (this.hasQuery) {
|
if (this.hasQuery) {
|
||||||
const request = fetch(`https://inventaire.io/api/entities?action=search&search=${encodeURIComponent(this.term)}&lang=${encodeURIComponent(this.lang)}`)
|
const request = fetch(`${this.inventaire}/api/entities?action=search&search=${encodeURIComponent(this.term)}&lang=${encodeURIComponent(this.lang)}`)
|
||||||
request.catch(exception => {
|
request.catch(exception => {
|
||||||
console.error(exception);
|
console.error(exception);
|
||||||
return {
|
return {
|
||||||
|
@ -54,7 +55,7 @@ class SearchController {
|
||||||
),
|
),
|
||||||
link: (
|
link: (
|
||||||
typeof human.uri !== 'undefined'
|
typeof human.uri !== 'undefined'
|
||||||
? `https://inventaire.io/entity/${human.uri}`
|
? `${this.inventaire}/entity/${human.uri}`
|
||||||
: null
|
: null
|
||||||
),
|
),
|
||||||
image: (
|
image: (
|
||||||
|
@ -89,7 +90,7 @@ class SearchController {
|
||||||
),
|
),
|
||||||
link: (
|
link: (
|
||||||
typeof serie.uri !== 'undefined'
|
typeof serie.uri !== 'undefined'
|
||||||
? `https://inventaire.io/entity/${serie.uri}`
|
? `${this.inventaire}/entity/${serie.uri}`
|
||||||
: null
|
: null
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
@ -98,7 +99,7 @@ class SearchController {
|
||||||
const works = responseJSON.works.map(work => {
|
const works = responseJSON.works.map(work => {
|
||||||
const hasLabels = typeof work.labels !== 'undefined';
|
const hasLabels = typeof work.labels !== 'undefined';
|
||||||
const hasDescriptions = typeof work.descriptions !== 'undefined';
|
const hasDescriptions = typeof work.descriptions !== 'undefined';
|
||||||
const hasImage = typeof work.image !== 'undefined';
|
|
||||||
return {
|
return {
|
||||||
name: (
|
name: (
|
||||||
hasLabels && typeof work.labels[this.lang] !== 'undefined'
|
hasLabels && typeof work.labels[this.lang] !== 'undefined'
|
||||||
|
@ -120,12 +121,12 @@ class SearchController {
|
||||||
),
|
),
|
||||||
link: (
|
link: (
|
||||||
typeof work.uri !== 'undefined'
|
typeof work.uri !== 'undefined'
|
||||||
? `https://inventaire.io/entity/${work.uri}`
|
? `${this.inventaire}/entity/${work.uri}`
|
||||||
: null
|
: null
|
||||||
),
|
),
|
||||||
image: (
|
uri: (
|
||||||
hasImage && typeof work.image.url !== 'undefined'
|
typeof work.uri !== 'undefined'
|
||||||
? human.image
|
? work.uri
|
||||||
: null
|
: null
|
||||||
),
|
),
|
||||||
// Ratings and review count will be added here
|
// Ratings and review count will be added here
|
||||||
|
@ -147,7 +148,7 @@ class SearchController {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: property `wdt:P629` is a given entity (uri)'s list of editions (ISBNs).
|
// Note: property `wdt:P629` is a given entity (uri)'s list of editions (ISBNs).
|
||||||
const editionsRequest = fetch(`https://inventaire.io/api/entities?action=reverse-claims&uri=${encodeURIComponent(inventaireURI)}&property=wdt:P629`)
|
const editionsRequest = fetch(`${this.inventaire}/api/entities?action=reverse-claims&uri=${encodeURIComponent(inventaireURI)}&property=wdt:P629`)
|
||||||
editionsRequest.catch(exception => {
|
editionsRequest.catch(exception => {
|
||||||
console.error(exception);
|
console.error(exception);
|
||||||
return {
|
return {
|
||||||
|
@ -172,7 +173,7 @@ class SearchController {
|
||||||
return Promise.resolve([]);
|
return Promise.resolve([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const isbnsRequest = fetch(`https://inventaire.io/api/entities?action=by-uris&uris=${encodeURIComponent(editionURIs)}`);
|
const isbnsRequest = fetch(`${this.inventaire}/api/entities?action=by-uris&uris=${encodeURIComponent(editionURIs)}`);
|
||||||
isbnsRequest.catch(exception => {
|
isbnsRequest.catch(exception => {
|
||||||
console.error(exception);
|
console.error(exception);
|
||||||
return {
|
return {
|
||||||
|
@ -202,7 +203,7 @@ class SearchController {
|
||||||
const entity = responseJSON.entities[key];
|
const entity = responseJSON.entities[key];
|
||||||
return {
|
return {
|
||||||
uri: entity.uri,
|
uri: entity.uri,
|
||||||
cover: `https://inventaire.io/img/entities/${entity.claims['invp:P2'][0]}`,
|
url: `${this.inventaire}/img/entities/${entity.claims['invp:P2'][0]}`,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,7 +4,7 @@ async function routes(fastify, options) {
|
||||||
fastify.get('/api/search', async (request, reply) => {
|
fastify.get('/api/search', async (request, reply) => {
|
||||||
const searchTerm = typeof request.query.for !== 'undefined' ? request.query.for.trim() : '';
|
const searchTerm = typeof request.query.for !== 'undefined' ? request.query.for.trim() : '';
|
||||||
const language = typeof request.query.lang !== 'undefined' ? request.query.lang.trim().split('-')[0] : undefined; // Get base language in cases like 'en-US'
|
const language = typeof request.query.lang !== 'undefined' ? request.query.lang.trim().split('-')[0] : undefined; // Get base language in cases like 'en-US'
|
||||||
const search = new SearchController(searchTerm, language);
|
const search = new SearchController(fastify.siteConfig.inventaireDomain, searchTerm, language);
|
||||||
|
|
||||||
return await search.searchInventaire();
|
return await search.searchInventaire();
|
||||||
});
|
});
|
||||||
|
@ -12,7 +12,7 @@ async function routes(fastify, options) {
|
||||||
fastify.get('/api/search/cover', async (request, reply) => {
|
fastify.get('/api/search/cover', async (request, reply) => {
|
||||||
const inventaireURI = typeof request.query.uri !== 'undefined' ? request.query.uri.trim() : false;
|
const inventaireURI = typeof request.query.uri !== 'undefined' ? request.query.uri.trim() : false;
|
||||||
const language = typeof request.query.lang !== 'undefined' ? request.query.lang.trim().split('-')[0] : undefined; // Get base language in cases like 'en-US'
|
const language = typeof request.query.lang !== 'undefined' ? request.query.lang.trim().split('-')[0] : undefined; // Get base language in cases like 'en-US'
|
||||||
const search = new SearchController(null, language);
|
const search = new SearchController(fastify.siteConfig.inventaireDomain, null, language);
|
||||||
|
|
||||||
return await search.getInventaireCovers(inventaireURI);
|
return await search.getInventaireCovers(inventaireURI);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue