@beseif-solutions/prow-core 0.3.1 → 0.3.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.
package/dist/core.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { AxiosStatic } from "axios";
1
+ import { AxiosInstance } from "axios";
2
2
  import Joi from "joi";
3
- import { LoDashStatic } from "lodash";
3
+ import lodash from "lodash";
4
4
  import moment from "moment-timezone";
5
5
  import { FromCatalog, FromDirectory, T } from "./minified-utils/locales";
6
6
  type SyncOrAsync<T> = T | Promise<T>;
@@ -15,9 +15,9 @@ export type SourceFunctions = {
15
15
  unregister: (source: string, sandbox: boolean, id: string) => SyncOrAsync<void>;
16
16
  };
17
17
  export type Core = {
18
- axios: AxiosStatic;
18
+ axios: AxiosInstance;
19
19
  moment: typeof moment;
20
- lodash: LoDashStatic;
20
+ lodash: lodash.LoDashStatic;
21
21
  env: Record<string, any>;
22
22
  joi: Joi.Root;
23
23
  sources?: SourceFunctions;
@@ -28,5 +28,5 @@ type CoreOptions = {
28
28
  sources?: SourceFunctions;
29
29
  localization: FromDirectory | FromCatalog;
30
30
  };
31
- export declare const core: (options: CoreOptions) => Core;
31
+ export declare const core: (options: CoreOptions) => Promise<Core>;
32
32
  export {};
package/dist/core.js CHANGED
@@ -20,8 +20,10 @@ const FALLBACK_SOURCES = {
20
20
  throw new Error(`Sources not available: valid implementation not provided`);
21
21
  },
22
22
  };
23
- const core = (options) => ({
24
- axios: axios_1.default,
23
+ const core = async (options) => ({
24
+ axios: axios_1.default.create({
25
+ adapter: axios_1.default.getAdapter(`http`),
26
+ }),
25
27
  moment: moment_timezone_1.default,
26
28
  lodash: lodash_1.default,
27
29
  env: options.env || {},
@@ -38,6 +38,7 @@ type StringInputField<Extend = Record<never, never>> = {
38
38
  max?: number;
39
39
  regexp?: string;
40
40
  pattern?: `email` | `uri`;
41
+ allow_empty?: boolean;
41
42
  } & (SelectField<string, Extend> | CustomDisplayTextField | IDETextField | {});
42
43
  type NumberInputField<Extend = Record<never, never>> = {
43
44
  type: `number`;
@@ -79,6 +79,7 @@ const stringInputFieldSchema = () => joi_1.default.object({
79
79
  max: joi_1.default.number(),
80
80
  regexp: joi_1.default.string(),
81
81
  pattern: joi_1.default.string().valid(`email`, `uri`),
82
+ allow_empty: joi_1.default.bool().default(false),
82
83
  })
83
84
  .concat(joi_1.default.object({
84
85
  format: joi_1.default.string().valid(`select`, `textarea`, `rich-text`, `markdown`, `code`),
@@ -164,6 +165,7 @@ const fileSchema = () => joi_1.default.object({
164
165
  const fileReplacer = (field) => ({
165
166
  key: field.key,
166
167
  type: `dict`,
168
+ unknown: true,
167
169
  items: [
168
170
  {
169
171
  key: `name`,
@@ -256,10 +258,10 @@ const getSimpleFieldSchema = (field) => {
256
258
  if (field.length) {
257
259
  typeSchema = typeSchema.length(field.length);
258
260
  }
259
- if (field.min) {
261
+ if (field.min > 0) {
260
262
  typeSchema = typeSchema.min(field.min);
261
263
  }
262
- else {
264
+ else if (field.allow_empty) {
263
265
  typeSchema = typeSchema.allow(``);
264
266
  }
265
267
  if (field.max) {
@@ -454,7 +456,9 @@ const recursive_schema = async (field, data, options) => {
454
456
  : newField.key;
455
457
  const value = lodash_1.default.get(data, relativeKey, newField.default);
456
458
  const resolved = context_1.default.resolve(newField, value, options.context);
457
- lodash_1.default.set(data, relativeKey, resolved);
459
+ if (typeof resolved !== `undefined`) {
460
+ lodash_1.default.set(data, relativeKey, resolved);
461
+ }
458
462
  let calculatedSchema;
459
463
  if (newField.as_array) {
460
464
  const noArrayField = lodash_1.default.omit(newField, [`as_array`, `array_min`, `array_max`, `array_length`]);
@@ -474,12 +478,10 @@ const recursive_schema = async (field, data, options) => {
474
478
  let genericItemSchema;
475
479
  if (noArrayField.type === `dict` && noArrayField.items) {
476
480
  const items = [];
477
- if (lodash_1.default.isObject(resolved)) {
478
- for (const item of noArrayField.items) {
479
- const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
480
- if (recursive) {
481
- items.push({ key: item.key, schema: recursive.schema, field: recursive.field });
482
- }
481
+ for (const item of noArrayField.items) {
482
+ const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: `${relativeKey}[0]`, context: options.context });
483
+ if (recursive) {
484
+ items.push({ key: item.key, schema: recursive.schema, field: recursive.field });
483
485
  }
484
486
  }
485
487
  genericItemSchema = getObjectFieldSchema(noArrayField, items);
@@ -493,12 +495,10 @@ const recursive_schema = async (field, data, options) => {
493
495
  }
494
496
  else if (newField.type === `dict` && newField.items) {
495
497
  const items = [];
496
- if (lodash_1.default.isObject(resolved)) {
497
- for (const item of newField.items) {
498
- const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
499
- if (recursive) {
500
- items.push({ key: item.key, schema: recursive.schema, field: recursive.field });
501
- }
498
+ for (const item of newField.items) {
499
+ const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
500
+ if (recursive) {
501
+ items.push({ key: item.key, schema: recursive.schema, field: recursive.field });
502
502
  }
503
503
  }
504
504
  calculatedSchema = getObjectFieldSchema(newField, items);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beseif-solutions/prow-core",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "Prow core functionalities and classes",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/core.ts CHANGED
@@ -1,6 +1,6 @@
1
- import axios, { AxiosStatic } from "axios";
1
+ import axios, { AxiosInstance } from "axios";
2
2
  import Joi from "joi";
3
- import _, { LoDashStatic } from "lodash";
3
+ import lodash from "lodash";
4
4
  import moment from "moment-timezone";
5
5
  import { FromCatalog, FromDirectory, i18n, T } from "./minified-utils/locales";
6
6
 
@@ -25,9 +25,9 @@ const FALLBACK_SOURCES: SourceFunctions = {
25
25
  };
26
26
 
27
27
  export type Core = {
28
- axios: AxiosStatic,
28
+ axios: AxiosInstance,
29
29
  moment: typeof moment,
30
- lodash: LoDashStatic,
30
+ lodash: lodash.LoDashStatic,
31
31
  env: Record<string, any>,
32
32
  joi: Joi.Root,
33
33
  sources?: SourceFunctions,
@@ -41,10 +41,13 @@ type CoreOptions = {
41
41
  };
42
42
 
43
43
  /* eslint-disable @typescript-eslint/unbound-method */
44
- export const core = (options: CoreOptions): Core => ({
45
- axios: axios,
44
+ export const core = async (options: CoreOptions): Promise<Core> => ({
45
+ // explicit adapter for NodeJS -> avoid DOM errors
46
+ axios: axios.create({
47
+ adapter: axios.getAdapter(`http`),
48
+ }),
46
49
  moment: moment,
47
- lodash: _,
50
+ lodash: lodash,
48
51
  env: options.env || {},
49
52
  joi: Joi,
50
53
  sources: options.sources || FALLBACK_SOURCES,
@@ -59,6 +59,7 @@ type StringInputField<Extend = Record<never, never>> = {
59
59
  max?: number,
60
60
  regexp?: string,
61
61
  pattern?: `email` | `uri`,
62
+ allow_empty?: boolean,
62
63
  } & (
63
64
  | SelectField<string, Extend>
64
65
  | CustomDisplayTextField
@@ -275,6 +276,7 @@ const stringInputFieldSchema = () =>
275
276
  max: Joi.number(),
276
277
  regexp: Joi.string(),
277
278
  pattern: Joi.string().valid(`email`, `uri`),
279
+ allow_empty: Joi.bool().default(false),
278
280
  })
279
281
  .concat(
280
282
  Joi.object({
@@ -375,6 +377,7 @@ const fileSchema = () => Joi.object({
375
377
  const fileReplacer = (field: Common & FileInputField): Field => ({
376
378
  key: field.key,
377
379
  type: `dict`,
380
+ unknown: true,
378
381
  items: [
379
382
  {
380
383
  key: `name`,
@@ -474,9 +477,9 @@ const getSimpleFieldSchema = (field: Field) => {
474
477
  case `string`:
475
478
  typeSchema = Joi.string();
476
479
  if (field.length) { typeSchema = (typeSchema as StringSchema).length(field.length); }
477
- if (field.min) {
480
+ if (field.min > 0) {
478
481
  typeSchema = (typeSchema as StringSchema).min(field.min);
479
- } else {
482
+ } else if (field.allow_empty) {
480
483
  typeSchema = typeSchema.allow(``);
481
484
  }
482
485
  if (field.max) { typeSchema = (typeSchema as StringSchema).max(field.max); }
@@ -658,9 +661,9 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
658
661
 
659
662
  const value = _.get(data, relativeKey, newField.default);
660
663
 
661
- // resolve value with context (even if it's not valid)
664
+ // resolve value with context (even if it's not valid - omit undefined)
662
665
  const resolved = context.resolve(newField, value, options.context);
663
- _.set(data, relativeKey, resolved);
666
+ if (typeof resolved !== `undefined`) { _.set(data, relativeKey, resolved); }
664
667
 
665
668
  // get schema for field with data
666
669
  let calculatedSchema: Joi.Schema;
@@ -682,11 +685,9 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
682
685
  if (noArrayField.type === `dict` && noArrayField.items) {
683
686
  const items: { key: string, schema: Joi.Schema, field: Field }[] = [];
684
687
 
685
- if (_.isObject(resolved)) {
686
- for (const item of noArrayField.items) {
687
- const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
688
- if (recursive) { items.push({ key: item.key, schema: recursive.schema, field: recursive.field }); }
689
- }
688
+ for (const item of noArrayField.items) {
689
+ const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: `${relativeKey}[0]`, context: options.context });
690
+ if (recursive) { items.push({ key: item.key, schema: recursive.schema, field: recursive.field }); }
690
691
  }
691
692
 
692
693
  genericItemSchema = getObjectFieldSchema(noArrayField, items);
@@ -700,11 +701,9 @@ const recursive_schema = async (field: Field, data: Record<string, any>, options
700
701
  } else if (newField.type === `dict` && newField.items) {
701
702
  const items: { key: string, schema: Joi.Schema, field: Field }[] = [];
702
703
 
703
- if (_.isObject(resolved)) {
704
- for (const item of newField.items) {
705
- const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
706
- if (recursive) { items.push({ key: item.key, schema: recursive.schema, field: recursive.field }); }
707
- }
704
+ for (const item of newField.items) {
705
+ const recursive = await recursive_schema(item, data, { alter: options.alter && true, relative: relativeKey, context: options.context });
706
+ if (recursive) { items.push({ key: item.key, schema: recursive.schema, field: recursive.field }); }
708
707
  }
709
708
 
710
709
  calculatedSchema = getObjectFieldSchema(newField, items);