@descope-ui/common 0.0.3 → 0.0.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/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
4
4
 
5
+ ## [0.0.4](https://github.com/descope/web-components-ui/compare/@descope-ui/common-0.0.3...@descope-ui/common-0.0.4) (2025-02-17)
6
+
5
7
  ## [0.0.3](https://github.com/descope/web-components-ui/compare/@descope-ui/common-0.0.2...@descope-ui/common-0.0.3) (2025-02-04)
6
8
 
7
9
  ## [0.0.2](https://github.com/descope/web-components-ui/compare/@descope-ui/common-0.0.1...@descope-ui/common-0.0.2) (2025-01-28)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@descope-ui/common",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "dependencies": {
5
5
  "element-internals-polyfill": "^1.3.9",
6
6
  "color": "^4.2.3",
@@ -0,0 +1,70 @@
1
+ export const CONNECTOR_ERRORS = {
2
+ CONNECTOR_INVALID: 'CONNECTOR_INVALID',
3
+ FETCH_RESULTS_ERROR: 'FETCH_RESULTS_ERROR',
4
+ };
5
+
6
+ export const createBaseConnectorClass = () =>
7
+ class BaseConnectorClass {
8
+ constructor(getAttribute) {
9
+ this.getAttribute = getAttribute;
10
+ this.isValid = this.#validateConfig();
11
+ }
12
+
13
+ checkConnectorValidity() {
14
+ this.isValid = this.#validateConfig();
15
+ }
16
+
17
+ #validateConfig() {
18
+ const missingOrInvalidParams = this.getRequiredParams().filter(
19
+ (param) => {
20
+ const value = this.getAttribute(param);
21
+ return !value || !this.validateParam(param, value);
22
+ },
23
+ );
24
+
25
+ if (missingOrInvalidParams.length) {
26
+ // eslint-disable-next-line no-console
27
+ console.error(
28
+ `[${this.constructor.name}] Invalid configuration. Issues with parameters:`,
29
+ missingOrInvalidParams.map((param) => ({
30
+ param,
31
+ value: this.getAttribute(param),
32
+ })),
33
+ );
34
+ return false;
35
+ }
36
+ return true;
37
+ }
38
+
39
+ // eslint-disable-next-line class-methods-use-this
40
+ validateParam(param, value) {
41
+ // Base validation - can be overridden by specific connectors
42
+ return value.trim().length > 0;
43
+ }
44
+
45
+ // eslint-disable-next-line class-methods-use-this
46
+ getRequiredParams() {
47
+ // eslint-disable-next-line no-console
48
+ console.error('Connector must implement getRequiredParams');
49
+ return [];
50
+ }
51
+
52
+ async fetch(query) {
53
+ if (!this.isValid) {
54
+ // eslint-disable-next-line no-console
55
+ console.error(
56
+ `[${this.constructor.name}] Cannot fetch results: connector is not properly configured`,
57
+ );
58
+ return { results: [], error: CONNECTOR_ERRORS.CONNECTOR_INVALID };
59
+ }
60
+
61
+ return this._fetchResults(query);
62
+ }
63
+
64
+ // eslint-disable-next-line class-methods-use-this, no-unused-vars
65
+ _fetchResults(query) {
66
+ // eslint-disable-next-line no-console
67
+ console.error('Connector must implement fetchResults');
68
+ return { results: [], error: undefined };
69
+ }
70
+ };
@@ -1,3 +1,4 @@
1
1
  export * from './baseClasses/createBaseClass';
2
2
  export * from './baseClasses/createCssVarImageClass';
3
3
  export * from './baseClasses/createBaseInputClass';
4
+ export * from './baseClasses/createBaseConnectorClass';
@@ -0,0 +1,68 @@
1
+ export const connectorMixin =
2
+ ({ connectorClasses }) =>
3
+ (superclass) =>
4
+ class ConnectorMixinClass extends superclass {
5
+ #connectorClasses = connectorClasses;
6
+
7
+ static get observedAttributes() {
8
+ return [...(superclass.observedAttributes || []), 'connector-template'];
9
+ }
10
+
11
+ get connectorClasses() {
12
+ return this.#connectorClasses;
13
+ }
14
+
15
+ set connectorClasses(value) {
16
+ this.#connectorClasses = value;
17
+ }
18
+
19
+ get connectorTemplate() {
20
+ return this.getAttribute('connector-template');
21
+ }
22
+
23
+ initializeConnector() {
24
+ const template = this.connectorTemplate;
25
+
26
+ if (!this.connectorClasses[template]) {
27
+ // eslint-disable-next-line no-console
28
+ console.error(`Unsupported connector template: ${template}`);
29
+ return;
30
+ }
31
+ const ConnectorClass = this.connectorClasses[template];
32
+ this.connector = new ConnectorClass(this.getAttribute.bind(this));
33
+ }
34
+
35
+ async fetchConnectorResults(query) {
36
+ if (!this.connector) {
37
+ // eslint-disable-next-line no-console
38
+ console.error('No connector initialized for the field');
39
+ return { results: [] };
40
+ }
41
+
42
+ const { results, error } = await this.connector.fetch(query);
43
+ if (error) {
44
+ return { results: [], error };
45
+ }
46
+ return {
47
+ results: results.map(({ label, value }) => ({
48
+ label,
49
+ value,
50
+ })),
51
+ };
52
+ }
53
+
54
+ init() {
55
+ super.init?.();
56
+ this.initializeConnector();
57
+ }
58
+
59
+ attributeChangedCallback(attrName, oldValue, newValue) {
60
+ super.attributeChangedCallback?.(attrName, oldValue, newValue);
61
+
62
+ if (oldValue !== newValue) {
63
+ if (attrName === 'connector-template') {
64
+ this.initializeConnector();
65
+ }
66
+ }
67
+ }
68
+ };
@@ -11,4 +11,5 @@ export { normalizeBooleanAttributesMixin } from './normalizeBooleanAttributesMix
11
11
  export { lifecycleEventsMixin } from './lifecycleEventsMixin';
12
12
  export { inputEventsDispatchingMixin } from './inputEventsDispatchingMixin';
13
13
  export { externalInputMixin } from './externalInputMixin';
14
- export {componentsContextMixin} from './componentsContextMixin'
14
+ export { componentsContextMixin } from './componentsContextMixin';
15
+ export { connectorMixin } from './connectorMixin';