@arcote.tech/arc 0.1.5 → 0.1.6

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.
@@ -9,12 +9,12 @@ export declare class ArcCommand<Name extends string, Params extends ArcObjectAny
9
9
  result: Results[number];
10
10
  }, Name> {
11
11
  readonly name: Name;
12
- private _description?;
13
- private _params?;
14
- private _results?;
15
- private _elements?;
16
- private _handler?;
17
- private _isPublic;
12
+ _description?: string;
13
+ _params?: Params;
14
+ _results?: Results;
15
+ _elements?: Elements;
16
+ _handler?: ArcCommmandHandler<Elements, Params, Results> | false;
17
+ _isPublic: boolean;
18
18
  constructor(name: Name);
19
19
  use<const E extends ArcContextElementAny[]>(elements: E): ArcCommand<Name, Params, Results, E>;
20
20
  description(description: string): ArcCommand<Name, Params, Results, Elements>;
@@ -30,7 +30,14 @@ export declare class ArcCommand<Name extends string, Params extends ArcObjectAny
30
30
  commandClient: (ctx: any) => (Params extends ArcObjectAny ? (params: $type<Params>) => Promise<$type<Results[number]>> : () => Promise<$type<Results[number]>>) & {
31
31
  params: Params;
32
32
  };
33
- private clone;
33
+ protected clone(): ArcCommand<Name, Params, Results, Elements>;
34
+ toJsonSchema(): {
35
+ type: string;
36
+ name: Name;
37
+ description: string | undefined;
38
+ parameters: any;
39
+ };
34
40
  }
35
41
  export declare function command<Name extends string>(name: Name): ArcCommand<Name, null, any[], any[]>;
42
+ export type ArcCommandAny = ArcCommand<any, any, any, any>;
36
43
  //# sourceMappingURL=command.d.ts.map
@@ -15,6 +15,8 @@ export type ArcContextElementMethodReturnType<Elements extends ArcContextElement
15
15
  }[number]>;
16
16
  export type ArcCommandContext<E extends ArcContextElementAny[]> = ArcContextElementMethodReturnType<E, "commandContext"> & {
17
17
  $auth: AuthContext;
18
+ } & {
19
+ get: <ArcElement extends ArcContextElementAny>(element: ArcElement) => ArcElement["commandContext"] extends infer CommandContext ? CommandContext extends (...args: any) => any ? ReturnType<CommandContext> : never : never;
18
20
  };
19
21
  export type RemoveFalseAndResolvedPromiseFromArray<T extends any[]> = T extends [infer First, ...infer Rest] ? First extends false ? RemoveFalseAndResolvedPromiseFromArray<Rest> : First extends Promise<{
20
22
  default: infer Inner;
@@ -41,6 +43,7 @@ export type ContextEvents<Context extends ArcContextAny> = {
41
43
  } & ElementEvents<Element>>;
42
44
  }[Context["elements"][number]["name"]];
43
45
  export type Listener<Context extends ArcContextAny> = (event: ContextEvents<Context>, context: CommandContext<Context>) => Promise<void> | void;
44
- export declare function context<const Elements extends ArcContextElementAny[]>(elementsPromise: Elements): Promise<ArcContext<Elements>>;
46
+ export declare function context<const Elements extends ArcContextElementAny[]>(elements: Elements): ArcContext<Elements>;
47
+ export declare function contextMerge<const Contexts extends ArcContextAny[]>(...contexts: Contexts): ArcContext<Contexts[number]["elements"]>;
45
48
  export {};
46
49
  //# sourceMappingURL=context.d.ts.map
@@ -24,6 +24,10 @@ export declare abstract class ArcAbstract<V extends Validators> implements ArcEl
24
24
  name: Name;
25
25
  validator: Val;
26
26
  }]>;
27
+ /**
28
+ * Default JSON Schema representation – concrete subclasses should override where applicable.
29
+ */
30
+ toJsonSchema(): any;
27
31
  getValidations(): V;
28
32
  }
29
33
  export type ArcAbstractAny = ArcAbstract<any>;
@@ -1,9 +1,10 @@
1
1
  import { ArcAbstract, type Validators } from "./abstract";
2
- export declare class ArcAny<V extends Validators = []> extends ArcAbstract<V> {
2
+ export declare class ArcAny<T, V extends Validators = []> extends ArcAbstract<V> {
3
3
  constructor();
4
- parse(value: any): any;
5
- serialize(value: any): any;
6
- deserialize(value: any): any;
4
+ parse(value: T): any;
5
+ serialize(value: T): any;
6
+ deserialize(value: any): T;
7
+ toJsonSchema(): {};
7
8
  }
8
- export declare function any(): ArcAny<[]>;
9
+ export declare function any<T extends any>(): ArcAny<T, any>;
9
10
  //# sourceMappingURL=any.d.ts.map
@@ -54,6 +54,10 @@ export declare class ArcArray<E extends ArcElement, V extends Validators = [
54
54
  name: Name;
55
55
  validator: Fn;
56
56
  }]>>>;
57
+ toJsonSchema(): {
58
+ type: string;
59
+ items: any;
60
+ };
57
61
  }
58
62
  export type ArcArrayAny = ArcArray<any>;
59
63
  export declare function array<E extends ArcElement>(element: E): ArcArray<E, [{
@@ -67,6 +67,11 @@ export declare class ArcBlob<V extends Validators = [typeof blobValidator]> exte
67
67
  * Override parse to validate blob objects
68
68
  */
69
69
  parse(value: any): Blob;
70
+ toJsonSchema(): {
71
+ type: string;
72
+ contentEncoding: string;
73
+ contentMediaType: string;
74
+ };
70
75
  }
71
76
  export declare function blob(): ArcBlob<[{
72
77
  readonly name: "blob";
@@ -11,6 +11,9 @@ export declare class ArcBoolean<V extends Validators> extends ArcPrimitive<V, bo
11
11
  name: Name;
12
12
  validator: Fn;
13
13
  }]>>>;
14
+ toJsonSchema(): {
15
+ type: string;
16
+ };
14
17
  }
15
18
  export declare function boolean(): ArcBoolean<Validators>;
16
19
  //# sourceMappingURL=boolean.d.ts.map
@@ -17,6 +17,7 @@ export declare class ArcBranded<E extends ArcAbstractAny, Brand extends string |
17
17
  };
18
18
  optional(): ArcOptional<this>;
19
19
  validate(value: unknown): unknown;
20
+ toJsonSchema(): any;
20
21
  }
21
22
  export type ArcBrandedAny = ArcBranded<ArcAbstractAny, any>;
22
23
  //# sourceMappingURL=branded.d.ts.map
@@ -30,6 +30,10 @@ export declare class ArcDate<V extends Validators = [typeof dateValidator]> exte
30
30
  name: Name;
31
31
  validator: Fn;
32
32
  }]>>>;
33
+ toJsonSchema(): {
34
+ type: string;
35
+ format: string;
36
+ };
33
37
  }
34
38
  export declare function date(): ArcDate<[{
35
39
  name: "type";
@@ -8,5 +8,6 @@ export declare class ArcDefault<E extends ArcElement> implements ArcElement {
8
8
  parse(value: util.FirstArgument<E["parse"]> | undefined): ReturnType<E["parse"]>;
9
9
  serialize(value: util.FirstArgument<E["serialize"]> | undefined): ReturnType<E["serialize"]>;
10
10
  deserialize(value: util.FirstArgument<E["deserialize"]> | undefined): ReturnType<E["deserialize"]>;
11
+ toJsonSchema(): any;
11
12
  }
12
13
  //# sourceMappingURL=default.d.ts.map
@@ -3,5 +3,9 @@ export interface ArcElement {
3
3
  deserialize(value: unknown): unknown;
4
4
  parse(value: unknown): unknown;
5
5
  validate(value: unknown): unknown;
6
+ /**
7
+ * Returns JSON Schema Draft-07 representation of this element.
8
+ */
9
+ toJsonSchema(): any;
6
10
  }
7
11
  //# sourceMappingURL=element.d.ts.map
@@ -127,6 +127,11 @@ export declare class ArcFile<V extends Validators = [typeof fileValidator]> exte
127
127
  * Override parse to validate file objects
128
128
  */
129
129
  parse(value: any): File;
130
+ toJsonSchema(): {
131
+ type: string;
132
+ contentEncoding: string;
133
+ contentMediaType: string;
134
+ };
130
135
  }
131
136
  export declare function file(): ArcFile<[{
132
137
  readonly name: "file";
@@ -27,6 +27,9 @@ export declare class ArcNumber<V extends Validators = [typeof numberValidator]>
27
27
  name: Name;
28
28
  validator: Fn;
29
29
  }]>>>;
30
+ toJsonSchema(): {
31
+ type: string;
32
+ };
30
33
  }
31
34
  export declare function number(): ArcNumber<[{
32
35
  name: "type";
@@ -63,6 +63,7 @@ export declare class ArcObject<E extends ArcRawShape, V extends Validators = [
63
63
  pick<Keys extends keyof E>(...keys: Keys[]): ArcObject<Pick<E, Keys>, V>;
64
64
  omit<Keys extends keyof E>(...keys: Keys[]): ArcObject<Omit<E, Keys>, V>;
65
65
  entries(): [string, ArcElement][];
66
+ toJsonSchema(): any;
66
67
  }
67
68
  export type ArcObjectAny = ArcObject<any, any>;
68
69
  export type ArcObjectKeys<T extends ArcObjectAny> = T extends ArcObject<infer E, any> ? Exclude<keyof E, symbol> : never;
@@ -80,5 +81,9 @@ export declare function object<E extends ArcRawShape>(element: E): ArcObject<E,
80
81
  name: "schema";
81
82
  validator: (value: any) => false | { [key in keyof E]: ReturnType<E[key]["validate"]>; };
82
83
  }]>;
84
+ export type ArcObjectMerge<A extends ArcObjectAny, B extends ArcObjectAny> = [A, B] extends [
85
+ ArcObject<infer AShape, infer AValidators>,
86
+ ArcObject<infer BShape, infer BValidators>
87
+ ] ? ArcObject<AShape & BShape, [...AValidators, ...BValidators]> : never;
83
88
  export {};
84
89
  //# sourceMappingURL=object.d.ts.map
@@ -8,6 +8,9 @@ export declare class ArcOptional<E extends ArcElement> implements ArcElement {
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
10
  validate(value: unknown): unknown;
11
+ toJsonSchema(): {
12
+ anyOf: any[];
13
+ };
11
14
  }
12
15
  export type ArcOptionalAny = ArcOptional<ArcAbstractAny>;
13
16
  //# sourceMappingURL=optional.d.ts.map
@@ -1,2 +1,37 @@
1
- export {};
1
+ import { type util } from "../utils";
2
+ import type { ArcElement } from "./element";
3
+ /**
4
+ * ArcOr allows you to create a union schema from existing Arc elements.
5
+ * The generic parameter keeps the type-information of every provided
6
+ * element which results in a **strongly-typed** union of all underlying
7
+ * element types.
8
+ */
9
+ export declare class ArcOr<T extends ArcElement[]> implements ArcElement {
10
+ private readonly elements;
11
+ constructor(elements: T);
12
+ /**
13
+ * Serialises a union value using the first matching element.
14
+ */
15
+ serialize(value: util.FirstArgument<T[number]["serialize"]>): ReturnType<T[number]["serialize"]>;
16
+ /**
17
+ * Deserialises a raw value using the first element that validates it.
18
+ */
19
+ deserialize(value: util.FirstArgument<T[number]["deserialize"]>): ReturnType<T[number]["deserialize"]>;
20
+ /**
21
+ * Parses an input value using the first element that validates it.
22
+ */
23
+ parse(value: util.FirstArgument<T[number]["parse"]>): ReturnType<T[number]["parse"]>;
24
+ /**
25
+ * Runs validation on a value against **all** underlying elements.
26
+ * Succeeds (returns `false`) when at least one element validates the value.
27
+ * Otherwise returns a map of errors keyed by the element index.
28
+ */
29
+ validate(value: unknown): unknown;
30
+ toJsonSchema(): {
31
+ anyOf: any[];
32
+ };
33
+ private pickElement;
34
+ private isTypeOf;
35
+ }
36
+ export declare function or<T extends ArcElement[]>(...elements: T): ArcOr<T>;
2
37
  //# sourceMappingURL=or.d.ts.map
@@ -19,6 +19,11 @@ export declare class ArcRecord<Key extends ArcKey, E extends ArcElement, V exten
19
19
  parse(value: Record<util.FirstArgument<Key["parse"]>, util.FirstArgument<E["parse"]>>): Record<ReturnType<Key["parse"]>, ReturnType<E["parse"]>>;
20
20
  serialize(value: Record<ReturnType<Key["serialize"]>, ReturnType<E["serialize"]>>): Record<ReturnType<Key["serialize"]>, ReturnType<E["serialize"]>>;
21
21
  deserialize(value: Record<ReturnType<Key["deserialize"]>, ReturnType<E["deserialize"]>>): Record<ReturnType<Key["deserialize"]>, ReturnType<E["deserialize"]>>;
22
+ toJsonSchema(): {
23
+ type: string;
24
+ propertyNames: any;
25
+ additionalProperties: any;
26
+ };
22
27
  }
23
28
  export type ArcRecordAny = ArcRecord<any, any>;
24
29
  export declare function record<Key extends ArcKey, E extends ArcElement>(key: Key, element: E): ArcRecord<Key, E, [{
@@ -13,6 +13,10 @@ export declare class ArcStringEnum<const T extends string[], V extends Validator
13
13
  parse<Value extends T[number]>(value: Value): Value;
14
14
  serialize<Value extends T[number]>(value: Value): Value;
15
15
  deserialize<Value extends T[number]>(value: Value): Value;
16
+ toJsonSchema(): {
17
+ readonly type: "string";
18
+ readonly enum: T;
19
+ };
16
20
  getEnumerators(): T;
17
21
  }
18
22
  export type ArcStringEnumAny = ArcStringEnum<any>;
@@ -64,6 +64,9 @@ export declare class ArcString<V extends Validators = [typeof stringValidator]>
64
64
  currentEmail: string;
65
65
  } | undefined;
66
66
  }]>;
67
+ toJsonSchema(): {
68
+ type: string;
69
+ };
67
70
  url(): ArcString<[...V, {
68
71
  name: "url";
69
72
  validator: (value: string) => {
package/dist/index.js CHANGED
@@ -37,6 +37,12 @@ class ArcOptional {
37
37
  return false;
38
38
  return this.parent.validate(value);
39
39
  }
40
+ toJsonSchema() {
41
+ const parentSchema = this.parent.toJsonSchema?.() ?? {};
42
+ return {
43
+ anyOf: [parentSchema, { type: "null" }]
44
+ };
45
+ }
40
46
  }
41
47
 
42
48
  // elements/branded.ts
@@ -62,6 +68,9 @@ class ArcBranded {
62
68
  validate(value) {
63
69
  return this.parent.validate(value);
64
70
  }
71
+ toJsonSchema() {
72
+ return this.parent.toJsonSchema?.() ?? {};
73
+ }
65
74
  }
66
75
 
67
76
  // elements/default.ts
@@ -94,6 +103,14 @@ class ArcDefault {
94
103
  } else
95
104
  return this.defaultValueOrCallback;
96
105
  }
106
+ toJsonSchema() {
107
+ const schema = this.parent.toJsonSchema?.() ?? {};
108
+ const defaultValue = typeof this.defaultValueOrCallback === "function" ? this.defaultValueOrCallback() : this.defaultValueOrCallback;
109
+ return {
110
+ ...schema,
111
+ default: defaultValue
112
+ };
113
+ }
97
114
  }
98
115
 
99
116
  // elements/abstract.ts
@@ -135,6 +152,9 @@ class ArcAbstract {
135
152
  newInstance.validations = [...this.validations, { name, validator }];
136
153
  return newInstance;
137
154
  }
155
+ toJsonSchema() {
156
+ return {};
157
+ }
138
158
  getValidations() {
139
159
  return this.validations;
140
160
  }
@@ -255,6 +275,9 @@ class ArcString extends ArcPrimitive {
255
275
  }
256
276
  });
257
277
  }
278
+ toJsonSchema() {
279
+ return { type: "string" };
280
+ }
258
281
  url() {
259
282
  const regex = /^(https?):\/\/(?![-0-9])(?!www\.[0-9])(?:[a-zA-Z0-9-]+(?::[a-zA-Z0-9-]+)?@)?(?:localhost|\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b|[a-zA-Z0-9-]+\.)?[a-zA-Z0-9-]+\.(?:[a-zA-Z]{2,}|[a-zA-Z]{2}\.[a-zA-Z]{2})(?::\d{1,5})?(?!\/\/)(?:\/[a-zA-Z0-9-._~:?#\[\]@!$&'()*+,;=]*)*(?!\/{2})(?:;[a-zA-Z0-9-._~:?#\[\]@!$&'()*+,;=]*)*(\?(?!=)[a-zA-Z0-9&=;]*)?(\#[a-zA-Z0-9-]*)?$|^(https?):\/\/(localhost|\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)(?::\d{1,5})?(?!\/\/)(?:\/[a-zA-Z0-9-._~:?#\[\]@!$&'()*+,;=]*)*(?!\/{2})(?:;[a-zA-Z0-9-._~:?#\[\]@!$&'()*+,;=]*)*(\?(?!=)[a-zA-Z0-9&=;]*)?(\#[a-zA-Z0-9-]*)?$/;
260
283
  return this.validation("url", (value) => {
@@ -329,6 +352,9 @@ class ArcAny extends ArcAbstract {
329
352
  deserialize(value) {
330
353
  return value;
331
354
  }
355
+ toJsonSchema() {
356
+ return {};
357
+ }
332
358
  }
333
359
  function any() {
334
360
  return new ArcAny;
@@ -440,6 +466,23 @@ class ArcObject extends ArcAbstract {
440
466
  entries() {
441
467
  return Object.entries(this.rawShape);
442
468
  }
469
+ toJsonSchema() {
470
+ const properties = {};
471
+ const required = [];
472
+ for (const [key, element] of Object.entries(this.rawShape)) {
473
+ properties[key] = element.toJsonSchema?.() ?? {};
474
+ if (!(element instanceof ArcOptional)) {
475
+ required.push(key);
476
+ }
477
+ }
478
+ const schema = {
479
+ type: "object",
480
+ properties
481
+ };
482
+ if (required.length)
483
+ schema.required = required;
484
+ return schema;
485
+ }
443
486
  }
444
487
  function object(element) {
445
488
  return new ArcObject(element);
@@ -529,6 +572,12 @@ class ArcArray extends ArcAbstract {
529
572
  const instance = this.pipeValidation(name, validator);
530
573
  return instance;
531
574
  }
575
+ toJsonSchema() {
576
+ return {
577
+ type: "array",
578
+ items: this.parent.toJsonSchema?.() ?? {}
579
+ };
580
+ }
532
581
  }
533
582
  function array(element) {
534
583
  return new ArcArray(element);
@@ -620,6 +669,13 @@ class ArcBlob extends ArcPrimitive {
620
669
  }
621
670
  throw new Error("Expected Blob object");
622
671
  }
672
+ toJsonSchema() {
673
+ return {
674
+ type: "string",
675
+ contentEncoding: "base64",
676
+ contentMediaType: "application/octet-stream"
677
+ };
678
+ }
623
679
  }
624
680
  function blob() {
625
681
  return new ArcBlob;
@@ -638,6 +694,9 @@ class ArcBoolean extends ArcPrimitive {
638
694
  const instance = this.pipeValidation(name, validator);
639
695
  return instance;
640
696
  }
697
+ toJsonSchema() {
698
+ return { type: "boolean" };
699
+ }
641
700
  }
642
701
  function boolean() {
643
702
  return new ArcBoolean;
@@ -677,6 +736,12 @@ class ArcDate extends ArcAbstract {
677
736
  const instance = this.pipeValidation(name, validator);
678
737
  return instance;
679
738
  }
739
+ toJsonSchema() {
740
+ return {
741
+ type: "string",
742
+ format: "date-time"
743
+ };
744
+ }
680
745
  }
681
746
  function date() {
682
747
  return new ArcDate;
@@ -844,6 +909,13 @@ class ArcFile extends ArcPrimitive {
844
909
  }
845
910
  throw new Error("Expected File object");
846
911
  }
912
+ toJsonSchema() {
913
+ return {
914
+ type: "string",
915
+ contentEncoding: "base64",
916
+ contentMediaType: "application/octet-stream"
917
+ };
918
+ }
847
919
  }
848
920
  function file() {
849
921
  return new ArcFile;
@@ -871,10 +943,65 @@ class ArcNumber extends ArcPrimitive {
871
943
  const instance = this.pipeValidation(name, validator);
872
944
  return instance;
873
945
  }
946
+ toJsonSchema() {
947
+ return { type: "number" };
948
+ }
874
949
  }
875
950
  function number() {
876
951
  return new ArcNumber;
877
952
  }
953
+ // elements/or.ts
954
+ class ArcOr {
955
+ elements;
956
+ constructor(elements) {
957
+ this.elements = elements;
958
+ }
959
+ serialize(value) {
960
+ const element = this.pickElement(value);
961
+ return element.serialize(value);
962
+ }
963
+ deserialize(value) {
964
+ const element = this.pickElement(value);
965
+ return element.deserialize(value);
966
+ }
967
+ parse(value) {
968
+ const element = this.pickElement(value);
969
+ return element.parse(value);
970
+ }
971
+ validate(value) {
972
+ const errors = this.elements.reduce((acc, element, idx) => {
973
+ const err = element.validate(value);
974
+ if (err)
975
+ acc[idx] = err;
976
+ return acc;
977
+ }, {});
978
+ if (Object.keys(errors).length !== this.elements.length)
979
+ return false;
980
+ return errors;
981
+ }
982
+ toJsonSchema() {
983
+ return {
984
+ anyOf: this.elements.map((el) => el.toJsonSchema?.() ?? {})
985
+ };
986
+ }
987
+ pickElement(value) {
988
+ for (const element of this.elements) {
989
+ if (this.isTypeOf(element, value))
990
+ return element;
991
+ }
992
+ throw new Error("Unable to match value with any of the provided elements for ArcOr");
993
+ }
994
+ isTypeOf(element, value) {
995
+ try {
996
+ return element.validate(value) === false;
997
+ } catch {
998
+ return false;
999
+ }
1000
+ }
1001
+ }
1002
+ function or(...elements) {
1003
+ return new ArcOr(elements);
1004
+ }
878
1005
  // elements/record.ts
879
1006
  class ArcRecord extends ArcAbstract {
880
1007
  key;
@@ -925,6 +1052,13 @@ class ArcRecord extends ArcAbstract {
925
1052
  return acc;
926
1053
  }, {});
927
1054
  }
1055
+ toJsonSchema() {
1056
+ return {
1057
+ type: "object",
1058
+ propertyNames: this.key.toJsonSchema?.() ?? {},
1059
+ additionalProperties: this.element.toJsonSchema?.() ?? {}
1060
+ };
1061
+ }
928
1062
  }
929
1063
  function record(key, element) {
930
1064
  return new ArcRecord(key, element);
@@ -954,6 +1088,12 @@ class ArcStringEnum extends ArcAbstract {
954
1088
  deserialize(value) {
955
1089
  return value;
956
1090
  }
1091
+ toJsonSchema() {
1092
+ return {
1093
+ type: "string",
1094
+ enum: this.values
1095
+ };
1096
+ }
957
1097
  getEnumerators() {
958
1098
  return this.values;
959
1099
  }
@@ -1364,6 +1504,18 @@ class ArcCommand extends ArcContextElement {
1364
1504
  clone._isPublic = this._isPublic;
1365
1505
  return clone;
1366
1506
  }
1507
+ toJsonSchema() {
1508
+ const parametersSchema = this._params ? this._params.toJsonSchema?.() ?? {
1509
+ type: "object",
1510
+ properties: {}
1511
+ } : { type: "object", properties: {} };
1512
+ return {
1513
+ type: "function",
1514
+ name: this.name,
1515
+ description: this._description ?? undefined,
1516
+ parameters: parametersSchema
1517
+ };
1518
+ }
1367
1519
  }
1368
1520
  function command(name) {
1369
1521
  return new ArcCommand(name);
@@ -1426,6 +1578,18 @@ class ArcContext {
1426
1578
  if (name === "$auth") {
1427
1579
  return authContext;
1428
1580
  }
1581
+ if (name === "get") {
1582
+ return (e) => {
1583
+ const element3 = this.elements.find((element4) => element4.name === e.name);
1584
+ if (!element3) {
1585
+ throw new Error(`Element "${String(name)}" not found`);
1586
+ }
1587
+ if (!element3.commandContext) {
1588
+ throw new Error(`Element "${String(name)}" does not have a command context`);
1589
+ }
1590
+ return element3.commandContext(dataStorage, publishEvent, authContext);
1591
+ };
1592
+ }
1429
1593
  const element2 = this.elements.find((element3) => element3.name === name);
1430
1594
  if (!element2) {
1431
1595
  throw new Error(`Element "${String(name)}" not found`);
@@ -1438,15 +1602,14 @@ class ArcContext {
1438
1602
  });
1439
1603
  }
1440
1604
  }
1441
- async function context(elementsPromise) {
1442
- const elements = await Promise.all(elementsPromise.map(async (element2) => {
1443
- if (element2 instanceof Promise) {
1444
- return (await element2).default;
1445
- }
1446
- return element2;
1447
- })).then((elements2) => elements2.filter(Boolean));
1605
+ function context(elements) {
1448
1606
  return new ArcContext(elements);
1449
1607
  }
1608
+ function contextMerge(...contexts) {
1609
+ return new ArcContext([
1610
+ ...contexts.map((context2) => context2.elements).flat()
1611
+ ]);
1612
+ }
1450
1613
  // context/event.ts
1451
1614
  var eventValidator = typeValidatorBuilder("object");
1452
1615
  var eventStoreSchema = {
@@ -2371,6 +2534,18 @@ class ArcListener extends ArcContextElement {
2371
2534
  if (name === "$auth") {
2372
2535
  return authContext;
2373
2536
  }
2537
+ if (name === "get") {
2538
+ return (e) => {
2539
+ const element4 = this._elements.find((element5) => element5.name === e.name);
2540
+ if (!element4) {
2541
+ throw new Error(`Element "${String(name)}" not found in listener "${this.name}"`);
2542
+ }
2543
+ if (!element4.commandContext) {
2544
+ throw new Error(`Element "${String(name)}" does not have a command context`);
2545
+ }
2546
+ return element4.commandContext(dataStorage, publishEvent, authContext);
2547
+ };
2548
+ }
2374
2549
  const element3 = this._elements.find((element4) => element4.name === name);
2375
2550
  if (!element3) {
2376
2551
  throw new Error(`Element "${String(name)}" not found in listener "${this.name}"`);
@@ -2712,7 +2887,10 @@ class RemoteModelClient extends ModelBase {
2712
2887
  const result = this.query(queryBuilderFn, authContext);
2713
2888
  result.then((initialResult) => {
2714
2889
  callback(initialResult);
2715
- }).catch(this.catchErrorCallback);
2890
+ }).catch((error) => {
2891
+ console.error("Error in subscribe", error);
2892
+ callback(null);
2893
+ });
2716
2894
  return {
2717
2895
  unsubscribe: () => {},
2718
2896
  result
@@ -3160,6 +3338,14 @@ class ArcView extends ArcContextElementWithStore {
3160
3338
  clone._handler = handler;
3161
3339
  return clone;
3162
3340
  }
3341
+ handleEvent(event3, handler) {
3342
+ const clone = this.clone();
3343
+ clone._handler = {
3344
+ ...clone._handler ?? {},
3345
+ [event3.name]: handler
3346
+ };
3347
+ return clone;
3348
+ }
3163
3349
  applyRestrictions(authContext, options) {
3164
3350
  if (!this.restrictions) {
3165
3351
  return options;
@@ -3282,6 +3468,7 @@ export {
3282
3468
  route,
3283
3469
  record,
3284
3470
  reactive,
3471
+ or,
3285
3472
  object,
3286
3473
  number,
3287
3474
  listener,
@@ -3292,12 +3479,14 @@ export {
3292
3479
  date,
3293
3480
  customId,
3294
3481
  createSQLiteAdapterFactory,
3482
+ contextMerge,
3295
3483
  context,
3296
3484
  command,
3297
3485
  collection,
3298
3486
  boolean,
3299
3487
  blob,
3300
3488
  array,
3489
+ arcObjectToStoreSchema,
3301
3490
  any,
3302
3491
  StoreState,
3303
3492
  SQLiteAdapter,
@@ -3318,6 +3507,7 @@ export {
3318
3507
  ArcRecord,
3319
3508
  ArcQueryBuilder,
3320
3509
  ArcQuery,
3510
+ ArcOr,
3321
3511
  ArcOptional,
3322
3512
  ArcObject,
3323
3513
  ArcNumber,
@@ -0,0 +1,2 @@
1
+ export * from "./arcObjectToStoreSchema";
2
+ //# sourceMappingURL=index.d.ts.map
package/dist/utils.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { ArcCollectionAny, CollectionItem } from "./collection/collection";
2
2
  import type { QueryCollectionResult } from "./collection/queries/find";
3
3
  import type { ArcElement } from "./elements/element";
4
+ export * from "./utils/index";
4
5
  export declare namespace objectUtil {
5
6
  export type MergeShapes<U, V> = {
6
7
  [k in Exclude<keyof U, keyof V>]: U[k];
@@ -1,9 +1,10 @@
1
+ import type { ArcEventAny, ArcEventInstance } from "../context";
1
2
  import { ArcContextElementWithStore, type ArcContextElementAny, type AuthContext, type ListenerConfig, type PublishEventFunction, type StoreSchema } from "../context/element";
2
3
  import type { QueryBuilderContext } from "../context/query-builder-context";
3
4
  import type { DataStorage } from "../data-storage";
4
5
  import type { FindOptions, WhereCondition } from "../data-storage/types";
5
6
  import { type ArcIdAny, type ArcObjectAny } from "../elements";
6
- import type { $type } from "../utils";
7
+ import type { $type, objectUtil } from "../utils";
7
8
  import { ArcViewFindQueryBuilder } from "./query-builders/find";
8
9
  import { ArcViewFindOneQueryBuilder } from "./query-builders/find-one";
9
10
  type GetEvents<Elements extends ArcContextElementAny[]> = Elements[number]["$event"];
@@ -19,8 +20,9 @@ type ArcViewItem<Id extends ArcIdAny, Schema extends ArcObjectAny> = {
19
20
  _id: $type<Id>;
20
21
  } & $type<Schema>;
21
22
  type ArcViewHandler<Elements extends ArcContextElementAny[], Id extends ArcIdAny, Schema extends ArcObjectAny> = {
22
- [Event in GetEvents<Elements> as Event["type"]]: (ctx: ArcViewHandlerContext<Id, Schema>, event: Event) => Promise<void>;
23
+ [Event in GetEvents<Elements> as Event["type"]]: ArcViewEventHandler<Id, Schema, Event>;
23
24
  };
25
+ type ArcViewEventHandler<Id extends ArcIdAny, Schema extends ArcObjectAny, Event extends any> = (ctx: ArcViewHandlerContext<Id, Schema>, event: Event) => Promise<void>;
24
26
  export declare class ArcView<Name extends string, Id extends ArcIdAny, Schema extends ArcObjectAny, const Elements extends ArcContextElementAny[]> extends ArcContextElementWithStore<null, Name> {
25
27
  readonly name: Name;
26
28
  readonly id: Id;
@@ -41,6 +43,7 @@ export declare class ArcView<Name extends string, Id extends ArcIdAny, Schema ex
41
43
  description(description: string): ArcView<Name, Id, Schema, Elements>;
42
44
  async(): ArcView<Name, Id, Schema, Elements>;
43
45
  handle<Handler extends ArcViewHandler<Elements, Id, Schema>>(handler: Handler): ArcView<Name, Id, Schema, Elements>;
46
+ handleEvent<Event extends ArcEventAny>(event: Event, handler: ArcViewEventHandler<Id, Schema, ArcEventInstance<Event>>): ArcView<Name, Id, Schema, [...Elements, Event]>;
44
47
  /**
45
48
  * Helper function to merge where conditions from restrictions with query options
46
49
  */
@@ -57,5 +60,6 @@ export declare class ArcView<Name extends string, Id extends ArcIdAny, Schema ex
57
60
  private clone;
58
61
  }
59
62
  export declare function view<Name extends string, Id extends ArcIdAny, Schema extends ArcObjectAny>(name: Name, id: Id, schema: Schema): ArcView<Name, Id, Schema, any[]>;
63
+ export type ArcViewRecord<View extends ArcView<any, any, any, any>> = objectUtil.simplify<ArcViewItem<View["id"], View["schema"]>>;
60
64
  export {};
61
65
  //# sourceMappingURL=view.d.ts.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@arcote.tech/arc",
3
3
  "type": "module",
4
- "version": "0.1.5",
4
+ "version": "0.1.6",
5
5
  "private": false,
6
6
  "author": "Przemysław Krasiński [arcote.tech]",
7
7
  "description": "Arc is a framework designed to align code closely with business logic, streamlining development and enhancing productivity.",