@open-tender/utils 0.1.88 → 0.1.90

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.
@@ -1,4 +1,4 @@
1
- import { CheckoutError, CheckoutErrorParams, RequestError } from '@open-tender/types';
1
+ import { CheckoutError, CheckoutErrorMessages, CheckoutErrorParams, RequestError } from '@open-tender/types';
2
2
  export declare const handleRespError: (err: RequestError) => {
3
3
  title: string;
4
4
  detail: string | undefined;
@@ -11,7 +11,7 @@ export declare const handleRespError: (err: RequestError) => {
11
11
  export declare const processError: (msg: string | null) => string;
12
12
  export declare const handleFormErrors: (params: Record<string, string | null>) => Record<string, string>;
13
13
  export declare const makeFormErrors: (error: RequestError) => Record<string, string>;
14
- export declare const handleCheckoutError: (error: CheckoutError, asMessages?: boolean) => Record<string, unknown>;
14
+ export declare const handleCheckoutError: (error: CheckoutError) => CheckoutErrorMessages;
15
15
  export declare const makeCheckError: (params: CheckoutErrorParams) => CheckoutError;
16
16
  export declare const errMessages: {
17
17
  unknown: string;
@@ -75,39 +75,40 @@ const makeFormErrors = (error) => {
75
75
  return errors;
76
76
  };
77
77
  exports.makeFormErrors = makeFormErrors;
78
- const handleCheckoutError = (error, asMessages = true) => {
78
+ const handleCheckoutError = (error) => {
79
79
  const { detail, params, message } = error || {};
80
80
  if (!params || (0, helpers_1.isEmpty)(params))
81
- return { form: detail || message };
81
+ return {
82
+ form: detail || message || 'Unknown error. Please contact support.'
83
+ };
82
84
  const errObj = Object.entries(params).reduce((obj, error) => {
83
85
  const [key, value] = error;
84
- const err = asMessages ? value.message : value;
86
+ const errMsg = value.message;
85
87
  const [, entity, index, field] = key.split('.');
86
- const existingEntity = obj[entity] || {};
88
+ const existingEntity = Object.hasOwn(obj, entity) ? obj[entity] : null;
87
89
  if (!index) {
88
- return Object.assign(Object.assign({}, obj), { [entity]: err });
90
+ return Object.assign(Object.assign({}, obj), { [entity]: errMsg });
89
91
  }
90
92
  else if (!field) {
91
- const newEntity = Object.assign(Object.assign({}, existingEntity), { [index]: err });
92
- return Object.assign(Object.assign({}, obj), { [entity]: newEntity });
93
+ if (existingEntity && typeof existingEntity !== 'string') {
94
+ return Object.assign(Object.assign({}, obj), { [entity]: Object.assign(Object.assign({}, existingEntity), { [index]: errMsg }) });
95
+ }
96
+ else {
97
+ return Object.assign(Object.assign({}, obj), { [entity]: { [index]: errMsg } });
98
+ }
93
99
  }
94
100
  else {
95
- if (existingEntity) {
96
- const existingIndex = existingEntity[index] || {};
97
- const newIndex = Object.assign(Object.assign({}, existingIndex), { [field]: err });
101
+ if (existingEntity && typeof existingEntity !== 'string') {
102
+ const existingIndex = existingEntity[index] || null;
103
+ const newIndex = existingIndex && typeof existingIndex !== 'string'
104
+ ? Object.assign(Object.assign({}, existingIndex), { [field]: errMsg }) : { [field]: errMsg };
98
105
  const newEntity = Object.assign(Object.assign({}, existingEntity), { [index]: newIndex });
99
106
  return Object.assign(Object.assign({}, obj), { [entity]: newEntity });
100
107
  }
101
108
  else {
102
- return Object.assign(Object.assign({}, obj), { [entity]: { [index]: { [field]: err } } });
109
+ return Object.assign(Object.assign({}, obj), { [entity]: { [index]: { [field]: errMsg } } });
103
110
  }
104
111
  }
105
- // const newEntity = obj[entity]
106
- // ? { ...obj[entity], [index]: err }
107
- // : index
108
- // ? { [index]: err }
109
- // : err
110
- // return { ...obj, [entity]: newEntity }
111
112
  }, { form: 'There are one or more errors below' });
112
113
  return errObj;
113
114
  };
@@ -1,4 +1,4 @@
1
- import { CheckoutError, CheckoutErrorParams, RequestError } from '@open-tender/types';
1
+ import { CheckoutError, CheckoutErrorMessages, CheckoutErrorParams, RequestError } from '@open-tender/types';
2
2
  export declare const handleRespError: (err: RequestError) => {
3
3
  title: string;
4
4
  detail: string | undefined;
@@ -11,7 +11,7 @@ export declare const handleRespError: (err: RequestError) => {
11
11
  export declare const processError: (msg: string | null) => string;
12
12
  export declare const handleFormErrors: (params: Record<string, string | null>) => Record<string, string>;
13
13
  export declare const makeFormErrors: (error: RequestError) => Record<string, string>;
14
- export declare const handleCheckoutError: (error: CheckoutError, asMessages?: boolean) => Record<string, unknown>;
14
+ export declare const handleCheckoutError: (error: CheckoutError) => CheckoutErrorMessages;
15
15
  export declare const makeCheckError: (params: CheckoutErrorParams) => CheckoutError;
16
16
  export declare const errMessages: {
17
17
  unknown: string;
@@ -68,39 +68,40 @@ export const makeFormErrors = (error) => {
68
68
  }
69
69
  return errors;
70
70
  };
71
- export const handleCheckoutError = (error, asMessages = true) => {
71
+ export const handleCheckoutError = (error) => {
72
72
  const { detail, params, message } = error || {};
73
73
  if (!params || isEmpty(params))
74
- return { form: detail || message };
74
+ return {
75
+ form: detail || message || 'Unknown error. Please contact support.'
76
+ };
75
77
  const errObj = Object.entries(params).reduce((obj, error) => {
76
78
  const [key, value] = error;
77
- const err = asMessages ? value.message : value;
79
+ const errMsg = value.message;
78
80
  const [, entity, index, field] = key.split('.');
79
- const existingEntity = obj[entity] || {};
81
+ const existingEntity = Object.hasOwn(obj, entity) ? obj[entity] : null;
80
82
  if (!index) {
81
- return Object.assign(Object.assign({}, obj), { [entity]: err });
83
+ return Object.assign(Object.assign({}, obj), { [entity]: errMsg });
82
84
  }
83
85
  else if (!field) {
84
- const newEntity = Object.assign(Object.assign({}, existingEntity), { [index]: err });
85
- return Object.assign(Object.assign({}, obj), { [entity]: newEntity });
86
+ if (existingEntity && typeof existingEntity !== 'string') {
87
+ return Object.assign(Object.assign({}, obj), { [entity]: Object.assign(Object.assign({}, existingEntity), { [index]: errMsg }) });
88
+ }
89
+ else {
90
+ return Object.assign(Object.assign({}, obj), { [entity]: { [index]: errMsg } });
91
+ }
86
92
  }
87
93
  else {
88
- if (existingEntity) {
89
- const existingIndex = existingEntity[index] || {};
90
- const newIndex = Object.assign(Object.assign({}, existingIndex), { [field]: err });
94
+ if (existingEntity && typeof existingEntity !== 'string') {
95
+ const existingIndex = existingEntity[index] || null;
96
+ const newIndex = existingIndex && typeof existingIndex !== 'string'
97
+ ? Object.assign(Object.assign({}, existingIndex), { [field]: errMsg }) : { [field]: errMsg };
91
98
  const newEntity = Object.assign(Object.assign({}, existingEntity), { [index]: newIndex });
92
99
  return Object.assign(Object.assign({}, obj), { [entity]: newEntity });
93
100
  }
94
101
  else {
95
- return Object.assign(Object.assign({}, obj), { [entity]: { [index]: { [field]: err } } });
102
+ return Object.assign(Object.assign({}, obj), { [entity]: { [index]: { [field]: errMsg } } });
96
103
  }
97
104
  }
98
- // const newEntity = obj[entity]
99
- // ? { ...obj[entity], [index]: err }
100
- // : index
101
- // ? { [index]: err }
102
- // : err
103
- // return { ...obj, [entity]: newEntity }
104
105
  }, { form: 'There are one or more errors below' });
105
106
  return errObj;
106
107
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.1.88",
3
+ "version": "0.1.90",
4
4
  "description": "A library of utils for use with Open Tender applications that utilize our cloud-based Order API.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",