@blocklet/meta 1.7.9 → 1.7.10

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.
@@ -17,7 +17,7 @@ const parseNavigation = (navigation, blocklet, prefix = '/', level = 1) => {
17
17
  };
18
18
 
19
19
  if (nav.link) {
20
- item.link = normalizePathPrefix(`${prefix}${nav.link || '/'}`);
20
+ item.link = nav.link.startsWith('/') ? normalizePathPrefix(`${prefix}${nav.link || '/'}`) : nav.link;
21
21
  }
22
22
 
23
23
  if (level === 1) {
package/lib/service.js ADDED
@@ -0,0 +1,102 @@
1
+ const fs = require('fs-extra');
2
+ const cloneDeep = require('lodash/cloneDeep');
3
+ const Ajv = require('ajv').default;
4
+
5
+ const { NODE_SERVICES } = require('@abtnode/constant');
6
+
7
+ const ajv = new Ajv({
8
+ useDefaults: true,
9
+ removeAdditional: 'all',
10
+ });
11
+
12
+ const SERVICES = {};
13
+
14
+ const setService = (meta) => {
15
+ const validate = ajv.compile(meta.schema.JSONSchema);
16
+
17
+ // parse empty custom config to get default config
18
+ const defaultConfig = {};
19
+ validate(defaultConfig);
20
+
21
+ SERVICES[meta.name] = {
22
+ meta,
23
+ validate,
24
+ defaultConfig,
25
+ };
26
+ };
27
+
28
+ setService(fs.readJSONSync(require.resolve('@abtnode/blocklet-services/configs/auth.json')));
29
+
30
+ // backward compatible
31
+ SERVICES[NODE_SERVICES.AUTH_SERVICE] = {
32
+ ...SERVICES[NODE_SERVICES.AUTH],
33
+ meta: {
34
+ ...SERVICES[NODE_SERVICES.AUTH].meta,
35
+ name: NODE_SERVICES.AUTH_SERVICE,
36
+ },
37
+ };
38
+
39
+ const getServiceMetas = ({ stringifySchema = false } = {}) => {
40
+ const list = Object.values(SERVICES).map(({ meta }) => {
41
+ const data = cloneDeep(meta);
42
+ if (stringifySchema) {
43
+ data.schema = JSON.stringify(meta.schema);
44
+ }
45
+ return data;
46
+ });
47
+
48
+ return list;
49
+ };
50
+
51
+ const getService = (serviceName) => {
52
+ if (!serviceName) {
53
+ throw new Error('service name should not be empty');
54
+ }
55
+
56
+ const service = SERVICES[serviceName];
57
+ if (!service) {
58
+ throw new Error(`service ${serviceName} does not exist`);
59
+ }
60
+
61
+ return service;
62
+ };
63
+
64
+ const getServiceConfig = (serviceName, customConfig, { validate } = {}) => {
65
+ const service = getService(serviceName);
66
+
67
+ const data = cloneDeep(customConfig || {});
68
+ // this method may have side effect thar will fill default value to customConfig
69
+ const valid = service.validate(data || {});
70
+
71
+ if (validate && !valid) {
72
+ const message = service.validate.errors.map((x) => `${x.instancePath} ${x.message}`).join(', ');
73
+ throw new Error(`Invalid blocklet service config: ${message}`);
74
+ }
75
+
76
+ return data;
77
+ };
78
+
79
+ const getDefaultServiceConfig = (serviceName) => {
80
+ const { defaultConfig } = getService(serviceName);
81
+
82
+ return defaultConfig;
83
+ };
84
+
85
+ const findService = (services, name) => {
86
+ const names = [name];
87
+
88
+ // backward compatible
89
+ if (name === NODE_SERVICES.AUTH) {
90
+ names.push(NODE_SERVICES.AUTH_SERVICE);
91
+ }
92
+
93
+ return (services || []).find((x) => names.includes(x.name));
94
+ };
95
+
96
+ module.exports = {
97
+ getServiceMetas,
98
+ getServiceConfig,
99
+ getDefaultServiceConfig,
100
+ findService,
101
+ setService,
102
+ };
package/lib/validate.js CHANGED
@@ -1,14 +1,7 @@
1
- const Ajv = require('ajv').default;
2
- const cloneDeep = require('lodash/cloneDeep');
3
-
4
1
  const { createBlockletSchema } = require('./schema');
2
+ const { getServiceConfig } = require('./service');
5
3
 
6
- const fixAndValidateService = (meta, serviceMetas) => {
7
- const ajv = new Ajv({
8
- useDefaults: true,
9
- removeAdditional: 'all',
10
- });
11
-
4
+ const fixAndValidateService = (meta) => {
12
5
  if (!meta.interfaces) {
13
6
  return meta;
14
7
  }
@@ -19,26 +12,7 @@ const fixAndValidateService = (meta, serviceMetas) => {
19
12
  meta.interfaces.forEach((d) => {
20
13
  if (d.services && d.services.length) {
21
14
  d.services.forEach((s) => {
22
- const config = cloneDeep(s.config) || {};
23
-
24
- // validate service.config if serviceMetaList is exist
25
- if (serviceMetas) {
26
- const serviceMeta = serviceMetas.find((x) => x.name === s.name);
27
- if (!serviceMeta) {
28
- throw new Error(`Invalid blocklet service: ${s.name} does not exist`);
29
- }
30
-
31
- const validate = ajv.compile(serviceMeta.schema.JSONSchema);
32
- // this method may have side effect thar will fill default value to config
33
- const valid = validate(config);
34
-
35
- if (!valid) {
36
- const message = validate.errors.map((x) => `${x.dataPath} ${x.message}`).join(', ');
37
- throw new Error(`Invalid blocklet service config: ${message}`);
38
- }
39
- }
40
-
41
- s.config = config;
15
+ s.config = getServiceConfig(s.name, s.config, { validate: true });
42
16
  });
43
17
  }
44
18
  });
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.7.9",
6
+ "version": "1.7.10",
7
7
  "description": "Library to parse/validate/fix blocklet meta",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -18,18 +18,18 @@
18
18
  "author": "wangshijun <wangshijun2020@gmail.com> (http://github.com/wangshijun)",
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@abtnode/constant": "1.7.9",
22
- "@abtnode/util": "1.7.9",
23
- "@arcblock/did": "^1.16.4",
24
- "@arcblock/did-ext": "^1.16.4",
25
- "@arcblock/did-util": "^1.16.4",
26
- "@arcblock/jwt": "^1.16.4",
27
- "@arcblock/nft": "^1.16.4",
28
- "@ocap/asset": "^1.16.4",
29
- "@ocap/mcrypto": "^1.16.4",
30
- "@ocap/util": "^1.16.4",
31
- "@ocap/wallet": "^1.16.4",
32
- "ajv": "^7.0.3",
21
+ "@abtnode/constant": "1.7.10",
22
+ "@abtnode/util": "1.7.10",
23
+ "@arcblock/did": "^1.16.5",
24
+ "@arcblock/did-ext": "^1.16.5",
25
+ "@arcblock/did-util": "^1.16.5",
26
+ "@arcblock/jwt": "^1.16.5",
27
+ "@arcblock/nft": "^1.16.5",
28
+ "@ocap/asset": "^1.16.5",
29
+ "@ocap/mcrypto": "^1.16.5",
30
+ "@ocap/util": "^1.16.5",
31
+ "@ocap/wallet": "^1.16.5",
32
+ "ajv": "^8.11.0",
33
33
  "cjk-length": "^1.0.0",
34
34
  "debug": "^4.3.3",
35
35
  "fs-extra": "^10.0.1",
@@ -46,5 +46,5 @@
46
46
  "devDependencies": {
47
47
  "jest": "^27.4.5"
48
48
  },
49
- "gitHead": "285f4fedd41fcb8e1814ce5d8250ac10616e67e0"
49
+ "gitHead": "8eab10fd39b6183a2fa4d2706f52e8b2ecaa059a"
50
50
  }