Skip to content

Commit

Permalink
fix: config and content client fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alinarublea committed Sep 24, 2024
1 parent 6685c1c commit 325c8d4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const validateSite = (site) => {
throw new Error('Site is required');
}

const contentSource = site.getConfig()?.content?.source;
const contentSource = site.getConfig()?.getHlxContentConfig()?.source;
if (!isObject(contentSource)) {
throw new Error('Site must have a valid content source');
}
Expand Down Expand Up @@ -181,7 +181,7 @@ export default class ContentClient {
const { log = console, env } = context;

const config = {};
const contentSourceType = site.getConfig().content?.source?.type;
const contentSourceType = site.getConfig()?.getHlxContentConfig()?.source?.type;
const envMapping = SUPPORTED_CONTENT_SOURCES.get(contentSourceType);

if (envMapping) {
Expand All @@ -195,11 +195,11 @@ export default class ContentClient {

constructor(config, site, log) {
validateSite(site);
validateConfiguration(config, site.getConfig().content.source.type);
validateConfiguration(config, site.getConfig()?.getHlxContentConfig()?.source?.type);

this.log = log;
this.config = config;
this.contentSource = site.getConfig().content.source;
this.contentSource = site.getConfig()?.getHlxContentConfig()?.source;
this.site = site;
this.rawClient = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ describe('ContentClient', () => {

const siteConfigGoogleDrive = {
getId: () => 'test-site',
getConfig: () => ({ content: { source: { type: 'drive.google' } } }),
getConfig: () => ({ getHlxContentConfig: () => ({ source: { type: 'drive.google' } }) }),
};

const siteConfigOneDrive = {
getId: () => 'test-site',
getConfig: () => ({ content: { source: { type: 'onedrive' } } }),
getConfig: () => ({ getHlxContentConfig: () => ({ source: { type: 'onedrive' } }) }),
};

const sampleMetadata = new Map(
Expand Down Expand Up @@ -166,12 +166,12 @@ describe('ContentClient', () => {
});

it('throws an error if site has no content source', () => {
const invalidSite = { getConfig: () => ({ }) };
const invalidSite = { getConfig: () => ({ getHlxContentConfig: () => ({ }) }) };
expect(() => new ContentClient(env, invalidSite, log)).to.throw('Site must have a valid content source');
});

it('throws an error if site\'s content source type is unsupported', () => {
const invalidSite = { getConfig: () => ({ content: { source: {} } }) };
const invalidSite = { getConfig: () => ({ getHlxContentConfig: () => ({ source: {} }) }) };
expect(() => new ContentClient(env, invalidSite, log)).to.throw('Unsupported content source type: undefined');
});

Expand Down
13 changes: 13 additions & 0 deletions packages/spacecat-shared-data-access/src/models/site/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const Config = (data = {}) => {
self.getSlackMentions = (type) => state?.handlers[type]?.mentions?.slack;
self.getHandlerConfig = (type) => state?.handlers[type];
self.getHandlers = () => state.handlers;
self.getHlxContentConfig = () => state.content;
self.getImports = () => state.imports;
self.getExcludedURLs = (type) => state?.handlers[type]?.excludedURLs;
self.getManualOverwrites = (type) => state?.handlers[type]?.manualOverwrites;
Expand All @@ -74,6 +75,17 @@ export const Config = (data = {}) => {
};
};

self.updateHlxContentConfig = (source, path) => {
state.content = {
source,
path,
};
};

self.updateImports = (imports) => {
state.imports = imports;
};

self.updateSlackMentions = (type, mentions) => {
state.handlers = state.handlers || {};
state.handlers[type] = state.handlers[type] || {};
Expand Down Expand Up @@ -106,6 +118,7 @@ Config.fromDynamoItem = (dynamoItem) => Config(dynamoItem);

Config.toDynamoItem = (config) => ({
slack: config.getSlackConfig(),
content: config.getHlxContentConfig(),
handlers: config.getHandlers(),
imports: config.getImports(),
});
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ describe('Config Tests', () => {
config.updateFixedURLs('broken-backlinks', []);
expect(config.getFixedURLs('broken-backlinks')).to.be.an('array').that.is.empty;
});

it('correctly updates the imports array', () => {
const config = Config();
const imports = [
{ type: 'import1' },
{ type: 'import2' },
];
config.updateImports(imports);

const updatedImports = config.getImports();
expect(updatedImports).to.deep.equal(imports);
});

it('correctly updates the hlx content configuration', () => {
const config = Config();
const source = 'source1';
const path = 'path1';
config.updateHlxContentConfig(source, path);

const hlxContentConfig = config.getHlxContentConfig();
expect(hlxContentConfig.source).to.equal(source);
expect(hlxContentConfig.path).to.equal(path);
});
});

describe('fromDynamoItem Static Method', () => {
Expand Down

0 comments on commit 325c8d4

Please sign in to comment.