Skip to content

Commit da0091f

Browse files
committed
converted to commonjs and made logging configurable
1 parent b4d993b commit da0091f

38 files changed

+255
-179
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ First install the Node SDK using your package manager.
1818
npm install --save unlaunch-node-sdk
1919
```
2020

21-
Next, import the UnlaunchFactory into your application.
21+
Next, require the UnlaunchFactory into your application.
2222

2323
```javascript
24-
import {UnlaunchFactory} from 'unlaunch-node-sdk';
24+
const {UnlaunchFactory} = require('unlaunch-node-sdk');
2525
```
2626

2727
Here is a simple example showing how to use the this SDK in your Node.js application.

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
{
22
"name": "unlaunch-node-sdk",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "Unlaunch Server-Side SDK for Node.js",
55
"main": "src/client/ulFactory.js",
66
"scripts": {
77
"test": "mocha"
88
},
9-
"type": "module",
109
"author": "Mahrukh Izhar",
1110
"license": "ISC",
1211
"dependencies": {

src/client/ulClient.js

+21-20
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import EventEmitter from 'events';
2-
import PollingProcessor from "../processor/pollingProcessor.js";
3-
import OfflineProcessor from "../processor/offlineProcessor.js";
4-
import EventProcessor from "../events/eventProcessor.js";
5-
import OfflineEventProcessor from "../events/offlineEventProcessor.js";
6-
import EventsCache from "../storage/eventsCache/InMemory.js";
7-
import CountCache from "../storage/countCache/InMemory.js";
8-
import { evaluate } from '../engine/evaluator.js';
9-
import { isObject } from "../../src/utils/lang/index.js";
10-
import UlFeature from "../dtos/UlFeature.js";
11-
import Impression from "../dtos/Impression.js";
12-
import { READY } from '../utils/store/constants.js';
1+
const EventEmitter = require('events')
2+
const PollingProcessor = require("../processor/pollingProcessor.js")
3+
const OfflineProcessor = require("../processor/offlineProcessor.js")
4+
const EventProcessor = require("../events/eventProcessor.js")
5+
const OfflineEventProcessor = require("../events/offlineEventProcessor.js")
6+
const EventsCache = require("../storage/eventsCache/InMemory.js")
7+
const CountCache = require("../storage/countCache/InMemory.js")
8+
const { evaluate } = require('../engine/evaluator.js')
9+
const { isObject } = require("../../src/utils/lang/index.js")
10+
const UlFeature = require("../dtos/UlFeature.js")
11+
const Impression = require("../dtos/Impression.js")
12+
const { READY } = require('../utils/store/constants.js')
1313

1414
/**
1515
* Unlaunch Client
1616
**/
17-
export function ulClient(configs, store) {
17+
const ulClient = (configs, store) => {
1818

1919
const newUnlaunchClient = function () {
2020
const client = new EventEmitter();
@@ -79,7 +79,7 @@ export function ulClient(configs, store) {
7979
const isReady = store.get(READY);
8080

8181
if (flagKey == undefined || flagKey.length <= 0) {
82-
configs.logger.error('Please provide valid flagKey');
82+
configs.logger.error('error: [Unlaunch] Please provide valid flagKey');
8383
return new UlFeature(
8484
"control",
8585
"Feature flag key was empty",
@@ -88,7 +88,7 @@ export function ulClient(configs, store) {
8888
}
8989

9090
if (identity == undefined || identity.length <= 0) {
91-
configs.logger.error('Please provide valid identity')
91+
configs.logger.error('error: [Unlaunch] Please provide valid identity')
9292
return new UlFeature(
9393
"control",
9494
"Identity was empty",
@@ -97,7 +97,7 @@ export function ulClient(configs, store) {
9797
}
9898

9999
if (attributes && Object.keys(attributes).length > 0 && !isObject(attributes)) {
100-
configs.logger.error('Please provide valid Unlaunch attributes')
100+
configs.logger.error('error: [Unlaunch] Please provide valid Unlaunch attributes')
101101
return new UlFeature(
102102
"control",
103103
"Invalid Unlaunch attribute(s)",
@@ -106,7 +106,7 @@ export function ulClient(configs, store) {
106106
}
107107

108108
if (offlineMode) {
109-
configs.logger.info('Offline mode selected - control served')
109+
configs.logger.info('info: [Unlaunch] Offline mode selected - control served')
110110
return new UlFeature(
111111
"control",
112112
"Offline mode selected - control served",
@@ -115,7 +115,7 @@ export function ulClient(configs, store) {
115115
}
116116

117117
if (!isReady) {
118-
configs.logger.warn("The SDK is not ready. Returning the SDK default 'control' " +
118+
configs.logger.warn("warn: [Unlaunch] The SDK is not ready. Returning the SDK default 'control' " +
119119
"as variation which may not give " +
120120
"the right result");
121121
return new UlFeature(
@@ -153,7 +153,7 @@ export function ulClient(configs, store) {
153153

154154
return ulFeature;
155155
} else {
156-
configs.logger.error(`Error flag with flagKey ${flagKey} not found`);
156+
configs.logger.error(`error: [Unlaunch] Error flag with flagKey ${flagKey} not found`);
157157
return new UlFeature(
158158
"control",
159159
"Feature flag was not found in memory",
@@ -178,4 +178,5 @@ export function ulClient(configs, store) {
178178
return {
179179
client: newUnlaunchClient
180180
}
181-
}
181+
}
182+
module.exports = {ulClient}

src/client/ulFactory.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@
22
* Factory method for Unlaunch Client
33
*/
44

5-
import { ConfigsFactory } from '../utils/settings/index.js'
6-
import { validateApiKey } from '../utils/validations/apiKey.js';
7-
import Store from '../utils/store/index.js';
8-
import { CONFIGURATIONS, READY } from '../utils/store/constants.js';
9-
import { ulClient } from '../client/ulClient.js'
5+
const { ConfigsFactory } = require('../utils/settings/index.js')
6+
const { validateApiKey } = require('../utils/validations/apiKey.js')
7+
var Store = require('../utils/store/index.js')
8+
const { CONFIGURATIONS, READY } = require('../utils/store/constants.js')
9+
const { ulClient } = require('../client/ulClient.js')
1010

11-
export function UnlaunchFactory(configurations) {
11+
const UnlaunchFactory = (configurations) => {
1212
const store = new Store();
1313
const configs = ConfigsFactory(configurations);
1414
store.set(CONFIGURATIONS, configs);
1515
store.set(READY, false);
1616

1717
if (!configs.core.sdkKey) {
18-
throw ('Factory instantiation requires a valid sdk key');
18+
throw ('error: [Unlaunch] Factory instantiation requires a valid sdk key');
1919
} else {
2020
if (!validateApiKey(configs.core.sdkKey, configs.logger)) {
21-
throw ('Client instantiation requires a valid sdk key');
21+
throw ('error: [Unlaunch] Client instantiation requires a valid sdk key');
2222
}
2323
}
2424
return {
2525
client() {
26-
configs.logger.info('New Unlaunch client instance created.');
27-
configs.logger.info(`Configuration: [ \n\t`+
26+
configs.logger.info('info: [Unlaunch] Initializing Unlaunch client instance and receiving feature flag updates');
27+
configs.logger.debug(`debug: [Unlaunch] Configuration: [ \n\t`+
2828
`sdkKey = ${configs.core.sdkKey},\n\t`+
2929
`pollingInterval = ${configs.intervals.pollingInterval} milliseconds,\n\t`+
3030
`httpConnectionTimeout = ${configs.intervals.httpConnectionTimeout} milliseconds,\n\t`+
@@ -37,3 +37,4 @@ export function UnlaunchFactory(configurations) {
3737
},
3838
}
3939
}
40+
module.exports = { UnlaunchFactory }

src/constants/constants.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const CONSTANTS = {
1+
const CONSTANTS = {
22
SDK_READY: `Getting ready - Unlaunch SDK`,
33
SDK_VARIATION: `SDK - Get Variation`,
44
SDK_VARIATION_WITH_CONFIG: `SDK - Get Variation with config`,
@@ -15,3 +15,6 @@ export const CONSTANTS = {
1515
`and provide it to this SDK. ` +
1616
`For more information, visit: https://docs.unlaunch.io/docs/sdks/sdk-keys`
1717
};
18+
module.exports={
19+
CONSTANTS
20+
}

src/dtos/Events.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import Impression from "./Impression.js";
1+
const Impression =require("./Impression.js")
22

3-
export default class Event {
3+
class Event {
44
constructor(createdTime,type,key,secondaryKey,properties) {
55
this.createdTime = createdTime; // milliseconds
66
this.type = type;
@@ -9,4 +9,6 @@ export default class Event {
99
this.properties = properties;
1010
}
1111

12-
}
12+
}
13+
14+
module.exports = Event

src/dtos/Impression.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default class Impression {
1+
class Impression {
22
constructor(createdTime, type, properties, sdk, sdkVersion, secondaryKey, flagKey,userId,variationKey,flagStatus,evaluationReason,machineName, machineIp) {
33

44
this.createdTime = createdTime;
@@ -16,4 +16,6 @@ export default class Impression {
1616
this.machineName = machineName;
1717
this.machineIp=machineIp;
1818
}
19-
}
19+
}
20+
21+
module.exports = Impression

src/dtos/UlFeature.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ class UnlaunchFeature{
1111
}
1212
}
1313

14-
export default UnlaunchFeature
14+
module.exports = UnlaunchFeature

src/engine/attributes/bool.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function boolApply(val, userVal, op){
1+
boolApply = (val, userVal, op) => {
22

33
if(userVal == null || userVal.constructor.name != "Boolean") {
44
throw new Error("Invalid boolean attribute value");
@@ -14,4 +14,8 @@ export default function boolApply(val, userVal, op){
1414
}
1515

1616
return false
17+
}
18+
19+
module.exports = {
20+
default: boolApply
1721
}

src/engine/attributes/datetime.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function dateOrDateTimeApply(val, userVal, op, discardTime){
1+
const dateOrDateTimeApply = (val, userVal, op, discardTime) => {
22
let v = val
33
let uv = userVal
44

@@ -43,4 +43,9 @@ function tsWithZeroTime(ts){
4343
const d = new Date(Number(ts));
4444
d.setHours(0,0,0,0);
4545
return d.getTime();
46+
}
47+
48+
49+
module.exports = {
50+
default: dateOrDateTimeApply
4651
}

src/engine/attributes/number.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function numberApply(val, userVal, op) {
1+
const numberApply = (val, userVal, op) => {
22

33
if (userVal == null || userVal.constructor.name != "Number") {
44
throw new Error("Invalid number attribute.")
@@ -27,4 +27,7 @@ export default function numberApply(val, userVal, op) {
2727
default:
2828
return false
2929
}
30+
}
31+
module.exports = {
32+
default: numberApply
3033
}

src/engine/attributes/set.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import "../../../src/engine/helper/setMatcher.js";
1+
require("../../../src/engine/helper/setMatcher.js")
22

3-
export default function setApply(val, userVal, op){
3+
const setApply = (val, userVal, op) => {
44

55
if (userVal == null) {
66
throw new Error("Invalid set attribute value.")
@@ -49,3 +49,7 @@ export default function setApply(val, userVal, op){
4949

5050
return false
5151
}
52+
53+
module.exports = {
54+
default: setApply
55+
}

src/engine/attributes/string.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function stringApply(val, userVal, op) {
1+
const stringApply = (val, userVal, op) => {
22

33
if (userVal == null || userVal.constructor.name != "String") {
44
throw new Error("Invalid string attribute value.")
@@ -31,3 +31,8 @@ export default function stringApply(val, userVal, op) {
3131

3232
return false
3333
}
34+
35+
36+
module.exports = {
37+
default: stringApply
38+
}

0 commit comments

Comments
 (0)