-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinbox.ts
More file actions
306 lines (271 loc) · 7.96 KB
/
inbox.ts
File metadata and controls
306 lines (271 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { APIResource } from '../core/resource';
import { APIPromise } from '../core/api-promise';
import { buildHeaders } from '../internal/headers';
import { RequestOptions } from '../internal/request-options';
/**
* Endpoints for importing CAS files directly from user email inboxes.
*
* **Supported Providers:** Gmail (more coming soon)
*
* **How it works:**
* 1. Call `POST /v4/inbox/connect` to get an OAuth URL
* 2. Redirect user to the OAuth URL for consent
* 3. User is redirected back to your `redirect_uri` with an encrypted `inbox_token`
* 4. Use the token to list/fetch CAS files from their inbox (`/v4/inbox/cas`)
* 5. Files are uploaded to temporary cloud storage (URLs expire in 24 hours)
*
* **Security:**
* - Read-only access (we cannot send emails)
* - Tokens are encrypted with server-side secret
* - User can revoke access anytime via `/v4/inbox/disconnect`
*/
export class Inbox extends APIResource {
/**
* Verify if an `inbox_token` is still valid and check connection status.
*
* Use this to check if the user needs to re-authenticate (e.g., if they revoked
* access in their email provider settings).
*
* @example
* ```ts
* const response = await client.inbox.checkConnectionStatus({
* 'x-inbox-token': 'x-inbox-token',
* });
* ```
*/
checkConnectionStatus(
params: InboxCheckConnectionStatusParams,
options?: RequestOptions,
): APIPromise<InboxCheckConnectionStatusResponse> {
const { 'x-inbox-token': xInboxToken } = params;
return this._client.post('/v4/inbox/status', {
...options,
headers: buildHeaders([{ 'x-inbox-token': xInboxToken }, options?.headers]),
});
}
/**
* Initiate OAuth flow to connect user's email inbox.
*
* Returns an `oauth_url` that you should redirect the user to. After
* authorization, they are redirected back to your `redirect_uri` with the
* following query parameters:
*
* **On success:**
*
* - `inbox_token` - Encrypted token to store client-side
* - `email` - Email address of the connected account
* - `state` - Your original state parameter (for CSRF verification)
*
* **On error:**
*
* - `error` - Error code (e.g., `access_denied`, `token_exchange_failed`)
* - `state` - Your original state parameter
*
* **Store the `inbox_token` client-side** and use it for all subsequent inbox API
* calls.
*
* @example
* ```ts
* const response = await client.inbox.connectEmail({
* redirect_uri: 'https://yourapp.com/oauth-callback',
* });
* ```
*/
connectEmail(
body: InboxConnectEmailParams,
options?: RequestOptions,
): APIPromise<InboxConnectEmailResponse> {
return this._client.post('/v4/inbox/connect', { body, ...options });
}
/**
* Revoke email access and invalidate the token.
*
* This calls the provider's token revocation API (e.g., Google's revoke endpoint)
* to ensure the user's consent is properly removed.
*
* After calling this, the `inbox_token` becomes unusable.
*
* @example
* ```ts
* const response = await client.inbox.disconnectEmail({
* 'x-inbox-token': 'x-inbox-token',
* });
* ```
*/
disconnectEmail(
params: InboxDisconnectEmailParams,
options?: RequestOptions,
): APIPromise<InboxDisconnectEmailResponse> {
const { 'x-inbox-token': xInboxToken } = params;
return this._client.post('/v4/inbox/disconnect', {
...options,
headers: buildHeaders([{ 'x-inbox-token': xInboxToken }, options?.headers]),
});
}
/**
* Search the user's email inbox for CAS files from known senders (CAMS, KFintech,
* CDSL, NSDL).
*
* Files are uploaded to temporary cloud storage. **URLs expire in 24 hours.**
*
* Optionally filter by CAS provider and date range.
*
* **Billing:** 0.2 credits per request (charged regardless of success or number of
* files found).
*
* @example
* ```ts
* const response = await client.inbox.listCasFiles({
* 'x-inbox-token': 'x-inbox-token',
* });
* ```
*/
listCasFiles(
params: InboxListCasFilesParams,
options?: RequestOptions,
): APIPromise<InboxListCasFilesResponse> {
const { 'x-inbox-token': xInboxToken, ...body } = params;
return this._client.post('/v4/inbox/cas', {
body,
...options,
headers: buildHeaders([{ 'x-inbox-token': xInboxToken }, options?.headers]),
});
}
}
export interface InboxCheckConnectionStatusResponse {
/**
* Whether the token is valid and usable
*/
connected?: boolean;
/**
* Email address of the connected account
*/
email?: string;
provider?: string;
status?: string;
}
export interface InboxConnectEmailResponse {
/**
* Seconds until the OAuth URL expires (typically 10 minutes)
*/
expires_in?: number;
/**
* Redirect user to this URL to start OAuth flow
*/
oauth_url?: string;
status?: string;
}
export interface InboxDisconnectEmailResponse {
msg?: string;
status?: string;
}
export interface InboxListCasFilesResponse {
/**
* Number of CAS files found
*/
count?: number;
files?: Array<InboxListCasFilesResponse.File>;
status?: string;
}
export namespace InboxListCasFilesResponse {
/**
* A CAS file found in the user's email inbox
*/
export interface File {
/**
* Detected CAS provider based on sender email
*/
cas_type?: 'cdsl' | 'nsdl' | 'cams' | 'kfintech';
/**
* URL expiration time in seconds (default 86400 = 24 hours)
*/
expires_in?: number;
/**
* Standardized filename (provider_YYYYMMDD_uniqueid.pdf)
*/
filename?: string;
/**
* Date the email was received
*/
message_date?: string;
/**
* Unique identifier for the email message (use for subsequent API calls)
*/
message_id?: string;
/**
* Original attachment filename from the email
*/
original_filename?: string;
/**
* Email address of the CAS authority (CDSL, NSDL, CAMS, or KFintech) who
* originally sent this statement
*/
sender_email?: string;
/**
* File size in bytes
*/
size?: number;
/**
* Direct download URL (presigned, expires based on expires_in)
*/
url?: string;
}
}
export interface InboxCheckConnectionStatusParams {
/**
* The encrypted inbox token
*/
'x-inbox-token': string;
}
export interface InboxConnectEmailParams {
/**
* Your callback URL to receive the inbox_token (must be http or https)
*/
redirect_uri: string;
/**
* State parameter for CSRF protection (returned in redirect)
*/
state?: string;
}
export interface InboxDisconnectEmailParams {
/**
* The encrypted inbox token to revoke
*/
'x-inbox-token': string;
}
export interface InboxListCasFilesParams {
/**
* Header param: The encrypted inbox token
*/
'x-inbox-token': string;
/**
* Body param: Filter by CAS provider(s):
*
* - `cdsl` → eCAS@cdslstatement.com
* - `nsdl` → NSDL-CAS@nsdl.co.in
* - `cams` → donotreply@camsonline.com
* - `kfintech` → samfS@kfintech.com
*/
cas_types?: Array<'cdsl' | 'nsdl' | 'cams' | 'kfintech'>;
/**
* Body param: End date in ISO format (YYYY-MM-DD). Defaults to today.
*/
end_date?: string;
/**
* Body param: Start date in ISO format (YYYY-MM-DD). Defaults to 30 days ago.
*/
start_date?: string;
}
export declare namespace Inbox {
export {
type InboxCheckConnectionStatusResponse as InboxCheckConnectionStatusResponse,
type InboxConnectEmailResponse as InboxConnectEmailResponse,
type InboxDisconnectEmailResponse as InboxDisconnectEmailResponse,
type InboxListCasFilesResponse as InboxListCasFilesResponse,
type InboxCheckConnectionStatusParams as InboxCheckConnectionStatusParams,
type InboxConnectEmailParams as InboxConnectEmailParams,
type InboxDisconnectEmailParams as InboxDisconnectEmailParams,
type InboxListCasFilesParams as InboxListCasFilesParams,
};
}