@schorts/shared-kernel 4.2.1 → 4.2.3
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/README.md +2 -0
- package/dist/cjs/value-objects/array-value.js +8 -0
- package/dist/cjs/value-objects/array-value.js.map +1 -1
- package/dist/cjs/value-objects/index.js +3 -1
- package/dist/cjs/value-objects/index.js.map +1 -1
- package/dist/cjs/value-objects/object-value.js +2 -0
- package/dist/cjs/value-objects/object-value.js.map +1 -1
- package/dist/cjs/value-objects/url-value.js +23 -0
- package/dist/cjs/value-objects/url-value.js.map +1 -0
- package/dist/esm/value-objects/array-value.js +8 -0
- package/dist/esm/value-objects/array-value.js.map +1 -1
- package/dist/esm/value-objects/index.js +3 -1
- package/dist/esm/value-objects/index.js.map +1 -1
- package/dist/esm/value-objects/object-value.js +2 -0
- package/dist/esm/value-objects/object-value.js.map +1 -1
- package/dist/esm/value-objects/url-value.js +23 -0
- package/dist/esm/value-objects/url-value.js.map +1 -0
- package/dist/types/value-objects/array-value.d.ts +5 -1
- package/dist/types/value-objects/array-value.d.ts.map +1 -1
- package/dist/types/value-objects/index.d.ts +1 -0
- package/dist/types/value-objects/index.d.ts.map +1 -1
- package/dist/types/value-objects/object-value.d.ts +2 -0
- package/dist/types/value-objects/object-value.d.ts.map +1 -1
- package/dist/types/value-objects/url-value.d.ts +11 -0
- package/dist/types/value-objects/url-value.d.ts.map +1 -0
- package/package.json +11 -3
- package/.idx/airules.md +0 -186
- package/.idx/dev.nix +0 -54
- package/.vscode/settings.json +0 -7
- package/CHANGELOG +0 -363
- package/__tests__/auth/auth-provider.test.ts +0 -50
- package/__tests__/auth/require-auth.decorator.test.ts +0 -88
- package/__tests__/criteria/criteria.test.ts +0 -159
- package/__tests__/criteria/direction.test.ts +0 -11
- package/__tests__/criteria/filter-criterion.test.ts +0 -15
- package/__tests__/criteria/operator.test.ts +0 -22
- package/__tests__/criteria/order.test.ts +0 -14
- package/__tests__/dao/dao.test.ts +0 -126
- package/__tests__/domain-events/domain-event-primitives.test.ts +0 -34
- package/__tests__/domain-events/domain-event.test.ts +0 -50
- package/__tests__/entities/entity-registry.test.ts +0 -71
- package/__tests__/entities/entity.test.ts +0 -114
- package/__tests__/entities/register-entity.decorator.test.ts +0 -54
- package/__tests__/formatters/pascal-camel-to-snake.test.ts +0 -19
- package/__tests__/http/fetch-http-provider.test.ts +0 -155
- package/__tests__/http/http-provider.test.ts +0 -55
- package/__tests__/json-api/json-api-connector.test.ts +0 -78
- package/__tests__/json-api/json-api-list.test.ts +0 -24
- package/__tests__/json-api/json-api-single.test.ts +0 -24
- package/__tests__/json-api/url-criteria-builder.test.ts +0 -74
- package/__tests__/messages/message.test.ts +0 -16
- package/__tests__/models/base-model.test.ts +0 -10
- package/__tests__/state-manager/state-manager.test.ts +0 -101
- package/__tests__/unit-of-work/unit-of-work.test.ts +0 -47
- package/__tests__/utils/url/url-with-params-builder.test.ts +0 -39
- package/__tests__/value-objects/coordinates-value.test.ts +0 -68
- package/__tests__/value-objects/email-value.test.ts +0 -43
- package/__tests__/value-objects/enum-value.test.ts +0 -51
- package/__tests__/value-objects/integer-value.test.ts +0 -115
- package/__tests__/value-objects/phone-value.test.ts +0 -82
- package/__tests__/value-objects/slug-value.test.ts +0 -43
- package/__tests__/value-objects/string-value.test.ts +0 -121
- package/__tests__/value-objects/uuid-value.test.ts +0 -67
- package/__tests__/value-objects/value-object.test.ts +0 -25
- package/jest.config.js +0 -25
- package/src/abac/index.ts +0 -1
- package/src/abac/predicate.ts +0 -3
- package/src/auth/auth-provider.ts +0 -11
- package/src/auth/exceptions/index.ts +0 -2
- package/src/auth/exceptions/not-authenticated.ts +0 -11
- package/src/auth/exceptions/require-auth-not-implemented-correctly.ts +0 -11
- package/src/auth/index.ts +0 -3
- package/src/auth/require-auth.decorator.ts +0 -42
- package/src/converters/index.ts +0 -1
- package/src/converters/remote-file-to-base64.ts +0 -19
- package/src/criteria/criteria.ts +0 -46
- package/src/criteria/direction.ts +0 -1
- package/src/criteria/exceptions/index.ts +0 -2
- package/src/criteria/exceptions/limit-not-valid.ts +0 -11
- package/src/criteria/exceptions/offset-not-valid.ts +0 -11
- package/src/criteria/index.ts +0 -5
- package/src/criteria/operator.ts +0 -12
- package/src/criteria/order.ts +0 -4
- package/src/dao/dao.ts +0 -16
- package/src/dao/index.ts +0 -1
- package/src/domain-events/domain-event-primitives.ts +0 -10
- package/src/domain-events/domain-event-registry.ts +0 -40
- package/src/domain-events/domain-event.ts +0 -29
- package/src/domain-events/exceptions/domain-event-not-registered.ts +0 -11
- package/src/domain-events/exceptions/index.ts +0 -1
- package/src/domain-events/index.ts +0 -4
- package/src/entities/entity-registry.ts +0 -30
- package/src/entities/entity.ts +0 -25
- package/src/entities/exceptions/entity-not-registered.ts +0 -11
- package/src/entities/exceptions/index.ts +0 -1
- package/src/entities/index.ts +0 -4
- package/src/entities/register-entity.decorator.ts +0 -16
- package/src/event-bus/event-store.ts +0 -9
- package/src/event-bus/event-subscriber.ts +0 -5
- package/src/event-bus/in-memory/async-in-memory-event-bus.ts +0 -67
- package/src/event-bus/in-memory/in-memory-event-store.ts +0 -28
- package/src/event-bus/in-memory/index.ts +0 -2
- package/src/event-bus/index.ts +0 -3
- package/src/formatters/index.ts +0 -1
- package/src/formatters/pascal-camel-to-snake.ts +0 -8
- package/src/http/exceptions/http-exception.ts +0 -8
- package/src/http/exceptions/index.ts +0 -1
- package/src/http/fetch-http-provider.ts +0 -151
- package/src/http/http-interceptor.ts +0 -3
- package/src/http/http-provider.ts +0 -7
- package/src/http/index.ts +0 -5
- package/src/i18n/index.ts +0 -2
- package/src/i18n/shared-kernel-translation-keys.ts +0 -19
- package/src/i18n/translation-resolver.ts +0 -3
- package/src/index.ts +0 -21
- package/src/json-api/exceptions/index.ts +0 -1
- package/src/json-api/exceptions/json-api-errors.ts +0 -17
- package/src/json-api/index.ts +0 -5
- package/src/json-api/json-api-connector.ts +0 -97
- package/src/json-api/json-api-list.ts +0 -13
- package/src/json-api/json-api-single.ts +0 -13
- package/src/json-api/url-criteria-builder.ts +0 -49
- package/src/messages/index.ts +0 -1
- package/src/messages/message.ts +0 -3
- package/src/models/base-model.ts +0 -3
- package/src/models/index.ts +0 -1
- package/src/pub-sub/index.ts +0 -2
- package/src/pub-sub/publisher.ts +0 -3
- package/src/pub-sub/subscription.ts +0 -6
- package/src/rbac/base-resource.ts +0 -3
- package/src/rbac/exceptions/index.ts +0 -1
- package/src/rbac/exceptions/not-authorized.ts +0 -1
- package/src/rbac/index.ts +0 -5
- package/src/rbac/permission.ts +0 -4
- package/src/rbac/rbac-policy.ts +0 -41
- package/src/result/index.ts +0 -1
- package/src/result/result.ts +0 -35
- package/src/state-manager/in-memory-state-manager.ts +0 -22
- package/src/state-manager/index.ts +0 -3
- package/src/state-manager/session-storage-state-manager.ts +0 -47
- package/src/state-manager/state-manager.ts +0 -28
- package/src/telemetry/console-telemetry.ts +0 -27
- package/src/telemetry/index.ts +0 -2
- package/src/telemetry/telemetry.ts +0 -5
- package/src/unit-of-work/index.ts +0 -1
- package/src/unit-of-work/unit-of-work.ts +0 -5
- package/src/utils/index.ts +0 -2
- package/src/utils/url/index.ts +0 -1
- package/src/utils/url/url-with-params-builder.ts +0 -19
- package/src/utils/value-objects/index.ts +0 -1
- package/src/utils/value-objects/rule.ts +0 -3
- package/src/value-objects/array-value.ts +0 -92
- package/src/value-objects/boolean-value.ts +0 -23
- package/src/value-objects/coordinates-value.ts +0 -50
- package/src/value-objects/date-value.ts +0 -36
- package/src/value-objects/email-value.ts +0 -25
- package/src/value-objects/enum-value.ts +0 -27
- package/src/value-objects/exceptions/index.ts +0 -2
- package/src/value-objects/exceptions/value-not-valid.ts +0 -3
- package/src/value-objects/exceptions/values-not-valid.ts +0 -11
- package/src/value-objects/float-valuet.ts +0 -39
- package/src/value-objects/index.ts +0 -17
- package/src/value-objects/integer-value.ts +0 -29
- package/src/value-objects/object-value.ts +0 -84
- package/src/value-objects/phone-value.ts +0 -53
- package/src/value-objects/slug-value.ts +0 -25
- package/src/value-objects/string-value.ts +0 -27
- package/src/value-objects/uuid-value.ts +0 -34
- package/src/value-objects/value-object.ts +0 -7
- package/tsconfig.cjs.json +0 -13
- package/tsconfig.esm.json +0 -12
- package/tsconfig.json +0 -47
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { HTTPProvider, HTTPException } from "../http";
|
|
2
|
-
import type { JSONAPIList } from "./json-api-list";
|
|
3
|
-
import type { JSONAPISingle } from "./json-api-single";
|
|
4
|
-
import { URLCriteriaBuilder } from "./url-criteria-builder";
|
|
5
|
-
import { Criteria } from "../criteria";
|
|
6
|
-
import { JSONAPIErrors } from "./exceptions";
|
|
7
|
-
|
|
8
|
-
export class JSONAPIConnector {
|
|
9
|
-
constructor(private http: HTTPProvider) {}
|
|
10
|
-
|
|
11
|
-
async findOne<EntityAttributes>(
|
|
12
|
-
url: URL,
|
|
13
|
-
criteria?: Criteria,
|
|
14
|
-
include?: string[]
|
|
15
|
-
): Promise<JSONAPISingle<EntityAttributes>> {
|
|
16
|
-
try {
|
|
17
|
-
const fullUrl = new URLCriteriaBuilder(url, criteria, include).build();
|
|
18
|
-
|
|
19
|
-
return await this.http.get(fullUrl);
|
|
20
|
-
} catch(error) {
|
|
21
|
-
this.handleError(error);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async findMany<EntityAttributes>(
|
|
26
|
-
url: URL,
|
|
27
|
-
criteria?: Criteria,
|
|
28
|
-
include?: string[]
|
|
29
|
-
): Promise<JSONAPIList<EntityAttributes>> {
|
|
30
|
-
try {
|
|
31
|
-
const fullUrl = new URLCriteriaBuilder(url, criteria, include).build();
|
|
32
|
-
|
|
33
|
-
return await this.http.get(fullUrl);
|
|
34
|
-
} catch(error) {
|
|
35
|
-
this.handleError(error);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
async create<EntityAttributes>(
|
|
40
|
-
url: URL,
|
|
41
|
-
payload: {
|
|
42
|
-
id: string;
|
|
43
|
-
type: string;
|
|
44
|
-
attributes: Omit<Partial<EntityAttributes>, "id">;
|
|
45
|
-
} | Array<{
|
|
46
|
-
id: string;
|
|
47
|
-
type: string;
|
|
48
|
-
attributes: Omit<Partial<EntityAttributes>, "id">;
|
|
49
|
-
}>,
|
|
50
|
-
meta?: Record<string, any>,
|
|
51
|
-
): Promise<JSONAPISingle<EntityAttributes>> {
|
|
52
|
-
try {
|
|
53
|
-
const body: Record<string, any> = { data: payload };
|
|
54
|
-
|
|
55
|
-
if (meta) {
|
|
56
|
-
body["meta"] = meta;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return await this.http.post(url, body);
|
|
60
|
-
} catch(error) {
|
|
61
|
-
this.handleError(error);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
async update<EntityAttributes>(
|
|
66
|
-
url: URL,
|
|
67
|
-
payload: {
|
|
68
|
-
id: string;
|
|
69
|
-
type: string;
|
|
70
|
-
attributes: Omit<Partial<EntityAttributes>, "id">;
|
|
71
|
-
}
|
|
72
|
-
): Promise<JSONAPISingle<EntityAttributes>> {
|
|
73
|
-
try {
|
|
74
|
-
return await this.http.patch(url, { data: payload });
|
|
75
|
-
} catch(error) {
|
|
76
|
-
this.handleError(error);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
async delete<EntityAttributes>(
|
|
81
|
-
url: URL
|
|
82
|
-
): Promise<JSONAPISingle<EntityAttributes>> {
|
|
83
|
-
try {
|
|
84
|
-
return await this.http.delete(url);
|
|
85
|
-
} catch(error) {
|
|
86
|
-
this.handleError(error);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
private handleError(error: unknown): never {
|
|
91
|
-
if (error instanceof HTTPException && error.body?.errors) {
|
|
92
|
-
throw new JSONAPIErrors(error.body.errors);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
throw error;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export type JSONAPIList<EntityAttributes> = {
|
|
2
|
-
data: Array<{
|
|
3
|
-
id: string;
|
|
4
|
-
type: string;
|
|
5
|
-
attributes: Omit<EntityAttributes, "id">;
|
|
6
|
-
}>;
|
|
7
|
-
included?: Array<{
|
|
8
|
-
id: string;
|
|
9
|
-
type: string;
|
|
10
|
-
attributes: Record<string, any>;
|
|
11
|
-
}>;
|
|
12
|
-
meta?: Record<string, any>;
|
|
13
|
-
};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export type JSONAPISingle<EntityAttributes> = {
|
|
2
|
-
data: {
|
|
3
|
-
id: string;
|
|
4
|
-
type: string;
|
|
5
|
-
attributes: Omit<EntityAttributes, "id">;
|
|
6
|
-
};
|
|
7
|
-
included?: Array<{
|
|
8
|
-
id: string;
|
|
9
|
-
type: string;
|
|
10
|
-
attributes: Record<string, any>;
|
|
11
|
-
}>;
|
|
12
|
-
meta?: Record<string, any>;
|
|
13
|
-
};
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { URLWithParamsBuilder } from "../utils";
|
|
2
|
-
import { Criteria } from "../criteria";
|
|
3
|
-
|
|
4
|
-
export class URLCriteriaBuilder {
|
|
5
|
-
constructor(
|
|
6
|
-
private readonly base: URL,
|
|
7
|
-
private readonly criteria?: Criteria,
|
|
8
|
-
private readonly include?: string[]
|
|
9
|
-
) {}
|
|
10
|
-
|
|
11
|
-
build(): URL {
|
|
12
|
-
const builder = new URLWithParamsBuilder(new URL(this.base.href));
|
|
13
|
-
|
|
14
|
-
if (this.include?.length) {
|
|
15
|
-
builder.with({ include: this.include });
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (this.criteria) {
|
|
19
|
-
Object.entries(this.criteria.filters).forEach(([field, { operator, value }]) => {
|
|
20
|
-
const encodedField = field.replace(/\./g, "."); // allows nested filters like roles.name
|
|
21
|
-
|
|
22
|
-
if (operator === "EQUAL") {
|
|
23
|
-
builder.with({ [`filter[${encodedField}]`]: value });
|
|
24
|
-
} else if (operator === "IN" && Array.isArray(value)) {
|
|
25
|
-
builder.with({ [`filter[${encodedField}]`]: value.join(",") });
|
|
26
|
-
} else {
|
|
27
|
-
builder.with({ [`filter[${encodedField}][${operator}]`]: value });
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
if (this.criteria.orders.length > 0) {
|
|
32
|
-
const sortParam = this.criteria.orders
|
|
33
|
-
.map(({ field, direction }) => (direction === "DESC" ? `-${field}` : field))
|
|
34
|
-
.join(",");
|
|
35
|
-
builder.with({ sort: sortParam });
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (this.criteria.limit !== undefined) {
|
|
39
|
-
builder.with({ "page[limit]": this.criteria.limit });
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (this.criteria.offset !== undefined) {
|
|
43
|
-
builder.with({ "page[offset]": this.criteria.offset });
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return builder.build();
|
|
48
|
-
}
|
|
49
|
-
}
|
package/src/messages/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type { Message } from "./message";
|
package/src/messages/message.ts
DELETED
package/src/models/base-model.ts
DELETED
package/src/models/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type { BaseModel } from "./base-model";
|
package/src/pub-sub/index.ts
DELETED
package/src/pub-sub/publisher.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { NotAuthorized } from "./not-authorized";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export class NotAuthorized extends Error {}
|
package/src/rbac/index.ts
DELETED
package/src/rbac/permission.ts
DELETED
package/src/rbac/rbac-policy.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { Permission } from './permission';
|
|
2
|
-
import { BaseResource } from './base-resource';
|
|
3
|
-
import { Predicate } from '../abac';
|
|
4
|
-
|
|
5
|
-
export abstract class RBACPolicy {
|
|
6
|
-
abstract getPermissions(role: string): Permission[];
|
|
7
|
-
|
|
8
|
-
can(role: string, action: Permission['action'], resource: BaseResource): boolean {
|
|
9
|
-
const permissions = this.getPermissions(role);
|
|
10
|
-
|
|
11
|
-
return permissions.some(
|
|
12
|
-
(perm) =>
|
|
13
|
-
(perm.resource === '*' || perm.resource === resource.name) &&
|
|
14
|
-
(perm.action === action || perm.action === 'manage')
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
canWithAttributes<User extends { id: string }, Resource extends BaseResource>(
|
|
19
|
-
user: User,
|
|
20
|
-
role: string,
|
|
21
|
-
action: Permission['action'],
|
|
22
|
-
resource: Resource,
|
|
23
|
-
predicates: Predicate<User, Resource>[],
|
|
24
|
-
): boolean {
|
|
25
|
-
if (!this.can(role, action, resource)) return false;
|
|
26
|
-
|
|
27
|
-
return predicates.every((predicate) => predicate(user, resource));
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
canAnyWithAttributes<User extends { id: string }, Resource extends BaseResource>(
|
|
31
|
-
user: User,
|
|
32
|
-
role: string,
|
|
33
|
-
action: Permission['action'],
|
|
34
|
-
resource: Resource,
|
|
35
|
-
predicates: Predicate<User, Resource>[]
|
|
36
|
-
): boolean {
|
|
37
|
-
if (!this.can(role, action, resource)) return false;
|
|
38
|
-
|
|
39
|
-
return predicates.some((predicate) => predicate(user, resource));
|
|
40
|
-
}
|
|
41
|
-
}
|
package/src/result/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { Result } from "./result";
|
package/src/result/result.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
export class Result<Type> {
|
|
2
|
-
private readonly success: boolean;
|
|
3
|
-
private readonly value: Type | undefined;
|
|
4
|
-
private readonly error: Error | undefined;
|
|
5
|
-
|
|
6
|
-
private constructor(success: boolean, value?: Type, error?: Error) {
|
|
7
|
-
this.success = success;
|
|
8
|
-
this.value = value;
|
|
9
|
-
this.error = error;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
static success<Type>(value: Type): Result<Type> {
|
|
13
|
-
return new Result<Type>(true, value);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
static error<Type>(error: Error): Result<Type> {
|
|
17
|
-
return new Result<Type>(false, undefined, error);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
isSuccess(): boolean {
|
|
21
|
-
return this.success;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
isFailure(): boolean {
|
|
25
|
-
return !this.isSuccess();
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
getValue(): Type | undefined {
|
|
29
|
-
return this.value;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
getError(): Error | undefined {
|
|
33
|
-
return this.error;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { StateManager } from './state-manager';
|
|
2
|
-
|
|
3
|
-
export class InMemoryStateManager<Schema extends Record<string, any>> extends StateManager<Schema> {
|
|
4
|
-
constructor(initialState: Schema = {} as Schema) {
|
|
5
|
-
super(initialState);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
async getValue<Key extends keyof Schema>(key: Key): Promise<Schema[Key]> {
|
|
9
|
-
return this.state[key];
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
async setValue<Key extends keyof Schema>(key: Key, value: Schema[Key]): Promise<void> {
|
|
13
|
-
this.state[key] = value;
|
|
14
|
-
|
|
15
|
-
this.notifyListeners();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async removeValue<Key extends keyof Schema>(key: Key): Promise<void> {
|
|
19
|
-
delete this.state[key];
|
|
20
|
-
this.notifyListeners();
|
|
21
|
-
}
|
|
22
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { StateManager } from './state-manager';
|
|
2
|
-
|
|
3
|
-
export class SessionStorageStateManager<Schema extends Record<string, any>> extends StateManager<Schema> {
|
|
4
|
-
constructor(initialState: Schema) {
|
|
5
|
-
super(initialState);
|
|
6
|
-
this.loadPersistedState();
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
private loadPersistedState(): void {
|
|
10
|
-
const keys = Object.keys(this.state) as Array<keyof Schema>;
|
|
11
|
-
|
|
12
|
-
for (const key of keys) {
|
|
13
|
-
const storageKey = String(key);
|
|
14
|
-
const persistedValue = sessionStorage.getItem(storageKey);
|
|
15
|
-
this.state[key] = JSON.parse(persistedValue || "null");
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
private persistValue<Key extends keyof Schema>(key: Key, value: Schema[Key]): void {
|
|
20
|
-
const storageKey = String(key);
|
|
21
|
-
|
|
22
|
-
sessionStorage.setItem(storageKey, JSON.stringify(value));
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public async getValue<Key extends keyof Schema>(key: Key): Promise<Schema[Key]> {
|
|
26
|
-
return this.state[key];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public async setValue<Key extends keyof Schema>(key: Key, value: Schema[Key]): Promise<void> {
|
|
30
|
-
this.state = {
|
|
31
|
-
...this.state,
|
|
32
|
-
[key]: value
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
this.persistValue(key, value);
|
|
36
|
-
this.notifyListeners();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public async removeValue<Key extends keyof Schema>(key: Key): Promise<void> {
|
|
40
|
-
const storageKey = String(key);
|
|
41
|
-
const { [key]: _, ...newState } = this.state;
|
|
42
|
-
this.state = newState as Schema;
|
|
43
|
-
|
|
44
|
-
sessionStorage.removeItem(storageKey);
|
|
45
|
-
this.notifyListeners();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
export abstract class StateManager<Schema extends Record<string, any>> {
|
|
2
|
-
protected state: Schema;
|
|
3
|
-
private listeners: Array<(state: Schema) => void> = [];
|
|
4
|
-
|
|
5
|
-
constructor(initialState: Schema = {} as Schema) {
|
|
6
|
-
this.state = initialState;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
abstract getValue<Key extends keyof Schema>(key: Key): Promise<Schema[Key]>;
|
|
10
|
-
abstract setValue<Key extends keyof Schema>(key: Key, value: Schema[Key]): Promise<void>;
|
|
11
|
-
abstract removeValue<Key extends keyof Schema>(key: Key): Promise<void>;
|
|
12
|
-
|
|
13
|
-
getState(): Schema {
|
|
14
|
-
return this.state;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
subscribe(listener: (state: Schema) => void): () => void {
|
|
18
|
-
this.listeners.push(listener);
|
|
19
|
-
|
|
20
|
-
return () => {
|
|
21
|
-
this.listeners = this.listeners.filter(l => l !== listener);
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
protected notifyListeners(): void {
|
|
26
|
-
this.listeners.forEach(listener => listener(this.state));
|
|
27
|
-
}
|
|
28
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { Telemetry } from "./telemetry";
|
|
2
|
-
|
|
3
|
-
export class ConsoleTelemetry implements Telemetry {
|
|
4
|
-
private readonly customContext: Record<string, string> = {};
|
|
5
|
-
private transaction: string | null = null;
|
|
6
|
-
|
|
7
|
-
init(): void {
|
|
8
|
-
console.log("Telemetry initialized");
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
trackError(error: Error): void {
|
|
12
|
-
console.error({
|
|
13
|
-
error,
|
|
14
|
-
timestamp: new Date(),
|
|
15
|
-
customContext: this.customContext,
|
|
16
|
-
transaction: this.transaction,
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
setCustomContext(key: string, value: string): void {
|
|
21
|
-
this.customContext[key] = value;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
setTransaction(name: string): void {
|
|
25
|
-
this.transaction = name;
|
|
26
|
-
}
|
|
27
|
-
}
|
package/src/telemetry/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type { UnitOfWork } from "./unit-of-work";
|
package/src/utils/index.ts
DELETED
package/src/utils/url/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { URLWithParamsBuilder } from "./url-with-params-builder";
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export class URLWithParamsBuilder {
|
|
2
|
-
constructor(private readonly base: URL) {}
|
|
3
|
-
|
|
4
|
-
with(params: Record<string, string | number | boolean | Array<string | number>>): URLWithParamsBuilder {
|
|
5
|
-
Object.entries(params).forEach(([key, value]) => {
|
|
6
|
-
if (Array.isArray(value)) {
|
|
7
|
-
value.forEach(v => this.base.searchParams.append(key, String(v)));
|
|
8
|
-
} else {
|
|
9
|
-
this.base.searchParams.set(key, String(value));
|
|
10
|
-
}
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
return this;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
build(): URL {
|
|
17
|
-
return this.base;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { rule } from "./rule";
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { ValueObject } from "./value-object";
|
|
2
|
-
|
|
3
|
-
type Primitive = string | number | boolean | null | undefined;
|
|
4
|
-
|
|
5
|
-
export type ValidationRule<Type> =
|
|
6
|
-
| { required: true }
|
|
7
|
-
| { greater_than: number }
|
|
8
|
-
| { greater_than_or_equal: number }
|
|
9
|
-
| { less_than: number }
|
|
10
|
-
| { less_than_or_equal: number }
|
|
11
|
-
| { type: "string" | "number" | "boolean" }
|
|
12
|
-
| { custom: (value: Type) => boolean };
|
|
13
|
-
|
|
14
|
-
type ObjectSchema<Type> = {
|
|
15
|
-
[Key in keyof Type]?: Type[Key] extends Primitive
|
|
16
|
-
? ValidationRule<Type[Key]>[]
|
|
17
|
-
: ObjectSchema<Type[Key]>;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export abstract class ArrayValue<Type = any> implements ValueObject {
|
|
21
|
-
readonly valueType = "Array";
|
|
22
|
-
readonly value: Type[];
|
|
23
|
-
readonly schema: ObjectSchema<Type> | ValidationRule<Type>[];
|
|
24
|
-
abstract readonly attributeName: string;
|
|
25
|
-
abstract readonly isPrimitive: boolean;
|
|
26
|
-
|
|
27
|
-
constructor(
|
|
28
|
-
value: Type[],
|
|
29
|
-
schema: ObjectSchema<Type> | ValidationRule<Type>[],
|
|
30
|
-
) {
|
|
31
|
-
this.value = this.deepFreeze(value);
|
|
32
|
-
this.schema = schema;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
get isValid(): boolean {
|
|
36
|
-
return this.value.every((item) => {
|
|
37
|
-
return this.isPrimitive
|
|
38
|
-
? (this.schema as ValidationRule<Type>[]).every(rule => this.validateRule(item, rule))
|
|
39
|
-
: this.validateObject(item, this.schema as ObjectSchema<Type>);
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
private validateObject(obj: any, schema: ObjectSchema<any>): boolean {
|
|
44
|
-
return Object.entries(schema).every(([key, rulesOrNested]) => {
|
|
45
|
-
const value = obj[key];
|
|
46
|
-
|
|
47
|
-
if (Array.isArray(rulesOrNested)) {
|
|
48
|
-
return rulesOrNested.every((rule) => this.validateRule(value, rule));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (typeof rulesOrNested === "object" && value !== null && typeof value === "object") {
|
|
52
|
-
return this.validateObject(value, rulesOrNested as ObjectSchema<any>);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return true;
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
private validateRule(value: any, rule: ValidationRule<any>): boolean {
|
|
60
|
-
if ("required" in rule) return value !== undefined && value !== null;
|
|
61
|
-
if ("greater_than" in rule) return typeof value === "number" && value > rule.greater_than;
|
|
62
|
-
if ("greater_than_or_equal" in rule) return typeof value === "number" && value >= rule.greater_than_or_equal;
|
|
63
|
-
if ("less_than" in rule) return typeof value === "number" && value < rule.less_than;
|
|
64
|
-
if ("less_than_or_equal" in rule) return typeof value === "number" && value <= rule.less_than_or_equal;
|
|
65
|
-
if ("type" in rule) return typeof value === rule.type;
|
|
66
|
-
if ("custom" in rule) return rule.custom(value);
|
|
67
|
-
|
|
68
|
-
return true;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
equals(valueObject: unknown): boolean {
|
|
72
|
-
if (!(valueObject instanceof ArrayValue)) return false;
|
|
73
|
-
if (!this.isValid || !valueObject.isValid) return false;
|
|
74
|
-
|
|
75
|
-
return JSON.stringify(this.value) === JSON.stringify(valueObject.value);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private deepFreeze<Type>(obj: Type): Type {
|
|
79
|
-
if (Array.isArray(obj)) {
|
|
80
|
-
obj.forEach(item => this.deepFreeze(item));
|
|
81
|
-
} else if (obj && typeof obj === "object") {
|
|
82
|
-
Object.getOwnPropertyNames(obj).forEach(prop => {
|
|
83
|
-
const value = (obj as any)[prop];
|
|
84
|
-
if (value && typeof value === "object") {
|
|
85
|
-
this.deepFreeze(value);
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return Object.freeze(obj);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { ValueObject } from "./";
|
|
2
|
-
|
|
3
|
-
export abstract class BooleanValue implements ValueObject {
|
|
4
|
-
readonly valueType = "Boolean";
|
|
5
|
-
readonly value: boolean;
|
|
6
|
-
|
|
7
|
-
constructor(value: boolean) {
|
|
8
|
-
this.value = value;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
get isValid(): boolean {
|
|
12
|
-
return true;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
equals(valueObject: unknown): boolean {
|
|
16
|
-
if (!(valueObject instanceof BooleanValue)) return false;
|
|
17
|
-
if (!this.isValid || !valueObject.isValid) return false;
|
|
18
|
-
|
|
19
|
-
return this.value === valueObject.value;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
abstract readonly attributeName: string;
|
|
23
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { ValueObject } from "./";
|
|
2
|
-
|
|
3
|
-
const EPSILON = 1e-6;
|
|
4
|
-
|
|
5
|
-
export abstract class CoordinatesValue implements ValueObject {
|
|
6
|
-
readonly valueType = "Coordinates";
|
|
7
|
-
readonly value: {
|
|
8
|
-
latitude: number,
|
|
9
|
-
longitude: number,
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
constructor(value: CoordinatesValue["value"]) {
|
|
13
|
-
this.value = value;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
get isValid(): boolean {
|
|
17
|
-
const validLatitud = -90 <= this.value.latitude && this.value.latitude <= 90;
|
|
18
|
-
const validLongitude = -180 <= this.value.longitude && this.value.longitude <= 180;
|
|
19
|
-
|
|
20
|
-
return validLatitud && validLongitude;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
get latitude(): number | null {
|
|
24
|
-
if (this.isValid) {
|
|
25
|
-
return this.value.latitude;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return null
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
get longitude(): number | null {
|
|
32
|
-
if (this.isValid) {
|
|
33
|
-
return this.value.longitude;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
equals(valueObject: unknown): boolean {
|
|
40
|
-
if (!(valueObject instanceof CoordinatesValue)) return false;
|
|
41
|
-
if (!this.isValid || !valueObject.isValid) return false;
|
|
42
|
-
|
|
43
|
-
const latDiff = Math.abs(this.value.latitude - valueObject.value.latitude);
|
|
44
|
-
const lonDiff = Math.abs(this.value.longitude - valueObject.value.longitude);
|
|
45
|
-
|
|
46
|
-
return latDiff < EPSILON && lonDiff < EPSILON;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
abstract readonly attributeName: string;
|
|
50
|
-
}
|