Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dist/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.config = void 0;
exports.config = {
contentStore: {
patterns: {
Expand Down
19 changes: 12 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.start = exports.getConfig = exports.setAssetStore = exports.setConfig = void 0;
const lodash_1 = require("lodash");
const config_1 = require("./config");
const s3_1 = require("./s3");
const setup_1 = require("./setup");
const validations_1 = require("./util/validations");
let appConfig = {};
let assetStore;
exports.setConfig = (config) => {
const setConfig = (config) => {
appConfig = config;
};
exports.setAssetStore = (instance) => {
exports.setConfig = setConfig;
const setAssetStore = (instance) => {
assetStore = instance;
};
exports.getConfig = () => {
exports.setAssetStore = setAssetStore;
const getConfig = () => {
return appConfig;
};
exports.start = (assetStoreInstance, config) => {
exports.getConfig = getConfig;
const start = (assetStoreInstance, config) => {
return new Promise((resolve, reject) => {
try {
appConfig = lodash_1.merge(config_1.config, appConfig, config || {});
validations_1.validateConfig(appConfig.contentStore);
appConfig = (0, lodash_1.merge)(config_1.config, appConfig, config || {});
(0, validations_1.validateConfig)(appConfig.contentStore);
assetStore = assetStoreInstance || assetStore;
return setup_1.init(appConfig.contentStore)
return (0, setup_1.init)(appConfig.contentStore)
.then((awsInstance) => {
const s3 = new s3_1.S3(assetStore, awsInstance, appConfig);
return resolve(s3);
Expand All @@ -34,3 +38,4 @@ exports.start = (assetStoreInstance, config) => {
}
});
};
exports.start = start;
41 changes: 41 additions & 0 deletions dist/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.errorMessages = exports.s3Messages = exports.deleteMessages = exports.fetchMessages = exports.searchMessages = exports.assetMessages = exports.contentTypeMessages = exports.entryMessages = exports.setupMessages = void 0;
exports.setupMessages = {
factoryConfig: (config) => `Factory configuration: ${JSON.stringify(config)}`,
factoryResult: (result) => `Result: ${JSON.stringify(result)}`,
};
exports.entryMessages = {
publishing: (entry) => `Publishing entry: ${JSON.stringify(entry)}`,
uploadResponse: (response) => `Entry S3 upload response: ${JSON.stringify(response)}`,
};
exports.contentTypeMessages = {
uploadResponse: (response) => `Content type S3 upload response: ${JSON.stringify(response)}`,
};
exports.assetMessages = {
publishing: (asset) => `Publishing asset: ${JSON.stringify(asset)}`,
uploadResponse: (response) => `Asset S3 upload response: ${JSON.stringify(response)}`,
unpublishing: (asset) => `Unpublishing asset: ${JSON.stringify(asset)}`,
};
exports.searchMessages = {
params: (params) => `Search S3 parameters: ${JSON.stringify(params)}`,
listObjectsResponse: (response) => `Search S3 – ListObjects response: ${JSON.stringify(response, null, 2)}`,
};
exports.fetchMessages = {
params: (params) => `Fetching contents with parameters: ${JSON.stringify(params)}`,
response: (response) => `Fetch contents response: ${JSON.stringify(response, null, 2)}`,
uniquePrefixes: (prefixes) => `Unique prefixes found: ${JSON.stringify(prefixes)}`,
uniqueContents: (contents) => `Unique contents found: ${JSON.stringify(contents)}`,
};
exports.deleteMessages = {
deleting: (input) => `Deleting item: ${JSON.stringify(input)}`,
deleteObjectResponse: (response) => `Delete object response: ${JSON.stringify(response, null, 2)}`,
removingFolders: (item) => `Removing folders: ${JSON.stringify(item)}`,
};
exports.s3Messages = {
itemsFound: (items) => `Items found: ${JSON.stringify(items)}`,
getObjectParams: (params) => `getObject called with parameters: ${JSON.stringify(params)}`,
};
exports.errorMessages = {
keyNotExist: (key, input) => `Key '${key}' does not exist in: ${JSON.stringify(input)}`,
};
99 changes: 51 additions & 48 deletions dist/s3.js

Large diffs are not rendered by default.

20 changes: 12 additions & 8 deletions dist/setup.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.init = void 0;
const aws_sdk_1 = __importDefault(require("aws-sdk"));
const debug_1 = __importDefault(require("debug"));
const util_1 = require("./util");
const debug = debug_1.default('cs-s3-setup');
const messages_1 = require("./messages");
const debug = (0, debug_1.default)('cs-s3-setup');
let S3;
const factory = (method, config) => {
debug(`Factory: ${JSON.stringify(config)}`);
debug(messages_1.setupMessages.factoryConfig(config));
return S3[method](config).promise().then((result) => {
debug(`Result: ${JSON.stringify(result)}`);
debug(messages_1.setupMessages.factoryResult(result));
return;
});
};
exports.init = (config) => {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
const init = (config) => {
return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
try {
config = util_1.formatConfig(config);
const awsConfig = util_1.buildAWSConfig(config);
config = (0, util_1.formatConfig)(config);
const awsConfig = (0, util_1.buildAWSConfig)(config);
S3 = new aws_sdk_1.default.S3(awsConfig);
yield factory('createBucket', config.bucketParams);
yield factory('putBucketVersioning', {
Expand All @@ -53,3 +56,4 @@ exports.init = (config) => {
}
}));
};
exports.init = init;
16 changes: 11 additions & 5 deletions dist/util/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.filterKeys = exports.buildAWSConfig = exports.formatConfig = exports.getPath = void 0;
const path_1 = require("path");
exports.getPath = (keys, input, versioning) => {
const messages_1 = require("../messages");
const getPath = (keys, input, versioning) => {
const path = [];
keys.forEach((key) => {
if (key.charAt(0) === ':') {
Expand All @@ -18,7 +20,7 @@ exports.getPath = (keys, input, versioning) => {
path.push(input[key]);
}
else {
throw new TypeError(`${key} did not exist in ${JSON.stringify(input)}!`);
throw new TypeError(messages_1.errorMessages.keyNotExist(key, input));
}
}
else {
Expand All @@ -27,7 +29,8 @@ exports.getPath = (keys, input, versioning) => {
});
return path_1.join.apply(this, path);
};
exports.formatConfig = (config) => {
exports.getPath = getPath;
const formatConfig = (config) => {
const bucket = config.bucketParams;
if (bucket.name) {
bucket.Bucket = bucket.name;
Expand All @@ -42,7 +45,8 @@ exports.formatConfig = (config) => {
config.apiVersion = config.apiVersion || 'latest';
return config;
};
exports.buildAWSConfig = (config) => {
exports.formatConfig = formatConfig;
const buildAWSConfig = (config) => {
const awsConfig = {
apiVersion: config.apiVersion,
region: config.region
Expand All @@ -55,11 +59,13 @@ exports.buildAWSConfig = (config) => {
}
return awsConfig;
};
exports.filterKeys = (input, keys = {}) => {
exports.buildAWSConfig = buildAWSConfig;
const filterKeys = (input, keys = {}) => {
for (const key in keys) {
if (keys[key]) {
delete input[key];
}
}
return input;
};
exports.filterKeys = filterKeys;
20 changes: 13 additions & 7 deletions dist/util/validations.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateDeletedObject = exports.validateUnpublishedObject = exports.validatePublishedObject = exports.validateLogger = exports.validateConfig = void 0;
const lodash_1 = require("lodash");
const config_1 = require("../config");
const requiredKeys = config_1.config.contentStore.internal.requiredKeys;
exports.validateConfig = (config) => {
const validateConfig = (config) => {
if (typeof config.bucketParams !== 'object' || !(config.bucketParams.Bucket || config.bucketParams.name)) {
throw new Error('Kindly provide valid bucket config!');
}
Expand All @@ -17,7 +18,8 @@ exports.validateConfig = (config) => {
}
return config;
};
exports.validateLogger = (logger) => {
exports.validateConfig = validateConfig;
const validateLogger = (logger) => {
let flag = false;
if (!logger) {
return flag;
Expand All @@ -30,31 +32,35 @@ exports.validateLogger = (logger) => {
});
return !flag;
};
exports.validateLogger = validateLogger;
const validateObject = (action, asset) => {
if (typeof asset !== 'object' || asset === null || asset instanceof Array) {
throw new Error(`Asset ${asset} should be of type 'plain object'`);
}
const keys = requiredKeys[action];
keys.forEach((key) => {
if (!(lodash_1.hasIn(asset, key))) {
if (!((0, lodash_1.hasIn)(asset, key))) {
throw new Error(`Required key '${key}' not found in ${JSON.stringify(asset)}`);
}
});
};
exports.validatePublishedObject = (obj, requiredKeys) => {
const validatePublishedObject = (obj, requiredKeys) => {
if (requiredKeys && typeof requiredKeys === 'object') {
for (const key in requiredKeys) {
if (requiredKeys[key]) {
if (!lodash_1.hasIn(obj, key)) {
if (!(0, lodash_1.hasIn)(obj, key)) {
throw new Error(`Required key '${key}' was not found in ${JSON.stringify(obj)}`);
}
}
}
}
};
exports.validateUnpublishedObject = (obj) => {
exports.validatePublishedObject = validatePublishedObject;
const validateUnpublishedObject = (obj) => {
validateObject('unpublish', obj);
};
exports.validateDeletedObject = (obj) => {
exports.validateUnpublishedObject = validateUnpublishedObject;
const validateDeletedObject = (obj) => {
validateObject('delete', obj);
};
exports.validateDeletedObject = validateDeletedObject;
16 changes: 16 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/src'],
testMatch: ['**/__tests__/**/*.test.ts'],
collectCoverageFrom: [
'src/**/*.ts',
'!src/**/*.d.ts',
'!src/**/__tests__/**',
],
coverageDirectory: 'coverage',
coverageReporters: ['text', 'lcov', 'html'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
verbose: true,
}

Loading
Loading