@beseif-solutions/prow-core 0.0.28 → 0.0.29

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/dist/core.d.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import axios from "axios";
2
+ import Joi from "joi";
2
3
  import _ from "lodash";
3
4
  import moment from "moment-timezone";
4
- import { Properties } from "./minified-utils/properties";
5
- import { i18n } from "./minified-utils/locales";
6
- import Joi from "joi";
5
+ import { FromCatalog, FromDirectory, T } from "./minified-utils/locales";
7
6
  type SourceFunctions = {
8
7
  register: <T>(source: string, data: T) => Promise<{
9
8
  id: string;
@@ -18,17 +17,15 @@ export type Core = {
18
17
  axios: typeof axios;
19
18
  moment: typeof moment;
20
19
  lodash: typeof _;
21
- env: Properties;
22
- t: ReturnType<typeof i18n>[`t`];
20
+ env: Record<string, any>;
23
21
  joi: typeof Joi;
24
22
  sources?: SourceFunctions;
23
+ t: T;
25
24
  };
26
25
  type CoreOptions = {
27
- root: string;
28
- env?: Properties;
29
- debug?: boolean;
30
- locale?: string;
26
+ env?: Record<string, any>;
31
27
  sources?: SourceFunctions;
28
+ localization: FromDirectory | FromCatalog;
32
29
  };
33
30
  export declare const core: (options: CoreOptions) => Core;
34
31
  export {};
package/dist/core.js CHANGED
@@ -5,13 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.core = void 0;
7
7
  const axios_1 = __importDefault(require("axios"));
8
+ const joi_1 = __importDefault(require("joi"));
8
9
  const lodash_1 = __importDefault(require("lodash"));
9
10
  const moment_timezone_1 = __importDefault(require("moment-timezone"));
10
- const proxy_1 = require("./minified-utils/proxy");
11
- const properties_1 = require("./minified-utils/properties");
12
11
  const locales_1 = require("./minified-utils/locales");
13
- const path_1 = __importDefault(require("path"));
14
- const joi_1 = __importDefault(require("joi"));
15
12
  const FALLBACK_SOURCES = {
16
13
  register: async () => {
17
14
  throw new Error(`Sources not available: valid implementation not provided`);
@@ -23,36 +20,13 @@ const FALLBACK_SOURCES = {
23
20
  throw new Error(`Sources not available: valid implementation not provided`);
24
21
  },
25
22
  };
26
- const axiosLogs = (method, debug = false) => ({
27
- before: (name, [url]) => {
28
- if (debug) {
29
- console.log(`> [${method}]`, url);
30
- }
31
- },
32
- after: (name, response) => {
33
- if (debug) {
34
- console.log(`< [${method}]`, response.config.url, response.status);
35
- }
36
- },
37
- error: (name, error) => {
38
- if (debug) {
39
- console.log(`< [${method}]`, error.config.url, error.status);
40
- }
41
- },
42
- });
43
23
  const core = (options) => ({
44
- axios: (0, proxy_1.accessProxy)(axios_1.default, {
45
- get: (0, proxy_1.logProxy)(`axios.get`, axios_1.default.get, axiosLogs(`GET`, options.debug)),
46
- post: (0, proxy_1.logProxy)(`axios.post`, axios_1.default.post, axiosLogs(`POST`, options.debug)),
47
- put: (0, proxy_1.logProxy)(`axios.put`, axios_1.default.put, axiosLogs(`PUT`, options.debug)),
48
- patch: (0, proxy_1.logProxy)(`axios.patch`, axios_1.default.patch, axiosLogs(`PATCH`, options.debug)),
49
- delete: (0, proxy_1.logProxy)(`axios.delete`, axios_1.default.delete, axiosLogs(`DELETE`, options.debug)),
50
- }),
24
+ axios: axios_1.default,
51
25
  moment: moment_timezone_1.default,
52
26
  lodash: lodash_1.default,
53
- env: options.env || new properties_1.Properties(options.root, `.env`),
54
- t: (0, locales_1.i18n)(path_1.default.join(options.root, `/locales`), { locale: options.locale }).t,
27
+ env: options.env || {},
55
28
  joi: joi_1.default,
56
29
  sources: options.sources || FALLBACK_SOURCES,
30
+ t: (0, locales_1.i18n)(options.localization).t,
57
31
  });
58
32
  exports.core = core;
@@ -14,10 +14,21 @@ type LocalizedProperties = {
14
14
  type TOptions = {
15
15
  fallback?: boolean;
16
16
  };
17
- export declare const i18n: (directory: string, options?: {
17
+ export declare const validateDirectory: (directory: string) => boolean;
18
+ type Common = {
18
19
  locale?: string;
19
- }) => {
20
- t: (code: string, opts?: TOptions) => string;
20
+ };
21
+ export type FromCatalog = Common & {
22
+ catalog: Record<string, any>;
23
+ };
24
+ export type FromDirectory = Common & {
25
+ directory: string;
26
+ locale?: string;
27
+ write?: boolean;
28
+ };
29
+ export type T = (code: string, opts?: TOptions) => string;
30
+ export declare const i18n: (options: FromCatalog | FromDirectory) => {
31
+ t: T;
21
32
  i18n: I18n.I18n;
22
33
  };
23
34
  export declare const localizeFields: (t: ReturnType<typeof i18n>[`t`], element: Action | Trigger | StaticCredentials | SessionCredentials) => Field<LocalizedField>[];
@@ -3,29 +3,69 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.localizeOutputs = exports.localizeInputs = exports.localizeProperties = exports.localizeFields = exports.i18n = void 0;
6
+ exports.localizeOutputs = exports.localizeInputs = exports.localizeProperties = exports.localizeFields = exports.i18n = exports.validateDirectory = void 0;
7
7
  const i18n_1 = __importDefault(require("i18n"));
8
8
  const lodash_1 = __importDefault(require("lodash"));
9
9
  const fs_1 = __importDefault(require("fs"));
10
10
  const path_1 = __importDefault(require("path"));
11
- const utils_1 = require("./utils");
12
11
  const DEFAULT_LOCALE = `en`;
13
- const i18n = (directory, options = {}) => {
12
+ const validateDirectory = (directory) => {
14
13
  try {
15
- if (!(0, utils_1.validateDirectory)(directory)) {
16
- throw new Error(`Invalid directory: ${directory}`);
14
+ if (!fs_1.default.existsSync(directory)) {
15
+ console.error(`Directory does not exist: ${directory}`);
16
+ return false;
17
17
  }
18
- const availableLocales = fs_1.default.readdirSync(directory).map((file) => path_1.default.basename(file, path_1.default.extname(file)));
19
- const requestedLocale = options.locale || DEFAULT_LOCALE;
20
- const activeLocale = availableLocales.includes(requestedLocale) ? requestedLocale : DEFAULT_LOCALE;
21
- const i18nInstance = new i18n_1.default.I18n({
22
- locales: availableLocales,
23
- directory: directory,
18
+ const files = fs_1.default.readdirSync(directory);
19
+ if (!files.some((file) => file.endsWith(`.json`))) {
20
+ console.error(`No valid localization files found in directory: ${directory}`);
21
+ return false;
22
+ }
23
+ return true;
24
+ }
25
+ catch (error) {
26
+ console.error(`Error validating directory: ${error.message}`);
27
+ return false;
28
+ }
29
+ };
30
+ exports.validateDirectory = validateDirectory;
31
+ const i18n = (options) => {
32
+ try {
33
+ const common = {
24
34
  defaultLocale: DEFAULT_LOCALE,
25
35
  objectNotation: true,
26
36
  autoReload: false,
27
37
  missingKeyFn: () => ``,
28
- });
38
+ };
39
+ const requestedLocale = options.locale || DEFAULT_LOCALE;
40
+ let i18nInstance;
41
+ let activeLocale;
42
+ if (`directory` in options) {
43
+ if (!(0, exports.validateDirectory)(options.directory)) {
44
+ throw new Error(`Invalid directory: ${options.directory}`);
45
+ }
46
+ const availableLocales = fs_1.default.readdirSync(options.directory).map((file) => path_1.default.basename(file, path_1.default.extname(file)));
47
+ activeLocale = availableLocales.includes(requestedLocale) ? requestedLocale : DEFAULT_LOCALE;
48
+ i18nInstance = new i18n_1.default.I18n({
49
+ ...common,
50
+ locales: availableLocales,
51
+ directory: options.directory,
52
+ updateFiles: options.write,
53
+ syncFiles: true,
54
+ });
55
+ }
56
+ else if (`catalog` in options) {
57
+ activeLocale = requestedLocale;
58
+ i18nInstance = new i18n_1.default.I18n({
59
+ ...common,
60
+ locales: [requestedLocale],
61
+ staticCatalog: {
62
+ [requestedLocale]: options.catalog,
63
+ },
64
+ });
65
+ }
66
+ else {
67
+ throw new Error(`Invalid options provided for i18n function`);
68
+ }
29
69
  i18nInstance.setLocale(activeLocale);
30
70
  return {
31
71
  t: (code, opts = {}) => {
@@ -34,7 +74,8 @@ const i18n = (directory, options = {}) => {
34
74
  response = i18nInstance.__({ phrase: code, locale: DEFAULT_LOCALE });
35
75
  }
36
76
  return response;
37
- }, i18n: i18nInstance,
77
+ },
78
+ i18n: i18nInstance,
38
79
  };
39
80
  }
40
81
  catch (e) {
@@ -1,6 +1,7 @@
1
+ import { DotenvParseOutput } from "dotenv";
1
2
  export declare class Properties {
2
3
  private readonly filepath;
3
- private readonly properties;
4
+ readonly properties: DotenvParseOutput;
4
5
  private readonly getFile;
5
6
  constructor(root: string, filename: string);
6
7
  readonly read: (name: string, unknown?: boolean) => Promise<string>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beseif-solutions/prow-core",
3
- "version": "0.0.28",
3
+ "version": "0.0.29",
4
4
  "description": "Prow core functionalities and classes",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/core.ts CHANGED
@@ -1,11 +1,8 @@
1
- import axios, { AxiosError, AxiosResponse } from "axios";
1
+ import axios from "axios";
2
+ import Joi from "joi";
2
3
  import _ from "lodash";
3
4
  import moment from "moment-timezone";
4
- import { accessProxy, logProxy, ProxyLogs } from "./minified-utils/proxy";
5
- import { Properties } from "./minified-utils/properties";
6
- import { i18n } from "./minified-utils/locales";
7
- import path from "path";
8
- import Joi from "joi";
5
+ import { FromCatalog, FromDirectory, i18n, T } from "./minified-utils/locales";
9
6
 
10
7
  type SourceFunctions = {
11
8
  register: <T>(source: string, data: T) => Promise<{ id: string }>,
@@ -25,55 +22,29 @@ const FALLBACK_SOURCES: SourceFunctions = {
25
22
  },
26
23
  };
27
24
 
28
-
29
25
  export type Core = {
30
26
  axios: typeof axios,
31
27
  moment: typeof moment,
32
28
  lodash: typeof _,
33
- env: Properties,
34
- t: ReturnType<typeof i18n>[`t`],
29
+ env: Record<string, any>,
35
30
  joi: typeof Joi,
36
31
  sources?: SourceFunctions,
32
+ t: T,
37
33
  };
38
34
 
39
- const axiosLogs = (method: string, debug = false): ProxyLogs<
40
- typeof axios[`get`]
41
- | typeof axios[`post`]
42
- | typeof axios[`put`]
43
- | typeof axios[`patch`]
44
- | typeof axios[`delete`]> => ({
45
- before: (name, [url]) => {
46
- if (debug) { console.log(`> [${method}]`, url); }
47
- },
48
- after: (name, response: AxiosResponse) => {
49
- if (debug) { console.log(`< [${method}]`, response.config.url, response.status); }
50
- },
51
- error: (name, error: AxiosError) => {
52
- if (debug) { console.log(`< [${method}]`, error.config.url, error.status); }
53
- },
54
- });
55
-
56
35
  type CoreOptions = {
57
- root: string,
58
- env?: Properties,
59
- debug?: boolean,
60
- locale?: string,
36
+ env?: Record<string, any>,
61
37
  sources?: SourceFunctions,
38
+ localization: FromDirectory | FromCatalog,
62
39
  };
63
40
 
64
41
  /* eslint-disable @typescript-eslint/unbound-method */
65
42
  export const core = (options: CoreOptions): Core => ({
66
- axios: accessProxy(axios, {
67
- get: logProxy(`axios.get`, axios.get, axiosLogs(`GET`, options.debug)),
68
- post: logProxy(`axios.post`, axios.post, axiosLogs(`POST`, options.debug)),
69
- put: logProxy(`axios.put`, axios.put, axiosLogs(`PUT`, options.debug)),
70
- patch: logProxy(`axios.patch`, axios.patch, axiosLogs(`PATCH`, options.debug)),
71
- delete: logProxy(`axios.delete`, axios.delete, axiosLogs(`DELETE`, options.debug)),
72
- }),
43
+ axios: axios,
73
44
  moment: moment,
74
45
  lodash: _,
75
- env: options.env || new Properties(options.root, `.env`),
76
- t: i18n(path.join(options.root, `/locales`), { locale: options.locale }).t,
46
+ env: options.env || {},
77
47
  joi: Joi,
78
48
  sources: options.sources || FALLBACK_SOURCES,
49
+ t: i18n(options.localization).t,
79
50
  });
@@ -6,7 +6,6 @@ import { Credentials, SessionCredentials, StaticCredentials } from '../entities/
6
6
  import { Action, ConnectableEvent, Trigger } from '../entities/events';
7
7
  import { Field, SelectFieldChoice } from './fields';
8
8
  import { Provider } from '../entities/provider';
9
- import { validateDirectory } from './utils';
10
9
 
11
10
  type LocalizedField = {
12
11
  label: string,
@@ -22,25 +21,84 @@ type TOptions = { fallback?: boolean };
22
21
 
23
22
  const DEFAULT_LOCALE = `en`;
24
23
 
25
- export const i18n = (directory: string, options: { locale?: string } = {}) => {
24
+ export const validateDirectory = (directory: string): boolean => {
26
25
  try {
27
- if (!validateDirectory(directory)) {
28
- throw new Error(`Invalid directory: ${directory}`);
26
+ if (!fs.existsSync(directory)) {
27
+ console.error(`Directory does not exist: ${directory}`);
28
+ return false;
29
29
  }
30
+ const files = fs.readdirSync(directory);
31
+ if (!files.some((file) => file.endsWith(`.json`))) {
32
+ console.error(`No valid localization files found in directory: ${directory}`);
33
+ return false;
34
+ }
35
+ return true;
36
+ } catch (error) {
37
+ console.error(`Error validating directory: ${error.message}`);
38
+ return false;
39
+ }
40
+ };
30
41
 
31
- const availableLocales = fs.readdirSync(directory).map((file) => path.basename(file, path.extname(file)));
32
- const requestedLocale = options.locale || DEFAULT_LOCALE;
42
+ type Common = {
43
+ locale?: string,
44
+ };
45
+
46
+ export type FromCatalog = Common & {
47
+ catalog: Record<string, any>,
48
+ };
49
+
50
+ export type FromDirectory = Common & {
51
+ directory: string,
52
+ locale?: string,
53
+ write?: boolean,
54
+ };
55
+
56
+ export type T = (code: string, opts?: TOptions) => string;
33
57
 
34
- const activeLocale = availableLocales.includes(requestedLocale) ? requestedLocale : DEFAULT_LOCALE;
58
+ export const i18n = (options: FromCatalog | FromDirectory): { t: T, i18n: I18n.I18n } => {
59
+ try {
35
60
 
36
- const i18nInstance: I18n.I18n = new I18n.I18n({
37
- locales: availableLocales,
38
- directory: directory,
61
+ const common: I18n.ConfigurationOptions = {
39
62
  defaultLocale: DEFAULT_LOCALE,
40
63
  objectNotation: true,
41
64
  autoReload: false,
42
65
  missingKeyFn: () => ``,
43
- });
66
+ };
67
+
68
+ const requestedLocale = options.locale || DEFAULT_LOCALE;
69
+
70
+ let i18nInstance: I18n.I18n;
71
+ let activeLocale: string;
72
+ if (`directory` in options) {
73
+ // validate directory
74
+ if (!validateDirectory(options.directory)) { throw new Error(`Invalid directory: ${options.directory}`); }
75
+
76
+ // get active locale
77
+ const availableLocales = fs.readdirSync(options.directory).map((file) => path.basename(file, path.extname(file)));
78
+ activeLocale = availableLocales.includes(requestedLocale) ? requestedLocale : DEFAULT_LOCALE;
79
+
80
+ // create i18n instance
81
+ i18nInstance = new I18n.I18n({
82
+ ...common,
83
+ locales: availableLocales,
84
+ directory: options.directory,
85
+ updateFiles: options.write,
86
+ syncFiles: true,
87
+ });
88
+ } else if (`catalog` in options) {
89
+ // get active locale
90
+ activeLocale = requestedLocale;
91
+
92
+ // create i18n instance
93
+ i18nInstance = new I18n.I18n({
94
+ ...common,
95
+ locales: [requestedLocale],
96
+ staticCatalog: {
97
+ [requestedLocale]: options.catalog,
98
+ },
99
+ });
100
+ } else { throw new Error(`Invalid options provided for i18n function`); }
101
+
44
102
  i18nInstance.setLocale(activeLocale);
45
103
 
46
104
  return {
@@ -50,7 +108,8 @@ export const i18n = (directory: string, options: { locale?: string } = {}) => {
50
108
  response = i18nInstance.__({ phrase: code, locale: DEFAULT_LOCALE });
51
109
  }
52
110
  return response;
53
- }, i18n: i18nInstance,
111
+ },
112
+ i18n: i18nInstance,
54
113
  };
55
114
  } catch (e) { throw e; }
56
115
  };
@@ -6,7 +6,7 @@ import path from "path";
6
6
  export class Properties {
7
7
 
8
8
  private readonly filepath: string;
9
- private readonly properties: DotenvParseOutput;
9
+ public readonly properties: DotenvParseOutput;
10
10
 
11
11
  private readonly getFile = (flag: `w+` | `r`): DotenvParseOutput => {
12
12
  try {
@@ -1 +0,0 @@
1
- export declare const validateDirectory: (directory: string) => boolean;
@@ -1,26 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.validateDirectory = void 0;
7
- const fs_1 = __importDefault(require("fs"));
8
- const validateDirectory = (directory) => {
9
- try {
10
- if (!fs_1.default.existsSync(directory)) {
11
- console.error(`Directory does not exist: ${directory}`);
12
- return false;
13
- }
14
- const files = fs_1.default.readdirSync(directory);
15
- if (!files.some((file) => file.endsWith(`.json`))) {
16
- console.error(`No valid localization files found in directory: ${directory}`);
17
- return false;
18
- }
19
- return true;
20
- }
21
- catch (error) {
22
- console.error(`Error validating directory: ${error.message}`);
23
- return false;
24
- }
25
- };
26
- exports.validateDirectory = validateDirectory;
@@ -1,18 +0,0 @@
1
- import fs from 'fs';
2
- export const validateDirectory = (directory: string): boolean => {
3
- try {
4
- if (!fs.existsSync(directory)) {
5
- console.error(`Directory does not exist: ${directory}`);
6
- return false;
7
- }
8
- const files = fs.readdirSync(directory);
9
- if (!files.some((file) => file.endsWith(`.json`))) {
10
- console.error(`No valid localization files found in directory: ${directory}`);
11
- return false;
12
- }
13
- return true;
14
- } catch (error) {
15
- console.error(`Error validating directory: ${error.message}`);
16
- return false;
17
- }
18
- };