Skip to content

Commit

Permalink
feat(hub-common): add function to get feed configuration (#1261)
Browse files Browse the repository at this point in the history
* feat(hub-common): add function to get feed configuration

* fix(hub-common): fix feed configuration unit tests

* fix(hub-common): fix additional feed  configuration unit tests

* feat(hub-common): add function to set feed configuration

* fix(hub-common): fix set feed configuration tests
  • Loading branch information
sansth1010 authored Oct 9, 2023
1 parent 07be412 commit 79fcc47
Show file tree
Hide file tree
Showing 3 changed files with 537 additions and 0 deletions.
129 changes: 129 additions & 0 deletions packages/common/src/sites/feed-configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { IModel } from "../types";

type FeedFormat = "dcat-us" | "dcat-ap" | "rss";

/**
* Returns feed configuration from a site model
*
* @param {IModel} site - site model
* @param {FeedFormat} format - feed format
* @param {string} version - semantic version
*/
export function getFeedConfiguration(
site: IModel,
format: FeedFormat,
version: string
) {
if (format === "dcat-us") {
return getDcatUsConfig(site, version);
}

if (format === "dcat-ap") {
return getDcatApConfig(site, version);
}

if (format === "rss") {
return getRssConfig(site, version);
}

throw new Error("Unsupported feed format");
}

/**
* Returns feed configuration from a site model
*
* @param {IModel} site - site model
* @param {FeedFormat} format - feed format
* @param {string} version - semantic version
* @param {Record<string, any>} feedConfig - feed configuration
*/
export function setFeedConfiguration(
site: IModel,
format: FeedFormat,
version: string,
feedConfig: Record<string, any>
) {
if (format === "dcat-us") {
setDcatUsConfig(site, version, feedConfig);
return;
}

if (format === "dcat-ap") {
setDcatApConfig(site, version, feedConfig);
return;
}

if (format === "rss") {
setRssConfig(site, version, feedConfig);
return;
}

throw new Error("Unsupported feed format");
}

function getDcatApConfig(site: IModel, version: string) {
if (getMajorVersion(version) === "2") {
return site.data.feeds.dcatAP2XX || site.data.feeds.dcatAP201;
}

throw new Error("Unsupported DCAT AP version");
}

function getDcatUsConfig(site: IModel, version: string) {
if (getMajorVersion(version) === "1") {
return site.data.feeds.dcatUS1X || site.data.feeds.dcatUS11;
}

throw new Error("Unsupported DCAT US version");
}

function getRssConfig(site: IModel, version: string) {
if (getMajorVersion(version) === "2") {
return site.data.feeds.rss2;
}

throw new Error("Unsupported RSS version");
}

function setDcatApConfig(
site: IModel,
version: string,
config: Record<string, any>
) {
if (getMajorVersion(version) === "2") {
site.data.feeds.dcatAP2XX = config;
return;
}

throw new Error("Unsupported DCAT AP Version");
}

function setDcatUsConfig(
site: IModel,
version: string,
config: Record<string, any>
) {
if (getMajorVersion(version) === "1") {
site.data.feeds.dcatUS1X = config;
return;
}

throw new Error("Unsupported DCAT US Version");
}

function setRssConfig(
site: IModel,
version: string,
config: Record<string, any>
) {
if (getMajorVersion(version) === "2") {
site.data.feeds.rss2 = config;
return;
}

throw new Error("Unsupported RSS Version");
}

function getMajorVersion(version: string) {
return version.split(".")[0];
}
1 change: 1 addition & 0 deletions packages/common/src/sites/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from "./HubSites";
export * from "./site-schema-version";
export * from "./themes";
export * from "./upgrade-site-schema";
export * from "./feed-configuration";
// No longer exported b/c site app registration is now handled
// by the domain service due to requirement to send signed HMAC request
// export * from "./registerSiteAsApplication";
Loading

0 comments on commit 79fcc47

Please sign in to comment.