@emartech/me-idempotency 2.0.5 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": [
3
+ "eslint:recommended",
4
+ "plugin:mocha/recommended",
5
+ "plugin:@typescript-eslint/recommended",
6
+ "plugin:prettier/recommended"
7
+ ],
8
+ "rules": {
9
+ "mocha/no-mocha-arrows": 0,
10
+ "mocha/no-setup-in-describe": 0
11
+ }
12
+ }
package/.prettierrc ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "tabWidth": 2,
3
+ "semi": false,
4
+ "singleQuote": true,
5
+ "printWidth": 120
6
+ }
package/CHANGELOG.md CHANGED
@@ -3,6 +3,22 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # 3.0.0 (2023-10-27)
7
+
8
+
9
+ * feat!(idempotency): upgrade to redis v4 ([0cd9ba1](https://github.com/emartech/me-idempotency-js/commit/0cd9ba17722ca3881b2e8fe0a1be9c057f13b342))
10
+
11
+
12
+ ### BREAKING CHANGES
13
+
14
+ * Upgraded to Redis client v4.
15
+
16
+ SUITEDEV-34567
17
+
18
+
19
+
20
+
21
+
6
22
  ## [2.0.5](https://github.com/emartech/me-idempotency-js/compare/@emartech/me-idempotency@2.0.4...@emartech/me-idempotency@2.0.5) (2023-10-17)
7
23
 
8
24
  **Note:** Version bump only for package @emartech/me-idempotency
@@ -0,0 +1 @@
1
+ export { createRedisIdempotency, defaultIdempotencyKeyGenerator } from './src/create-idempotency-fn';
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultIdempotencyKeyGenerator = exports.createRedisIdempotency = void 0;
3
4
  var create_idempotency_fn_1 = require("./src/create-idempotency-fn");
4
- exports.createRedisIdempotency = create_idempotency_fn_1.createRedisIdempotency;
5
- exports.defaultIdempotencyKeyGenerator = create_idempotency_fn_1.defaultIdempotencyKeyGenerator;
5
+ Object.defineProperty(exports, "createRedisIdempotency", { enumerable: true, get: function () { return create_idempotency_fn_1.createRedisIdempotency; } });
6
+ Object.defineProperty(exports, "defaultIdempotencyKeyGenerator", { enumerable: true, get: function () { return create_idempotency_fn_1.defaultIdempotencyKeyGenerator; } });
6
7
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;AAAA,qEAAoG;AAA3F,yDAAA,sBAAsB,CAAA;AAAE,iEAAA,8BAA8B,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA,qEAAoG;AAA3F,+HAAA,sBAAsB,OAAA;AAAE,uIAAA,8BAA8B,OAAA"}
@@ -0,0 +1,7 @@
1
+ import { RedisClientType } from 'redis';
2
+ export declare const defaultIdempotencyKeyGenerator: (...args: unknown[]) => string;
3
+ export declare const createRedisIdempotency: (client: RedisClientType, ttlInSec?: number) => {
4
+ del: (key: string) => Promise<number>;
5
+ set: (key: string) => Promise<boolean>;
6
+ get: (key: string) => Promise<boolean>;
7
+ };
@@ -1,24 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const { promisify } = require('util');
4
- exports.defaultIdempotencyKeyGenerator = (...args) => {
3
+ exports.createRedisIdempotency = exports.defaultIdempotencyKeyGenerator = void 0;
4
+ const defaultIdempotencyKeyGenerator = (...args) => {
5
5
  const prefix = 'idempotency';
6
- return [prefix].concat(args).join(':');
6
+ return [prefix].concat(args.map((arg) => `${arg}`)).join(':');
7
7
  };
8
- exports.createRedisIdempotency = (client, ttlInSec = 60 * 60 * 6) => {
9
- const asyncDel = promisify(client.del).bind(client);
10
- const asyncGet = promisify(client.get).bind(client);
11
- const asyncSet = promisify(client.set).bind(client);
8
+ exports.defaultIdempotencyKeyGenerator = defaultIdempotencyKeyGenerator;
9
+ const createRedisIdempotency = (client, ttlInSec = 60 * 60 * 6) => {
12
10
  return {
13
- del: async (key) => asyncDel(key),
11
+ del: async (key) => client.del(key),
14
12
  set: async (key) => {
15
- const result = await asyncSet(key, '1', 'EX', ttlInSec, 'NX');
16
- return (result === 'OK');
13
+ const result = await client.set(key, '1', { EX: ttlInSec, NX: true });
14
+ return result === 'OK';
17
15
  },
18
16
  get: async (key) => {
19
- const result = await asyncGet(key);
17
+ const result = await client.get(key);
20
18
  return result !== null;
21
- }
19
+ },
22
20
  };
23
21
  };
22
+ exports.createRedisIdempotency = createRedisIdempotency;
24
23
  //# sourceMappingURL=create-idempotency-fn.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"create-idempotency-fn.js","sourceRoot":"","sources":["../../src/create-idempotency-fn.ts"],"names":[],"mappings":";;AACA,MAAM,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;AAExB,QAAA,8BAA8B,GAAG,CAAC,GAAG,IAAS,EAAE,EAAE;IAC7D,MAAM,MAAM,GAAG,aAAa,CAAA;IAC5B,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;AACxC,CAAC,CAAA;AAEY,QAAA,sBAAsB,GAAG,CAAC,MAAmB,EAAE,WAAmB,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,EAAE;IAC5F,MAAM,QAAQ,GAAG,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IACnD,MAAM,QAAQ,GAAG,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IACnD,MAAM,QAAQ,GAAG,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC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AE,EAAE,QAAQ,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,CAAA;YACrE,OAAO,MAAM,KAAK,IAAI,CAAA;QACxB,CAAC;QACD,GAAG,EAAE,KAAK,EAAE,GAAW,EAAE,EAAE;YACzB,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;YACpC,OAAO,MAAM,KAAK,IAAI,CAAA;QACxB,CAAC;KACF,CAAA;AACH,CAAC,CAAA;AAZY,QAAA,sBAAsB,0BAYlC"}
@@ -0,0 +1 @@
1
+ import 'mocha';
@@ -2,44 +2,51 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const chai_1 = require("chai");
4
4
  require("mocha");
5
- const create_idempotency_fn_1 = require("./create-idempotency-fn");
6
5
  const redis_1 = require("redis");
7
- describe('defaultIdempotencyKeyGenerator', () => {
8
- it('should generate a string key', () => {
9
- const key = create_idempotency_fn_1.defaultIdempotencyKeyGenerator(1, 'str', true);
10
- chai_1.expect(key).to.eql('idempotency:1:str:true');
11
- });
12
- });
13
- describe('createRedisIdempotency', () => {
14
- const redisClient = redis_1.createClient(process.env.REDIS_URL);
15
- const idemp = create_idempotency_fn_1.createRedisIdempotency(redisClient);
16
- const key = `testIdemp-${new Date().toISOString()}`;
17
- after(() => redisClient.quit());
18
- afterEach(() => idemp.del(key));
19
- it('should set a new key and return true', async () => {
20
- const result = await idemp.set(key);
21
- chai_1.expect(result).to.eql(true);
22
- });
23
- it('should return false setting the key twice', async () => {
24
- await idemp.set(key);
25
- const result = await idemp.set(key);
26
- chai_1.expect(result).to.eql(false);
27
- });
28
- it('should delete the key and return the number of deleted key', async () => {
29
- await idemp.set(key);
30
- const result = await idemp.del(key);
31
- const resultSecondDeletion = await idemp.del(key);
32
- chai_1.expect(result).to.eql(1);
33
- chai_1.expect(resultSecondDeletion).to.eql(0);
34
- });
35
- it('should return true when getting existing key', async () => {
36
- await idemp.set(key);
37
- const result = await idemp.get(key);
38
- chai_1.expect(result).to.eql(true);
6
+ const create_idempotency_fn_1 = require("./create-idempotency-fn");
7
+ describe('me-idempotency', () => {
8
+ describe('defaultIdempotencyKeyGenerator', () => {
9
+ it('should generate a string key', () => {
10
+ const key = (0, create_idempotency_fn_1.defaultIdempotencyKeyGenerator)(1, 'str', true);
11
+ (0, chai_1.expect)(key).to.eql('idempotency:1:str:true');
12
+ });
39
13
  });
40
- it('should return false when getting non-existing key', async () => {
41
- const result = await idemp.get('i-do-not-exist');
42
- chai_1.expect(result).to.eql(false);
14
+ describe('createRedisIdempotency', () => {
15
+ const key = `testIdemp-${new Date().toISOString()}`;
16
+ let redisClient;
17
+ let idemp;
18
+ before(async () => {
19
+ redisClient = (0, redis_1.createClient)({ url: process.env.REDIS_URL });
20
+ await redisClient.connect();
21
+ idemp = (0, create_idempotency_fn_1.createRedisIdempotency)(redisClient);
22
+ });
23
+ afterEach(() => idemp.del(key));
24
+ after(() => redisClient.quit());
25
+ it('should set a new key and return true', async () => {
26
+ const result = await idemp.set(key);
27
+ (0, chai_1.expect)(result).to.eql(true);
28
+ });
29
+ it('should return false setting the key twice', async () => {
30
+ await idemp.set(key);
31
+ const result = await idemp.set(key);
32
+ (0, chai_1.expect)(result).to.eql(false);
33
+ });
34
+ it('should delete the key and return the number of deleted key', async () => {
35
+ await idemp.set(key);
36
+ const result = await idemp.del(key);
37
+ const resultSecondDeletion = await idemp.del(key);
38
+ (0, chai_1.expect)(result).to.eql(1);
39
+ (0, chai_1.expect)(resultSecondDeletion).to.eql(0);
40
+ });
41
+ it('should return true when getting existing key', async () => {
42
+ await idemp.set(key);
43
+ const result = await idemp.get(key);
44
+ (0, chai_1.expect)(result).to.eql(true);
45
+ });
46
+ it('should return false when getting non-existing key', async () => {
47
+ const result = await idemp.get('i-do-not-exist');
48
+ (0, chai_1.expect)(result).to.eql(false);
49
+ });
43
50
  });
44
51
  });
45
52
  //# sourceMappingURL=create-idempotency-fn.spec.js.map
@@ -1 +1 @@
1
- {"version":3,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C,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,58 +1,58 @@
1
1
  {
2
2
  "name": "@emartech/me-idempotency",
3
+ "version": "3.0.0",
3
4
  "description": "",
4
- "version": "2.0.5",
5
+ "license": "UNLICENSED",
6
+ "homepage": "https://github.com/emartech/me-idempotency-js#readme",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/emartech/me-idempotency-js.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/emartech/me-idempotency-js/issues"
13
+ },
5
14
  "main": "dist/index.js",
6
15
  "types": "dist/index.d.ts",
7
16
  "scripts": {
8
- "clean": "rm -rf build && rm -rf dist",
9
17
  "build": "npm run clean && npm run format && tsc",
10
18
  "build-docs": "rm -rf docs && typedoc --out docs --target es2018 --theme markdown --exclude 'test/**'",
11
19
  "build-release": "npm run build",
12
- "pretest": "npm run format || echo",
13
- "format": "tslint --project '.' --fix",
20
+ "clean": "rm -rf build && rm -rf dist",
21
+ "format": "eslint ./src --fix --ext .ts",
22
+ "lint": "eslint ./src --ext .ts",
14
23
  "prepare": "npm run clean && npm run build-release",
15
- "test": "mocha --reporter spec --colors -r dotenv/config -r ts-node/register **/*.spec.*",
16
- "lint": "tslint --project tsconfig.json"
17
- },
18
- "repository": {
19
- "type": "git",
20
- "url": "git+https://github.com/emartech/me-idempotency-js.git"
21
- },
22
- "engines": {
23
- "node": ">=18.x"
24
- },
25
- "author": "",
26
- "license": "ISC",
27
- "bugs": {
28
- "url": "https://github.com/emartech/me-idempotency-js/issues"
24
+ "pretest": "npm run format || echo",
25
+ "test": "mocha -r dotenv/config -r ts-node/register **/*.spec.*"
29
26
  },
30
- "standard": {
31
- "env": [
32
- "mocha"
33
- ]
27
+ "dependencies": {
28
+ "redis": "^4.6.10"
34
29
  },
35
- "homepage": "https://github.com/emartech/me-idempotency-js#readme",
36
30
  "devDependencies": {
31
+ "@tsconfig/node18": "^18.2.2",
37
32
  "@types/chai": "^4.2.10",
38
33
  "@types/mocha": "^7.0.2",
39
- "@types/redis": "^2.8.16",
34
+ "@types/node": "^18.18.7",
40
35
  "@types/sinon": "^7.5.2",
36
+ "@typescript-eslint/eslint-plugin": "^6.9.0",
37
+ "@typescript-eslint/parser": "^6.9.0",
38
+ "@typescript-eslint/typescript-estree": "^6.9.0",
41
39
  "chai": "^3.5.0",
42
40
  "chai-string": "1.3.0",
43
41
  "chai-subset": "1.5.0",
44
42
  "dotenv": "^8.2.0",
43
+ "eslint": "^8.52.0",
44
+ "eslint-config-prettier": "^9.0.0",
45
+ "eslint-plugin-mocha": "^10.2.0",
46
+ "eslint-plugin-prettier": "^5.0.1",
45
47
  "mocha": "^10.2.0",
48
+ "prettier": "3.0.3",
46
49
  "sinon": "2.2.0",
47
50
  "sinon-chai": "2.10.0",
48
51
  "ts-node": "^8.6.2",
49
- "tslint": "^6.0.0",
50
- "tslint-config-standard": "^9.0.0",
51
- "tslint-no-unused-expression-chai": "^0.1.4"
52
+ "typescript": "^5.2.2"
52
53
  },
53
- "dependencies": {
54
- "redis": "^3.1.2",
55
- "typescript": "^3.8.3"
54
+ "engines": {
55
+ "node": ">=18.x"
56
56
  },
57
- "gitHead": "e04ac4b923eeaa2e0ba16889c9d8efdd52fd1f57"
57
+ "gitHead": "8e93203cc613f6dbefc95f526a0243f7198a43dd"
58
58
  }
@@ -1,51 +1,58 @@
1
1
  import { expect } from 'chai'
2
2
  import 'mocha'
3
-
3
+ import { createClient, RedisClientType } from 'redis'
4
4
  import { createRedisIdempotency, defaultIdempotencyKeyGenerator } from './create-idempotency-fn'
5
- import { createClient } from 'redis'
6
-
7
- describe('defaultIdempotencyKeyGenerator', () => {
8
- it('should generate a string key', () => {
9
- const key = defaultIdempotencyKeyGenerator(1, 'str', true)
10
- expect(key).to.eql('idempotency:1:str:true')
11
- })
12
- })
13
-
14
- describe('createRedisIdempotency', () => {
15
- const redisClient = createClient(process.env.REDIS_URL!)
16
- const idemp = createRedisIdempotency(redisClient)
17
- const key = `testIdemp-${new Date().toISOString()}`
18
- after(() => redisClient.quit())
19
-
20
- afterEach(() => idemp.del(key))
21
-
22
- it('should set a new key and return true', async () => {
23
- const result = await idemp.set(key)
24
- expect(result).to.eql(true)
25
- })
26
-
27
- it('should return false setting the key twice', async () => {
28
- await idemp.set(key)
29
- const result = await idemp.set(key)
30
- expect(result).to.eql(false)
31
- })
32
-
33
- it('should delete the key and return the number of deleted key', async () => {
34
- await idemp.set(key)
35
- const result = await idemp.del(key)
36
- const resultSecondDeletion = await idemp.del(key)
37
- expect(result).to.eql(1)
38
- expect(resultSecondDeletion).to.eql(0)
39
- })
40
5
 
41
- it('should return true when getting existing key', async () => {
42
- await idemp.set(key)
43
- const result = await idemp.get(key)
44
- expect(result).to.eql(true)
6
+ describe('me-idempotency', () => {
7
+ describe('defaultIdempotencyKeyGenerator', () => {
8
+ it('should generate a string key', () => {
9
+ const key = defaultIdempotencyKeyGenerator(1, 'str', true)
10
+ expect(key).to.eql('idempotency:1:str:true')
11
+ })
45
12
  })
46
13
 
47
- it('should return false when getting non-existing key', async () => {
48
- const result = await idemp.get('i-do-not-exist')
49
- expect(result).to.eql(false)
14
+ describe('createRedisIdempotency', () => {
15
+ const key = `testIdemp-${new Date().toISOString()}`
16
+
17
+ let redisClient: RedisClientType
18
+ let idemp: ReturnType<typeof createRedisIdempotency>
19
+
20
+ before(async () => {
21
+ redisClient = createClient({ url: process.env.REDIS_URL! })
22
+ await redisClient.connect()
23
+ idemp = createRedisIdempotency(redisClient)
24
+ })
25
+ afterEach(() => idemp.del(key))
26
+ after(() => redisClient.quit())
27
+
28
+ it('should set a new key and return true', async () => {
29
+ const result = await idemp.set(key)
30
+ expect(result).to.eql(true)
31
+ })
32
+
33
+ it('should return false setting the key twice', async () => {
34
+ await idemp.set(key)
35
+ const result = await idemp.set(key)
36
+ expect(result).to.eql(false)
37
+ })
38
+
39
+ it('should delete the key and return the number of deleted key', async () => {
40
+ await idemp.set(key)
41
+ const result = await idemp.del(key)
42
+ const resultSecondDeletion = await idemp.del(key)
43
+ expect(result).to.eql(1)
44
+ expect(resultSecondDeletion).to.eql(0)
45
+ })
46
+
47
+ it('should return true when getting existing key', async () => {
48
+ await idemp.set(key)
49
+ const result = await idemp.get(key)
50
+ expect(result).to.eql(true)
51
+ })
52
+
53
+ it('should return false when getting non-existing key', async () => {
54
+ const result = await idemp.get('i-do-not-exist')
55
+ expect(result).to.eql(false)
56
+ })
50
57
  })
51
58
  })
@@ -1,24 +1,20 @@
1
- import { ClientOpts, createClient, RedisClient } from 'redis'
2
- const { promisify } = require('util')
1
+ import { RedisClientType } from 'redis'
3
2
 
4
- export const defaultIdempotencyKeyGenerator = (...args: any) => {
3
+ export const defaultIdempotencyKeyGenerator = (...args: unknown[]) => {
5
4
  const prefix = 'idempotency'
6
- return [prefix].concat(args).join(':')
5
+ return [prefix].concat(args.map((arg) => `${arg}`)).join(':')
7
6
  }
8
7
 
9
- export const createRedisIdempotency = (client: RedisClient, ttlInSec: number = 60 * 60 * 6) => {
10
- const asyncDel = promisify(client.del).bind(client)
11
- const asyncGet = promisify(client.get).bind(client)
12
- const asyncSet = promisify(client.set).bind(client)
8
+ export const createRedisIdempotency = (client: RedisClientType, ttlInSec: number = 60 * 60 * 6) => {
13
9
  return {
14
- del: async (key: string) => asyncDel(key),
10
+ del: async (key: string) => client.del(key),
15
11
  set: async (key: string) => {
16
- const result = await asyncSet(key, '1', 'EX', ttlInSec, 'NX')
17
- return (result === 'OK')
12
+ const result = await client.set(key, '1', { EX: ttlInSec, NX: true })
13
+ return result === 'OK'
18
14
  },
19
15
  get: async (key: string) => {
20
- const result = await asyncGet(key)
16
+ const result = await client.get(key)
21
17
  return result !== null
22
- }
18
+ },
23
19
  }
24
20
  }
package/tsconfig.json CHANGED
@@ -1,66 +1,14 @@
1
1
  {
2
+ "extends": "@tsconfig/node18/tsconfig.json",
3
+ "exclude": [
4
+ "dist",
5
+ "node_modules"
6
+ ],
2
7
  "compilerOptions": {
3
- /* Basic Options */
4
- // "incremental": true, /* Enable incremental compilation */
5
- "target": "ES2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
6
- "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
7
- // "lib": [], /* Specify library files to be included in the compilation. */
8
- "allowJs": true, /* Allow javascript files to be compiled. */
9
- "checkJs": false, /* Report errors in .js files. */
10
- // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
11
- // "declaration": true, /* Generates corresponding '.d.ts' file. */
12
- // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
13
- "sourceMap": true, /* Generates corresponding '.map' file. */
14
- // "outFile": "./", /* Concatenate and emit output to single file. */
15
- "outDir": "./dist", /* Redirect output structure to the directory. */
16
- // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
17
- // "composite": true, /* Enable project compilation */
18
- // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
19
- // "removeComments": true, /* Do not emit comments to output. */
20
- // "noEmit": true, /* Do not emit outputs. */
21
- // "importHelpers": true, /* Import emit helpers from 'tslib'. */
22
- // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
23
- // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
24
-
25
- /* Strict Type-Checking Options */
26
- "strict": true, /* Enable all strict type-checking options. */
27
- // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
28
- // "strictNullChecks": true, /* Enable strict null checks. */
29
- // "strictFunctionTypes": true, /* Enable strict checking of function types. */
30
- // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
31
- // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
32
- // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
33
- // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
34
-
35
- /* Additional Checks */
36
- // "noUnusedLocals": true, /* Report errors on unused locals. */
37
- // "noUnusedParameters": true, /* Report errors on unused parameters. */
38
- // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
39
- // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
40
-
41
- /* Module Resolution Options */
42
- // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
43
- // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
44
- // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
45
- // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
46
- // "typeRoots": [], /* List of folders to include type definitions from. */
47
- // "types": [], /* Type declaration files to be included in compilation. */
48
- // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
49
- "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
50
- // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51
- // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
52
-
53
- /* Source Map Options */
54
- // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
55
- // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
56
- // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
57
- // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
58
-
59
- /* Experimental Options */
60
- // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
61
- // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
62
-
63
- /* Advanced Options */
64
- "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
8
+ "baseUrl": "./",
9
+ "rootDir": "./",
10
+ "outDir": "./dist",
11
+ "declaration": true,
12
+ "sourceMap": true
65
13
  }
66
14
  }
package/tslint.json DELETED
@@ -1,12 +0,0 @@
1
- {
2
- "defaultSeverity": "error",
3
- "extends": [
4
- "tslint-config-standard",
5
- "tslint-no-unused-expression-chai"
6
- ],
7
- "jsRules": {},
8
- "rules": {
9
- "max-line-length": [true, 120]
10
- },
11
- "rulesDirectory": []
12
- }