@retailcrm/embed-ui-v1-types 0.4.5

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 ADDED
@@ -0,0 +1,16 @@
1
+ # `@retailcrm/embed-ui-v1-types`
2
+
3
+ Базовые TypeScript типы, используемые в JS-расширениях RetailCRM
4
+
5
+ ## Установка
6
+
7
+ npm:
8
+
9
+ ```bash
10
+ npm i --save @retailcrm/embed-ui-v1-types
11
+ ```
12
+
13
+ yarn:
14
+ ```bash
15
+ yarn add @retailcrm/embed-ui-v1-types
16
+ ```
@@ -0,0 +1,13 @@
1
+ import type { ContextSchema } from './context'
2
+ import type { TranslationList } from './doc'
3
+
4
+ export type ContextSchemaDescription<S extends ContextSchema> = {
5
+ [K in keyof S]: {
6
+ description: TranslationList;
7
+ }
8
+ }
9
+
10
+ export type ContextSchemaUsage = {
11
+ import: string;
12
+ call: string;
13
+ }
package/context.d.ts ADDED
@@ -0,0 +1,117 @@
1
+ import type { If, IsTilda, Maybe } from './scaffolding'
2
+
3
+ export type Field<Type, Readonly extends boolean = false> = {
4
+ accepts (value: Type): boolean;
5
+ defaults (): Type;
6
+ readonly: Readonly;
7
+ }
8
+
9
+ export type ReadonlyField<Type = unknown> = Field<Type, true>
10
+
11
+ export type TypeOf<F> = F extends Field<infer T>
12
+ ? T
13
+ : F extends ReadonlyField<infer T>
14
+ ? T
15
+ : never;
16
+
17
+ export type IsReadonly<F> = F extends Field<unknown, infer R>
18
+ ? R
19
+ : F extends ReadonlyField
20
+ ? true
21
+ : false;
22
+
23
+ export type ContextSchema = {
24
+ [key: string]: Field<unknown, boolean>;
25
+ }
26
+
27
+ export type ContextSchemaMap = {
28
+ [key: string]: ContextSchema;
29
+ }
30
+
31
+ export type Context<S extends ContextSchema> = {
32
+ [F in keyof S]: TypeOf<S[F]>;
33
+ }
34
+
35
+ export type Writable<S extends ContextSchema> = {
36
+ [F in keyof S as IsReadonly<S[F]> extends true ? never : F]: S[F];
37
+ }
38
+
39
+ export type EventMap<S extends ContextSchema> = {
40
+ [K in keyof S as K extends string ? `change:${K}` : never]: K;
41
+ }
42
+
43
+ export type EventPayloadMap<S extends ContextSchema> = {
44
+ [K in keyof S as K extends string ? `change:${K}` : never]: TypeOf<S[K]>;
45
+ }
46
+
47
+ export type EventHandler<
48
+ S extends ContextSchema,
49
+ E extends keyof EventPayloadMap<S>
50
+ > = (payload: EventPayloadMap<S>[E]) => void
51
+
52
+ export type ContextAccessor<M extends ContextSchemaMap = ContextSchemaMap> = {
53
+ get <
54
+ C extends keyof M,
55
+ F extends keyof M[C]
56
+ >(context: C, field: F | '~', onReject?: Maybe<RejectionHandler>): If<
57
+ IsTilda<typeof field>,
58
+ Context<M[C]>,
59
+ Context<M[C]>[F]
60
+ >;
61
+
62
+ set <
63
+ C extends keyof M,
64
+ F extends keyof Writable<M[C]>
65
+ >(context: C, field: F, value: Context<M[C]>[F], onReject?: Maybe<RejectionHandler>): void;
66
+
67
+ on<
68
+ C extends keyof M,
69
+ E extends keyof EventMap<M[C]>
70
+ >(context: C, event: E, handler: EventHandler<M[C], E>): void;
71
+ }
72
+
73
+ export type FieldAccessor<S extends ContextSchema> = {
74
+ get <F extends keyof S>(field: F | '~'): If<
75
+ IsTilda<typeof field>,
76
+ Context<S>,
77
+ Context<S>[F]
78
+ >;
79
+
80
+ set <F extends keyof Writable<S>>(
81
+ field: F,
82
+ value: Context<S>[F]
83
+ ): void;
84
+
85
+ on<E extends keyof EventMap<S>>(
86
+ event: E,
87
+ handler: EventHandler<S, E>
88
+ ): void;
89
+ }
90
+
91
+ export type FieldGetters<S extends ContextSchema> = {
92
+ [F in keyof S]: () => Context<S>[F]
93
+ }
94
+
95
+ export type FieldSetters<S extends ContextSchema> = {
96
+ [F in keyof Writable<S>]: (value: Context<S>[F]) => void
97
+ }
98
+
99
+ export interface Rejection {
100
+ message: string;
101
+ }
102
+
103
+ export interface RejectionHandler {
104
+ (rejection: Rejection): void;
105
+ }
106
+
107
+ export interface DomainRejection extends Rejection {
108
+ type: 'domain';
109
+ }
110
+
111
+ export interface LogicalRejection extends Rejection {
112
+ type: 'logical';
113
+ }
114
+
115
+ export interface RuntimeRejection extends Rejection {
116
+ type: 'runtime';
117
+ }
package/doc.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export type TranslationList = {
2
+ 'en-GB': string;
3
+ 'es-ES': string;
4
+ 'ru-RU': string;
5
+ }
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@retailcrm/embed-ui-v1-types",
3
+ "description": "Basic types declarations for RetailCRM JS API",
4
+ "type": "module",
5
+ "version": "0.4.5",
6
+ "license": "MIT",
7
+ "author": "RetailDriverLLC <integration@retailcrm.ru>",
8
+ "repository": "git@github.com:retailcrm/embed-ui.git",
9
+ "exports": {
10
+ ".": null,
11
+ "./*": "./*"
12
+ }
13
+ }
@@ -0,0 +1,12 @@
1
+ export type IsExact<A, B> = A extends B ? (B extends A ? true : false) : false;
2
+ export type IsTilda<A> = IsExact<A, '~'>;
3
+
4
+ export type If<Condition, Then, Else> = Condition extends true ? Then : Else;
5
+
6
+ export type Maybe<T> = T | null
7
+
8
+ export type UnionToArray<T, U = T> = [T] extends [never]
9
+ ? []
10
+ : T extends T
11
+ ? [T, ...UnionToArray<Exclude<U, T>>]
12
+ : []