Skip to content

Commit 5ab70fb

Browse files
authored
Merge pull request #1514 from Adyen/format-webhooks-code
Format code (adding method overloading)
2 parents d86971c + 22bf706 commit 5ab70fb

File tree

5 files changed

+115
-13
lines changed

5 files changed

+115
-13
lines changed

src/__tests__/webhooks/bankingWebhooks.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import { NegativeBalanceWarningWebhooksHandler } from "../../typings/negativeBal
1717
import { TransactionWebhooksHandler } from "../../typings/transactionWebhooks/transactionWebhooksHandler";
1818
import { BalanceWebhooksHandler } from "../../typings/balanceWebhooks/balanceWebhooksHandler";
1919
import { ReportNotificationRequest } from "../../typings/reportWebhooks/reportNotificationRequest";
20+
import { DisputeWebhooksHandler } from "../../typings/disputeWebhooks/disputeWebhooksHandler";
21+
import { DisputeNotificationRequest } from "../../typings/disputeWebhooks/disputeNotificationRequest";
22+
import { DisputeEventNotification } from "../../typings/disputeWebhooks/disputeEventNotification";
2023

2124
describe("BankingWebhooks Tests", function (): void {
2225

@@ -514,4 +517,43 @@ describe("BankingWebhooks Tests", function (): void {
514517

515518
});
516519

520+
it("should deserialize DisputeWebhooks DisputeNotificationRequest", function (): void {
521+
const json = {
522+
"type": "balancePlatform.dispute.created",
523+
"data": {
524+
"id": "DS00000000000000000001",
525+
"balancePlatform": "YOUR_BALANCE_PLATFORM",
526+
"disputedAmount": {
527+
"currency": "EUR",
528+
"value": 10000
529+
},
530+
"status": "open",
531+
"type": DisputeEventNotification.TypeEnum.NotDelivered,
532+
533+
}
534+
};
535+
const jsonString = JSON.stringify(json);
536+
const disputeWebhooksHandler = new DisputeWebhooksHandler(jsonString);
537+
const disputeNotificationRequest: DisputeNotificationRequest = disputeWebhooksHandler.getDisputeNotificationRequest();
538+
539+
expect(disputeNotificationRequest).toBeTruthy();
540+
expect(disputeNotificationRequest.type).toBe("balancePlatform.dispute.created");
541+
expect(disputeNotificationRequest.data).toBeDefined();
542+
expect(disputeNotificationRequest.data.id).toBe("DS00000000000000000001");
543+
expect(disputeNotificationRequest.data.balancePlatform).toBe("YOUR_BALANCE_PLATFORM");
544+
expect(disputeNotificationRequest.data.status).toBe("open");
545+
expect(disputeNotificationRequest.data.type).toBe(DisputeEventNotification.TypeEnum.NotDelivered);
546+
// test getGenericWebhook
547+
const genericWebhook = disputeWebhooksHandler.getGenericWebhook();
548+
expect(genericWebhook instanceof DisputeNotificationRequest).toBe(true);
549+
expect(genericWebhook.type).toEqual("balancePlatform.dispute.created");
550+
});
551+
552+
it("should throw SyntaxError when JSON is invalid", function (): void {
553+
const invalidJsonString = "{ invalid json }";
554+
expect(() => {
555+
new DisputeWebhooksHandler(invalidJsonString);
556+
}).toThrowError(SyntaxError);
557+
});
558+
517559
});

src/services/payment/paymentsApi.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ export class PaymentsApi extends Service {
4747
const endpoint = `${this.baseUrl}/authorise`;
4848
const resource = new Resource(this, endpoint);
4949

50-
const request: PaymentRequest = ObjectSerializer.serialize(paymentRequest, "PaymentRequest", "");
50+
const request: PaymentRequest = ObjectSerializer.serialize(paymentRequest, "PaymentRequest");
5151
const response = await getJsonResponse<PaymentRequest, PaymentResult>(
5252
resource,
5353
request,
5454
{ ...requestOptions, method: "POST" }
5555
);
5656

57-
return ObjectSerializer.deserialize(response, "PaymentResult", "");
57+
return ObjectSerializer.deserialize(response, "PaymentResult");
5858
}
5959

6060
/**
@@ -67,14 +67,14 @@ export class PaymentsApi extends Service {
6767
const endpoint = `${this.baseUrl}/authorise3d`;
6868
const resource = new Resource(this, endpoint);
6969

70-
const request: PaymentRequest3d = ObjectSerializer.serialize(paymentRequest3d, "PaymentRequest3d", "");
70+
const request: PaymentRequest3d = ObjectSerializer.serialize(paymentRequest3d, "PaymentRequest3d");
7171
const response = await getJsonResponse<PaymentRequest3d, PaymentResult>(
7272
resource,
7373
request,
7474
{ ...requestOptions, method: "POST" }
7575
);
7676

77-
return ObjectSerializer.deserialize(response, "PaymentResult", "");
77+
return ObjectSerializer.deserialize(response, "PaymentResult");
7878
}
7979

8080
/**
@@ -87,14 +87,14 @@ export class PaymentsApi extends Service {
8787
const endpoint = `${this.baseUrl}/authorise3ds2`;
8888
const resource = new Resource(this, endpoint);
8989

90-
const request: PaymentRequest3ds2 = ObjectSerializer.serialize(paymentRequest3ds2, "PaymentRequest3ds2", "");
90+
const request: PaymentRequest3ds2 = ObjectSerializer.serialize(paymentRequest3ds2, "PaymentRequest3ds2");
9191
const response = await getJsonResponse<PaymentRequest3ds2, PaymentResult>(
9292
resource,
9393
request,
9494
{ ...requestOptions, method: "POST" }
9595
);
9696

97-
return ObjectSerializer.deserialize(response, "PaymentResult", "");
97+
return ObjectSerializer.deserialize(response, "PaymentResult");
9898
}
9999

100100
/**
@@ -107,14 +107,14 @@ export class PaymentsApi extends Service {
107107
const endpoint = `${this.baseUrl}/getAuthenticationResult`;
108108
const resource = new Resource(this, endpoint);
109109

110-
const request: AuthenticationResultRequest = ObjectSerializer.serialize(authenticationResultRequest, "AuthenticationResultRequest", "");
110+
const request: AuthenticationResultRequest = ObjectSerializer.serialize(authenticationResultRequest, "AuthenticationResultRequest");
111111
const response = await getJsonResponse<AuthenticationResultRequest, AuthenticationResultResponse>(
112112
resource,
113113
request,
114114
{ ...requestOptions, method: "POST" }
115115
);
116116

117-
return ObjectSerializer.deserialize(response, "AuthenticationResultResponse", "");
117+
return ObjectSerializer.deserialize(response, "AuthenticationResultResponse");
118118
}
119119

120120
/**
@@ -127,14 +127,14 @@ export class PaymentsApi extends Service {
127127
const endpoint = `${this.baseUrl}/retrieve3ds2Result`;
128128
const resource = new Resource(this, endpoint);
129129

130-
const request: ThreeDS2ResultRequest = ObjectSerializer.serialize(threeDS2ResultRequest, "ThreeDS2ResultRequest", "");
130+
const request: ThreeDS2ResultRequest = ObjectSerializer.serialize(threeDS2ResultRequest, "ThreeDS2ResultRequest");
131131
const response = await getJsonResponse<ThreeDS2ResultRequest, ThreeDS2ResultResponse>(
132132
resource,
133133
request,
134134
{ ...requestOptions, method: "POST" }
135135
);
136136

137-
return ObjectSerializer.deserialize(response, "ThreeDS2ResultResponse", "");
137+
return ObjectSerializer.deserialize(response, "ThreeDS2ResultResponse");
138138
}
139139

140140
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* The version of the OpenAPI document: v1
3+
*
4+
*
5+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
6+
* https://openapi-generator.tech
7+
* Do not edit this class manually.
8+
*/
9+
10+
import { disputeWebhooks } from "..";
11+
12+
/**
13+
* Union type for all supported webhook requests.
14+
* Allows generic handling of configuration-related webhook events.
15+
*/
16+
export type GenericWebhook =
17+
| disputeWebhooks.DisputeNotificationRequest;
18+
19+
/**
20+
* Handler for processing DisputeWebhooks.
21+
*
22+
* This class provides functionality to deserialize the payload of DisputeWebhooks events.
23+
*/
24+
export class DisputeWebhooksHandler {
25+
26+
private readonly payload: Record<string, any>;
27+
28+
public constructor(jsonPayload: string) {
29+
this.payload = JSON.parse(jsonPayload);
30+
}
31+
32+
/**
33+
* This method checks the type of the webhook payload and returns the appropriate deserialized object.
34+
*
35+
* @returns A deserialized object of type GenericWebhook.
36+
* @throws Error if the type is not recognized.
37+
*/
38+
public getGenericWebhook(): GenericWebhook {
39+
40+
const type = this.payload["type"];
41+
42+
if(Object.values(disputeWebhooks.DisputeNotificationRequest.TypeEnum).includes(type)) {
43+
return this.getDisputeNotificationRequest();
44+
}
45+
46+
throw new Error("Could not parse the json payload: " + this.payload);
47+
48+
}
49+
50+
/**
51+
* Deserialize the webhook payload into a DisputeNotificationRequest
52+
*
53+
* @returns Deserialized DisputeNotificationRequest object.
54+
*/
55+
public getDisputeNotificationRequest(): disputeWebhooks.DisputeNotificationRequest {
56+
return disputeWebhooks.ObjectSerializer.deserialize(this.payload, "DisputeNotificationRequest");
57+
}
58+
59+
}

src/typings/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ export * as managementWebhooks from "./managementWebhooks/models";
3434
export * as acsWebhooks from "./acsWebhooks/models";
3535
export * as transactionWebhooks from "./transactionWebhooks/models";
3636
export * as negativeBalanceWarningWebhooks from "./negativeBalanceWarningWebhooks/models";
37-
export * as balanceWebhooks from "./balanceWebhooks/models";
37+
export * as balanceWebhooks from "./balanceWebhooks/models";
38+
export * as disputeWebhooks from "./disputeWebhooks/models";

templates-v7/typescript/api/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class {{classname}} extends Service {
4242
const resource = new Resource(this, endpoint);
4343

4444
{{#bodyParam}}
45-
const request: {{{dataType}}} = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}", "");
45+
const request: {{{dataType}}} = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
4646
{{/bodyParam}}
4747
{{#hasQueryParams}}
4848
const hasDefinedQueryParams = {{#queryParams}}{{paramName}}{{^-last}} ?? {{/-last}}{{/queryParams}};
@@ -61,7 +61,7 @@ export class {{classname}} extends Service {
6161
);
6262
{{#returnProperty}}
6363

64-
return ObjectSerializer.deserialize(response, "{{{returnType}}}", "");
64+
return ObjectSerializer.deserialize(response, "{{{returnType}}}");
6565
{{/returnProperty}}
6666
}
6767

0 commit comments

Comments
 (0)