Skip to content

Commit

Permalink
feat: add webhooks apis
Browse files Browse the repository at this point in the history
  • Loading branch information
tianyingchun committed Jun 6, 2024
1 parent 9ad04bb commit 8585943
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/dry-cougars-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hyperse/paypal-node-sdk": patch
---

Add `webhooks` apis
4 changes: 4 additions & 0 deletions src/webhook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ export {
VerifyWebhookSignature,
type VerifyWebhookSignatureBody,
} from './verifyWebhookSignature.js';
export { WebhookCreateRequest } from './webhookCreateRequest.js';
export { WebhookDeleteRequest } from './webhookDeleteRequest.js';
export { WebhookListRequest } from './webhookListRequest.js';
export { WebhookUpdateRequest } from './webhookUpdateRequest.js';
44 changes: 44 additions & 0 deletions src/webhook/webhookCreateRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import querystring from 'querystring';
import { HttpRequestBase } from '../core/HttpRequestBase.js';
import { type BaseWebhookHeaders } from '../types/type-webhook.js';

type WebhookCreateRequestBody = {
/**
* The URL that is configured to listen on localhost for incoming POST notification messages that contain event information.
* https://example.com/example_webhook
*/
url: string;
/**
* An array of events to which to subscribe your webhook. To subscribe to all events, including events as they are added, specify the asterisk wild card.
* To replace the event_types array, specify the asterisk wild card. To list all supported events: @link https://developer.paypal.com/docs/api/webhooks/v1/#event-type_list
* [ { "name": "PAYMENT.AUTHORIZATION.CREATED" }, { "name": "PAYMENT.AUTHORIZATION.VOIDED" } ]
*/
event_types: Array<{ name: string }>;
};

/**
* Subscribes your webhook listener to events.
* @see {@link https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_post}
*/
export class WebhookCreateRequest extends HttpRequestBase<
BaseWebhookHeaders,
WebhookCreateRequestBody
> {
constructor(webhookId: string) {
super();
this.verb = 'POST';
this.path = '/v1/notifications/webhooks/{webhook_id}?';
this.path = this.path.replace(
'{webhook_id}',
querystring.escape(webhookId)
);
this.headers = {
'Content-Type': 'application/json',
};
}

requestBody(createRequest: WebhookCreateRequestBody) {
this.body = createRequest;
return this;
}
}
23 changes: 23 additions & 0 deletions src/webhook/webhookDeleteRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import querystring from 'querystring';
import { HttpRequestBase } from '../core/HttpRequestBase.js';
import { type BaseWebhookHeaders } from '../types/type-webhook.js';

/**
* Delete webhook, Deletes a webhook, by ID.
* @see {@link https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_delete}
*
*/
export class WebhookDeleteRequest extends HttpRequestBase<BaseWebhookHeaders> {
constructor(webhookId: string) {
super();
this.verb = 'DELETE';
this.path = '/v1/notifications/webhooks/{webhook_id}?';
this.path = this.path.replace(
'{webhook_id}',
querystring.escape(webhookId)
);
this.headers = {
'Content-Type': 'application/json',
};
}
}
23 changes: 23 additions & 0 deletions src/webhook/webhookListRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import querystring from 'querystring';
import { HttpRequestBase } from '../core/HttpRequestBase.js';
import { type BaseWebhookHeaders } from '../types/type-webhook.js';

/**
* Lists webhooks for an app.
* @see {@link https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_list}
*
*/
export class WebhookListRequest extends HttpRequestBase<BaseWebhookHeaders> {
constructor(anchorType: 'APPLICATION' | 'ACCOUNT' = 'APPLICATION') {
super();
this.verb = 'GET';
this.path = '/v1/notifications/webhooks?anchor_type={anchor_type}';
this.path = this.path.replace(
'{anchor_type}',
querystring.escape(anchorType)
);
this.headers = {
'Content-Type': 'application/json',
};
}
}
53 changes: 53 additions & 0 deletions src/webhook/webhookUpdateRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import querystring from 'querystring';
import { HttpRequestBase } from '../core/HttpRequestBase.js';
import { type BaseWebhookHeaders } from '../types/type-webhook.js';

type WebhookUpdateRequestBody = {
/**
* The operation.
*/
op: 'add' | 'replace' | 'remove' | 'move' | 'copy';
/**
* The JSON Pointer to the target document location at which to complete the operation.
* @example `/event_types`
*/
path: string;
/**
* The value to apply. The remove operation does not require a value.
* [ { "name": "PAYMENT.SALE.REFUNDED" } ]
*/
value: string | Array<{ name: string }>;
/**
* The JSON Pointer to the target document location from which to move the value. Required for the move operation.
*/
from: string;
};

/**
* Updates a webhook to replace webhook fields with new values. Supports only the replace operation.
* Pass a json_patch object with replace operation and path, which is /url for a URL or /event_types for events. The value is either the URL or a list of events.
* @see {@link https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_update}
*
*/
export class WebhookUpdateRequest extends HttpRequestBase<
BaseWebhookHeaders,
WebhookUpdateRequestBody
> {
constructor(webhookId: string) {
super();
this.verb = 'POST';
this.path = '/v1/notifications/webhooks/{webhook_id}?';
this.path = this.path.replace(
'{webhook_id}',
querystring.escape(webhookId)
);
this.headers = {
'Content-Type': 'application/json',
};
}

requestBody(updateRequest: WebhookUpdateRequestBody) {
this.body = updateRequest;
return this;
}
}
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"extends": "@hyperse/eslint-config-hyperse/tsconfig.base.json",
"compilerOptions": {
"baseUrl": "./",
"baseUrl": ".",
"rootDir": ".",
"outDir": "dist",
"noImplicitAny": false,
"allowJs": false,
"noEmit": false,
Expand Down

0 comments on commit 8585943

Please sign in to comment.