@enonic/nextjs-adapter 0.0.1-alpha.6 → 0.10.1

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.
@@ -16,15 +16,16 @@ export interface ComponentDefinition {
16
16
  }
17
17
  export declare type DataProcessor = (data: any, context?: Context) => Promise<Record<string, any>>;
18
18
  export declare type VariablesGetter = (path: string, context?: Context, config?: any) => VariablesGetterResult;
19
+ export declare type QueryGetter = (config?: any) => string;
19
20
  export declare type VariablesGetterResult = {
20
21
  [variables: string]: any;
21
22
  path: string;
22
23
  };
23
- export declare type SelectedQueryMaybeVariablesFunc = string | {
24
- query: string;
24
+ export declare type SelectedQueryMaybeVariablesFunc = string | QueryGetter | {
25
+ query: string | QueryGetter;
25
26
  variables: VariablesGetter;
26
27
  } | [
27
- string,
28
+ string | QueryGetter,
28
29
  VariablesGetter
29
30
  ];
30
31
  export declare const CATCH_ALL = "*";
@@ -36,6 +37,9 @@ export declare class ComponentRegistry {
36
37
  private static layouts;
37
38
  private static macros;
38
39
  private static commonQuery;
40
+ private static getSelector;
41
+ private static getType;
42
+ private static addType;
39
43
  static getByComponent(component: PageComponent): ComponentDefinition | undefined;
40
44
  static setCommonQuery(query: SelectedQueryMaybeVariablesFunc): void;
41
45
  static getCommonQuery(): SelectedQueryMaybeVariablesFunc;
@@ -55,7 +59,4 @@ export declare class ComponentRegistry {
55
59
  static getLayout(name: string): ComponentDefinition | undefined;
56
60
  static addComponent(name: string, obj: ComponentDefinition): void;
57
61
  static getComponent(name: string): ComponentDefinition | undefined;
58
- private static getSelector;
59
- private static getType;
60
- private static addType;
61
62
  }
@@ -11,6 +11,41 @@ function toSelectorName(type) {
11
11
  }
12
12
  export const CATCH_ALL = '*';
13
13
  export class ComponentRegistry {
14
+ static getSelector(name) {
15
+ switch (name) {
16
+ case 'contentType':
17
+ return this.contentTypes;
18
+ case 'page':
19
+ return this.pages;
20
+ case 'component':
21
+ return this.components;
22
+ case 'layout':
23
+ return this.layouts;
24
+ case 'part':
25
+ return this.parts;
26
+ case 'macro':
27
+ return this.macros;
28
+ }
29
+ }
30
+ static getType(selectorName, typeName, useCatchAll = true) {
31
+ const selector = ComponentRegistry.getSelector(selectorName);
32
+ let type = selector[typeName];
33
+ if (type) {
34
+ type.catchAll = false;
35
+ }
36
+ else if (!type && useCatchAll) {
37
+ type = selector[CATCH_ALL];
38
+ if (type) {
39
+ type.catchAll = true;
40
+ }
41
+ }
42
+ return type;
43
+ }
44
+ static addType(selectorName, name, obj) {
45
+ const selector = ComponentRegistry.getSelector(selectorName);
46
+ const curr = selector[name];
47
+ selector[name] = curr ? Object.assign(curr, obj) : obj;
48
+ }
14
49
  static getByComponent(component) {
15
50
  const selName = toSelectorName(component.type);
16
51
  const cmpData = component[component.type];
@@ -71,41 +106,6 @@ export class ComponentRegistry {
71
106
  static getComponent(name) {
72
107
  return ComponentRegistry.getType('component', name);
73
108
  }
74
- static getSelector(name) {
75
- switch (name) {
76
- case 'contentType':
77
- return this.contentTypes;
78
- case 'page':
79
- return this.pages;
80
- case 'component':
81
- return this.components;
82
- case 'layout':
83
- return this.layouts;
84
- case 'part':
85
- return this.parts;
86
- case 'macro':
87
- return this.macros;
88
- }
89
- }
90
- static getType(selectorName, typeName, useCatchAll = true) {
91
- const selector = ComponentRegistry.getSelector(selectorName);
92
- let type = selector[typeName];
93
- if (type) {
94
- type.catchAll = false;
95
- }
96
- else if (!type && useCatchAll) {
97
- type = selector[CATCH_ALL];
98
- if (type) {
99
- type.catchAll = true;
100
- }
101
- }
102
- return type;
103
- }
104
- static addType(selectorName, name, obj) {
105
- const selector = ComponentRegistry.getSelector(selectorName);
106
- const curr = selector[name];
107
- selector[name] = curr ? Object.assign(curr, obj) : obj;
108
- }
109
109
  }
110
110
  ComponentRegistry.contentTypes = {};
111
111
  ComponentRegistry.pages = {};
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { getMetaQuery, pageFragmentQuery, } from './getMetaData';
10
+ import { getMetaQuery, pageFragmentQuery } from './getMetaData';
11
11
  import adapterConstants, { APP_NAME, APP_NAME_DASHED, FRAGMENT_CONTENTTYPE_NAME, FRAGMENT_DEFAULT_REGION_NAME, getContentApiUrl, getXpBaseUrl, IS_DEV_MODE, PAGE_TEMPLATE_CONTENTTYPE_NAME, PAGE_TEMPLATE_FOLDER, RENDER_MODE, sanitizeGraphqlName, SITE_KEY, XP_COMPONENT_TYPE, XP_REQUEST_TYPE, } from '../utils';
12
12
  import { ComponentRegistry } from '../ComponentRegistry';
13
13
  const NO_PROPS_PROCESSOR = (props) => __awaiter(void 0, void 0, void 0, function* () { var _a; return (_a = yield props) !== null && _a !== void 0 ? _a : {}; });
@@ -406,7 +406,7 @@ function processComponentConfig(myAppName, myAppNameDashed, cmp) {
406
406
  }
407
407
  function getQueryAndVariables(type, path, selectedQuery, context, config) {
408
408
  let query, getVariables;
409
- if (typeof selectedQuery === 'string') {
409
+ if (typeof selectedQuery === 'string' || typeof selectedQuery === 'function') {
410
410
  query = selectedQuery;
411
411
  }
412
412
  else if (Array.isArray(selectedQuery)) {
@@ -420,12 +420,15 @@ function getQueryAndVariables(type, path, selectedQuery, context, config) {
420
420
  if (getVariables && typeof getVariables !== 'function') {
421
421
  throw Error(`getVariables for content type ${type} should be a function, not: ${typeof getVariables}`);
422
422
  }
423
- if (query && typeof query !== 'string') {
424
- throw Error(`Query for content type ${type} should be a string, not: ${typeof query}`);
423
+ if (query && typeof query !== 'string' && typeof query !== 'function') {
424
+ throw Error(`Query for content type ${type} should be a string or function, not: ${typeof query}`);
425
+ }
426
+ if (typeof query === 'function') {
427
+ query = query(config);
425
428
  }
426
429
  if (query) {
427
430
  return {
428
- query: query,
431
+ query,
429
432
  variables: getVariables ? getVariables(path, context, config) : { path },
430
433
  };
431
434
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enonic/nextjs-adapter",
3
- "version": "0.0.1-alpha.6",
3
+ "version": "0.10.1",
4
4
  "license": "Apache-2.0",
5
5
  "main": "index.js",
6
6
  "publishConfig": {