@tramvai/module-environment 3.41.1 → 3.41.3

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.
@@ -0,0 +1,13 @@
1
+ import type { EnvParameter, EnvironmentManager, ClientEnvironmentRepository as Interface } from '@tramvai/tokens-common';
2
+ export declare class ClientEnvironmentRepository implements Interface {
3
+ private envManager;
4
+ private tokens;
5
+ protected parameters: Record<string, string | undefined>;
6
+ constructor(envManager: EnvironmentManager, tokens: EnvParameter[]);
7
+ get(name: string): string | undefined;
8
+ set(name: string, value: string | undefined): void;
9
+ getAll(): Record<string, string | undefined>;
10
+ update(result: Record<string, string>): void;
11
+ private processing;
12
+ }
13
+ //# sourceMappingURL=ClientEnvironmentRepository.d.ts.map
@@ -0,0 +1,31 @@
1
+ class ClientEnvironmentRepository {
2
+ constructor(envManager, tokens) {
3
+ this.envManager = envManager;
4
+ this.tokens = tokens;
5
+ this.parameters = {};
6
+ this.processing();
7
+ }
8
+ get(name) {
9
+ return this.parameters[name];
10
+ }
11
+ set(name, value) {
12
+ this.parameters[name] = value;
13
+ }
14
+ getAll() {
15
+ return this.parameters;
16
+ }
17
+ update(result) {
18
+ this.parameters = Object.assign(this.parameters, result);
19
+ }
20
+ processing() {
21
+ const envParameters = this.envManager.getAll();
22
+ this.tokens.forEach(({ key, dehydrate }) => {
23
+ const value = envParameters[key];
24
+ if (dehydrate !== false) {
25
+ this.set(key, value);
26
+ }
27
+ });
28
+ }
29
+ }
30
+
31
+ export { ClientEnvironmentRepository };
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ class ClientEnvironmentRepository {
6
+ constructor(envManager, tokens) {
7
+ this.envManager = envManager;
8
+ this.tokens = tokens;
9
+ this.parameters = {};
10
+ this.processing();
11
+ }
12
+ get(name) {
13
+ return this.parameters[name];
14
+ }
15
+ set(name, value) {
16
+ this.parameters[name] = value;
17
+ }
18
+ getAll() {
19
+ return this.parameters;
20
+ }
21
+ update(result) {
22
+ this.parameters = Object.assign(this.parameters, result);
23
+ }
24
+ processing() {
25
+ const envParameters = this.envManager.getAll();
26
+ this.tokens.forEach(({ key, dehydrate }) => {
27
+ const value = envParameters[key];
28
+ if (dehydrate !== false) {
29
+ this.set(key, value);
30
+ }
31
+ });
32
+ }
33
+ }
34
+
35
+ exports.ClientEnvironmentRepository = ClientEnvironmentRepository;
@@ -1,10 +1,19 @@
1
- import type { EnvParameter } from '@tramvai/tokens-common';
1
+ import type { EnvParameter, EnvTemplate } from '@tramvai/tokens-common';
2
2
  import { EnvironmentManager } from '../shared/EnvironmentManager';
3
3
  export declare class EnvironmentManagerServer extends EnvironmentManager {
4
4
  private tokens;
5
- private clientUsedList;
6
- constructor(tokens: EnvParameter[]);
7
- clientUsed(): Record<string, string>;
5
+ private clientEnvRepository;
6
+ private templates;
7
+ constructor(tokens: EnvParameter[], templates?: EnvTemplate[]);
8
+ get(name: string): string | undefined;
9
+ getAll(): Record<string, string | undefined>;
10
+ /**
11
+ * @deprecated use CLIENT_ENV_MANAGER_TOKEN
12
+ */
13
+ clientUsed(): Record<string, string | undefined>;
14
+ /**
15
+ * @deprecated use CLIENT_ENV_MANAGER_TOKEN
16
+ */
8
17
  updateClientUsed(result: Record<string, string>): void;
9
18
  private getEnvInFiles;
10
19
  private getEnvInApp;
@@ -1,5 +1,7 @@
1
1
  import noop from '@tinkoff/utils/function/noop';
2
2
  import { EnvironmentManager } from '../shared/EnvironmentManager.es.js';
3
+ import { ClientEnvironmentRepository } from './ClientEnvironmentRepository.es.js';
4
+ import { interpolate } from '../shared/template.es.js';
3
5
 
4
6
  const readFileWithEnv = (path) => {
5
7
  try {
@@ -13,17 +15,41 @@ const readFileWithEnv = (path) => {
13
15
  }
14
16
  };
15
17
  class EnvironmentManagerServer extends EnvironmentManager {
16
- constructor(tokens) {
18
+ constructor(tokens, templates = []) {
17
19
  super();
18
20
  this.tokens = tokens;
19
- this.clientUsedList = {};
21
+ this.templates = templates.reduce((acc, { key, fn }) => {
22
+ // TODO: key duplicates?
23
+ acc[key] = fn;
24
+ return acc;
25
+ }, {});
20
26
  this.processing();
27
+ // for backward compatibility.
28
+ this.clientEnvRepository = new ClientEnvironmentRepository(this, this.tokens);
21
29
  }
30
+ get(name) {
31
+ const value = super.get(name);
32
+ return interpolate({ envKey: name, envValue: value, templates: this.templates });
33
+ }
34
+ getAll() {
35
+ const values = super.getAll();
36
+ const result = {};
37
+ for (const name in values) {
38
+ result[name] = this.get(name);
39
+ }
40
+ return result;
41
+ }
42
+ /**
43
+ * @deprecated use CLIENT_ENV_MANAGER_TOKEN
44
+ */
22
45
  clientUsed() {
23
- return this.clientUsedList;
46
+ return this.clientEnvRepository.getAll();
24
47
  }
48
+ /**
49
+ * @deprecated use CLIENT_ENV_MANAGER_TOKEN
50
+ */
25
51
  updateClientUsed(result) {
26
- this.clientUsedList = Object.assign(this.clientUsedList, result);
52
+ this.clientEnvRepository.update(result);
27
53
  }
28
54
  // eslint-disable-next-line class-methods-use-this
29
55
  getEnvInFiles() {
@@ -53,22 +79,25 @@ class EnvironmentManagerServer extends EnvironmentManager {
53
79
  processing() {
54
80
  const result = {};
55
81
  const envParameters = this.collectionEnv();
56
- this.tokens.forEach(({ key, validator = noop, optional, dehydrate }) => {
82
+ this.tokens.forEach(({ key, validator = noop, optional }) => {
57
83
  const value = envParameters[key];
84
+ const interpolatedValue = interpolate({
85
+ envKey: key,
86
+ envValue: value,
87
+ templates: this.templates,
88
+ });
58
89
  if (typeof value === 'undefined' && !optional) {
59
90
  throw new Error(`Env parameter ${key} not found. You need add a this env parameter. If you have questions read the docs`);
60
91
  }
61
92
  // Not calling validation on empty values.
62
- const validation = typeof value !== 'undefined' && validator(value);
93
+ const validation = typeof interpolatedValue !== 'undefined' && validator(interpolatedValue);
63
94
  if (typeof validation === 'string') {
64
95
  throw new Error(`Env parameter ${key} with value ${value} not valid, message: ${validation}`);
65
96
  }
66
97
  result[key] = value;
67
- if (dehydrate !== false) {
68
- this.clientUsedList[key] = value;
69
- }
70
98
  });
71
- process.env = envParameters; // Записываем в process.env итоговый результат. TODO убрать позже
99
+ // sync process.env
100
+ process.env = envParameters;
72
101
  this.update(result);
73
102
  }
74
103
  }
@@ -4,6 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var noop = require('@tinkoff/utils/function/noop');
6
6
  var EnvironmentManager = require('../shared/EnvironmentManager.js');
7
+ var ClientEnvironmentRepository = require('./ClientEnvironmentRepository.js');
8
+ var template = require('../shared/template.js');
7
9
 
8
10
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
11
 
@@ -21,17 +23,41 @@ const readFileWithEnv = (path) => {
21
23
  }
22
24
  };
23
25
  class EnvironmentManagerServer extends EnvironmentManager.EnvironmentManager {
24
- constructor(tokens) {
26
+ constructor(tokens, templates = []) {
25
27
  super();
26
28
  this.tokens = tokens;
27
- this.clientUsedList = {};
29
+ this.templates = templates.reduce((acc, { key, fn }) => {
30
+ // TODO: key duplicates?
31
+ acc[key] = fn;
32
+ return acc;
33
+ }, {});
28
34
  this.processing();
35
+ // for backward compatibility.
36
+ this.clientEnvRepository = new ClientEnvironmentRepository.ClientEnvironmentRepository(this, this.tokens);
29
37
  }
38
+ get(name) {
39
+ const value = super.get(name);
40
+ return template.interpolate({ envKey: name, envValue: value, templates: this.templates });
41
+ }
42
+ getAll() {
43
+ const values = super.getAll();
44
+ const result = {};
45
+ for (const name in values) {
46
+ result[name] = this.get(name);
47
+ }
48
+ return result;
49
+ }
50
+ /**
51
+ * @deprecated use CLIENT_ENV_MANAGER_TOKEN
52
+ */
30
53
  clientUsed() {
31
- return this.clientUsedList;
54
+ return this.clientEnvRepository.getAll();
32
55
  }
56
+ /**
57
+ * @deprecated use CLIENT_ENV_MANAGER_TOKEN
58
+ */
33
59
  updateClientUsed(result) {
34
- this.clientUsedList = Object.assign(this.clientUsedList, result);
60
+ this.clientEnvRepository.update(result);
35
61
  }
36
62
  // eslint-disable-next-line class-methods-use-this
37
63
  getEnvInFiles() {
@@ -61,22 +87,25 @@ class EnvironmentManagerServer extends EnvironmentManager.EnvironmentManager {
61
87
  processing() {
62
88
  const result = {};
63
89
  const envParameters = this.collectionEnv();
64
- this.tokens.forEach(({ key, validator = noop__default["default"], optional, dehydrate }) => {
90
+ this.tokens.forEach(({ key, validator = noop__default["default"], optional }) => {
65
91
  const value = envParameters[key];
92
+ const interpolatedValue = template.interpolate({
93
+ envKey: key,
94
+ envValue: value,
95
+ templates: this.templates,
96
+ });
66
97
  if (typeof value === 'undefined' && !optional) {
67
98
  throw new Error(`Env parameter ${key} not found. You need add a this env parameter. If you have questions read the docs`);
68
99
  }
69
100
  // Not calling validation on empty values.
70
- const validation = typeof value !== 'undefined' && validator(value);
101
+ const validation = typeof interpolatedValue !== 'undefined' && validator(interpolatedValue);
71
102
  if (typeof validation === 'string') {
72
103
  throw new Error(`Env parameter ${key} with value ${value} not valid, message: ${validation}`);
73
104
  }
74
105
  result[key] = value;
75
- if (dehydrate !== false) {
76
- this.clientUsedList[key] = value;
77
- }
78
106
  });
79
- process.env = envParameters; // Записываем в process.env итоговый результат. TODO убрать позже
107
+ // sync process.env
108
+ process.env = envParameters;
80
109
  this.update(result);
81
110
  }
82
111
  }
package/lib/server.es.js CHANGED
@@ -1,12 +1,13 @@
1
1
  import { __decorate } from 'tslib';
2
2
  import { Module, provide, Scope, commandLineListTokens } from '@tramvai/core';
3
3
  import flatten from '@tinkoff/utils/array/flatten';
4
- import { ENV_MANAGER_TOKEN, ENV_USED_TOKEN, COMBINE_REDUCERS, CONTEXT_TOKEN } from '@tramvai/tokens-common';
4
+ import { ENV_MANAGER_TOKEN, ENV_USED_TOKEN, ENV_TEMPLATE_TOKEN, CLIENT_ENV_REPOSITORY_TOKEN, COMBINE_REDUCERS, CONTEXT_TOKEN } from '@tramvai/tokens-common';
5
5
  export { ENV_MANAGER_TOKEN, ENV_USED_TOKEN } from '@tramvai/tokens-common';
6
6
  import { SERVER_MODULE_PAPI_PUBLIC_ROUTE } from '@tramvai/tokens-server';
7
7
  import { createPapiMethod } from '@tramvai/papi';
8
8
  import { EnvironmentStore } from './shared/EnvironmentStore.es.js';
9
9
  import { EnvironmentManagerServer } from './server/EnvironmentManagerServer.es.js';
10
+ import { ClientEnvironmentRepository } from './server/ClientEnvironmentRepository.es.js';
10
11
 
11
12
  let EnvironmentModule = class EnvironmentModule {
12
13
  };
@@ -15,16 +16,34 @@ EnvironmentModule = __decorate([
15
16
  providers: [
16
17
  provide({
17
18
  provide: ENV_MANAGER_TOKEN,
18
- useFactory: ({ tokens }) => {
19
- return new EnvironmentManagerServer(flatten(tokens !== null && tokens !== void 0 ? tokens : []));
19
+ scope: Scope.SINGLETON,
20
+ useFactory: ({ tokens, templates }) => {
21
+ return new EnvironmentManagerServer(flatten(tokens !== null && tokens !== void 0 ? tokens : []), templates !== null && templates !== void 0 ? templates : []);
20
22
  },
21
23
  deps: {
22
24
  tokens: {
23
25
  token: ENV_USED_TOKEN,
24
26
  optional: true,
25
27
  },
28
+ templates: {
29
+ token: ENV_TEMPLATE_TOKEN,
30
+ optional: true,
31
+ },
32
+ },
33
+ }),
34
+ provide({
35
+ provide: CLIENT_ENV_REPOSITORY_TOKEN,
36
+ scope: Scope.REQUEST,
37
+ useFactory: ({ envManager, tokens }) => {
38
+ return new ClientEnvironmentRepository(envManager, flatten(tokens !== null && tokens !== void 0 ? tokens : []));
39
+ },
40
+ deps: {
41
+ envManager: ENV_MANAGER_TOKEN,
42
+ tokens: {
43
+ token: ENV_USED_TOKEN,
44
+ optional: true,
45
+ },
26
46
  },
27
- scope: Scope.SINGLETON,
28
47
  }),
29
48
  provide({
30
49
  provide: COMBINE_REDUCERS,
@@ -33,14 +52,14 @@ EnvironmentModule = __decorate([
33
52
  }),
34
53
  provide({
35
54
  provide: commandLineListTokens.customerStart,
36
- useFactory: ({ context, environmentManager }) => {
55
+ useFactory: ({ context, clientEnvRepository }) => {
37
56
  return function envCommand() {
38
- context.getStore('environment').setState(environmentManager.clientUsed());
57
+ context.getStore('environment').setState(clientEnvRepository.getAll());
39
58
  };
40
59
  },
41
60
  multi: true,
42
61
  deps: {
43
- environmentManager: ENV_MANAGER_TOKEN,
62
+ clientEnvRepository: CLIENT_ENV_REPOSITORY_TOKEN,
44
63
  context: CONTEXT_TOKEN,
45
64
  },
46
65
  }),
package/lib/server.js CHANGED
@@ -10,6 +10,7 @@ var tokensServer = require('@tramvai/tokens-server');
10
10
  var papi = require('@tramvai/papi');
11
11
  var EnvironmentStore = require('./shared/EnvironmentStore.js');
12
12
  var EnvironmentManagerServer = require('./server/EnvironmentManagerServer.js');
13
+ var ClientEnvironmentRepository = require('./server/ClientEnvironmentRepository.js');
13
14
 
14
15
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
15
16
 
@@ -22,16 +23,34 @@ exports.EnvironmentModule = tslib.__decorate([
22
23
  providers: [
23
24
  core.provide({
24
25
  provide: tokensCommon.ENV_MANAGER_TOKEN,
25
- useFactory: ({ tokens }) => {
26
- return new EnvironmentManagerServer.EnvironmentManagerServer(flatten__default["default"](tokens !== null && tokens !== void 0 ? tokens : []));
26
+ scope: core.Scope.SINGLETON,
27
+ useFactory: ({ tokens, templates }) => {
28
+ return new EnvironmentManagerServer.EnvironmentManagerServer(flatten__default["default"](tokens !== null && tokens !== void 0 ? tokens : []), templates !== null && templates !== void 0 ? templates : []);
27
29
  },
28
30
  deps: {
29
31
  tokens: {
30
32
  token: tokensCommon.ENV_USED_TOKEN,
31
33
  optional: true,
32
34
  },
35
+ templates: {
36
+ token: tokensCommon.ENV_TEMPLATE_TOKEN,
37
+ optional: true,
38
+ },
39
+ },
40
+ }),
41
+ core.provide({
42
+ provide: tokensCommon.CLIENT_ENV_REPOSITORY_TOKEN,
43
+ scope: core.Scope.REQUEST,
44
+ useFactory: ({ envManager, tokens }) => {
45
+ return new ClientEnvironmentRepository.ClientEnvironmentRepository(envManager, flatten__default["default"](tokens !== null && tokens !== void 0 ? tokens : []));
46
+ },
47
+ deps: {
48
+ envManager: tokensCommon.ENV_MANAGER_TOKEN,
49
+ tokens: {
50
+ token: tokensCommon.ENV_USED_TOKEN,
51
+ optional: true,
52
+ },
33
53
  },
34
- scope: core.Scope.SINGLETON,
35
54
  }),
36
55
  core.provide({
37
56
  provide: tokensCommon.COMBINE_REDUCERS,
@@ -40,14 +59,14 @@ exports.EnvironmentModule = tslib.__decorate([
40
59
  }),
41
60
  core.provide({
42
61
  provide: core.commandLineListTokens.customerStart,
43
- useFactory: ({ context, environmentManager }) => {
62
+ useFactory: ({ context, clientEnvRepository }) => {
44
63
  return function envCommand() {
45
- context.getStore('environment').setState(environmentManager.clientUsed());
64
+ context.getStore('environment').setState(clientEnvRepository.getAll());
46
65
  };
47
66
  },
48
67
  multi: true,
49
68
  deps: {
50
- environmentManager: tokensCommon.ENV_MANAGER_TOKEN,
69
+ clientEnvRepository: tokensCommon.CLIENT_ENV_REPOSITORY_TOKEN,
51
70
  context: tokensCommon.CONTEXT_TOKEN,
52
71
  },
53
72
  }),
@@ -1,12 +1,12 @@
1
1
  import type { EnvironmentManager as Interface } from '@tramvai/tokens-common';
2
2
  export declare class EnvironmentManager implements Interface {
3
- protected parameters: Record<string, string>;
3
+ protected parameters: Record<string, string | undefined>;
4
4
  constructor();
5
5
  get(name: string): string | undefined;
6
6
  getInt(name: string, def: number): number;
7
- getAll(): Record<string, string>;
8
- update(result: Record<string, string>): void;
9
- clientUsed(): Record<string, string>;
7
+ getAll(): Record<string, string | undefined>;
8
+ update(result: Record<string, string | undefined>): void;
9
+ clientUsed(): Record<string, string | undefined>;
10
10
  updateClientUsed(result: Record<string, string>): void;
11
11
  }
12
12
  //# sourceMappingURL=EnvironmentManager.d.ts.map
@@ -0,0 +1,8 @@
1
+ import type { EnvTemplate } from '@tramvai/tokens-common';
2
+ export type Templates = Record<string, EnvTemplate['fn']>;
3
+ export declare function interpolate({ envKey, envValue, templates, }: {
4
+ envKey: string;
5
+ envValue: string | undefined;
6
+ templates: Templates;
7
+ }): string | undefined;
8
+ //# sourceMappingURL=template.d.ts.map
@@ -0,0 +1,20 @@
1
+ // use `$[...]` as template placeholder, because this symbols are not used in popular terminal shell for string interpolation
2
+ const templateRegex = /(?:\$\[(.+?)\])/g;
3
+ function interpolate({ envKey, envValue, templates, }) {
4
+ const originalValue = envValue;
5
+ if (!envValue || typeof envValue !== 'string') {
6
+ return envValue;
7
+ }
8
+ return envValue.replace(templateRegex, (templateRaw, templateStr) => {
9
+ // expect string in `key:param1,param2` format
10
+ const [key, paramsRaw = ''] = templateStr.split(':');
11
+ const params = paramsRaw.split(',').filter(Boolean);
12
+ const template = templates[key];
13
+ if (template) {
14
+ return template(...params);
15
+ }
16
+ throw Error(`Problem with "${envKey}=${originalValue}" env variable - template for ${templateRaw} not found.`);
17
+ });
18
+ }
19
+
20
+ export { interpolate };
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ // use `$[...]` as template placeholder, because this symbols are not used in popular terminal shell for string interpolation
6
+ const templateRegex = /(?:\$\[(.+?)\])/g;
7
+ function interpolate({ envKey, envValue, templates, }) {
8
+ const originalValue = envValue;
9
+ if (!envValue || typeof envValue !== 'string') {
10
+ return envValue;
11
+ }
12
+ return envValue.replace(templateRegex, (templateRaw, templateStr) => {
13
+ // expect string in `key:param1,param2` format
14
+ const [key, paramsRaw = ''] = templateStr.split(':');
15
+ const params = paramsRaw.split(',').filter(Boolean);
16
+ const template = templates[key];
17
+ if (template) {
18
+ return template(...params);
19
+ }
20
+ throw Error(`Problem with "${envKey}=${originalValue}" env variable - template for ${templateRaw} not found.`);
21
+ });
22
+ }
23
+
24
+ exports.interpolate = interpolate;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/module-environment",
3
- "version": "3.41.1",
3
+ "version": "3.41.3",
4
4
  "description": "",
5
5
  "browser": "lib/browser.js",
6
6
  "main": "lib/server.js",
@@ -19,11 +19,11 @@
19
19
  },
20
20
  "peerDependencies": {
21
21
  "@tinkoff/utils": "^2.1.2",
22
- "@tramvai/core": "3.41.1",
23
- "@tramvai/papi": "3.41.1",
24
- "@tramvai/state": "3.41.1",
25
- "@tramvai/tokens-common": "3.41.1",
26
- "@tramvai/tokens-server": "3.41.1",
22
+ "@tramvai/core": "3.41.3",
23
+ "@tramvai/papi": "3.41.3",
24
+ "@tramvai/state": "3.41.3",
25
+ "@tramvai/tokens-common": "3.41.3",
26
+ "@tramvai/tokens-server": "3.41.3",
27
27
  "@tinkoff/dippy": "0.9.4",
28
28
  "react": ">=16.14.0",
29
29
  "react-dom": ">=16.14.0",