@retailcrm/embed-ui-v1-contexts 0.4.4
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 +16 -0
- package/dist/common/customer/card-phone.d.ts +6 -0
- package/dist/common/customer/card.d.ts +6 -0
- package/dist/common/order/card.d.ts +6 -0
- package/dist/common/settings.d.ts +7 -0
- package/dist/common/user/current.d.ts +6 -0
- package/dist/host.cjs +104 -0
- package/dist/host.d.ts +16 -0
- package/dist/host.js +104 -0
- package/dist/meta.json +442 -0
- package/dist/predicates.cjs.js +28 -0
- package/dist/predicates.d.ts +11 -0
- package/dist/predicates.es.js +29 -0
- package/dist/remote/customer/card-phone.cjs +20 -0
- package/dist/remote/customer/card-phone.d.ts +12 -0
- package/dist/remote/customer/card-phone.js +20 -0
- package/dist/remote/customer/card.cjs +30 -0
- package/dist/remote/customer/card.d.ts +12 -0
- package/dist/remote/customer/card.js +30 -0
- package/dist/remote/order/card.cjs +138 -0
- package/dist/remote/order/card.d.ts +12 -0
- package/dist/remote/order/card.js +138 -0
- package/dist/remote/settings.cjs +17 -0
- package/dist/remote/settings.d.ts +13 -0
- package/dist/remote/settings.js +17 -0
- package/dist/remote/user/current.cjs +50 -0
- package/dist/remote/user/current.d.ts +12 -0
- package/dist/remote/user/current.js +50 -0
- package/dist/remote.cjs +62 -0
- package/dist/remote.d.ts +22 -0
- package/dist/remote.js +62 -0
- package/dist/utilities.cjs.js +3 -0
- package/dist/utilities.d.ts +1 -0
- package/dist/utilities.es.js +4 -0
- package/package.json +106 -0
- package/types/customer/card-phone.d.ts +6 -0
- package/types/customer/card.d.ts +8 -0
- package/types/index.d.ts +13 -0
- package/types/order/card.d.ts +29 -0
- package/types/settings.d.ts +6 -0
- package/types/user/current.d.ts +12 -0
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# `@retailcrm/embed-ui-v1-contexts`
|
|
2
|
+
|
|
3
|
+
Логика реактивного контекста, используемого в JS-расширениях RetailCRM
|
|
4
|
+
|
|
5
|
+
## Установка
|
|
6
|
+
|
|
7
|
+
npm:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i --save @retailcrm/embed-ui-v1-contexts
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
yarn:
|
|
14
|
+
```bash
|
|
15
|
+
yarn add @retailcrm/embed-ui-v1-contexts
|
|
16
|
+
```
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ContextSchemaDescription, ContextSchemaUsage } from '@retailcrm/embed-ui-v1-types/context-doc';
|
|
2
|
+
import { Schema } from '../../../types/customer/card-phone';
|
|
3
|
+
export declare const id = "customer/card:phone";
|
|
4
|
+
export declare const schema: Schema;
|
|
5
|
+
export declare const description: ContextSchemaDescription<Schema>;
|
|
6
|
+
export declare const usage: ContextSchemaUsage;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ContextSchemaDescription, ContextSchemaUsage } from '@retailcrm/embed-ui-v1-types/context-doc';
|
|
2
|
+
import { Schema } from '../../../types/customer/card';
|
|
3
|
+
export declare const id = "customer/card";
|
|
4
|
+
export declare const schema: Schema;
|
|
5
|
+
export declare const description: ContextSchemaDescription<Schema>;
|
|
6
|
+
export declare const usage: ContextSchemaUsage;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ContextSchemaDescription, ContextSchemaUsage } from '@retailcrm/embed-ui-v1-types/context-doc';
|
|
2
|
+
import { Schema } from '../../../types/order/card';
|
|
3
|
+
export declare const id = "order/card";
|
|
4
|
+
export declare const schema: Schema;
|
|
5
|
+
export declare const description: ContextSchemaDescription<Schema>;
|
|
6
|
+
export declare const usage: ContextSchemaUsage;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ContextSchemaDescription, ContextSchemaUsage } from '@retailcrm/embed-ui-v1-types/context-doc';
|
|
2
|
+
import { Locale, Schema } from '../../types/settings';
|
|
3
|
+
export declare const id = "settings";
|
|
4
|
+
export declare const locales: Locale[];
|
|
5
|
+
export declare const schema: Schema;
|
|
6
|
+
export declare const description: ContextSchemaDescription<Schema>;
|
|
7
|
+
export declare const usage: ContextSchemaUsage;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ContextSchemaDescription, ContextSchemaUsage } from '@retailcrm/embed-ui-v1-types/context-doc';
|
|
2
|
+
import { Schema } from '../../../types/user/current';
|
|
3
|
+
export declare const id = "user/current";
|
|
4
|
+
export declare const schema: Schema;
|
|
5
|
+
export declare const description: ContextSchemaDescription<Schema>;
|
|
6
|
+
export declare const usage: ContextSchemaUsage;
|
package/dist/host.cjs
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const utilities = require("./utilities.cjs.js");
|
|
4
|
+
const rpc = require("@remote-ui/rpc");
|
|
5
|
+
class HostError extends Error {
|
|
6
|
+
constructor(message, previous = void 0) {
|
|
7
|
+
super(`[crm:embed:host] ${message}`);
|
|
8
|
+
if (previous == null ? void 0 : previous.stack) {
|
|
9
|
+
this.stack = `${previous.stack}
|
|
10
|
+
${this.stack}`;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
get rejection() {
|
|
14
|
+
return {
|
|
15
|
+
message: this.message
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
class LogicalError extends HostError {
|
|
20
|
+
get rejection() {
|
|
21
|
+
return {
|
|
22
|
+
type: "logical",
|
|
23
|
+
message: this.message
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
class RuntimeError extends HostError {
|
|
28
|
+
get rejection() {
|
|
29
|
+
return {
|
|
30
|
+
type: "runtime",
|
|
31
|
+
message: this.message
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const createGetter = (id, getters) => {
|
|
36
|
+
return (field) => {
|
|
37
|
+
if (field === "~") {
|
|
38
|
+
return utilities.keysOf(getters).reduce((context, field2) => ({
|
|
39
|
+
...context,
|
|
40
|
+
[field2]: getters[field2]()
|
|
41
|
+
}), {});
|
|
42
|
+
}
|
|
43
|
+
if (field in getters) {
|
|
44
|
+
return getters[field]();
|
|
45
|
+
}
|
|
46
|
+
throw new LogicalError(`Field ${String(field)} is not supported in context ${id}`);
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
const createSetter = (id, setters) => {
|
|
50
|
+
return (field, value) => {
|
|
51
|
+
if (!(field in setters)) {
|
|
52
|
+
throw new LogicalError(`Setting field ${String(field)} is not supported in context ${id}`);
|
|
53
|
+
}
|
|
54
|
+
setters[field](value);
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
const createContextAccessor = (accessors, onError = null) => {
|
|
58
|
+
const guard = (context) => {
|
|
59
|
+
if (!(context in accessors)) {
|
|
60
|
+
throw new LogicalError(`Context ${String(context)} is not available`);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
get(context, field, onReject = null) {
|
|
65
|
+
return run(() => {
|
|
66
|
+
guard(context);
|
|
67
|
+
return accessors[context].get(field);
|
|
68
|
+
}, onReject, onError);
|
|
69
|
+
},
|
|
70
|
+
set(context, field, value, onReject = null) {
|
|
71
|
+
run(() => {
|
|
72
|
+
guard(context);
|
|
73
|
+
accessors[context].set(field, value);
|
|
74
|
+
}, onReject, onError);
|
|
75
|
+
},
|
|
76
|
+
on(context, event, handler) {
|
|
77
|
+
guard(context);
|
|
78
|
+
accessors[context].on(event, handler);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
function run(fn, onReject = null, onError = null) {
|
|
83
|
+
rpc.retain(onReject);
|
|
84
|
+
try {
|
|
85
|
+
return fn();
|
|
86
|
+
} catch (e) {
|
|
87
|
+
if (onReject) {
|
|
88
|
+
onReject(e instanceof HostError ? e.rejection : { message: String(e) });
|
|
89
|
+
}
|
|
90
|
+
if (onError) {
|
|
91
|
+
onError(e);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
throw e;
|
|
95
|
+
} finally {
|
|
96
|
+
rpc.release(onReject);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.HostError = HostError;
|
|
100
|
+
exports.LogicalError = LogicalError;
|
|
101
|
+
exports.RuntimeError = RuntimeError;
|
|
102
|
+
exports.createContextAccessor = createContextAccessor;
|
|
103
|
+
exports.createGetter = createGetter;
|
|
104
|
+
exports.createSetter = createSetter;
|
package/dist/host.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Context, ContextAccessor, ContextSchema, ContextSchemaMap, FieldAccessor, FieldGetters, FieldSetters, LogicalRejection, Rejection, RuntimeRejection, Writable } from '@retailcrm/embed-ui-v1-types/context';
|
|
2
|
+
import { Maybe } from '@retailcrm/embed-ui-v1-types/scaffolding';
|
|
3
|
+
export declare class HostError extends Error {
|
|
4
|
+
constructor(message: string, previous?: Error | undefined);
|
|
5
|
+
get rejection(): Rejection;
|
|
6
|
+
}
|
|
7
|
+
export declare class LogicalError extends HostError {
|
|
8
|
+
get rejection(): LogicalRejection;
|
|
9
|
+
}
|
|
10
|
+
export declare class RuntimeError extends HostError {
|
|
11
|
+
get rejection(): RuntimeRejection;
|
|
12
|
+
}
|
|
13
|
+
export declare const createGetter: <S extends ContextSchema>(id: string, getters: FieldGetters<S>) => <F extends keyof S>(field: "~" | F) => Context<S> | Context<S>[F];
|
|
14
|
+
export declare const createSetter: <S extends ContextSchema>(id: string, setters: FieldSetters<S>) => <F extends keyof Writable<S>>(field: F, value: Context<S>[F]) => void;
|
|
15
|
+
export declare const createContextAccessor: <M extends ContextSchemaMap>(accessors: { [K in keyof M]: FieldAccessor<M[K]>; }, onError?: Maybe<ErrorHandler>) => ContextAccessor<M>;
|
|
16
|
+
export type ErrorHandler = (e: unknown) => void;
|
package/dist/host.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { k as keysOf } from "./utilities.es.js";
|
|
2
|
+
import { retain, release } from "@remote-ui/rpc";
|
|
3
|
+
class HostError extends Error {
|
|
4
|
+
constructor(message, previous = void 0) {
|
|
5
|
+
super(`[crm:embed:host] ${message}`);
|
|
6
|
+
if (previous == null ? void 0 : previous.stack) {
|
|
7
|
+
this.stack = `${previous.stack}
|
|
8
|
+
${this.stack}`;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
get rejection() {
|
|
12
|
+
return {
|
|
13
|
+
message: this.message
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
class LogicalError extends HostError {
|
|
18
|
+
get rejection() {
|
|
19
|
+
return {
|
|
20
|
+
type: "logical",
|
|
21
|
+
message: this.message
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
class RuntimeError extends HostError {
|
|
26
|
+
get rejection() {
|
|
27
|
+
return {
|
|
28
|
+
type: "runtime",
|
|
29
|
+
message: this.message
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const createGetter = (id, getters) => {
|
|
34
|
+
return (field) => {
|
|
35
|
+
if (field === "~") {
|
|
36
|
+
return keysOf(getters).reduce((context, field2) => ({
|
|
37
|
+
...context,
|
|
38
|
+
[field2]: getters[field2]()
|
|
39
|
+
}), {});
|
|
40
|
+
}
|
|
41
|
+
if (field in getters) {
|
|
42
|
+
return getters[field]();
|
|
43
|
+
}
|
|
44
|
+
throw new LogicalError(`Field ${String(field)} is not supported in context ${id}`);
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
const createSetter = (id, setters) => {
|
|
48
|
+
return (field, value) => {
|
|
49
|
+
if (!(field in setters)) {
|
|
50
|
+
throw new LogicalError(`Setting field ${String(field)} is not supported in context ${id}`);
|
|
51
|
+
}
|
|
52
|
+
setters[field](value);
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
const createContextAccessor = (accessors, onError = null) => {
|
|
56
|
+
const guard = (context) => {
|
|
57
|
+
if (!(context in accessors)) {
|
|
58
|
+
throw new LogicalError(`Context ${String(context)} is not available`);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
return {
|
|
62
|
+
get(context, field, onReject = null) {
|
|
63
|
+
return run(() => {
|
|
64
|
+
guard(context);
|
|
65
|
+
return accessors[context].get(field);
|
|
66
|
+
}, onReject, onError);
|
|
67
|
+
},
|
|
68
|
+
set(context, field, value, onReject = null) {
|
|
69
|
+
run(() => {
|
|
70
|
+
guard(context);
|
|
71
|
+
accessors[context].set(field, value);
|
|
72
|
+
}, onReject, onError);
|
|
73
|
+
},
|
|
74
|
+
on(context, event, handler) {
|
|
75
|
+
guard(context);
|
|
76
|
+
accessors[context].on(event, handler);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
function run(fn, onReject = null, onError = null) {
|
|
81
|
+
retain(onReject);
|
|
82
|
+
try {
|
|
83
|
+
return fn();
|
|
84
|
+
} catch (e) {
|
|
85
|
+
if (onReject) {
|
|
86
|
+
onReject(e instanceof HostError ? e.rejection : { message: String(e) });
|
|
87
|
+
}
|
|
88
|
+
if (onError) {
|
|
89
|
+
onError(e);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
throw e;
|
|
93
|
+
} finally {
|
|
94
|
+
release(onReject);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
export {
|
|
98
|
+
HostError,
|
|
99
|
+
LogicalError,
|
|
100
|
+
RuntimeError,
|
|
101
|
+
createContextAccessor,
|
|
102
|
+
createGetter,
|
|
103
|
+
createSetter
|
|
104
|
+
};
|