@pocket-architect/core 0.1.15 → 0.1.17

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.
@@ -1,7 +1,7 @@
1
1
  export declare class EntityId<T> {
2
2
  protected _recordId: T | null;
3
3
  protected _uuid: string;
4
- constructor(recordId?: T, uuid?: string);
4
+ constructor(recordId?: T, isHash?: boolean, uuid?: string);
5
5
  private createUUID;
6
6
  get hashOptions(): [string, number];
7
7
  toHash(): string;
package/build/EntityId.js CHANGED
@@ -5,13 +5,18 @@ var tslib_1 = require("tslib");
5
5
  var uuid_by_string_1 = tslib_1.__importDefault(require("uuid-by-string"));
6
6
  var cuid2_1 = require("@paralleldrive/cuid2");
7
7
  var hashids_1 = tslib_1.__importDefault(require("hashids"));
8
+ var HashError_1 = require("./error/HashError");
8
9
  var EntityId = /** @class */ (function () {
9
- function EntityId(recordId, uuid) {
10
+ function EntityId(recordId, isHash, uuid) {
10
11
  if (recordId === void 0) { recordId = null; }
12
+ if (isHash === void 0) { isHash = false; }
11
13
  if (uuid === void 0) { uuid = null; }
12
14
  this._recordId = null;
13
15
  this._uuid = null;
14
16
  this._recordId = recordId ? recordId : null;
17
+ if (isHash) {
18
+ this.fromHash(recordId);
19
+ }
15
20
  if (!uuid) {
16
21
  this.createUUID();
17
22
  }
@@ -36,6 +41,9 @@ var EntityId = /** @class */ (function () {
36
41
  EntityId.prototype.fromHash = function (hash) {
37
42
  var hashids = new (hashids_1.default.bind.apply(hashids_1.default, tslib_1.__spreadArray([void 0], this.hashOptions, false)))();
38
43
  var num = hashids.decode(hash)[0];
44
+ if (!num) {
45
+ throw new HashError_1.HashError("Invalid hash ".concat(hash));
46
+ }
39
47
  this._recordId = (typeof this._recordId === 'string' ? num.toString() : num);
40
48
  this.createUUID();
41
49
  return this;
@@ -1 +1 @@
1
- {"version":3,"file":"EntityId.js","sourceRoot":"","sources":["../src/EntityId.ts"],"names":[],"mappings":";;;;AAAA,0EAAuC;AACvC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,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,yBAAM,GAAN,UAAO,EAAe;QACpB,OAAO,IAAI,CAAC,KAAK,KAAK,EAAE,CAAC,KAAK,CAAC;IACjC,CAAC;IACH,eAAC;AAAD,CAAC,AApDD,IAoDC;AApDY,4BAAQ"}
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var tslib_1 = require("tslib");
4
4
  var EntityId_1 = require("./EntityId");
5
+ var HashError_1 = require("./error/HashError");
5
6
  var TestId = /** @class */ (function (_super) {
6
7
  tslib_1.__extends(TestId, _super);
7
8
  function TestId() {
@@ -18,13 +19,30 @@ var Test2Id = /** @class */ (function (_super) {
18
19
  }(EntityId_1.EntityId));
19
20
  describe('ValueObject', function () {
20
21
  test('base', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
21
- var id, id2, id3;
22
+ var id, id2, id3, id4;
22
23
  return tslib_1.__generator(this, function (_a) {
23
24
  id = new TestId(1);
24
25
  id2 = new EntityId_1.EntityId('1');
25
26
  id3 = new TestId(1);
26
27
  expect(id).toEqual(id3);
27
28
  expect(id).not.toEqual(id2);
29
+ id4 = new Test2Id('9KdMR', true);
30
+ expect(id4.toPrimitive()).toEqual('123');
31
+ return [2 /*return*/];
32
+ });
33
+ }); });
34
+ test('fromHash', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
35
+ return tslib_1.__generator(this, function (_a) {
36
+ expect.assertions(2);
37
+ try {
38
+ new Test2Id('123', true);
39
+ }
40
+ catch (err) {
41
+ // eslint-disable-next-line jest/no-conditional-expect
42
+ expect(err).toBeInstanceOf(HashError_1.HashError);
43
+ // eslint-disable-next-line jest/no-conditional-expect
44
+ expect(err.message).toEqual('Invalid hash 123');
45
+ }
28
46
  return [2 /*return*/];
29
47
  });
30
48
  }); });
@@ -1 +1 @@
1
- {"version":3,"file":"EntityId.spec.js","sourceRoot":"","sources":["../src/EntityId.spec.ts"],"names":[],"mappings":";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class HashError extends Error {
2
+ constructor(message: string);
3
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HashError = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var HashError = /** @class */ (function (_super) {
6
+ tslib_1.__extends(HashError, _super);
7
+ function HashError(message) {
8
+ var _this = _super.call(this, message) || this;
9
+ Object.setPrototypeOf(_this, HashError.prototype);
10
+ return _this;
11
+ }
12
+ return HashError;
13
+ }(Error));
14
+ exports.HashError = HashError;
15
+ //# sourceMappingURL=HashError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HashError.js","sourceRoot":"","sources":["../../src/error/HashError.ts"],"names":[],"mappings":";;;;AACA;IACwB,qCAAK;IAC3B,mBAAY,OAAe;QACzB,YAAA,MAAK,YAAC,OAAO,CAAC,SAAC;QACf,MAAM,CAAC,cAAc,CAAC,KAAI,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;;IACnD,CAAC;IACH,gBAAC;AAAD,CAAC,AAND,CACwB,KAAK,GAK5B;AALK,8BAAS"}
package/build/index.d.ts CHANGED
@@ -2,4 +2,5 @@ import { Entity } from './Entity';
2
2
  import { EntityId } from './EntityId';
3
3
  import { ValueObject } from './ValueObject';
4
4
  import { AggregateRoot } from './AggregateRoot';
5
- export { Entity, EntityId, ValueObject, AggregateRoot };
5
+ import { HashError } from './error/HashError';
6
+ export { Entity, EntityId, ValueObject, AggregateRoot, HashError };
package/build/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AggregateRoot = exports.ValueObject = exports.EntityId = exports.Entity = void 0;
3
+ exports.HashError = exports.AggregateRoot = exports.ValueObject = exports.EntityId = exports.Entity = void 0;
4
4
  var Entity_1 = require("./Entity");
5
5
  Object.defineProperty(exports, "Entity", { enumerable: true, get: function () { return Entity_1.Entity; } });
6
6
  var EntityId_1 = require("./EntityId");
@@ -9,4 +9,6 @@ var ValueObject_1 = require("./ValueObject");
9
9
  Object.defineProperty(exports, "ValueObject", { enumerable: true, get: function () { return ValueObject_1.ValueObject; } });
10
10
  var AggregateRoot_1 = require("./AggregateRoot");
11
11
  Object.defineProperty(exports, "AggregateRoot", { enumerable: true, get: function () { return AggregateRoot_1.AggregateRoot; } });
12
+ var HashError_1 = require("./error/HashError");
13
+ Object.defineProperty(exports, "HashError", { enumerable: true, get: function () { return HashError_1.HashError; } });
12
14
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAAkC;AAMhC,uFANO,eAAM,OAMP;AALR,uCAAsC;AAMpC,yFANO,mBAAQ,OAMP;AALV,6CAA4C;AAM1C,4FANO,yBAAW,OAMP;AALb,iDAAgD;AAM9C,8FANO,6BAAa,OAMP"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAAkC;AAOhC,uFAPO,eAAM,OAOP;AANR,uCAAsC;AAOpC,yFAPO,mBAAQ,OAOP;AANV,6CAA4C;AAO1C,4FAPO,yBAAW,OAOP;AANb,iDAAgD;AAO9C,8FAPO,6BAAa,OAOP;AANf,+CAA8C;AAO5C,0FAPO,qBAAS,OAOP"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pocket-architect/core",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -1,4 +1,5 @@
1
1
  import { EntityId } from './EntityId';
2
+ import {HashError} from "./error/HashError";
2
3
 
3
4
  class TestId extends EntityId<number> {
4
5
  }
@@ -13,8 +14,22 @@ describe('ValueObject', () => {
13
14
  const id3 = new TestId(1);
14
15
  expect(id).toEqual(id3);
15
16
  expect(id).not.toEqual(id2);
17
+
18
+ const id4 = new Test2Id('9KdMR', true);
19
+ expect(id4.toPrimitive()).toEqual('123')
16
20
  });
21
+ test('fromHash', async () => {
22
+ expect.assertions(2);
17
23
 
24
+ try {
25
+ new Test2Id('123', true)
26
+ } catch (err) {
27
+ // eslint-disable-next-line jest/no-conditional-expect
28
+ expect(err).toBeInstanceOf(HashError);
29
+ // eslint-disable-next-line jest/no-conditional-expect
30
+ expect(err.message).toEqual('Invalid hash 123');
31
+ }
32
+ });
18
33
  test('toHash', async () => {
19
34
  const id = new Test2Id('123');
20
35
  expect(id.toHash()).toEqual('9KdMR');
package/src/EntityId.ts CHANGED
@@ -1,13 +1,17 @@
1
1
  import createUUID from 'uuid-by-string'
2
2
  import { createId } from '@paralleldrive/cuid2'
3
3
  import Hashids from 'hashids';
4
+ import {HashError} from "./error/HashError";
4
5
 
5
6
  export class EntityId<T> {
6
7
  protected _recordId: T|null = null;
7
8
  protected _uuid: string = null;
8
9
 
9
- constructor(recordId: T = null, uuid: string = null) {
10
+ constructor(recordId: T = null, isHash:boolean = false, uuid: string = null) {
10
11
  this._recordId = recordId ? recordId : null;
12
+ if (isHash) {
13
+ this.fromHash(recordId as string);
14
+ }
11
15
  if (!uuid) {
12
16
  this.createUUID();
13
17
  }
@@ -32,6 +36,9 @@ export class EntityId<T> {
32
36
  fromHash(hash: string) : EntityId<T> {
33
37
  const hashids = new Hashids(...this.hashOptions);
34
38
  const num = hashids.decode(hash)[0];
39
+ if (!num) {
40
+ throw new HashError(`Invalid hash ${hash}`);
41
+ }
35
42
  this._recordId = <T>(typeof this._recordId === 'string' ? num.toString() : num);
36
43
  this.createUUID();
37
44
  return this;
@@ -0,0 +1,8 @@
1
+
2
+ export
3
+ class HashError extends Error {
4
+ constructor(message: string) {
5
+ super(message);
6
+ Object.setPrototypeOf(this, HashError.prototype);
7
+ }
8
+ }
package/src/index.ts CHANGED
@@ -2,10 +2,12 @@ import { Entity } from './Entity';
2
2
  import { EntityId } from './EntityId';
3
3
  import { ValueObject } from './ValueObject';
4
4
  import { AggregateRoot } from './AggregateRoot';
5
+ import { HashError } from './error/HashError';
5
6
 
6
7
  export {
7
8
  Entity,
8
9
  EntityId,
9
10
  ValueObject,
10
- AggregateRoot
11
+ AggregateRoot,
12
+ HashError
11
13
  }