Skip to content

Commit db52ce1

Browse files
authored
Merge pull request #334 from razorpay/type-declaration
feat: type declaration
2 parents 9c1bc23 + 97b78c7 commit db52ce1

24 files changed

+4680
-4
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## 2.8.5 - 2023-02-22
4+
5+
feat(Typescript): add typescript definitions
6+
7+
- TypeScript definitions for all modules and functions in the SDK.
8+
- Add comments throughout the codebase to improve readability and maintainability.
9+
- Add a type declarations file (*.d.ts) to provide better type checking and editor support for consumers of the SDK.
10+
11+
Overall, this update should provide a better developer experience for anyone using the SDK, by leveraging the power of TypeScript's static type checking and providing clearer documentation and comments throughout the codebase.
12+
313
## 2.8.4 - 2022-11-21
414

515
- [#310](https://github.com/razorpay/razorpay-node/pull/310) [`3e6daf3`](https://github.com/razorpay/razorpay-node/commit/3e6daf3c555f62eb23660a54eaae756e395ea3b6) : Thanks [@ankitdas13](https://github.com/ankitdas13)! - Fixed `virtualAccount.close` function implementation

lib/razorpay.d.ts

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,113 @@
1-
declare module "razorpay";
1+
import API, { RazorpayHeaders } from './types/api'
2+
import addons from "./types/addons"
3+
import plans from "./types/plans"
4+
import items from "./types/items"
5+
import fundAccount from "./types/fundAccount"
6+
import invoices from "./types/invoices"
7+
import transfers from "./types/transfers"
8+
import settlements from './types/settlements'
9+
import orders from './types/orders'
10+
import refunds from './types/refunds'
11+
import qrCode from './types/qrCode'
12+
import virtualAccounts from './types/virtualAccounts'
13+
import payments from './types/payments'
14+
import subscriptions from './types/subscriptions'
15+
import paymentLink from './types/paymentLink'
16+
import cards from './types/cards'
17+
import { validateWebhookSignature } from "./utils/razorpay-utils"
18+
import customers from './types/customers'
19+
20+
interface IRazorpayConfig {
21+
key_id: string;
22+
key_secret?: string;
23+
headers?: RazorpayHeaders;
24+
}
25+
26+
declare class Razorpay {
27+
static VERSION: string
28+
static validateWebhookSignature: typeof validateWebhookSignature
29+
30+
constructor(config: IRazorpayConfig)
31+
api: API
32+
/**
33+
* Customers Entity
34+
* @see https://razorpay.com/docs/api/customers/
35+
*/
36+
customers: ReturnType<typeof customers>
37+
/**
38+
* Addons Entity
39+
* @see https://razorpay.com/docs/api/payments/subscriptions/#add-on
40+
*/
41+
addons: ReturnType<typeof addons>
42+
/**
43+
* Plans Entity
44+
* @see https://razorpay.com/docs/api/payments/subscriptions/#plans
45+
*/
46+
plans: ReturnType<typeof plans>
47+
/**
48+
* Orders Entity
49+
* @see https://razorpay.com/docs/api/orders
50+
*/
51+
orders: ReturnType<typeof orders>
52+
/**
53+
* Orders Entity
54+
* @see https://razorpay.com/docs/api/payments
55+
*/
56+
payments: ReturnType<typeof payments>
57+
/**
58+
* Payments Entity
59+
* @see https://razorpay.com/docs/api/payments/route/transfers
60+
*/
61+
transfers: ReturnType<typeof transfers>
62+
/**
63+
* Transfers Entity
64+
* @see https://razorpay.com/docs/api/refunds
65+
*/
66+
refunds: ReturnType<typeof refunds>
67+
/**
68+
* Cards Entity
69+
*/
70+
cards: ReturnType<typeof cards>
71+
/**
72+
* FundaAccount Entity
73+
* @see https://razorpay.com/docs/api/x/fund-accounts/
74+
*/
75+
fundAccount: ReturnType<typeof fundAccount>
76+
/**
77+
* Items Entity
78+
* @see https://razorpay.com/docs/payments/invoices/items/api/
79+
*/
80+
items: ReturnType<typeof items>
81+
/**
82+
* PaymentLinks Entity
83+
* @see https://razorpay.com/docs/payments/payment-links/apis
84+
*/
85+
paymentLink: ReturnType<typeof paymentLink>
86+
/**
87+
* Invoices Entity
88+
* @see https://razorpay.com/docs/payments/invoices/apis/
89+
*/
90+
invoices: ReturnType<typeof invoices>
91+
/**
92+
* QrCode Entity
93+
* @see https://razorpay.com/docs/payments/qr-codes/apis/
94+
*/
95+
qrCode: ReturnType<typeof qrCode>
96+
/**
97+
* Subscrptions Entity
98+
* @see https://razorpay.com/docs/api/payments/subscriptions/#subscriptions
99+
*/
100+
subscriptions: ReturnType<typeof subscriptions>
101+
/**
102+
* Settlements Entity
103+
* @see https://razorpay.com/docs/api/settlements
104+
*/
105+
settlements: ReturnType<typeof settlements>
106+
/**
107+
* VirtualAccounts Entity
108+
* @see https://razorpay.com/docs/api/payments/smart-collect/
109+
*/
110+
virtualAccounts: ReturnType<typeof virtualAccounts>
111+
}
112+
113+
export = Razorpay

lib/types/addons.d.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import API, { INormalizeError, RazorpayPaginationOptions } from './api'
2+
import { Items } from './items';
3+
4+
export declare namespace Addons {
5+
interface RazorpayAddon {
6+
/**
7+
* The unique identifier of the created add-on.
8+
*/
9+
id: string;
10+
/**
11+
* Indicates the type of entity.
12+
*/
13+
entity: string;
14+
/**
15+
* Details of the created add-on.
16+
*/
17+
item: Items.RazorpayItem;
18+
/**
19+
* This specifies the number of units of the add-on to be charged to the customer. For example, `2`. The total amount is calculated as `amount` * `quantity`.
20+
*/
21+
quantity: number;
22+
/**
23+
* The Unix timestamp, indicates when the add-on was created. For example, `1581597318`.
24+
*/
25+
created_at: number;
26+
/**
27+
* The unique identifier of the Subscription to which the add-on is being added.
28+
*/
29+
subscription_id: string;
30+
/**
31+
* The add-on is added to the next invoice that is generated after it is created. This field is populated only after the invoice is generated. Until then, it is `null`. Once the add-on is linked to an invoice, it cannot be deleted.
32+
*/
33+
invoice_id: string;
34+
}
35+
}
36+
37+
38+
declare function addons(api: API): {
39+
/**
40+
* Fetches an addon given Addon ID
41+
*
42+
* @param addonId - addon id to be fetched
43+
*
44+
*/
45+
fetch(addonId: string): Promise<Addons.RazorpayAddon>
46+
fetch(addonId: string, callback: (err: INormalizeError | null, data: Addons.RazorpayAddon) => void): void;
47+
/**
48+
* Delete a addon given Addon ID
49+
*
50+
* @param addonId - addon id to be fetched
51+
*
52+
*/
53+
delete(addonId: string): Promise<[]>
54+
delete(addonId: string, callback: (err: INormalizeError | null, data: []) => void): void;
55+
/**
56+
* Get all addons
57+
*
58+
* @param params - Check [doc](https://razorpay.com/docs/api/payments/subscriptions/#fetch-all-add-ons) for required params
59+
*
60+
*/
61+
all(params?: RazorpayPaginationOptions): Promise<{
62+
entity: string,
63+
count: number,
64+
items: Array<Addons.RazorpayAddon>
65+
}>;
66+
all(params: RazorpayPaginationOptions, callback: (err: INormalizeError | null, data: {
67+
entity: string,
68+
count: number,
69+
items: Array<Addons.RazorpayAddon>
70+
}) => void): void
71+
};
72+
export default addons;

lib/types/api.d.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import nodeify from '../utils/nodeify'
2+
3+
interface IOption {
4+
hostUrl: string;
5+
key_id: string;
6+
key_secret?: string;
7+
ua: string;
8+
headers?: string;
9+
}
10+
11+
interface IPayload<T> {
12+
url: string;
13+
data: T;
14+
}
15+
16+
export type INotify = 'email' | 'sms'
17+
18+
export interface RazorpayHeaders {
19+
'X-Razorpay-Account'?: string;
20+
'Content-Type'?: string;
21+
}
22+
23+
/**
24+
* Key-value pairs
25+
*/
26+
export interface IMap<T> {
27+
[key: string]: T | null;
28+
}
29+
30+
export type PartialOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
31+
32+
export interface RazorpayPaginationOptions {
33+
/**
34+
* The Unix timestamp from when data are to be fetched
35+
*/
36+
from?: number;
37+
/**
38+
* The Unix timestamp till when data are to be fetched.
39+
*/
40+
to?: number;
41+
/**
42+
* The number of data to be fetched. Default value is `10`. Maximum value is `100`.
43+
* This can be used for pagination, in combination with skip.
44+
*/
45+
count?: number;
46+
/**
47+
* The number of data to be skipped. Default value is `0`.
48+
* This can be used for pagination, in combination with count.
49+
*/
50+
skip?: number;
51+
}
52+
53+
export interface INormalizeError {
54+
statusCode: string | number;
55+
error: {
56+
code: string;
57+
description: string;
58+
field?: any;
59+
source?: string;
60+
step?: string;
61+
reason?: string;
62+
metadata?: { [key: string]: string };
63+
}
64+
}
65+
66+
declare class API {
67+
constructor(options: IOption)
68+
get<T, V>(params: IPayload<T>): Promise<V>
69+
get<T, V>(params: IPayload<T>, callback: (err: INormalizeError, data: V) => void): void
70+
71+
post<T, V>(params: IPayload<T>): Promise<V>
72+
post<T, V>(params: IPayload<T>, callback: (err: INormalizeError, data: V) => void): void
73+
74+
put<T, V>(params: IPayload<T>): Promise<V>
75+
put<T, V>(params: IPayload<T>, callback: (err: INormalizeError, data: V) => void): void
76+
77+
patch<T, V>(params: IPayload<T>): Promise<V>
78+
patch<T, V>(params: IPayload<T>, callback: (err: INormalizeError, data: V) => void): void
79+
80+
delete<T, V>(params: IPayload<T>): Promise<V>
81+
delete<T, V>(params: IPayload<T>, callback: (err: INormalizeError, data: V) => void): void
82+
}
83+
84+
export default API

lib/types/cards.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Payments } from "./payments";
2+
import { INormalizeError } from "./api";
3+
4+
declare function cards(api: any): {
5+
/**
6+
* Fetch a card given a Card ID
7+
*
8+
* @param cardId - The unique identifier of the card
9+
*
10+
*/
11+
fetch(cardId: string): Promise<Payments.RazorpayCard>
12+
fetch(cardId: string, callback: (err: INormalizeError | null, data: Payments.RazorpayCard) => void): void
13+
14+
}
15+
16+
export default cards

0 commit comments

Comments
 (0)