Skip to content

Commit

Permalink
fix: Do not require uri if redisClient object provided.
Browse files Browse the repository at this point in the history
  • Loading branch information
palmtown committed Mar 2, 2024
1 parent 2f507c3 commit d887071
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@ const adaptors = {

const { SYNC } = core;
const init = options => {
const { uri, deserialize, serialize } = options;
const { uri, deserialize, serialize, redisClient } = options;

if (!uri) {
throw new Error('A `uri` option with the database connection string has to be provided to feathers-sync');
if (!uri && !redisClient) {
throw new Error('A `uri` option with the database connection string, or a `redisClient` object has to be provided to feathers-sync');
}

let adapter;

if (redisClient) {
if (typeof redisClient !== "object") {
throw new Error('`redisClient` option provided to feathers-sync is not an object');
}

adapter = adaptors["redis"];
}

if (deserialize && typeof deserialize !== 'function') {
throw new Error('`deserialize` option provided to feathers-sync is not a function');
}
Expand All @@ -27,14 +37,16 @@ const init = options => {
throw new Error('`serialize` option provided to feathers-sync is not a function');
}

const { protocol } = new URL(uri);
const name = protocol.substring(0, protocol.length - 1);
const identifiedProtocolName = Object.keys(adaptors).filter((adaptor) => name.indexOf(adaptor) !== -1 ? adaptor : null);
const adapter = adaptors[identifiedProtocolName];
if (typeof adapter !== "function") {
const { protocol } = new URL(uri);
const name = protocol.substring(0, protocol.length - 1);
const identifiedProtocolName = Object.keys(adaptors).filter((adaptor) => name.indexOf(adaptor) !== -1 ? adaptor : null);
adapter = adaptors[identifiedProtocolName];

if (!adapter) {
throw new Error(`${name} is an invalid adapter (uri ${uri})`);
}
if (typeof adapter !== "function") {
throw new Error(`${name} is an invalid adapter (uri ${uri})`);
}
}

return adapter({
serialize: JSON.stringify,
Expand All @@ -49,4 +61,4 @@ module.exports = init;
Object.assign(module.exports, adaptors, {
default: init,
SYNC
});
});

0 comments on commit d887071

Please sign in to comment.