@sankhyalabs/core 1.0.42 → 1.0.43

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 (135) hide show
  1. package/.eslintignore +1 -1
  2. package/.eslintrc.cjs +34 -34
  3. package/README.md +54 -54
  4. package/dist/dataunit/DataUnit.d.ts +82 -82
  5. package/dist/dataunit/DataUnit.js +278 -278
  6. package/dist/dataunit/metadata/DataType.d.ts +9 -9
  7. package/dist/dataunit/metadata/DataType.js +36 -36
  8. package/dist/dataunit/metadata/UnitMetadata.d.ts +71 -71
  9. package/dist/dataunit/metadata/UnitMetadata.js +31 -31
  10. package/dist/dataunit/state/HistReducer.d.ts +10 -10
  11. package/dist/dataunit/state/HistReducer.js +27 -27
  12. package/dist/dataunit/state/StateManager.d.ts +57 -57
  13. package/dist/dataunit/state/StateManager.js +96 -96
  14. package/dist/dataunit/state/action/DataUnitAction.d.ts +28 -28
  15. package/dist/dataunit/state/action/DataUnitAction.js +33 -33
  16. package/dist/dataunit/state/slice/AddedRecordsSlice.d.ts +11 -11
  17. package/dist/dataunit/state/slice/AddedRecordsSlice.js +25 -25
  18. package/dist/dataunit/state/slice/ChangesSlice.d.ts +12 -12
  19. package/dist/dataunit/state/slice/ChangesSlice.js +72 -72
  20. package/dist/dataunit/state/slice/CurrentRecordsSlice.d.ts +11 -11
  21. package/dist/dataunit/state/slice/CurrentRecordsSlice.js +45 -45
  22. package/dist/dataunit/state/slice/RecordsSlice.d.ts +10 -10
  23. package/dist/dataunit/state/slice/RecordsSlice.js +43 -43
  24. package/dist/dataunit/state/slice/RemovedRecordsSlice.d.ts +9 -9
  25. package/dist/dataunit/state/slice/RemovedRecordsSlice.js +24 -24
  26. package/dist/dataunit/state/slice/SelectionSlice.d.ts +11 -11
  27. package/dist/dataunit/state/slice/SelectionSlice.js +111 -111
  28. package/dist/dataunit/state/slice/UnitMetadataSlice.d.ts +11 -11
  29. package/dist/dataunit/state/slice/UnitMetadataSlice.js +20 -20
  30. package/dist/http/AuthorizedServiceCaller.d.ts +11 -11
  31. package/dist/http/AuthorizedServiceCaller.js +53 -53
  32. package/dist/http/HttpProvider.d.ts +25 -25
  33. package/dist/http/HttpProvider.js +73 -73
  34. package/dist/http/RequestMetadata.d.ts +30 -30
  35. package/dist/http/RequestMetadata.js +24 -24
  36. package/dist/http/SkwHttpProvider.d.ts +9 -9
  37. package/dist/http/SkwHttpProvider.js +66 -66
  38. package/dist/index.d.ts +16 -16
  39. package/dist/index.js +17 -17
  40. package/dist/ui/FloatingManager.d.ts +24 -24
  41. package/dist/ui/FloatingManager.js +122 -122
  42. package/dist/utils/ApplicationContext.d.ts +5 -5
  43. package/dist/utils/ApplicationContext.js +16 -16
  44. package/dist/utils/CriteriaModel.d.ts +109 -109
  45. package/dist/utils/CriteriaModel.js +173 -173
  46. package/dist/utils/CriteriaParameter.d.ts +69 -69
  47. package/dist/utils/CriteriaParameter.js +82 -82
  48. package/dist/utils/DateUtils.d.ts +5 -5
  49. package/dist/utils/DateUtils.js +42 -42
  50. package/dist/utils/MaskFormatter.d.ts +126 -126
  51. package/dist/utils/MaskFormatter.js +275 -275
  52. package/dist/utils/NumberUtils.d.ts +53 -53
  53. package/dist/utils/NumberUtils.js +141 -141
  54. package/dist/utils/StringUtils.d.ts +18 -18
  55. package/dist/utils/StringUtils.js +53 -53
  56. package/dist/utils/TimeFormatter.d.ts +33 -33
  57. package/dist/utils/TimeFormatter.js +97 -97
  58. package/jest.config.ts +16 -16
  59. package/mock/http/XMLHttpRequest-mock.js +25 -25
  60. package/package.json +37 -37
  61. package/src/dataunit/DataUnit.ts +356 -356
  62. package/src/dataunit/metadata/DataType.ts +37 -37
  63. package/src/dataunit/metadata/UnitMetadata.ts +82 -82
  64. package/src/dataunit/state/HistReducer.ts +33 -33
  65. package/src/dataunit/state/StateManager.ts +141 -141
  66. package/src/dataunit/state/action/DataUnitAction.ts +50 -50
  67. package/src/dataunit/state/slice/AddedRecordsSlice.ts +32 -32
  68. package/src/dataunit/state/slice/ChangesSlice.ts +90 -90
  69. package/src/dataunit/state/slice/CurrentRecordsSlice.ts +53 -53
  70. package/src/dataunit/state/slice/RecordsSlice.ts +56 -56
  71. package/src/dataunit/state/slice/RemovedRecordsSlice.ts +29 -29
  72. package/src/dataunit/state/slice/SelectionSlice.ts +130 -130
  73. package/src/dataunit/state/slice/UnitMetadataSlice.ts +29 -29
  74. package/src/http/AuthorizedServiceCaller.ts +58 -58
  75. package/src/http/HttpProvider.ts +93 -93
  76. package/src/http/RequestMetadata.ts +41 -41
  77. package/src/http/SkwHttpProvider.ts +77 -77
  78. package/src/index.ts +44 -44
  79. package/src/ui/FloatingManager.ts +165 -165
  80. package/src/utils/ApplicationContext.ts +18 -18
  81. package/src/utils/CriteriaModel.ts +207 -207
  82. package/src/utils/CriteriaParameter.ts +107 -107
  83. package/src/utils/DateUtils.ts +52 -52
  84. package/src/utils/MaskFormatter.ts +322 -322
  85. package/src/utils/NumberUtils.ts +174 -174
  86. package/src/utils/StringUtils.ts +60 -60
  87. package/src/utils/TimeFormatter.ts +97 -97
  88. package/test/http/HttpProvider.spec.ts +34 -34
  89. package/test/http/SkwHttpProvider.ts.spec.ts +33 -33
  90. package/test/util/CriteriaModel.spec.ts +231 -231
  91. package/test/util/CriteriaParameter.spec.ts +51 -51
  92. package/test/util/MaskFormatter.spec.ts +138 -138
  93. package/test/util/NumberUtils.spec.ts +156 -156
  94. package/test/util/StringUtils.spec.ts +43 -43
  95. package/test/util/TimeFormatter.spec.ts +25 -25
  96. package/tsconfig.json +15 -15
  97. package/dist/application/Application.d.ts +0 -8
  98. package/dist/application/Application.js +0 -19
  99. package/dist/application/Application.js.map +0 -1
  100. package/dist/http/data-fetcher/HttpFetcher.d.ts +0 -14
  101. package/dist/http/data-fetcher/HttpFetcher.js +0 -164
  102. package/dist/http/data-fetcher/HttpFetcher.js.map +0 -1
  103. package/dist/http/data-fetcher/default/DefaultGraphQL.d.ts +0 -3
  104. package/dist/http/data-fetcher/default/DefaultGraphQL.js +0 -18
  105. package/dist/http/data-fetcher/default/DefaultGraphQL.js.map +0 -1
  106. package/dist/http/data-fetcher/default/application-config-fetcher.d.ts +0 -6
  107. package/dist/http/data-fetcher/default/application-config-fetcher.js +0 -21
  108. package/dist/http/data-fetcher/default/application-config-fetcher.js.map +0 -1
  109. package/dist/http/data-fetcher/default/data-unit-graphql.d.ts +0 -3
  110. package/dist/http/data-fetcher/default/data-unit-graphql.js +0 -18
  111. package/dist/http/data-fetcher/default/data-unit-graphql.js.map +0 -1
  112. package/dist/http/data-fetcher/default/dataunit-executor.d.ts +0 -10
  113. package/dist/http/data-fetcher/default/dataunit-executor.js +0 -56
  114. package/dist/http/data-fetcher/default/dataunit-executor.js.map +0 -1
  115. package/dist/http/data-fetcher/default/dataunit-fetcher.d.ts +0 -9
  116. package/dist/http/data-fetcher/default/dataunit-fetcher.js +0 -69
  117. package/dist/http/data-fetcher/default/dataunit-fetcher.js.map +0 -1
  118. package/dist/http/data-fetcher/default/form-config-fetcher.d.ts +0 -2
  119. package/dist/http/data-fetcher/default/form-config-fetcher.js +0 -3
  120. package/dist/http/data-fetcher/default/form-config-fetcher.js.map +0 -1
  121. package/dist/http/data-fetcher/default/grid-config-fetcher.d.ts +0 -2
  122. package/dist/http/data-fetcher/default/grid-config-fetcher.js +0 -3
  123. package/dist/http/data-fetcher/default/grid-config-fetcher.js.map +0 -1
  124. package/dist/http/data-fetcher/default/index.d.ts +0 -0
  125. package/dist/http/data-fetcher/default/index.js +0 -2
  126. package/dist/http/data-fetcher/default/index.js.map +0 -1
  127. package/dist/http/data-fetcher/default/sankhya-graphql.d.ts +0 -3
  128. package/dist/http/data-fetcher/default/sankhya-graphql.js +0 -8
  129. package/dist/http/data-fetcher/default/sankhya-graphql.js.map +0 -1
  130. package/dist/http/data-fetcher/graphql/DefaultGraphQl.d.ts +0 -3
  131. package/dist/http/data-fetcher/graphql/DefaultGraphQl.js +0 -18
  132. package/dist/http/data-fetcher/graphql/DefaultGraphQl.js.map +0 -1
  133. package/dist/http/data-fetcher/state/LoadStateManager.d.ts +0 -23
  134. package/dist/http/data-fetcher/state/LoadStateManager.js +0 -78
  135. package/dist/http/data-fetcher/state/LoadStateManager.js.map +0 -1
@@ -1,131 +1,131 @@
1
- import { ActionReducer, StateAction } from "../StateManager.js";
2
- import { Action } from "../action/DataUnitAction.js";
3
- import StateManager from "../StateManager.js";
4
- import { getCurrentRecords } from "./CurrentRecordsSlice.js";
5
- import { Record, SavedRecord } from "../../DataUnit.js";
6
-
7
- class SelectionReducerImpl implements ActionReducer {
8
-
9
- public sliceName: string = "hist::selection";
10
-
11
- public reduce(stateManager: StateManager, currentState: Array<string>, action: StateAction): Array<string>|undefined {
12
-
13
- switch (action.type) {
14
-
15
- case Action.RECORDS_ADDED:
16
- case Action.RECORDS_COPIED:
17
- return action.payload.map((r: Record)=>r.__record__id__);
18
- case Action.DATA_SAVED:
19
- return updateSavedIds(stateManager, action.payload.records);
20
- case Action.RECORDS_REMOVED:
21
- const removed: Array<string> = action.payload.records;
22
- if (currentState && removed) {
23
- return currentState.filter(recordId => !removed.includes(recordId))
24
- }
25
- return currentState;
26
-
27
- case Action.NEXT_SELECTED:
28
- case Action.PREVIOUS_SELECTED:
29
-
30
- const currentRecords = getCurrentRecords(stateManager);
31
- if (currentRecords && currentRecords.size > 0) {
32
- let index: number;
33
- if (!currentState || currentState.length === 0) {
34
- index = action.type === Action.PREVIOUS_SELECTED ? 0 : Math.min(1, currentRecords.size);
35
- } else {
36
- index = getItemIndex(currentState[0], currentRecords) + (action.type === Action.PREVIOUS_SELECTED ? -1 : 1);
37
- }
38
- if (index < currentRecords.size && index >= 0) {
39
- return [Array.from(currentRecords.values())[index].__record__id__];
40
- }
41
- }
42
-
43
- return undefined;
44
-
45
- case Action.SELECTION_CHANGED:
46
- const { type, selection: selectionSource } = action.payload;
47
-
48
- if (selectionSource && type === "index") {
49
- const currentRecords = getCurrentRecords(stateManager);
50
- if (currentRecords) {
51
- const records = Array.from(currentRecords.values());
52
- const selectionById: Array<string> = [];
53
- selectionSource.forEach((i: number) => {
54
- if (i > 0 && i < currentRecords.size) {
55
- selectionById.push(records[i].__record__id__);
56
- }
57
- });
58
- return selectionById;
59
- }
60
- }
61
-
62
- return selectionSource;
63
- }
64
-
65
- return currentState;
66
- }
67
- }
68
-
69
- export const SelectionReducer = new SelectionReducerImpl();
70
-
71
- export const getSelection = (stateManager: StateManager): Array<string> => {
72
- let selection: Array<string> = stateManager.select(SelectionReducer.sliceName, (state: Array<string>) => state);
73
- const currentRecords = Array.from((getCurrentRecords(stateManager)||new Map()).keys());
74
-
75
- if(selection){
76
- selection = selection.filter( id => currentRecords.includes(id));
77
- }
78
-
79
- if (!selection || selection.length === 0) {
80
- if (currentRecords && currentRecords.length > 0) {
81
- return [currentRecords[0]];
82
- }
83
- }
84
-
85
- return selection;
86
- };
87
-
88
- export const hasNext = (stateManager: StateManager): boolean => {
89
- const records = getCurrentRecords(stateManager);
90
- if (records) {
91
- const selection = stateManager.select(SelectionReducer.sliceName, (state: Array<string>) => state);
92
- if (!selection || selection.length === 0) {
93
- return records.size > 0;
94
- }
95
- return records.size > (getItemIndex(selection[0], records) + 1);
96
- }
97
- return false;
98
- };
99
-
100
- export const hasPrevious = (stateManager: StateManager): boolean => {
101
- const records = getCurrentRecords(stateManager);
102
- if (records) {
103
- const selection = stateManager.select(SelectionReducer.sliceName, (state: Array<string>) => state);
104
- if (!selection || selection.length === 0) {
105
- return false;
106
- }
107
- return getItemIndex(selection[0], records) > 0;
108
- }
109
- return false;
110
- };
111
-
112
- function getItemIndex(key: string, map: Map<string, any>): number {
113
- return Array.from(map.keys()).indexOf(key);
114
- }
115
-
116
- function updateSavedIds(stateManager:StateManager, savedRecords:Array<SavedRecord>): Array<string>{
117
- const currentSelection: Array<string> = getSelection(stateManager);
118
- if(currentSelection){
119
- const newSelection: Array<string> = [];
120
- currentSelection.forEach( id => {
121
- const record = savedRecords.find(r => r.__old__id__ === id);
122
- if(record){
123
- newSelection.push(record.__record__id__);
124
- } else {
125
- newSelection.push(id);
126
- }
127
- });
128
- return newSelection;
129
- }
130
- return currentSelection;
1
+ import { ActionReducer, StateAction } from "../StateManager.js";
2
+ import { Action } from "../action/DataUnitAction.js";
3
+ import StateManager from "../StateManager.js";
4
+ import { getCurrentRecords } from "./CurrentRecordsSlice.js";
5
+ import { Record, SavedRecord } from "../../DataUnit.js";
6
+
7
+ class SelectionReducerImpl implements ActionReducer {
8
+
9
+ public sliceName: string = "hist::selection";
10
+
11
+ public reduce(stateManager: StateManager, currentState: Array<string>, action: StateAction): Array<string>|undefined {
12
+
13
+ switch (action.type) {
14
+
15
+ case Action.RECORDS_ADDED:
16
+ case Action.RECORDS_COPIED:
17
+ return action.payload.map((r: Record)=>r.__record__id__);
18
+ case Action.DATA_SAVED:
19
+ return updateSavedIds(stateManager, action.payload.records);
20
+ case Action.RECORDS_REMOVED:
21
+ const removed: Array<string> = action.payload.records;
22
+ if (currentState && removed) {
23
+ return currentState.filter(recordId => !removed.includes(recordId))
24
+ }
25
+ return currentState;
26
+
27
+ case Action.NEXT_SELECTED:
28
+ case Action.PREVIOUS_SELECTED:
29
+
30
+ const currentRecords = getCurrentRecords(stateManager);
31
+ if (currentRecords && currentRecords.size > 0) {
32
+ let index: number;
33
+ if (!currentState || currentState.length === 0) {
34
+ index = action.type === Action.PREVIOUS_SELECTED ? 0 : Math.min(1, currentRecords.size);
35
+ } else {
36
+ index = getItemIndex(currentState[0], currentRecords) + (action.type === Action.PREVIOUS_SELECTED ? -1 : 1);
37
+ }
38
+ if (index < currentRecords.size && index >= 0) {
39
+ return [Array.from(currentRecords.values())[index].__record__id__];
40
+ }
41
+ }
42
+
43
+ return undefined;
44
+
45
+ case Action.SELECTION_CHANGED:
46
+ const { type, selection: selectionSource } = action.payload;
47
+
48
+ if (selectionSource && type === "index") {
49
+ const currentRecords = getCurrentRecords(stateManager);
50
+ if (currentRecords) {
51
+ const records = Array.from(currentRecords.values());
52
+ const selectionById: Array<string> = [];
53
+ selectionSource.forEach((i: number) => {
54
+ if (i > 0 && i < currentRecords.size) {
55
+ selectionById.push(records[i].__record__id__);
56
+ }
57
+ });
58
+ return selectionById;
59
+ }
60
+ }
61
+
62
+ return selectionSource;
63
+ }
64
+
65
+ return currentState;
66
+ }
67
+ }
68
+
69
+ export const SelectionReducer = new SelectionReducerImpl();
70
+
71
+ export const getSelection = (stateManager: StateManager): Array<string> => {
72
+ let selection: Array<string> = stateManager.select(SelectionReducer.sliceName, (state: Array<string>) => state);
73
+ const currentRecords = Array.from((getCurrentRecords(stateManager)||new Map()).keys());
74
+
75
+ if(selection){
76
+ selection = selection.filter( id => currentRecords.includes(id));
77
+ }
78
+
79
+ if (!selection || selection.length === 0) {
80
+ if (currentRecords && currentRecords.length > 0) {
81
+ return [currentRecords[0]];
82
+ }
83
+ }
84
+
85
+ return selection;
86
+ };
87
+
88
+ export const hasNext = (stateManager: StateManager): boolean => {
89
+ const records = getCurrentRecords(stateManager);
90
+ if (records) {
91
+ const selection = stateManager.select(SelectionReducer.sliceName, (state: Array<string>) => state);
92
+ if (!selection || selection.length === 0) {
93
+ return records.size > 0;
94
+ }
95
+ return records.size > (getItemIndex(selection[0], records) + 1);
96
+ }
97
+ return false;
98
+ };
99
+
100
+ export const hasPrevious = (stateManager: StateManager): boolean => {
101
+ const records = getCurrentRecords(stateManager);
102
+ if (records) {
103
+ const selection = stateManager.select(SelectionReducer.sliceName, (state: Array<string>) => state);
104
+ if (!selection || selection.length === 0) {
105
+ return false;
106
+ }
107
+ return getItemIndex(selection[0], records) > 0;
108
+ }
109
+ return false;
110
+ };
111
+
112
+ function getItemIndex(key: string, map: Map<string, any>): number {
113
+ return Array.from(map.keys()).indexOf(key);
114
+ }
115
+
116
+ function updateSavedIds(stateManager:StateManager, savedRecords:Array<SavedRecord>): Array<string>{
117
+ const currentSelection: Array<string> = getSelection(stateManager);
118
+ if(currentSelection){
119
+ const newSelection: Array<string> = [];
120
+ currentSelection.forEach( id => {
121
+ const record = savedRecords.find(r => r.__old__id__ === id);
122
+ if(record){
123
+ newSelection.push(record.__record__id__);
124
+ } else {
125
+ newSelection.push(id);
126
+ }
127
+ });
128
+ return newSelection;
129
+ }
130
+ return currentSelection;
131
131
  }
@@ -1,30 +1,30 @@
1
-
2
- import { ActionReducer, StateAction } from "../StateManager.js";
3
- import { UnitMetadata, FieldDescriptor } from "../../metadata/UnitMetadata.js";
4
- import { Action } from "../action/DataUnitAction.js";
5
- import StateManager from "../StateManager.js";
6
-
7
-
8
- class UnitMetadataReducerImpl implements ActionReducer{
9
-
10
- public sliceName: string = "unitMetadata";
11
-
12
- public reduce(_stateManager:StateManager, currentState: UnitMetadata, action: StateAction): UnitMetadata {
13
- if(action.type === Action.METADATA_LOADED){
14
- return action.payload;
15
- }
16
- return currentState;
17
- }
18
-
19
- }
20
-
21
- export const UnitMetadataReducer = new UnitMetadataReducerImpl();
22
-
23
- export const getMetadata = (stateManager: StateManager): UnitMetadata => {
24
- return stateManager.select(UnitMetadataReducer.sliceName, (state: UnitMetadata) => state);
25
- };
26
-
27
- export const getField = (stateManager: StateManager, fieldName: string): FieldDescriptor|undefined => {
28
- const md: UnitMetadata = getMetadata(stateManager);
29
- return md ? md.fields.find(fmd => fmd.name === fieldName) : undefined;
1
+
2
+ import { ActionReducer, StateAction } from "../StateManager.js";
3
+ import { UnitMetadata, FieldDescriptor } from "../../metadata/UnitMetadata.js";
4
+ import { Action } from "../action/DataUnitAction.js";
5
+ import StateManager from "../StateManager.js";
6
+
7
+
8
+ class UnitMetadataReducerImpl implements ActionReducer{
9
+
10
+ public sliceName: string = "unitMetadata";
11
+
12
+ public reduce(_stateManager:StateManager, currentState: UnitMetadata, action: StateAction): UnitMetadata {
13
+ if(action.type === Action.METADATA_LOADED){
14
+ return action.payload;
15
+ }
16
+ return currentState;
17
+ }
18
+
19
+ }
20
+
21
+ export const UnitMetadataReducer = new UnitMetadataReducerImpl();
22
+
23
+ export const getMetadata = (stateManager: StateManager): UnitMetadata => {
24
+ return stateManager.select(UnitMetadataReducer.sliceName, (state: UnitMetadata) => state);
25
+ };
26
+
27
+ export const getField = (stateManager: StateManager, fieldName: string): FieldDescriptor|undefined => {
28
+ const md: UnitMetadata = getMetadata(stateManager);
29
+ return md ? md.fields.find(fmd => fmd.name === fieldName) : undefined;
30
30
  }
@@ -1,58 +1,58 @@
1
- export class AuthorizedServiceCaller {
2
-
3
- private serverURL: string = "http://192.168.1.218:8503"; //todo request deve fazer replace de ${serverURL} pelo conteúdo da variável
4
- private unauthorizedPath: string = "/";
5
-
6
- constructor(serverURL: string, unauthorizedPath: string) {
7
- this.serverURL = serverURL;
8
- this.unauthorizedPath = unauthorizedPath;
9
-
10
- }
11
-
12
- async requestService(request: AuthorizedRequest, callback?: Function) {
13
-
14
- const token = localStorage.getItem('token');
15
- const body = request?.body;
16
- const method = request?.method;
17
-
18
- let headers;
19
-
20
- if (!token) {
21
- window.location.pathname = this.unauthorizedPath;
22
- } else {
23
- headers = {
24
- 'Authorization': `${token}`,
25
- 'Content-Type': 'application/json',
26
- 'Customer-Host': 'http://localhost:8080/sankhyaw'
27
- }
28
- }
29
- const response: void | Response = await await fetch(this.serverURL,
30
- {
31
- method: method,
32
- headers: headers,
33
- body: body
34
-
35
- })
36
- .catch(e => {
37
- console.log(e);
38
- });
39
-
40
- if (response instanceof Response && response.status === 401) {
41
- localStorage.removeItem('token');
42
- window.location.pathname = this.unauthorizedPath;
43
- }
44
-
45
- if (callback && response instanceof Response) {
46
- callback(response.json().then());
47
- } else {
48
- return response;
49
- }
50
-
51
- }
52
- }
53
-
54
- export interface AuthorizedRequest {
55
- url: string,
56
- body: string,
57
- method: string
58
- }
1
+ export class AuthorizedServiceCaller {
2
+
3
+ private serverURL: string = "http://192.168.1.218:8503"; //todo request deve fazer replace de ${serverURL} pelo conteúdo da variável
4
+ private unauthorizedPath: string = "/";
5
+
6
+ constructor(serverURL: string, unauthorizedPath: string) {
7
+ this.serverURL = serverURL;
8
+ this.unauthorizedPath = unauthorizedPath;
9
+
10
+ }
11
+
12
+ async requestService(request: AuthorizedRequest, callback?: Function) {
13
+
14
+ const token = localStorage.getItem('token');
15
+ const body = request?.body;
16
+ const method = request?.method;
17
+
18
+ let headers;
19
+
20
+ if (!token) {
21
+ window.location.pathname = this.unauthorizedPath;
22
+ } else {
23
+ headers = {
24
+ 'Authorization': `${token}`,
25
+ 'Content-Type': 'application/json',
26
+ 'Customer-Host': 'http://localhost:8080/sankhyaw'
27
+ }
28
+ }
29
+ const response: void | Response = await await fetch(this.serverURL,
30
+ {
31
+ method: method,
32
+ headers: headers,
33
+ body: body
34
+
35
+ })
36
+ .catch(e => {
37
+ console.log(e);
38
+ });
39
+
40
+ if (response instanceof Response && response.status === 401) {
41
+ localStorage.removeItem('token');
42
+ window.location.pathname = this.unauthorizedPath;
43
+ }
44
+
45
+ if (callback && response instanceof Response) {
46
+ callback(response.json().then());
47
+ } else {
48
+ return response;
49
+ }
50
+
51
+ }
52
+ }
53
+
54
+ export interface AuthorizedRequest {
55
+ url: string,
56
+ body: string,
57
+ method: string
58
+ }
@@ -1,94 +1,94 @@
1
- import { RequestMetadata, Method, Header } from "./RequestMetadata.js";
2
-
3
- /**
4
- * Abstração do XMLHttpRequest. Este serviço é responsável por realizar as requisições
5
- * ao backend. Todos os métodos são estáticos.
6
- */
7
- export class HttpProvider {
8
-
9
- /**
10
- * Faz uma requisição usando o método GET do HTTP para uma URL específica.
11
- *
12
- * @param {string} url A URL que deve ser chamada.
13
- * @param {Array<Header>} headers [Opcional] Cabeçalhos HTTP.
14
- * @returns {Promise<Object>} Uma promessa de que a requisição será preenchida.
15
- */
16
- public static get(url: string, headers?: Array<Header>): Promise<Object> {
17
- return this.dispatch(new RequestMetadata(url, Method.GET, headers));
18
- }
19
-
20
- /**
21
- * Faz uma requisição usando o método POST do HTTP para uma URL específica.
22
- *
23
- * @param {string} url A URL que deve ser chamada.
24
- * @param {Object} payload Informações a serem enviadas.
25
- * @param {Array<Header>} headers [Opcional] Cabeçalhos HTTP.
26
- * @returns {Promise<Object>} Uma promessa de que a requisição será preenchida.
27
- */
28
- public static post(url: string, payload:Object, headers?: Array<Header>): Promise<any> {
29
- return this.dispatch(new RequestMetadata(url, Method.POST, headers), payload);
30
- }
31
-
32
- private static dispatch(requestMetadata: RequestMetadata, payload?: Object): Promise<any> {
33
-
34
- return new Promise<Object>((resolve, reject) => {
35
-
36
- const request = new XMLHttpRequest();
37
-
38
- request.timeout = requestMetadata.timeout;
39
-
40
- request.onerror = function handleError() {
41
- reject({
42
- status: 404,
43
- statusText: 'Not Found'
44
- });
45
- };
46
-
47
- request.ontimeout = function handleTimeout() {
48
- reject({
49
- status: 408,
50
- statusText: 'Request Timeout'
51
- });
52
- };
53
-
54
- request.onreadystatechange = () => {
55
-
56
- // While readState isn't done, there's nothing to do...
57
- if (request.readyState != 4) {
58
- return;
59
- }
60
-
61
- const status = request.status;
62
-
63
- // The request has failed so we'll handled by onerror instead
64
- if (status === 0) {
65
- return;
66
- }
67
-
68
- const response = {
69
- status,
70
- statusText: request.statusText,
71
- };
72
-
73
- // Anything out of this range is an error, so we just send the status and statusText.
74
- if (status >= 200 && status < 300) {
75
- resolve({
76
- ...response,
77
- data: request.response,
78
- dataAsString: request.responseText
79
- });
80
- } else {
81
- reject(response);
82
- }
83
-
84
- };
85
-
86
- request.open(requestMetadata.method.toString(), requestMetadata.url, true);
87
- requestMetadata.headers.map((h: Header) => request.setRequestHeader(h.name, h.value));
88
-
89
- if(payload != null){
90
- request.send(JSON.stringify(payload));
91
- }
92
- });
93
- }
1
+ import { RequestMetadata, Method, Header } from "./RequestMetadata.js";
2
+
3
+ /**
4
+ * Abstração do XMLHttpRequest. Este serviço é responsável por realizar as requisições
5
+ * ao backend. Todos os métodos são estáticos.
6
+ */
7
+ export class HttpProvider {
8
+
9
+ /**
10
+ * Faz uma requisição usando o método GET do HTTP para uma URL específica.
11
+ *
12
+ * @param {string} url A URL que deve ser chamada.
13
+ * @param {Array<Header>} headers [Opcional] Cabeçalhos HTTP.
14
+ * @returns {Promise<Object>} Uma promessa de que a requisição será preenchida.
15
+ */
16
+ public static get(url: string, headers?: Array<Header>): Promise<Object> {
17
+ return this.dispatch(new RequestMetadata(url, Method.GET, headers));
18
+ }
19
+
20
+ /**
21
+ * Faz uma requisição usando o método POST do HTTP para uma URL específica.
22
+ *
23
+ * @param {string} url A URL que deve ser chamada.
24
+ * @param {Object} payload Informações a serem enviadas.
25
+ * @param {Array<Header>} headers [Opcional] Cabeçalhos HTTP.
26
+ * @returns {Promise<Object>} Uma promessa de que a requisição será preenchida.
27
+ */
28
+ public static post(url: string, payload:Object, headers?: Array<Header>): Promise<any> {
29
+ return this.dispatch(new RequestMetadata(url, Method.POST, headers), payload);
30
+ }
31
+
32
+ private static dispatch(requestMetadata: RequestMetadata, payload?: Object): Promise<any> {
33
+
34
+ return new Promise<Object>((resolve, reject) => {
35
+
36
+ const request = new XMLHttpRequest();
37
+
38
+ request.timeout = requestMetadata.timeout;
39
+
40
+ request.onerror = function handleError() {
41
+ reject({
42
+ status: 404,
43
+ statusText: 'Not Found'
44
+ });
45
+ };
46
+
47
+ request.ontimeout = function handleTimeout() {
48
+ reject({
49
+ status: 408,
50
+ statusText: 'Request Timeout'
51
+ });
52
+ };
53
+
54
+ request.onreadystatechange = () => {
55
+
56
+ // While readState isn't done, there's nothing to do...
57
+ if (request.readyState != 4) {
58
+ return;
59
+ }
60
+
61
+ const status = request.status;
62
+
63
+ // The request has failed so we'll handled by onerror instead
64
+ if (status === 0) {
65
+ return;
66
+ }
67
+
68
+ const response = {
69
+ status,
70
+ statusText: request.statusText,
71
+ };
72
+
73
+ // Anything out of this range is an error, so we just send the status and statusText.
74
+ if (status >= 200 && status < 300) {
75
+ resolve({
76
+ ...response,
77
+ data: request.response,
78
+ dataAsString: request.responseText
79
+ });
80
+ } else {
81
+ reject(response);
82
+ }
83
+
84
+ };
85
+
86
+ request.open(requestMetadata.method.toString(), requestMetadata.url, true);
87
+ requestMetadata.headers.map((h: Header) => request.setRequestHeader(h.name, h.value));
88
+
89
+ if(payload != null){
90
+ request.send(JSON.stringify(payload));
91
+ }
92
+ });
93
+ }
94
94
  }