1
1
/* eslint-disable @typescript-eslint/no-unused-vars */
2
2
/* eslint no-console: ["error", { allow: ["warn", "error"] }] */
3
- /*global navigator*/
4
3
import common from '../common' ;
5
4
import * as valid from './validation' ;
6
5
import {
@@ -24,46 +23,30 @@ import {
24
23
StyleObject ,
25
24
} from '../common/pay_theory_types' ;
26
25
27
- type ProcessedObject = {
28
- card : {
29
- elements : {
30
- transacting : processedElement < cardElementIds , PayTheoryHostedFieldTransactional > [ ] ;
31
- siblings : processedElement < cardElementIds , PayTheoryHostedField > [ ] ;
32
- } ;
33
- errorCheck : (
34
- allElements : ( PayTheoryHostedField | PayTheoryHostedFieldTransactional ) [ ] ,
35
- transacting : PayTheoryHostedFieldTransactional [ ] ,
36
- ) => string | false ;
37
- } ;
38
- ach : {
39
- elements : {
40
- transacting : processedElement < achElementIds , PayTheoryHostedFieldTransactional > [ ] ;
41
- siblings : processedElement < achElementIds , PayTheoryHostedField > [ ] ;
42
- } ;
43
- errorCheck : (
44
- allElements : ( PayTheoryHostedField | PayTheoryHostedFieldTransactional ) [ ] ,
45
- transacting : PayTheoryHostedFieldTransactional [ ] ,
46
- ) => string | false ;
26
+ interface ProcessedObjectValue < T extends cashElementIds | cardElementIds | achElementIds > {
27
+ elements : {
28
+ transacting : processedElement < T , PayTheoryHostedFieldTransactional > [ ] ;
29
+ siblings : processedElement < T , PayTheoryHostedField > [ ] ;
47
30
} ;
48
- cash : {
49
- elements : {
50
- transacting : processedElement < cashElementIds , PayTheoryHostedFieldTransactional > [ ] ;
51
- siblings : processedElement < cashElementIds , PayTheoryHostedField > [ ] ;
52
- } ;
53
- errorCheck : (
54
- allElements : ( PayTheoryHostedField | PayTheoryHostedFieldTransactional ) [ ] ,
55
- transacting : PayTheoryHostedFieldTransactional [ ] ,
56
- ) => string | false ;
57
- } ;
58
- } ;
31
+ errorCheck : (
32
+ allElements : ( PayTheoryHostedField | PayTheoryHostedFieldTransactional ) [ ] ,
33
+ transacting : PayTheoryHostedFieldTransactional [ ] ,
34
+ ) => string | false ;
35
+ }
36
+
37
+ interface ProcessedObject {
38
+ card : ProcessedObjectValue < cardElementIds > ;
39
+ ach : ProcessedObjectValue < achElementIds > ;
40
+ cash : ProcessedObjectValue < cashElementIds > ;
41
+ }
59
42
60
43
const mountProcessedElements = async ( props : {
61
44
processed : ProcessedObject ;
62
45
apiKey : string ;
63
46
styles : StyleObject ;
64
47
placeholders : PlaceholderObject ;
65
48
session : string | undefined ;
66
- metadata : { [ key : string | number ] : string | number | boolean } ;
49
+ metadata : Record < string | number , string | number | boolean > ;
67
50
removeEventListeners : ( ) => void ;
68
51
feeMode : typeof MERCHANT_FEE | typeof SERVICE_FEE | undefined ;
69
52
amount : number | undefined ;
@@ -81,15 +64,18 @@ const mountProcessedElements = async (props: {
81
64
amount,
82
65
} = props ;
83
66
for ( const value of Object . values ( processed ) ) {
84
- const transactingElements = value . elements . transacting . map ( element => element . frame ) ;
85
- const siblingsElements = value . elements . siblings . map ( element => element . frame ) ;
67
+ const typedValue = value as ProcessedObjectValue <
68
+ cashElementIds | cardElementIds | achElementIds
69
+ > ;
70
+ const transactingElements = typedValue . elements . transacting . map ( element => element . frame ) ;
71
+ const siblingsElements = typedValue . elements . siblings . map ( element => element . frame ) ;
86
72
const allElements = [ ...transactingElements , ...siblingsElements ] ;
87
73
if ( allElements . length > 0 ) {
88
- const error = value . errorCheck ( allElements , transactingElements ) ;
74
+ const error = typedValue . errorCheck ( allElements , transactingElements ) ;
89
75
if ( error ) {
90
76
return common . handleTypedError ( ErrorType . FIELD_ERROR , error ) ;
91
77
}
92
- value . elements . siblings . forEach ( sibling => {
78
+ typedValue . elements . siblings . forEach ( sibling => {
93
79
const container = document . getElementById ( String ( sibling . containerId ) ) ;
94
80
sibling . frame . styles = styles ;
95
81
sibling . frame . placeholders = placeholders ;
@@ -98,7 +84,7 @@ const mountProcessedElements = async (props: {
98
84
container . appendChild ( sibling . frame ) ;
99
85
}
100
86
} ) ;
101
- value . elements . transacting . forEach ( element => {
87
+ typedValue . elements . transacting . forEach ( element => {
102
88
const container = document . getElementById ( String ( element . containerId ) ) ;
103
89
element . frame . apiKey = apiKey ;
104
90
element . frame . styles = styles ;
@@ -108,8 +94,8 @@ const mountProcessedElements = async (props: {
108
94
element . frame . feeMode = feeMode ;
109
95
element . frame . amount = amount ;
110
96
element . frame . session = session ;
111
- const processedElementTypes = value . elements . siblings . map ( sibling => sibling . type ) ;
112
- const transactingElementType = value . elements . transacting . map (
97
+ const processedElementTypes = typedValue . elements . siblings . map ( sibling => sibling . type ) ;
98
+ const transactingElementType = typedValue . elements . transacting . map (
113
99
transacting => transacting . type ,
114
100
) ;
115
101
element . frame . processedElements = [ ...processedElementTypes , ...transactingElementType ] ;
0 commit comments