diff --git a/src/dcat-us/constants/contexts.ts b/src/dcat-us/constants/contexts.ts index 6e80744..c476ef5 100644 --- a/src/dcat-us/constants/contexts.ts +++ b/src/dcat-us/constants/contexts.ts @@ -1,5 +1,5 @@ // Context header for DCAT US 1.1 -export const HEADER_V_1_1 = { +export const HEADER_V_1X = { '@context': 'https://project-open-data.cio.gov/v1.1/schema/catalog.jsonld', '@type': 'dcat:Catalog', @@ -9,7 +9,7 @@ export const HEADER_V_1_1 = { // Context header for DCAT US 3.0 // source: https://raw.githubusercontent.com/DOI-DO/dcat-us/refs/heads/main/context/dcat-us-3.0.jsonld -export const HEADER_V_3 = { +export const HEADER_V_3X = { '@context': { '@version': 1.1, '@protected': true, diff --git a/src/dcat-us/index.test.ts b/src/dcat-us/index.test.ts index 7b6c986..c2bdc9c 100644 --- a/src/dcat-us/index.test.ts +++ b/src/dcat-us/index.test.ts @@ -1,7 +1,7 @@ import { readableFromArray, streamToString } from '../test-helpers/stream-utils'; import { getDataStreamDcatUs } from './'; import * as datasetFromApi from '../test-helpers/mock-dataset.json'; -import { HEADER_V_3 } from './constants/contexts'; +import { HEADER_V_3X } from './constants/contexts'; async function generateDcatFeed(dataset, template, templateTransforms, version) { const { stream: dcatStream } = getDataStreamDcatUs(template, templateTransforms, version); @@ -68,7 +68,7 @@ describe('generating DCAT-US 3.0 feed', () => { it('formats catalog correctly', async function () { const { feed } = await generateDcatFeed([], {}, {}, version); - expect(feed['@context']).toStrictEqual(HEADER_V_3['@context']); + expect(feed['@context']).toStrictEqual(HEADER_V_3X['@context']); expect(feed['conformsTo']).toBe('https://resource.data.gov/profile/dcat-us#'); expect(feed['@type']).toBe('dcat:Catalog'); expect(Array.isArray(feed['dcat:dataset'])).toBeTruthy(); @@ -100,7 +100,7 @@ describe('generating DCAT-US 3.0 feed', () => { }, version); - expect(feed['@context']).toStrictEqual(HEADER_V_3['@context']); + expect(feed['@context']).toStrictEqual(HEADER_V_3X['@context']); expect(feed['@type']).toBe('dcat:Catalog'); expect(feed['@id']).toBe('hub.arcgis.com'); expect(feed['conformsTo']).toBe('https://resource.data.gov/profile/dcat-us#'); @@ -115,5 +115,4 @@ describe('generating DCAT-US 3.0 feed', () => { expect(feedResponse.publisher).toStrictEqual({ name: 'QA Premium Alpha Hub' }); expect(feedResponse.keyword).toStrictEqual(['Data collection', 'just modified']); }); - }); diff --git a/src/dcat-us/index.ts b/src/dcat-us/index.ts index d8e861f..8def459 100644 --- a/src/dcat-us/index.ts +++ b/src/dcat-us/index.ts @@ -1,27 +1,22 @@ import { compileDcatFeedEntry } from './compile-dcat-feed'; import { FeedFormatterStream } from './feed-formatter-stream'; import { TransformsList } from 'adlib'; -import { HEADER_V_3, HEADER_V_1_1 } from './constants/contexts'; +import { HEADER_V_3X, HEADER_V_1X } from './constants/contexts'; export function getDataStreamDcatUs(feedTemplate: any, feedTemplateTransforms: TransformsList, version: string) { const footer = '\n\t]\n}'; let header: string; let template: Record; - + if (version === '3.0') { const { header: templateHeader, ...restFeedTemplate } = feedTemplate; template = restFeedTemplate; - const catalogStr = JSON.stringify({ ...HEADER_V_3, ...templateHeader }, null, '\t'); - header = `${catalogStr.substring(0, catalogStr.length - 2)},\n\t"dcat:dataset": [\n`; + header = generateDcatUs3XHeader(templateHeader); } if (version === '1.1') { - const catalogStr = JSON.stringify(HEADER_V_1_1, null, '\t'); - header = `${catalogStr.substring( - 0, - catalogStr.length - 2, - )},\n\t"dataset": [\n`; template = feedTemplate; + header = generateDcatUs1XHeader(); } const formatFn = (chunk) => { @@ -32,3 +27,16 @@ export function getDataStreamDcatUs(feedTemplate: any, feedTemplateTransforms: T stream: new FeedFormatterStream(header, footer, ',\n', formatFn) }; } + +function generateDcatUs1XHeader() { + const catalogStr = JSON.stringify(HEADER_V_1X, null, '\t'); + return `${catalogStr.substring( + 0, + catalogStr.length - 2, + )},\n\t"dataset": [\n`; +} + +function generateDcatUs3XHeader(header: Record) { + const catalogStr = JSON.stringify({ ...HEADER_V_3X, ...header }, null, '\t'); + return `${catalogStr.substring(0, catalogStr.length - 2)},\n\t"dcat:dataset": [\n`; +} diff --git a/src/index.test.ts b/src/index.test.ts index f3fdd9e..4c59256 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -8,7 +8,7 @@ import { createMockKoopApp } from './test-helpers/create-mock-koop-app'; import { readableFromArray } from './test-helpers/stream-utils'; import { DcatUsError } from './dcat-us/dcat-us-error'; import { PassThrough } from 'stream'; -import { HEADER_V_3 } from './dcat-us/constants/contexts'; +import { HEADER_V_3X } from './dcat-us/constants/contexts'; function buildPluginAndApp(feedTemplate, feedTemplateTransforms) { let Output; @@ -166,7 +166,7 @@ describe('Output Plugin', () => { expect(res.body).toBeDefined(); const dcatStream = res.body; expect(dcatStream['@context']).toBeDefined(); - expect(dcatStream['@context']).toStrictEqual(HEADER_V_3['@context']); + expect(dcatStream['@context']).toStrictEqual(HEADER_V_3X['@context']); expect(dcatStream['@type']).toBe('dcat:Catalog'); expect(dcatStream['conformsTo']).toBe('https://resource.data.gov/profile/dcat-us#'); expect(dcatStream['@type']).toBe('dcat:Catalog');