api: add support for service name aliases

currently only used for bluesky
This commit is contained in:
wukko 2024-09-01 15:28:29 +06:00
parent 57050fb742
commit 740a75851e
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
3 changed files with 19 additions and 5 deletions

View file

@ -1,12 +1,13 @@
import { getVersion } from "@imput/version-info";
import { services } from "./processing/service-config.js";
import { friendlyServiceName } from "./processing/service-alias.js";
const version = await getVersion();
const disabledServices = process.env.DISABLED_SERVICES?.split(',') || [];
const enabledServices = new Set(Object.keys(services).filter(e => {
if (!disabledServices.includes(e)) {
return e;
return friendlyServiceName(e);
}
}));

View file

@ -6,6 +6,8 @@ import { createResponse } from "../processing/request.js";
import { testers } from "./service-patterns.js";
import matchAction from "./match-action.js";
import { friendlyServiceName } from "./service-alias.js";
import bilibili from "./services/bilibili.js";
import reddit from "./services/reddit.js";
import twitter from "./services/twitter.js";
@ -59,7 +61,7 @@ export default async function(host, patternMatch, obj) {
return createResponse("error", {
code: "error.api.link.unsupported",
context: {
service: host
service: friendlyServiceName(host),
}
});
}
@ -273,14 +275,14 @@ export default async function(host, patternMatch, obj) {
case "link.unsupported":
case "content.video.unavailable":
context = {
service: host,
service: friendlyServiceName(host),
}
break;
}
return createResponse("error", {
code: `error.api.${r.error}`,
context
context,
})
}
@ -301,7 +303,7 @@ export default async function(host, patternMatch, obj) {
return createResponse("error", {
code: "error.api.fetch.critical",
context: {
service: host
service: friendlyServiceName(host),
}
})
}

View file

@ -0,0 +1,11 @@
const friendlyNames = {
bsky: "bluesky",
}
const friendlyKeys = Object.keys(friendlyNames);
export const friendlyServiceName = (service) => {
if (service in friendlyKeys) {
return friendlyNames[service];
}
return service;
}