Skip to content

Commit 74874cb

Browse files
author
Bartosz Pasiński
committed
feat(identity): simplify Uuid class
1 parent 271304f commit 74874cb

File tree

3 files changed

+13
-18
lines changed

3 files changed

+13
-18
lines changed

packages/identity/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ Creating value objects:
4141
```ts
4242
import { Uuid } from '@code-net/identity';
4343

44-
class UserId extends Uuid.for('User') {}
45-
class ProductId extends Uuid.for('Product') {}
44+
class UserId extends Uuid<'User'> {}
45+
class ProductId extends Uuid<'Product'> {}
4646

4747
let userId = new UserId();
4848
let productId = new ProductId();

packages/identity/src/uuid.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class Uuid<T = unknown> {
1515
constructor(private readonly id: string = uuid.v4()) {
1616
if (!uuid.isValid(id)) {
1717
throw new InvalidValue(
18-
`${this.__brand} ID is not valid UUID, given: "${id}"`
18+
`${this.constructor.name} is not valid UUID, given: "${id}"`
1919
);
2020
}
2121
}
@@ -43,17 +43,4 @@ export class Uuid<T = unknown> {
4343
valueOf() {
4444
return this.id;
4545
}
46-
47-
static for<T>(): new (id?: string) => Uuid<T> {
48-
return class UuidFor extends Uuid<T> {
49-
constructor(id: string = uuid.v4()) {
50-
super(id);
51-
if (!uuid.isValid(id)) {
52-
throw new InvalidValue(
53-
`${this.__brand} is not valid UUID, given: "${id}"`
54-
);
55-
}
56-
}
57-
};
58-
}
5946
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Uuid } from '../src';
22

33
it('should fail typechecking', () => {
4-
class UserId extends Uuid.for<'User'>() {}
5-
class ProductId extends Uuid.for<'Product'>() {}
4+
class UserId extends Uuid<'User'> {}
5+
class ProductId extends Uuid<'Product'> {}
66

77
let userId = new UserId();
88
const productId = new ProductId();
@@ -11,3 +11,11 @@ it('should fail typechecking', () => {
1111
userId = productId;
1212
expect(userId).toBeDefined();
1313
});
14+
15+
it('will throw InvalidValue for invalid UUID', () => {
16+
class MyId extends Uuid<'My'> {}
17+
18+
expect(() => {
19+
new MyId('invalid-uuid');
20+
}).toThrow(`MyId is not valid UUID, given: "invalid-uuid"`);
21+
});

0 commit comments

Comments
 (0)