@infosoftas/s4-orders-js-sdk 0.1.21-3 → 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.
Files changed (51) hide show
  1. package/.editorconfig +8 -8
  2. package/.release-it.json +10 -10
  3. package/README.md +565 -471
  4. package/package.json +62 -58
  5. package/src/App.completion.test.tsx +738 -0
  6. package/src/App.tsx +753 -639
  7. package/src/ErrorBoundary.tsx +41 -41
  8. package/src/api/InvoiceApi.ts +23 -23
  9. package/src/api/OrdersApi.ts +74 -59
  10. package/src/api/SubscribeApi.ts +18 -18
  11. package/src/bundle.ts +4 -4
  12. package/src/components/Alert/Alert.tsx +23 -23
  13. package/src/components/Button/Button.tsx +39 -39
  14. package/src/components/EHFForm/EHFForm.tsx +253 -253
  15. package/src/components/FormFields/AddressField.tsx +55 -55
  16. package/src/components/FormFields/EmailField.tsx +53 -53
  17. package/src/components/FormFields/FormFields.helper.ts +34 -34
  18. package/src/components/FormFields/FormFieldsMapper.ts +34 -34
  19. package/src/components/FormFields/InputField.tsx +52 -52
  20. package/src/components/FormFields/NameField.tsx +52 -52
  21. package/src/components/FormFields/PhoneField.tsx +53 -53
  22. package/src/components/FormFields/TermsCheckbox.tsx +70 -70
  23. package/src/components/FormFields/ToggleFiled.tsx +52 -52
  24. package/src/components/FormFields/fieldValidators.ts +71 -71
  25. package/src/components/MainIframe/MainIframe.tsx +42 -42
  26. package/src/components/OIOForm/OIOForm.tsx +307 -307
  27. package/src/components/OrderDenialModal/OrderDenialModal.tsx +102 -102
  28. package/src/components/OrderForm/ContactDetailsSection.tsx +71 -71
  29. package/src/components/OrderForm/OrderForm.tsx +420 -420
  30. package/src/components/OrderForm/PaymentMethodContent.tsx +62 -62
  31. package/src/components/OrderForm/PaymentMethodSection.tsx +79 -79
  32. package/src/components/OrderForm/SubmitConfirmationDialog.tsx +55 -55
  33. package/src/components/OrderForm/orderFormState.ts +171 -171
  34. package/src/components/OrderForm/useSlotElement.tsx +89 -89
  35. package/src/constants/index.ts +13 -13
  36. package/src/enums/api.ts +5 -5
  37. package/src/enums/general.ts +40 -40
  38. package/src/hooks/useIframe.tsx +59 -59
  39. package/src/hooks/useMessageEvent.tsx +194 -194
  40. package/src/hooks/useOrderForm.tsx +403 -403
  41. package/src/hooks/useQueryParams.ts +5 -5
  42. package/src/index.tsx +30 -30
  43. package/src/lib/utils.ts +1 -1
  44. package/src/types/api.ts +28 -28
  45. package/src/types/general.ts +154 -136
  46. package/src/types/order.ts +91 -70
  47. package/src/utils/fetcher.ts +131 -113
  48. package/src/utils/helper.ts +78 -78
  49. package/src/utils/order.helper.ts +272 -272
  50. package/src/utils/waitForOrderCompletion.ts +125 -0
  51. package/tsconfig.json +38 -38
@@ -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
- export const getSdkApiKey = (): string => {
23
- return (
24
- sessionStorage.getItem('sdk_api_key') ||
25
- localStorage.getItem('sdk_api_key') ||
26
- ''
27
- );
28
- };
29
-
30
- export const getSdkApiUrl = (): string => {
31
- return (
32
- sessionStorage.getItem('sdk_api_url') ||
33
- localStorage.getItem('sdk_api_url') ||
34
- BASE_API
35
- );
36
- };
37
-
38
- function createHeader<T>(options: Fetcher<T>): Fetcher<T> {
39
- const accessToken = localStorage.getItem('authTokenKey') || '';
40
-
41
- const headers = new Headers({
42
- accept: 'application/json',
43
- ...(accessToken && {
44
- Authorization: `Bearer ${accessToken}`,
45
- }),
46
- ...options.headers,
47
- });
48
-
49
- if (!(options.body instanceof FormData)) {
50
- headers.append('Content-Type', 'application/json');
51
- }
52
-
53
- return { method: 'GET', ...options, headers };
54
- }
55
-
56
- async function fetcher<ResponseType, BodyType = void>(
57
- options: Fetcher<BodyType>
58
- ): Promise<ApiResponseType<ResponseType> | ResponseType> {
59
- const { url, method, headers, body, params } = createHeader(options);
60
-
61
- const apiUrl = getSdkApiUrl();
62
- const endpoint = new URL(`${apiUrl || BASE_API}${url}`);
63
-
64
- if (params) {
65
- Object.entries(params).forEach(([key, value]) => {
66
- if (Array.isArray(value)) {
67
- value.forEach((v) => endpoint.searchParams.append(key, v));
68
- return;
69
- }
70
- endpoint.searchParams.append(key, value);
71
- });
72
- }
73
-
74
- return new Promise<ApiResponseType<ResponseType> | ResponseType>(
75
- (resolve, reject) => {
76
- return fetch(endpoint.toString(), {
77
- method,
78
- headers,
79
- body: body instanceof FormData ? body : JSON.stringify(body),
80
- })
81
- .then(async (response) => {
82
- if (response.status === HttpStatusCode.UNAUTHORIZED) {
83
- localStorage.removeItem('authTokenKey');
84
- reject({ message: 'Not Authorized user!' });
85
- } else if (response.status === HttpStatusCode.FORBIDDEN) {
86
- reject({ message: 'Something Wrong with permission!' });
87
- } else if (response.status === HttpStatusCode.LIMIT) {
88
- reject({
89
- message: 'System busy please try again shortly!',
90
- });
91
- } else {
92
- if (response.ok) {
93
- response
94
- .json()
95
- .then((data) => resolve(data))
96
- .catch(() => resolve(null as ResponseType));
97
- } else {
98
- const data = await response?.json();
99
- const message =
100
- data.details || data.message || data.title;
101
- reject({ ...data, message });
102
- }
103
- }
104
- })
105
- .catch((error) => {
106
- console.log(error, 'error');
107
- reject(error?.response);
108
- });
109
- }
110
- );
111
- }
112
-
113
- export default fetcher;
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;
@@ -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
+ };