@infosoftas/s4-orders-js-sdk 0.1.21 → 0.2.0
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/.editorconfig +8 -8
- package/.release-it.json +10 -10
- package/README.md +565 -471
- package/package.json +62 -58
- package/src/App.completion.test.tsx +738 -0
- package/src/App.tsx +753 -639
- package/src/ErrorBoundary.tsx +41 -41
- package/src/api/InvoiceApi.ts +23 -23
- package/src/api/OrdersApi.ts +74 -59
- package/src/api/SubscribeApi.ts +18 -18
- package/src/bundle.ts +4 -4
- package/src/components/Alert/Alert.tsx +23 -23
- package/src/components/Button/Button.tsx +39 -39
- package/src/components/EHFForm/EHFForm.tsx +253 -253
- package/src/components/FormFields/AddressField.tsx +55 -55
- package/src/components/FormFields/EmailField.tsx +53 -53
- package/src/components/FormFields/FormFields.helper.ts +34 -34
- package/src/components/FormFields/FormFieldsMapper.ts +34 -34
- package/src/components/FormFields/InputField.tsx +52 -52
- package/src/components/FormFields/NameField.tsx +52 -52
- package/src/components/FormFields/PhoneField.tsx +53 -53
- package/src/components/FormFields/TermsCheckbox.tsx +70 -70
- package/src/components/FormFields/ToggleFiled.tsx +52 -52
- package/src/components/FormFields/fieldValidators.ts +71 -71
- package/src/components/MainIframe/MainIframe.tsx +42 -42
- package/src/components/OIOForm/OIOForm.tsx +307 -307
- package/src/components/OrderDenialModal/OrderDenialModal.tsx +102 -102
- package/src/components/OrderForm/ContactDetailsSection.tsx +71 -71
- package/src/components/OrderForm/OrderForm.tsx +420 -420
- package/src/components/OrderForm/PaymentMethodContent.tsx +62 -62
- package/src/components/OrderForm/PaymentMethodSection.tsx +79 -79
- package/src/components/OrderForm/SubmitConfirmationDialog.tsx +55 -55
- package/src/components/OrderForm/orderFormState.ts +171 -171
- package/src/components/OrderForm/useSlotElement.tsx +89 -89
- package/src/constants/index.ts +13 -13
- package/src/enums/api.ts +5 -5
- package/src/enums/general.ts +40 -40
- package/src/hooks/useIframe.tsx +59 -59
- package/src/hooks/useMessageEvent.tsx +194 -194
- package/src/hooks/useOrderForm.tsx +403 -403
- package/src/hooks/useQueryParams.ts +5 -5
- package/src/index.tsx +30 -30
- package/src/lib/utils.ts +1 -1
- package/src/types/api.ts +28 -28
- package/src/types/general.ts +154 -136
- package/src/types/order.ts +91 -70
- package/src/utils/fetcher.ts +131 -113
- package/src/utils/helper.ts +78 -78
- package/src/utils/order.helper.ts +272 -272
- package/src/utils/waitForOrderCompletion.ts +125 -0
- package/tsconfig.json +38 -38
package/src/utils/fetcher.ts
CHANGED
|
@@ -1,113 +1,131 @@
|
|
|
1
|
-
import { HttpStatusCode } from '../enums/api';
|
|
2
|
-
import type { ApiResponseType } from '../types/api';
|
|
3
|
-
import { BASE_API } from '../constants/index';
|
|
4
|
-
|
|
5
|
-
export type Method =
|
|
6
|
-
| 'GET'
|
|
7
|
-
| 'POST'
|
|
8
|
-
| 'PUT'
|
|
9
|
-
| 'DELETE'
|
|
10
|
-
| 'HEAD'
|
|
11
|
-
| 'OPTIONS'
|
|
12
|
-
| 'PATCH';
|
|
13
|
-
|
|
14
|
-
type Fetcher<T> = {
|
|
15
|
-
url: string;
|
|
16
|
-
params?: Record<string, string | string[]>;
|
|
17
|
-
method?: Method;
|
|
18
|
-
headers?: HeadersInit;
|
|
19
|
-
body?: T;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
''
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
1
|
+
import { HttpStatusCode } from '../enums/api';
|
|
2
|
+
import type { ApiResponseType } from '../types/api';
|
|
3
|
+
import { BASE_API } from '../constants/index';
|
|
4
|
+
|
|
5
|
+
export type Method =
|
|
6
|
+
| 'GET'
|
|
7
|
+
| 'POST'
|
|
8
|
+
| 'PUT'
|
|
9
|
+
| 'DELETE'
|
|
10
|
+
| 'HEAD'
|
|
11
|
+
| 'OPTIONS'
|
|
12
|
+
| 'PATCH';
|
|
13
|
+
|
|
14
|
+
type Fetcher<T> = {
|
|
15
|
+
url: string;
|
|
16
|
+
params?: Record<string, string | string[]>;
|
|
17
|
+
method?: Method;
|
|
18
|
+
headers?: HeadersInit;
|
|
19
|
+
body?: T;
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
cache?: RequestCache;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const getSdkApiKey = (): string => {
|
|
25
|
+
return (
|
|
26
|
+
sessionStorage.getItem('sdk_api_key') ||
|
|
27
|
+
localStorage.getItem('sdk_api_key') ||
|
|
28
|
+
''
|
|
29
|
+
);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const getSdkApiUrl = (): string => {
|
|
33
|
+
return (
|
|
34
|
+
sessionStorage.getItem('sdk_api_url') ||
|
|
35
|
+
localStorage.getItem('sdk_api_url') ||
|
|
36
|
+
BASE_API
|
|
37
|
+
);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
function createHeader<T>(options: Fetcher<T>): Fetcher<T> {
|
|
41
|
+
const accessToken = localStorage.getItem('authTokenKey') || '';
|
|
42
|
+
|
|
43
|
+
const headers = new Headers({
|
|
44
|
+
accept: 'application/json',
|
|
45
|
+
...(accessToken && {
|
|
46
|
+
Authorization: `Bearer ${accessToken}`,
|
|
47
|
+
}),
|
|
48
|
+
...options.headers,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
if (!(options.body instanceof FormData)) {
|
|
52
|
+
headers.append('Content-Type', 'application/json');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return { method: 'GET', ...options, headers };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function fetcher<ResponseType, BodyType = void>(
|
|
59
|
+
options: Fetcher<BodyType>
|
|
60
|
+
): Promise<ApiResponseType<ResponseType> | ResponseType> {
|
|
61
|
+
const { url, method, headers, body, params, signal, cache } =
|
|
62
|
+
createHeader(options);
|
|
63
|
+
|
|
64
|
+
const apiUrl = getSdkApiUrl();
|
|
65
|
+
const endpoint = new URL(`${apiUrl || BASE_API}${url}`);
|
|
66
|
+
|
|
67
|
+
if (params) {
|
|
68
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
69
|
+
if (Array.isArray(value)) {
|
|
70
|
+
value.forEach((v) => endpoint.searchParams.append(key, v));
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
endpoint.searchParams.append(key, value);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return new Promise<ApiResponseType<ResponseType> | ResponseType>(
|
|
78
|
+
(resolve, reject) => {
|
|
79
|
+
return fetch(endpoint.toString(), {
|
|
80
|
+
method,
|
|
81
|
+
headers,
|
|
82
|
+
signal,
|
|
83
|
+
cache,
|
|
84
|
+
body: body instanceof FormData ? body : JSON.stringify(body),
|
|
85
|
+
})
|
|
86
|
+
.then(async (response) => {
|
|
87
|
+
if (response.status === HttpStatusCode.UNAUTHORIZED) {
|
|
88
|
+
localStorage.removeItem('authTokenKey');
|
|
89
|
+
reject({
|
|
90
|
+
status: response.status,
|
|
91
|
+
message: 'Not Authorized user!',
|
|
92
|
+
});
|
|
93
|
+
} else if (response.status === HttpStatusCode.FORBIDDEN) {
|
|
94
|
+
reject({
|
|
95
|
+
status: response.status,
|
|
96
|
+
message: 'Something Wrong with permission!',
|
|
97
|
+
});
|
|
98
|
+
} else if (response.status === HttpStatusCode.LIMIT) {
|
|
99
|
+
reject({
|
|
100
|
+
status: response.status,
|
|
101
|
+
retryAfter: response.headers.get('Retry-After'),
|
|
102
|
+
message: 'System busy please try again shortly!',
|
|
103
|
+
});
|
|
104
|
+
} else {
|
|
105
|
+
if (response.ok) {
|
|
106
|
+
response
|
|
107
|
+
.json()
|
|
108
|
+
.then((data) => resolve(data))
|
|
109
|
+
.catch(() => resolve(null as ResponseType));
|
|
110
|
+
} else {
|
|
111
|
+
const data =
|
|
112
|
+
(await response.json().catch(() => null)) || {};
|
|
113
|
+
const message =
|
|
114
|
+
data.details || data.message || data.title;
|
|
115
|
+
reject({
|
|
116
|
+
...data,
|
|
117
|
+
status: response.status,
|
|
118
|
+
message,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
})
|
|
123
|
+
.catch((error) => {
|
|
124
|
+
console.log(error, 'error');
|
|
125
|
+
reject(error?.response || error);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export default fetcher;
|
package/src/utils/helper.ts
CHANGED
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
import { WRONG_MSG } from '../constants/index';
|
|
2
|
-
import type { ErrorsMsg } from '../types/general';
|
|
3
|
-
|
|
4
|
-
export const prepareErrorMessage = (
|
|
5
|
-
error: Error,
|
|
6
|
-
defaultMsg?: string,
|
|
7
|
-
translations?: ErrorMessages
|
|
8
|
-
) => {
|
|
9
|
-
if (typeof error === 'string') {
|
|
10
|
-
try {
|
|
11
|
-
const parsedError = JSON.parse(error);
|
|
12
|
-
return (parsedError as { message: string }).message;
|
|
13
|
-
} catch (e) {
|
|
14
|
-
return translateError(error, translations);
|
|
15
|
-
}
|
|
16
|
-
} else {
|
|
17
|
-
const serverErrorMsg = (error as { message: string })
|
|
18
|
-
?.message as string;
|
|
19
|
-
return (
|
|
20
|
-
translateError(serverErrorMsg, translations) ||
|
|
21
|
-
defaultMsg ||
|
|
22
|
-
WRONG_MSG
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export const prepareErrorsArrayMessage = (
|
|
28
|
-
errors: ErrorsMsg | null,
|
|
29
|
-
translations?: ErrorMessages
|
|
30
|
-
) => {
|
|
31
|
-
const msg: string[] = [];
|
|
32
|
-
|
|
33
|
-
if (errors) {
|
|
34
|
-
Object.entries(errors).forEach(([_, values]) => {
|
|
35
|
-
if (values?.length > 0) {
|
|
36
|
-
values.forEach((i) => {
|
|
37
|
-
msg.push(translateError(i, translations));
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return msg;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export function uuidv4() {
|
|
47
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
|
|
48
|
-
/[xy]/g,
|
|
49
|
-
function (c) {
|
|
50
|
-
const r = (Math.random() * 16) | 0;
|
|
51
|
-
const v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
52
|
-
return v.toString(16);
|
|
53
|
-
}
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export type ErrorMessages = {
|
|
58
|
-
errorValidationTitleMsg: string;
|
|
59
|
-
errorValidationBlockingOffersMsg: string;
|
|
60
|
-
errorValidationDenialOrderBlockingMsg: string;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const translateError = (errorMsg: string, translations?: ErrorMessages) => {
|
|
64
|
-
if (!translations) {
|
|
65
|
-
return errorMsg;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
switch (errorMsg) {
|
|
69
|
-
case 'One or more validation errors occurred.':
|
|
70
|
-
return translations?.errorValidationTitleMsg;
|
|
71
|
-
case 'The order/subscription will not be created because the subscriber has a denial order blocking offers.':
|
|
72
|
-
return translations?.errorValidationBlockingOffersMsg;
|
|
73
|
-
case 'The order/subscription will not be created because the subscriber has a denial order blocking all.':
|
|
74
|
-
return translations?.errorValidationDenialOrderBlockingMsg;
|
|
75
|
-
default:
|
|
76
|
-
return errorMsg;
|
|
77
|
-
}
|
|
78
|
-
};
|
|
1
|
+
import { WRONG_MSG } from '../constants/index';
|
|
2
|
+
import type { ErrorsMsg } from '../types/general';
|
|
3
|
+
|
|
4
|
+
export const prepareErrorMessage = (
|
|
5
|
+
error: Error,
|
|
6
|
+
defaultMsg?: string,
|
|
7
|
+
translations?: ErrorMessages
|
|
8
|
+
) => {
|
|
9
|
+
if (typeof error === 'string') {
|
|
10
|
+
try {
|
|
11
|
+
const parsedError = JSON.parse(error);
|
|
12
|
+
return (parsedError as { message: string }).message;
|
|
13
|
+
} catch (e) {
|
|
14
|
+
return translateError(error, translations);
|
|
15
|
+
}
|
|
16
|
+
} else {
|
|
17
|
+
const serverErrorMsg = (error as { message: string })
|
|
18
|
+
?.message as string;
|
|
19
|
+
return (
|
|
20
|
+
translateError(serverErrorMsg, translations) ||
|
|
21
|
+
defaultMsg ||
|
|
22
|
+
WRONG_MSG
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const prepareErrorsArrayMessage = (
|
|
28
|
+
errors: ErrorsMsg | null,
|
|
29
|
+
translations?: ErrorMessages
|
|
30
|
+
) => {
|
|
31
|
+
const msg: string[] = [];
|
|
32
|
+
|
|
33
|
+
if (errors) {
|
|
34
|
+
Object.entries(errors).forEach(([_, values]) => {
|
|
35
|
+
if (values?.length > 0) {
|
|
36
|
+
values.forEach((i) => {
|
|
37
|
+
msg.push(translateError(i, translations));
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return msg;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export function uuidv4() {
|
|
47
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
|
|
48
|
+
/[xy]/g,
|
|
49
|
+
function (c) {
|
|
50
|
+
const r = (Math.random() * 16) | 0;
|
|
51
|
+
const v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
52
|
+
return v.toString(16);
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type ErrorMessages = {
|
|
58
|
+
errorValidationTitleMsg: string;
|
|
59
|
+
errorValidationBlockingOffersMsg: string;
|
|
60
|
+
errorValidationDenialOrderBlockingMsg: string;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const translateError = (errorMsg: string, translations?: ErrorMessages) => {
|
|
64
|
+
if (!translations) {
|
|
65
|
+
return errorMsg;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
switch (errorMsg) {
|
|
69
|
+
case 'One or more validation errors occurred.':
|
|
70
|
+
return translations?.errorValidationTitleMsg;
|
|
71
|
+
case 'The order/subscription will not be created because the subscriber has a denial order blocking offers.':
|
|
72
|
+
return translations?.errorValidationBlockingOffersMsg;
|
|
73
|
+
case 'The order/subscription will not be created because the subscriber has a denial order blocking all.':
|
|
74
|
+
return translations?.errorValidationDenialOrderBlockingMsg;
|
|
75
|
+
default:
|
|
76
|
+
return errorMsg;
|
|
77
|
+
}
|
|
78
|
+
};
|