@arcote.tech/arc 0.0.26 → 0.0.27

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.
@@ -15,7 +15,7 @@ declare class ArcContext<const C extends ArcContextElementAny[], const Cmds exte
15
15
  [Collection in C[number] as Collection["name"]]: ReturnType<Collection["queryBuilder"]>;
16
16
  };
17
17
  commandContext(client: string, dataStorage: DataStorage, publishEvent: (event: any) => Promise<void>): { [ContextElement in C[number] as ContextElement["name"]]: ReturnType<ContextElement["commandContext"]>; } & {
18
- $client: string;
18
+ $client: any;
19
19
  };
20
20
  commandsClient(client: string, dataStorage: DataStorage, catchErrorCallback: (error: any) => void): CommandsClient<Cmds>;
21
21
  }
@@ -16,9 +16,17 @@ export declare class MasterDataStorage extends DataStorage {
16
16
  _id: string;
17
17
  }>(storeName: string): StoreState<Item>;
18
18
  applyChanges(changes: DataStorageChanges[]): Promise<void[]>;
19
+ applySerializedChanges(changes: any[]): Promise<{
20
+ from: {
21
+ _id: string;
22
+ } | null;
23
+ to: {
24
+ _id: string;
25
+ } | null;
26
+ }[][]>;
19
27
  commitChanges(changes: DataStorageChanges[]): Promise<void>;
20
28
  fork(): ForkedDataStorage;
21
- sync(progressCallback: ({ store, size }: {
29
+ sync(progressCallback: ({ store, size, }: {
22
30
  store: string;
23
31
  size: number;
24
32
  }) => void): Promise<void>;
@@ -27,6 +27,10 @@ export declare abstract class StoreState<Item extends {
27
27
  from: Item | null;
28
28
  to: Item | null;
29
29
  }>;
30
+ applySerializedChanges(changes: StoreStateChange<any>[]): Promise<{
31
+ from: Item | null;
32
+ to: Item | null;
33
+ }[]>;
30
34
  unsubscribe(listener: QueryListenerCallback<Item>): void;
31
35
  protected notifyListeners(events: ListenerEvent<Item>[]): void;
32
36
  }
@@ -1,5 +1,5 @@
1
- import { ArcAbstract } from "./abstract";
2
- export declare abstract class ArcPrimitive<T> extends ArcAbstract {
1
+ import { ArcAbstract, type Validators } from "./abstract";
2
+ export declare abstract class ArcPrimitive<V extends Validators, T> extends ArcAbstract<V> {
3
3
  constructor();
4
4
  serialize(value: T): T;
5
5
  parse(value: T): T;
@@ -2,12 +2,28 @@ import { ArcBranded } from "./branded";
2
2
  import { ArcDefault } from "./default";
3
3
  import type { ArcElement } from "./element";
4
4
  import { ArcOptional } from "./optional";
5
- export declare abstract class ArcAbstract implements ArcElement {
5
+ export type CheckFn<T> = (value: any) => T | undefined;
6
+ export type Validator<T> = {
7
+ name: string;
8
+ validator: CheckFn<T>;
9
+ };
10
+ export type Validators = Validator<any>[];
11
+ export type GetValidator<A extends ArcAbstract<any>> = A extends ArcAbstract<infer T> ? T : [];
12
+ export declare abstract class ArcAbstract<V extends Validators> implements ArcElement {
13
+ protected validations: V;
6
14
  abstract serialize(value: any): any;
7
15
  abstract deserialize(value: any): any;
8
16
  abstract parse(value: any): any;
9
17
  default(defaultValueOrCallback: (() => ReturnType<this["deserialize"]>) | ReturnType<this["deserialize"]>): ArcDefault<this>;
10
18
  optional(): ArcOptional<this>;
11
19
  branded<Brand extends string | symbol>(name: Brand): ArcBranded<this, Brand>;
20
+ protected clone(): this;
21
+ validate(value: any): { [Key in V[number] as Key["name"]]: ReturnType<Key["validator"]>; };
22
+ protected pipeValidation<Name extends string, Val extends CheckFn<any>>(name: Name, validator: Val): ArcAbstract<[...V, {
23
+ name: Name;
24
+ validator: Val;
25
+ }]>;
26
+ getValidations(): V;
12
27
  }
28
+ export type ArcAbstractAny = ArcAbstract<any>;
13
29
  //# sourceMappingURL=abstract.d.ts.map
@@ -1,7 +1,7 @@
1
1
  import { type util } from "../utils";
2
2
  import { ArcAbstract } from "./abstract";
3
3
  import type { ArcElement } from "./element";
4
- export declare class ArcArray<E extends ArcElement> extends ArcAbstract {
4
+ export declare class ArcArray<E extends ArcElement> extends ArcAbstract<[]> {
5
5
  private parent;
6
6
  constructor(parent: E);
7
7
  parse(value: util.FirstArgument<E["deserialize"]>[]): ReturnType<E["parse"]>[];
@@ -1,5 +1,5 @@
1
1
  import { ArcPrimitive } from "./abstract-primitive";
2
- export declare class ArcBoolean extends ArcPrimitive<boolean> {
2
+ export declare class ArcBoolean extends ArcPrimitive<[], boolean> {
3
3
  }
4
4
  export declare function boolean(): ArcBoolean;
5
5
  //# sourceMappingURL=boolean.d.ts.map
@@ -1,8 +1,8 @@
1
1
  import { type util } from "../utils";
2
- import { ArcAbstract } from "./abstract";
2
+ import { type ArcAbstractAny } from "./abstract";
3
3
  import type { ArcElement } from "./element";
4
4
  import { ArcOptional } from "./optional";
5
- export declare class ArcBranded<E extends ArcAbstract, Brand extends string | symbol> implements ArcElement {
5
+ export declare class ArcBranded<E extends ArcAbstractAny, Brand extends string | symbol> implements ArcElement {
6
6
  private parent;
7
7
  private brand;
8
8
  constructor(parent: E, brand: Brand);
@@ -16,6 +16,7 @@ export declare class ArcBranded<E extends ArcAbstract, Brand extends string | sy
16
16
  __brand: Brand;
17
17
  };
18
18
  optional(): ArcOptional<this>;
19
+ validate(value: unknown): unknown;
19
20
  }
20
- export type ArcBrandedAny = ArcBranded<ArcAbstract, any>;
21
+ export type ArcBrandedAny = ArcBranded<ArcAbstractAny, any>;
21
22
  //# sourceMappingURL=branded.d.ts.map
@@ -0,0 +1,25 @@
1
+ import type { util } from "../utils";
2
+ import type { ArcElement } from "./element";
3
+ import type { ArcRawShape } from "./object";
4
+ import { ArcOptional } from "./optional";
5
+ export declare function ArcClass<E extends ArcRawShape>(shape: E): {
6
+ new (): { [key in keyof E]: ReturnType<E[key]["deserialize"]>; };
7
+ } & {
8
+ create<T extends {
9
+ new (): any;
10
+ }>(this: T, data: { [key in keyof E]: util.FirstArgument<E[key]["deserialize"]>; }): InstanceType<T>;
11
+ };
12
+ export declare class ArcInstanceOf<E> implements ArcElement {
13
+ private Class;
14
+ constructor(Class: {
15
+ new (): E;
16
+ });
17
+ serialize(value: unknown): unknown;
18
+ deserialize(value: unknown): E;
19
+ parse(value: unknown): unknown;
20
+ optional(): ArcOptional<this>;
21
+ }
22
+ export declare function instanceOf<E>(element: {
23
+ new (): E;
24
+ }): ArcInstanceOf<E>;
25
+ //# sourceMappingURL=class.d.ts.map
@@ -1,5 +1,5 @@
1
1
  import { ArcAbstract } from "./abstract";
2
- export declare class ArcDate extends ArcAbstract {
2
+ export declare class ArcDate extends ArcAbstract<[]> {
3
3
  constructor();
4
4
  parse(value: string | number | Date): Date;
5
5
  serialize(value: Date): number;
@@ -4,6 +4,7 @@ export declare class ArcDefault<E extends ArcElement> implements ArcElement {
4
4
  private parent;
5
5
  private defaultValueOrCallback;
6
6
  constructor(parent: E, defaultValueOrCallback: (() => ReturnType<E["deserialize"]>) | ReturnType<E["deserialize"]>);
7
+ validate(value: unknown): unknown;
7
8
  parse(value: util.FirstArgument<E["parse"]> | undefined): ReturnType<E["parse"]>;
8
9
  serialize(value: util.FirstArgument<E["serialize"]> | undefined): ReturnType<E["serialize"]>;
9
10
  deserialize(value: util.FirstArgument<E["deserialize"]> | undefined): ReturnType<E["deserialize"]>;
@@ -2,5 +2,6 @@ export interface ArcElement {
2
2
  serialize(value: unknown): unknown;
3
3
  deserialize(value: unknown): unknown;
4
4
  parse(value: unknown): unknown;
5
+ validate(value: unknown): unknown;
5
6
  }
6
7
  //# sourceMappingURL=element.d.ts.map
@@ -1,5 +1,24 @@
1
+ import type { CheckFn, GetValidator, Validators } from "./abstract";
1
2
  import { ArcPrimitive } from "./abstract-primitive";
2
- export declare class ArcNumber extends ArcPrimitive<number> {
3
+ export declare class ArcNumber<V extends Validators = []> extends ArcPrimitive<V, number> {
4
+ min(min: number): ArcNumber<[...V, {
5
+ name: "min";
6
+ validator: (value: any) => {
7
+ current: any;
8
+ min: number;
9
+ } | undefined;
10
+ }]>;
11
+ max(max: number): ArcNumber<[...V, {
12
+ name: "max";
13
+ validator: (value: any) => {
14
+ current: any;
15
+ max: number;
16
+ } | undefined;
17
+ }]>;
18
+ validation<Name extends string, Fn extends CheckFn<any>>(name: Name, validator: Fn): ArcNumber<GetValidator<import("./abstract").ArcAbstract<[...V, {
19
+ name: Name;
20
+ validator: Fn;
21
+ }]>>>;
3
22
  }
4
- export declare function number(): ArcNumber;
23
+ export declare function number(): ArcNumber<[]>;
5
24
  //# sourceMappingURL=number.d.ts.map
@@ -4,7 +4,7 @@ import type { ArcElement } from "./element";
4
4
  export declare type ArcRawShape = {
5
5
  [k: string]: ArcElement;
6
6
  };
7
- export declare class ArcObject<E extends ArcRawShape> extends ArcAbstract {
7
+ export declare class ArcObject<E extends ArcRawShape> extends ArcAbstract<[]> {
8
8
  private rawShape;
9
9
  constructor(rawShape: E);
10
10
  parse(value: {
@@ -38,7 +38,13 @@ export declare class ArcObject<E extends ArcRawShape> extends ArcAbstract {
38
38
  }>): {
39
39
  [key in keyof E]: ReturnType<E[key]["serialize"]>;
40
40
  };
41
+ pick<Keys extends keyof E>(...keys: Keys[]): ArcObject<Pick<E, Keys>>;
42
+ validate(value: any): {
43
+ [key in keyof E]: ReturnType<E[key]["validate"]>;
44
+ };
41
45
  }
42
46
  export type ArcObjectAny = ArcObject<any>;
47
+ export type ArcObjectKeys<T extends ArcObjectAny> = T extends ArcObject<infer E> ? Exclude<keyof E, symbol> : never;
48
+ export type ArcObjectValueByKey<T extends ArcObjectAny, K extends ArcObjectKeys<T>> = T extends ArcObject<infer E> ? E[K] : never;
43
49
  export declare function object<E extends ArcRawShape>(element: E): ArcObject<E>;
44
50
  //# sourceMappingURL=object.d.ts.map
@@ -1,5 +1,5 @@
1
1
  import { type util } from "../utils";
2
- import { ArcAbstract } from "./abstract";
2
+ import { type ArcAbstractAny } from "./abstract";
3
3
  import type { ArcElement } from "./element";
4
4
  export declare class ArcOptional<E extends ArcElement> implements ArcElement {
5
5
  private parent;
@@ -7,6 +7,7 @@ export declare class ArcOptional<E extends ArcElement> implements ArcElement {
7
7
  parse(value: util.FirstArgument<E["parse"]> | null | undefined): ReturnType<E["parse"]> | null;
8
8
  serialize(value: util.FirstArgument<E["serialize"]> | null | undefined): ReturnType<E["serialize"]> | null;
9
9
  deserialize(value: util.FirstArgument<E["deserialize"]> | null | undefined): ReturnType<E["deserialize"]> | null;
10
+ validate(value: unknown): unknown;
10
11
  }
11
- export type ArcOptionalAny = ArcOptional<ArcAbstract>;
12
+ export type ArcOptionalAny = ArcOptional<ArcAbstractAny>;
12
13
  //# sourceMappingURL=optional.d.ts.map
@@ -0,0 +1,12 @@
1
+ import type { util } from "../utils";
2
+ import type { ArcElement } from "./element";
3
+ export declare class ArcOr<T extends ArcElement[]> implements ArcElement {
4
+ private elements;
5
+ constructor(elements: T);
6
+ serialize(value: util.FirstArgument<T[number]["serialize"]>): ReturnType<T[number]["serialize"]>;
7
+ deserialize(value: util.FirstArgument<T[number]["deserialize"]>): ReturnType<T[number]["deserialize"]>;
8
+ parse(value: util.FirstArgument<T[number]["parse"]>): ReturnType<T[number]["parse"]>;
9
+ private isTypeOf;
10
+ }
11
+ export declare function or<T extends ArcElement[]>(...elements: T): ArcOr<T>;
12
+ //# sourceMappingURL=or.d.ts.map
@@ -5,7 +5,7 @@ import type { ArcId } from "./id";
5
5
  import type { ArcNumber } from "./number";
6
6
  import type { ArcString } from "./string";
7
7
  type ArcKey = ArcString | ArcNumber | ArcId<any>;
8
- export declare class ArcRecord<Key extends ArcKey, E extends ArcElement> extends ArcAbstract {
8
+ export declare class ArcRecord<Key extends ArcKey, E extends ArcElement> extends ArcAbstract<[]> {
9
9
  private key;
10
10
  private element;
11
11
  constructor(key: Key, element: E);
@@ -1,5 +1,5 @@
1
1
  import { ArcAbstract } from "./abstract";
2
- export declare class ArcStringEnum<const T extends string[]> extends ArcAbstract {
2
+ export declare class ArcStringEnum<const T extends string[]> extends ArcAbstract<[]> {
3
3
  private values;
4
4
  constructor(values: T);
5
5
  parse<Value extends T[number]>(value: Value): Value;
@@ -1,5 +1,24 @@
1
+ import type { CheckFn, GetValidator, Validators } from "./abstract";
1
2
  import { ArcPrimitive } from "./abstract-primitive";
2
- export declare class ArcString extends ArcPrimitive<string> {
3
+ export declare class ArcString<V extends Validators = []> extends ArcPrimitive<V, string> {
4
+ minLength(min: number): ArcString<[...V, {
5
+ name: "minLength";
6
+ validator: (value: any) => {
7
+ currentLength: any;
8
+ minLength: number;
9
+ } | undefined;
10
+ }]>;
11
+ maxLength(max: number): ArcString<[...V, {
12
+ name: "maxLength";
13
+ validator: (value: any) => {
14
+ currentLength: any;
15
+ maxLength: number;
16
+ } | undefined;
17
+ }]>;
18
+ validation<Name extends string, Fn extends CheckFn<any>>(name: Name, validator: Fn): ArcString<GetValidator<import("./abstract").ArcAbstract<[...V, {
19
+ name: Name;
20
+ validator: Fn;
21
+ }]>>>;
3
22
  }
4
- export declare function string(): ArcString;
23
+ export declare function string(): ArcString<[]>;
5
24
  //# sourceMappingURL=string.d.ts.map
@@ -0,0 +1,6 @@
1
+ export declare const instanceof_parse: (value: {
2
+ test: string;
3
+ }) => {
4
+ test: string;
5
+ };
6
+ //# sourceMappingURL=example.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=test.d.ts.map
package/dist/index.js CHANGED
@@ -24,6 +24,11 @@ class ArcOptional {
24
24
  return null;
25
25
  return this.parent.deserialize(value);
26
26
  }
27
+ validate(value) {
28
+ if (!value)
29
+ return {};
30
+ return this.parent.validate(value);
31
+ }
27
32
  }
28
33
 
29
34
  // elements/branded.ts
@@ -46,6 +51,9 @@ class ArcBranded {
46
51
  optional() {
47
52
  return new ArcOptional(this);
48
53
  }
54
+ validate(value) {
55
+ return this.parent.validate(value);
56
+ }
49
57
  }
50
58
 
51
59
  // elements/default.ts
@@ -56,6 +64,9 @@ class ArcDefault {
56
64
  this.parent = parent;
57
65
  this.defaultValueOrCallback = defaultValueOrCallback;
58
66
  }
67
+ validate(value) {
68
+ throw new Error("Method not implemented.");
69
+ }
59
70
  parse(value) {
60
71
  if (value)
61
72
  return this.parent.parse(value);
@@ -79,6 +90,7 @@ class ArcDefault {
79
90
 
80
91
  // elements/abstract.ts
81
92
  class ArcAbstract {
93
+ validations = [];
82
94
  default(defaultValueOrCallback) {
83
95
  return new ArcDefault(this, defaultValueOrCallback);
84
96
  }
@@ -88,6 +100,25 @@ class ArcAbstract {
88
100
  branded(name) {
89
101
  return new ArcBranded(this, name);
90
102
  }
103
+ clone() {
104
+ const Constructor = this.constructor;
105
+ const newInstance = Object.assign(new Constructor, this);
106
+ return newInstance;
107
+ }
108
+ validate(value) {
109
+ return this.validations.reduce((acc, { name, validator }) => {
110
+ acc[name] = validator(value);
111
+ return acc;
112
+ }, {});
113
+ }
114
+ pipeValidation(name, validator) {
115
+ const newInstance = this.clone();
116
+ newInstance.validations = [...this.validations, { name, validator }];
117
+ return newInstance;
118
+ }
119
+ getValidations() {
120
+ return this.validations;
121
+ }
91
122
  }
92
123
 
93
124
  // elements/abstract-primitive.ts
@@ -108,6 +139,28 @@ class ArcPrimitive extends ArcAbstract {
108
139
 
109
140
  // elements/string.ts
110
141
  class ArcString extends ArcPrimitive {
142
+ minLength(min) {
143
+ return this.validation("minLength", (value) => {
144
+ if (value.length < min)
145
+ return {
146
+ currentLength: value.length,
147
+ minLength: min
148
+ };
149
+ });
150
+ }
151
+ maxLength(max) {
152
+ return this.validation("maxLength", (value) => {
153
+ if (value.length > max)
154
+ return {
155
+ currentLength: value.length,
156
+ maxLength: max
157
+ };
158
+ });
159
+ }
160
+ validation(name, validator) {
161
+ const instance = this.pipeValidation(name, validator);
162
+ return instance;
163
+ }
111
164
  }
112
165
  function string() {
113
166
  return new ArcString;
@@ -693,6 +746,9 @@ class StoreState {
693
746
  const { from, to } = await this.applyChange(change);
694
747
  return { from, to };
695
748
  }
749
+ applySerializedChanges(changes) {
750
+ return Promise.all(changes.map((change) => this.applyChange(change)));
751
+ }
696
752
  unsubscribe(listener) {
697
753
  this.listeners.delete(listener);
698
754
  }
@@ -1037,6 +1093,9 @@ class MasterDataStorage extends DataStorage {
1037
1093
  async applyChanges(changes) {
1038
1094
  return Promise.all(changes.map(({ store, changes: changes2 }) => this.getStore(store).applyChanges(changes2)));
1039
1095
  }
1096
+ applySerializedChanges(changes) {
1097
+ return Promise.all(changes.map(({ store, changes: changes2 }) => this.getStore(store).applySerializedChanges(changes2)));
1098
+ }
1040
1099
  async commitChanges(changes) {
1041
1100
  await Promise.all([
1042
1101
  this.applyChanges(changes),
@@ -1112,6 +1171,20 @@ class ArcObject extends ArcAbstract {
1112
1171
  return acc;
1113
1172
  }, {});
1114
1173
  }
1174
+ pick(...keys) {
1175
+ return new ArcObject(Object.fromEntries(keys.map((key) => [key, this.rawShape[key]])));
1176
+ }
1177
+ validate(value) {
1178
+ console.log(this.rawShape);
1179
+ return Object.entries(this.rawShape).reduce((acc, [key, element]) => {
1180
+ if (!element.validate) {
1181
+ return acc;
1182
+ }
1183
+ console.log(element);
1184
+ acc[key] = element.validate(value[key]);
1185
+ return acc;
1186
+ }, {});
1187
+ }
1115
1188
  }
1116
1189
  function object(element) {
1117
1190
  return new ArcObject(element);
@@ -1131,6 +1204,8 @@ class ArcArray extends ArcAbstract {
1131
1204
  return value.map((v) => this.parent.serialize(v));
1132
1205
  }
1133
1206
  deserialize(value) {
1207
+ if (!Array.isArray(value))
1208
+ return [];
1134
1209
  return value.map((v) => this.parent.deserialize(v));
1135
1210
  }
1136
1211
  deserializePath(path, value) {
@@ -1172,6 +1247,22 @@ function date() {
1172
1247
  }
1173
1248
  // elements/number.ts
1174
1249
  class ArcNumber extends ArcPrimitive {
1250
+ min(min) {
1251
+ return this.validation("min", (value) => {
1252
+ if (value < min)
1253
+ return { current: value, min };
1254
+ });
1255
+ }
1256
+ max(max) {
1257
+ return this.validation("max", (value) => {
1258
+ if (value > max)
1259
+ return { current: value, max };
1260
+ });
1261
+ }
1262
+ validation(name, validator) {
1263
+ const instance = this.pipeValidation(name, validator);
1264
+ return instance;
1265
+ }
1175
1266
  }
1176
1267
  function number() {
1177
1268
  return new ArcNumber;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=query.test.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=query-notification-optimization.test%20copy.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=validation.test.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=number.test.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=string.test.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=validation.test.d.ts.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
7
- "version": "0.0.26",
7
+ "version": "0.0.27",
8
8
  "private": false,
9
9
  "author": "Przemysław Krasiński [arcote.tech]",
10
10
  "description": "Arc is a framework designed to align code closely with business logic, streamlining development and enhancing productivity.",