-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hub-common): add function to get feed configuration (#1261)
* 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
1 parent
07be412
commit 79fcc47
Showing
3 changed files
with
537 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.