@descope-ui/common 0.0.3 → 0.0.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/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
4
4
 
5
+ ## [0.0.5](https://github.com/descope/web-components-ui/compare/@descope-ui/common-0.0.4...@descope-ui/common-0.0.5) (2025-02-26)
6
+
7
+ ## [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)
8
+
5
9
  ## [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
10
 
7
11
  ## [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.5",
4
4
  "dependencies": {
5
5
  "element-internals-polyfill": "^1.3.9",
6
6
  "color": "^4.2.3",
@@ -0,0 +1,62 @@
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
+ return false;
27
+ }
28
+ return true;
29
+ }
30
+
31
+ // eslint-disable-next-line class-methods-use-this
32
+ validateParam(param, value) {
33
+ // Base validation - can be overridden by specific connectors
34
+ return value.trim().length > 0;
35
+ }
36
+
37
+ // eslint-disable-next-line class-methods-use-this
38
+ getRequiredParams() {
39
+ // eslint-disable-next-line no-console
40
+ console.error('Connector must implement getRequiredParams');
41
+ return [];
42
+ }
43
+
44
+ async fetch(query) {
45
+ if (!this.isValid) {
46
+ // eslint-disable-next-line no-console
47
+ console.error(
48
+ `[${this.constructor.name}] Cannot fetch results: connector is not properly configured`,
49
+ );
50
+ return { results: [], error: CONNECTOR_ERRORS.CONNECTOR_INVALID };
51
+ }
52
+
53
+ return this._fetchResults(query);
54
+ }
55
+
56
+ // eslint-disable-next-line class-methods-use-this, no-unused-vars
57
+ _fetchResults(query) {
58
+ // eslint-disable-next-line no-console
59
+ console.error('Connector must implement fetchResults');
60
+ return { results: [], error: undefined };
61
+ }
62
+ };
@@ -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
+ return;
28
+ }
29
+ const ConnectorClass = this.connectorClasses[template];
30
+ this.connector = new ConnectorClass(this.getAttribute.bind(this));
31
+ }
32
+
33
+ async fetchConnectorResults(query) {
34
+ if (!this.connector) {
35
+ // eslint-disable-next-line no-console
36
+ console.error(
37
+ `No connector initialized for the field with template: ${this.connectorTemplate}`,
38
+ );
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';