Skip to content

Commit b2e78b8

Browse files
committed
chore: linting fixes
1 parent d9b6f58 commit b2e78b8

File tree

8 files changed

+56
-38
lines changed

8 files changed

+56
-38
lines changed

src/common/dom.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export const processElements = <ET extends cashElementIds | cardElementIds | ach
103103
if (typeof result === 'string') {
104104
error = result;
105105
} else {
106-
//@ts-expect-error
106+
//@ts-expect-error - result is not a string
107107
processed[key].push(result);
108108
}
109109
}

src/components/pay-theory-hosted-field-transactional/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class PayTheoryHostedFieldTransactional extends PayTheoryHostedField {
171171
type: `pt-static:connection_token` | `pt-static:reset_host`,
172172
): Promise<ErrorResponse | ReadyResponse> {
173173
try {
174-
const ptToken = await common.fetchPtToken(this._apiKey!, this._session);
174+
const ptToken = await common.fetchPtToken(this._apiKey ?? '', this._session);
175175
if (ptToken) {
176176
this._challengeOptions = ptToken.challengeOptions;
177177
const transactingIFrame = document.getElementById(this._transactingIFrameId) as
@@ -270,7 +270,9 @@ class PayTheoryHostedFieldTransactional extends PayTheoryHostedField {
270270
type: string;
271271
body: { fee: number; payment_type: string };
272272
field: ElementTypes;
273-
}) => this.handleFeeMessage(message),
273+
}) => {
274+
this.handleFeeMessage(message);
275+
},
274276
);
275277

276278
this._removeFeeCalcReconnect = common.handleHostedFieldMessage(

src/field-set/actions.ts

+26-11
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,12 @@ export const transact = async (
126126
updateElementFromAction(parsedResponse, transactingElement);
127127
sendObserverMessage(parsedResponse);
128128
return parsedResponse;
129-
} catch (e) {
130-
const errorString: string = e?.error || e?.message || e;
131-
return common.handleError(errorString);
129+
} catch (e: unknown) {
130+
if (e instanceof Error) {
131+
return common.handleError(e.message);
132+
} else {
133+
return common.handleError('An unknown error occurred');
134+
}
132135
}
133136
}
134137
} else {
@@ -155,8 +158,12 @@ export const confirm = async (): Promise<
155158
updateElementFromAction(parsedResult, transactingElement);
156159
sendObserverMessage(parsedResult, true);
157160
return parsedResult;
158-
} catch (e) {
159-
return common.handleError(e?.error || e?.message || e);
161+
} catch (e: unknown) {
162+
if (e instanceof Error) {
163+
return common.handleError(e.message);
164+
} else {
165+
return common.handleError('An unknown error occurred');
166+
}
160167
}
161168
} else {
162169
return common.handleTypedError(
@@ -171,8 +178,12 @@ export const cancel = async (): Promise<true | ErrorResponse> => {
171178
if (transactingElement) {
172179
try {
173180
return await transactingElement.cancel();
174-
} catch (e) {
175-
return common.handleError(e?.error || e?.message || e);
181+
} catch (e: unknown) {
182+
if (e instanceof Error) {
183+
return common.handleError(e.message);
184+
} else {
185+
return common.handleError('An unknown error occurred');
186+
}
176187
}
177188
}
178189
};
@@ -192,9 +203,9 @@ export const tokenizePaymentMethod = async (
192203
ErrorType.ACTION_IN_PROGRESS,
193204
'this function has already been called',
194205
);
195-
} else if (transactingElement.valid == false) {
206+
} else if (!transactingElement.valid) {
196207
return common.handleTypedError(ErrorType.NOT_VALID, 'The transaction element is invalid');
197-
} else if (transactingElement.ready == false) {
208+
} else if (!transactingElement.ready) {
198209
return common.handleTypedError(ErrorType.NOT_READY, 'The transaction element is not ready');
199210
} else {
200211
const reconnectError = await reconnectIfDisconnected(transactingElement);
@@ -227,8 +238,12 @@ export const tokenizePaymentMethod = async (
227238
updateElementFromAction(parsedResult, transactingElement);
228239
sendObserverMessage(parsedResult);
229240
return parsedResult as TokenizedPaymentMethodResponse | ErrorResponse;
230-
} catch (e) {
231-
return common.handleError(e?.error || e?.message || e);
241+
} catch (e: unknown) {
242+
if (e instanceof Error) {
243+
return common.handleError(e.message);
244+
} else {
245+
return common.handleError('An unknown error occurred');
246+
}
232247
}
233248
}
234249
} else {

src/field-set/handler.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@ import payTheoryHostedFieldTransactional, {
88
IncomingFieldState,
99
} from '../components/pay-theory-hosted-field-transactional';
1010

11-
type relayMessage = {
11+
interface relayMessage {
1212
type: string;
1313
value: string | object;
1414
element: ElementTypes | 'card-autofill' | 'address-autofill';
15-
};
15+
}
1616

1717
//relays state to the hosted fields to tokenize the instrument
1818
const verifyRelay = (fields: string[], message: relayMessage) => {
1919
fields.forEach(field => {
20-
if (document.getElementsByName(field)[0]) {
21-
common.postMessageToHostedField(field, message);
22-
}
20+
common.postMessageToHostedField(field, message);
2321
});
2422
};
2523

@@ -81,7 +79,9 @@ export const hostedErrorHandler = (message: {
8179
const transactingElement = element[0] as payTheoryHostedFieldTransactional;
8280
if (transactingElement.initialized) {
8381
transactingElement.initialized = false;
84-
transactingElement.resetToken();
82+
transactingElement.resetToken().catch(() => {
83+
common.handleError('Error resetting token');
84+
});
8585
}
8686

8787
// If the session has expired, set the ready state to false

src/field-set/payment-button.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export default async (inputParams: PayTheoryButtonInput) => {
112112
// Remove the error listener because we added it to the button iFrame and do not want it to be called twice
113113
removeErrorListener();
114114
removeHostedErrorListener();
115-
if (data && data.sessionId) {
115+
if (data.sessionId) {
116116
const buttonElement = getCheckoutButton();
117117
buttonElement.session = data.sessionId;
118118
}

src/field-set/payment-fields-v2.ts

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable @typescript-eslint/no-unused-vars */
1+
/* eslint-disable no-unused-vars */
22
/* eslint no-console: ["error", { allow: ["warn", "error"] }] */
33
import common from '../common';
44
import * as valid from './validation';
@@ -40,7 +40,7 @@ interface ProcessedObject {
4040
cash: ProcessedObjectValue<cashElementIds>;
4141
}
4242

43-
const mountProcessedElements = async (props: {
43+
const mountProcessedElements = (props: {
4444
processed: ProcessedObject;
4545
apiKey: string;
4646
styles: StyleObject;
@@ -51,7 +51,7 @@ const mountProcessedElements = async (props: {
5151
feeMode: typeof MERCHANT_FEE | typeof SERVICE_FEE | undefined;
5252
amount: number | undefined;
5353
port: MessagePort;
54-
}) => {
54+
}): ErrorResponse | null => {
5555
const {
5656
processed,
5757
apiKey,
@@ -106,9 +106,13 @@ const mountProcessedElements = async (props: {
106106
});
107107
}
108108
}
109+
return null;
109110
};
110111

111-
const initializeFields = async (props: PayTheoryPaymentFieldsInput, port: MessagePort) => {
112+
const initializeFields = (
113+
props: PayTheoryPaymentFieldsInput,
114+
port: MessagePort,
115+
): ErrorResponse | null => {
112116
const {
113117
apiKey,
114118
styles = common.defaultStyles,
@@ -200,7 +204,7 @@ const initializeFields = async (props: PayTheoryPaymentFieldsInput, port: Messag
200204
},
201205
};
202206
// Mount the elements to the DOM
203-
const result = await mountProcessedElements({
207+
return mountProcessedElements({
204208
processed,
205209
apiKey,
206210
styles,
@@ -212,10 +216,6 @@ const initializeFields = async (props: PayTheoryPaymentFieldsInput, port: Messag
212216
amount,
213217
port,
214218
});
215-
216-
if (result) {
217-
return result;
218-
}
219219
};
220220

221221
const payTheoryFields = async (inputParams: PayTheoryPaymentFieldsInput) =>
@@ -232,12 +232,11 @@ const payTheoryFields = async (inputParams: PayTheoryPaymentFieldsInput) =>
232232
};
233233

234234
if (document.readyState === 'complete') {
235-
initializeFields(inputParams, channel.port2).then(result => {
236-
if (result) resolve(result);
237-
});
235+
const result = initializeFields(inputParams, channel.port2);
236+
if (result) resolve(result);
238237
} else {
239238
document.addEventListener('DOMContentLoaded', async () => {
240-
const result = await initializeFields(inputParams, channel.port2);
239+
const result = initializeFields(inputParams, channel.port2);
241240
if (result) resolve(result);
242241
});
243242
}

src/field-set/payment-fields.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default async (
88
apiKey: string,
99
legacy: undefined, // this used to be client id, left in place to preserve backwards compatibility
1010
styles: StyleObject = common.defaultStyles,
11-
sessionMetadata: { [key: string | number]: string | number | boolean } = {},
11+
sessionMetadata: Record<string | number, string | number | boolean> = {},
1212
fee_mode: typeof MERCHANT_FEE | typeof SERVICE_FEE = common.defaultFeeMode,
1313
) => {
1414
const mount = async (
@@ -30,10 +30,12 @@ export default async (
3030
});
3131
};
3232

33-
const initTransaction = (amount: number, payorInfo: PayorInfo, confirmation: boolean = false) => {
33+
const initTransaction = (amount: number, payorInfo: PayorInfo, confirmation = false) => {
3434
console.warn('initTransaction is deprecated. Please use transact instead.');
3535
//Passing in the session metadata from create because those used to be the only metadata that were passed in
36-
transact({ amount, payorInfo, confirmation, feeMode: fee_mode });
36+
transact({ amount, payorInfo, confirmation, feeMode: fee_mode }).catch(error => {
37+
console.error(error);
38+
});
3739
};
3840

3941
return common.generateReturn(mount, initTransaction);

src/field-set/validation.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ const nullifyEmptyStrings = (params: object) => {
282282

283283
const formatPayorObject = (payorInfo: PayorInfo): PayorInfo => {
284284
// Make a deep copy of the payorInfo object
285-
let payorCopy: PayorInfo = JSON.parse(JSON.stringify(payorInfo));
285+
let payorCopy = structuredClone(payorInfo);
286286
// Nullify unknown empty strings
287287
payorCopy = nullifyEmptyStrings(payorCopy);
288288
// Strip out unknown non-numeric characters from the phone number

0 commit comments

Comments
 (0)