@isnap/sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,77 @@
1
+ import type { iSnapClient } from '../client.js';
2
+ import type { Message, SendMessageParams, ListMessagesParams, ListMessagesResponse, ListConversationsParams, ListConversationsResponse, ConversationThreadParams, ConversationThreadResponse } from '../types.js';
3
+ export declare class MessagesResource {
4
+ private client;
5
+ constructor(client: iSnapClient);
6
+ /**
7
+ * Send a new message.
8
+ *
9
+ * @param params - Message details (line, recipient, body)
10
+ * @returns The created message
11
+ * @throws {iSnapError} On validation or delivery errors
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const msg = await client.messages.send({
16
+ * line_id: 'ln-1',
17
+ * to: '+15551234567',
18
+ * body: 'Hello from iSnap!',
19
+ * });
20
+ * ```
21
+ */
22
+ send(params: SendMessageParams): Promise<Message>;
23
+ /**
24
+ * Retrieve a single message by ID.
25
+ *
26
+ * @param messageId - The message ID
27
+ * @returns The message
28
+ * @throws {iSnapError} If the message is not found
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * const msg = await client.messages.get('msg-abc123');
33
+ * ```
34
+ */
35
+ get(messageId: string): Promise<Message>;
36
+ /**
37
+ * List messages with optional filters and pagination.
38
+ *
39
+ * @param params - Optional filters (line_id, status, direction) and pagination
40
+ * @returns Paginated list of messages
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const { messages, total } = await client.messages.list({ status: 'delivered', limit: 50 });
45
+ * ```
46
+ */
47
+ list(params?: ListMessagesParams): Promise<ListMessagesResponse>;
48
+ /**
49
+ * List all conversations with optional filters and pagination.
50
+ *
51
+ * @param params - Optional line_id filter and pagination
52
+ * @returns Paginated list of conversations
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * const { conversations } = await client.messages.listConversations({ line_id: 'ln-1' });
57
+ * ```
58
+ */
59
+ listConversations(params?: ListConversationsParams): Promise<ListConversationsResponse>;
60
+ /**
61
+ * Retrieve a full conversation thread by ID, including its messages.
62
+ *
63
+ * @param conversationId - The conversation ID
64
+ * @param params - Optional pagination for messages within the thread
65
+ * @returns The conversation with paginated messages
66
+ * @throws {iSnapError} If the conversation is not found
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * const thread = await client.messages.getConversation('conv-abc123', { page: 1, limit: 25 });
71
+ * console.log(thread.conversation.contact_number);
72
+ * console.log(thread.messages.length);
73
+ * ```
74
+ */
75
+ getConversation(conversationId: string, params?: ConversationThreadParams): Promise<ConversationThreadResponse>;
76
+ }
77
+ //# sourceMappingURL=messages.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../src/resources/messages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,KAAK,EACV,OAAO,EACP,iBAAiB,EACjB,kBAAkB,EAClB,oBAAoB,EACpB,uBAAuB,EACvB,yBAAyB,EACzB,wBAAwB,EACxB,0BAA0B,EAC3B,MAAM,aAAa,CAAC;AAErB,qBAAa,gBAAgB;IACf,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,WAAW;IAEvC;;;;;;;;;;;;;;;OAeG;IACG,IAAI,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAAC,OAAO,CAAC;IAIvD;;;;;;;;;;;OAWG;IACG,GAAG,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAI9C;;;;;;;;;;OAUG;IACG,IAAI,CAAC,MAAM,CAAC,EAAE,kBAAkB,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAWtE;;;;;;;;;;OAUG;IACG,iBAAiB,CAAC,MAAM,CAAC,EAAE,uBAAuB,GAAG,OAAO,CAAC,yBAAyB,CAAC;IAS7F;;;;;;;;;;;;;;OAcG;IACG,eAAe,CAAC,cAAc,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,wBAAwB,GAAG,OAAO,CAAC,0BAA0B,CAAC;CAYtH"}
@@ -0,0 +1,110 @@
1
+ export class MessagesResource {
2
+ client;
3
+ constructor(client) {
4
+ this.client = client;
5
+ }
6
+ /**
7
+ * Send a new message.
8
+ *
9
+ * @param params - Message details (line, recipient, body)
10
+ * @returns The created message
11
+ * @throws {iSnapError} On validation or delivery errors
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const msg = await client.messages.send({
16
+ * line_id: 'ln-1',
17
+ * to: '+15551234567',
18
+ * body: 'Hello from iSnap!',
19
+ * });
20
+ * ```
21
+ */
22
+ async send(params) {
23
+ return this.client.request('POST', '/v1/messages', params);
24
+ }
25
+ /**
26
+ * Retrieve a single message by ID.
27
+ *
28
+ * @param messageId - The message ID
29
+ * @returns The message
30
+ * @throws {iSnapError} If the message is not found
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * const msg = await client.messages.get('msg-abc123');
35
+ * ```
36
+ */
37
+ async get(messageId) {
38
+ return this.client.request('GET', `/v1/messages/${messageId}`);
39
+ }
40
+ /**
41
+ * List messages with optional filters and pagination.
42
+ *
43
+ * @param params - Optional filters (line_id, status, direction) and pagination
44
+ * @returns Paginated list of messages
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * const { messages, total } = await client.messages.list({ status: 'delivered', limit: 50 });
49
+ * ```
50
+ */
51
+ async list(params) {
52
+ const query = {};
53
+ if (params?.line_id)
54
+ query.line_id = params.line_id;
55
+ if (params?.status)
56
+ query.status = params.status;
57
+ if (params?.direction)
58
+ query.direction = params.direction;
59
+ if (params?.page)
60
+ query.page = String(params.page);
61
+ if (params?.limit)
62
+ query.limit = String(params.limit);
63
+ return this.client.request('GET', '/v1/messages', undefined, query);
64
+ }
65
+ /**
66
+ * List all conversations with optional filters and pagination.
67
+ *
68
+ * @param params - Optional line_id filter and pagination
69
+ * @returns Paginated list of conversations
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * const { conversations } = await client.messages.listConversations({ line_id: 'ln-1' });
74
+ * ```
75
+ */
76
+ async listConversations(params) {
77
+ const query = {};
78
+ if (params?.line_id)
79
+ query.line_id = params.line_id;
80
+ if (params?.page)
81
+ query.page = String(params.page);
82
+ if (params?.limit)
83
+ query.limit = String(params.limit);
84
+ return this.client.request('GET', '/v1/messages/conversations', undefined, query);
85
+ }
86
+ /**
87
+ * Retrieve a full conversation thread by ID, including its messages.
88
+ *
89
+ * @param conversationId - The conversation ID
90
+ * @param params - Optional pagination for messages within the thread
91
+ * @returns The conversation with paginated messages
92
+ * @throws {iSnapError} If the conversation is not found
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * const thread = await client.messages.getConversation('conv-abc123', { page: 1, limit: 25 });
97
+ * console.log(thread.conversation.contact_number);
98
+ * console.log(thread.messages.length);
99
+ * ```
100
+ */
101
+ async getConversation(conversationId, params) {
102
+ const query = {};
103
+ if (params?.page)
104
+ query.page = String(params.page);
105
+ if (params?.limit)
106
+ query.limit = String(params.limit);
107
+ return this.client.request('GET', `/v1/messages/conversations/${conversationId}`, undefined, query);
108
+ }
109
+ }
110
+ //# sourceMappingURL=messages.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/resources/messages.ts"],"names":[],"mappings":"AAYA,MAAM,OAAO,gBAAgB;IACP;IAApB,YAAoB,MAAmB;QAAnB,WAAM,GAAN,MAAM,CAAa;IAAG,CAAC;IAE3C;;;;;;;;;;;;;;;OAeG;IACH,KAAK,CAAC,IAAI,CAAC,MAAyB;QAClC,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAU,MAAM,EAAE,cAAc,EAAE,MAAM,CAAC,CAAC;IACtE,CAAC;IAED;;;;;;;;;;;OAWG;IACH,KAAK,CAAC,GAAG,CAAC,SAAiB;QACzB,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAU,KAAK,EAAE,gBAAgB,SAAS,EAAE,CAAC,CAAC;IAC1E,CAAC;IAED;;;;;;;;;;OAUG;IACH,KAAK,CAAC,IAAI,C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B,EAAE,CAAC;QACzC,IAAI,MAAM,EAAE,IAAI;YAAE,KAAK,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACnD,IAAI,MAAM,EAAE,KAAK;YAAE,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAEtD,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CACxB,KAAK,EACL,8BAA8B,cAAc,EAAE,EAC9C,SAAS,EACT,KAAK,CACN,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,48 @@
1
+ import type { iSnapClient } from '../client.js';
2
+ import type { Webhook, CreateWebhookParams } from '../types.js';
3
+ export declare class WebhooksResource {
4
+ private client;
5
+ constructor(client: iSnapClient);
6
+ /**
7
+ * Register a new webhook endpoint.
8
+ *
9
+ * @param params - Webhook URL and optional event filter
10
+ * @returns The created webhook (includes the `secret` for signature verification)
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const wh = await client.webhooks.create({
15
+ * url: 'https://example.com/webhooks/isnap',
16
+ * events: ['message.sent', 'message.delivered'],
17
+ * });
18
+ * console.log(wh.secret); // save this for verification
19
+ * ```
20
+ */
21
+ create(params: CreateWebhookParams): Promise<Webhook>;
22
+ /**
23
+ * List all registered webhooks.
24
+ *
25
+ * @returns Object containing the webhooks array
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * const { webhooks } = await client.webhooks.list();
30
+ * ```
31
+ */
32
+ list(): Promise<{
33
+ webhooks: Webhook[];
34
+ }>;
35
+ /**
36
+ * Delete a webhook by ID.
37
+ *
38
+ * @param webhookId - The webhook ID to delete
39
+ * @throws {iSnapError} If the webhook is not found
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * await client.webhooks.delete('wh-abc123');
44
+ * ```
45
+ */
46
+ delete(webhookId: string): Promise<void>;
47
+ }
48
+ //# sourceMappingURL=webhooks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhooks.d.ts","sourceRoot":"","sources":["../../src/resources/webhooks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,KAAK,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAEhE,qBAAa,gBAAgB;IACf,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,WAAW;IAEvC;;;;;;;;;;;;;;OAcG;IACG,MAAM,CAAC,MAAM,EAAE,mBAAmB,GAAG,OAAO,CAAC,OAAO,CAAC;IAI3D;;;;;;;;;OASG;IACG,IAAI,IAAI,OAAO,CAAC;QAAE,QAAQ,EAAE,OAAO,EAAE,CAAA;KAAE,CAAC;IAI9C;;;;;;;;;;OAUG;IACG,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAG/C"}
@@ -0,0 +1,52 @@
1
+ export class WebhooksResource {
2
+ client;
3
+ constructor(client) {
4
+ this.client = client;
5
+ }
6
+ /**
7
+ * Register a new webhook endpoint.
8
+ *
9
+ * @param params - Webhook URL and optional event filter
10
+ * @returns The created webhook (includes the `secret` for signature verification)
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const wh = await client.webhooks.create({
15
+ * url: 'https://example.com/webhooks/isnap',
16
+ * events: ['message.sent', 'message.delivered'],
17
+ * });
18
+ * console.log(wh.secret); // save this for verification
19
+ * ```
20
+ */
21
+ async create(params) {
22
+ return this.client.request('POST', '/v1/webhooks', params);
23
+ }
24
+ /**
25
+ * List all registered webhooks.
26
+ *
27
+ * @returns Object containing the webhooks array
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * const { webhooks } = await client.webhooks.list();
32
+ * ```
33
+ */
34
+ async list() {
35
+ return this.client.request('GET', '/v1/webhooks');
36
+ }
37
+ /**
38
+ * Delete a webhook by ID.
39
+ *
40
+ * @param webhookId - The webhook ID to delete
41
+ * @throws {iSnapError} If the webhook is not found
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * await client.webhooks.delete('wh-abc123');
46
+ * ```
47
+ */
48
+ async delete(webhookId) {
49
+ return this.client.request('DELETE', `/v1/webhooks/${webhookId}`);
50
+ }
51
+ }
52
+ //# sourceMappingURL=webhooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhooks.js","sourceRoot":"","sources":["../../src/resources/webhooks.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,gBAAgB;IACP;IAApB,YAAoB,MAAmB;QAAnB,WAAM,GAAN,MAAM,CAAa;IAAG,CAAC;IAE3C;;;;;;;;;;;;;;OAcG;IACH,KAAK,CAAC,MAAM,CAAC,MAA2B;QACtC,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAU,MAAM,EAAE,cAAc,EAAE,MAAM,CAAC,CAAC;IACtE,CAAC;IAED;;;;;;;;;OASG;IACH,KAAK,CAAC,IAAI;QACR,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAA0B,KAAK,EAAE,cAAc,CAAC,CAAC;IAC7E,CAAC;IAED;;;;;;;;;;OAUG;IACH,KAAK,CAAC,MAAM,CAAC,SAAiB;QAC5B,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAO,QAAQ,EAAE,gBAAgB,SAAS,EAAE,CAAC,CAAC;IAC1E,CAAC;CACF"}
@@ -0,0 +1,199 @@
1
+ /** A message sent or received through a line. */
2
+ export interface Message {
3
+ id: string;
4
+ line_id: string;
5
+ direction: string;
6
+ from_number: string;
7
+ to_number: string;
8
+ body: string;
9
+ channel: string;
10
+ status: string;
11
+ error_code: string | null;
12
+ error_message: string | null;
13
+ metadata: unknown;
14
+ queued_at: string;
15
+ sent_at: string | null;
16
+ delivered_at: string | null;
17
+ read_at: string | null;
18
+ failed_at: string | null;
19
+ created_at: string;
20
+ }
21
+ /** Parameters for sending a new message. */
22
+ export interface SendMessageParams {
23
+ /** The line ID to send the message from. */
24
+ line_id: string;
25
+ /** Recipient phone number in E.164 format. */
26
+ to: string;
27
+ /** Message body text. */
28
+ body: string;
29
+ /** Messaging channel. Defaults to `'auto'`. */
30
+ channel?: 'auto' | 'imessage' | 'sms' | 'whatsapp';
31
+ /** Optional webhook URL to receive delivery status updates. */
32
+ webhook_url?: string;
33
+ /** Arbitrary key-value metadata attached to the message. */
34
+ metadata?: Record<string, unknown>;
35
+ }
36
+ /** Filter/pagination parameters for listing messages. */
37
+ export interface ListMessagesParams {
38
+ line_id?: string;
39
+ status?: 'queued' | 'sent' | 'delivered' | 'read' | 'failed';
40
+ direction?: 'outbound' | 'inbound';
41
+ page?: number;
42
+ limit?: number;
43
+ }
44
+ /** Paginated list of messages. */
45
+ export interface ListMessagesResponse {
46
+ messages: Message[];
47
+ total: number;
48
+ page: number;
49
+ limit: number;
50
+ }
51
+ /** A conversation thread between a line and a contact. */
52
+ export interface Conversation {
53
+ id: string;
54
+ line_id: string;
55
+ contact_number: string;
56
+ last_message_at: string;
57
+ message_count: number;
58
+ created_at: string;
59
+ }
60
+ /** Filter/pagination parameters for listing conversations. */
61
+ export interface ListConversationsParams {
62
+ line_id?: string;
63
+ page?: number;
64
+ limit?: number;
65
+ }
66
+ /** Paginated list of conversations. */
67
+ export interface ListConversationsResponse {
68
+ conversations: Conversation[];
69
+ total: number;
70
+ page: number;
71
+ limit: number;
72
+ }
73
+ /** Pagination parameters for retrieving a conversation thread. */
74
+ export interface ConversationThreadParams {
75
+ /** Page number (1-based). */
76
+ page?: number;
77
+ /** Number of messages per page. */
78
+ limit?: number;
79
+ }
80
+ /** A conversation thread with its messages. */
81
+ export interface ConversationThreadResponse {
82
+ conversation: Conversation;
83
+ messages: Message[];
84
+ total: number;
85
+ page: number;
86
+ limit: number;
87
+ }
88
+ /** A phone line (iPhone or Android). */
89
+ export interface Line {
90
+ id: string;
91
+ phone_number: string;
92
+ area_code: string;
93
+ type: string;
94
+ capabilities: unknown;
95
+ status: string;
96
+ monthly_price: number;
97
+ activated_at: string | null;
98
+ created_at: string;
99
+ }
100
+ /** Paginated list of lines. */
101
+ export interface ListLinesResponse {
102
+ lines: Line[];
103
+ total: number;
104
+ page: number;
105
+ limit: number;
106
+ }
107
+ /** Filter/pagination parameters for listing lines. */
108
+ export interface ListLinesParams {
109
+ type?: string;
110
+ area_code?: string;
111
+ page?: number;
112
+ limit?: number;
113
+ }
114
+ /** A registered webhook endpoint. */
115
+ export interface Webhook {
116
+ id: string;
117
+ url: string;
118
+ events: string[];
119
+ active: boolean;
120
+ failure_count: number;
121
+ last_success_at: string | null;
122
+ last_failure_at: string | null;
123
+ created_at: string;
124
+ secret?: string;
125
+ }
126
+ /** Parameters for creating a webhook. */
127
+ export interface CreateWebhookParams {
128
+ /** The URL to receive webhook events. */
129
+ url: string;
130
+ /** Event types to subscribe to. Defaults to all events. */
131
+ events?: string[];
132
+ }
133
+ /** All supported webhook event types. */
134
+ export type WebhookEventType = 'message.sent' | 'message.delivered' | 'message.read' | 'message.failed' | 'message.received';
135
+ /** Base shape shared by all webhook events. */
136
+ interface WebhookEventBase {
137
+ webhook_id: string;
138
+ timestamp: string;
139
+ }
140
+ /** Fired when a message is successfully sent. */
141
+ export interface MessageSentEvent extends WebhookEventBase {
142
+ event: 'message.sent';
143
+ data: Message;
144
+ }
145
+ /** Fired when a message is delivered to the recipient. */
146
+ export interface MessageDeliveredEvent extends WebhookEventBase {
147
+ event: 'message.delivered';
148
+ data: Message;
149
+ }
150
+ /** Fired when a message is read by the recipient. */
151
+ export interface MessageReadEvent extends WebhookEventBase {
152
+ event: 'message.read';
153
+ data: Message;
154
+ }
155
+ /** Fired when a message fails to send. */
156
+ export interface MessageFailedEvent extends WebhookEventBase {
157
+ event: 'message.failed';
158
+ data: Message;
159
+ }
160
+ /** Fired when an inbound message is received. */
161
+ export interface MessageReceivedEvent extends WebhookEventBase {
162
+ event: 'message.received';
163
+ data: Message;
164
+ }
165
+ /** Discriminated union of all webhook event types. Switch on `event.event` for type narrowing. */
166
+ export type WebhookEvent = MessageSentEvent | MessageDeliveredEvent | MessageReadEvent | MessageFailedEvent | MessageReceivedEvent;
167
+ /** Configuration for the iSnap SDK client. */
168
+ export interface iSnapConfig {
169
+ /** Your iSnap API key (starts with `isnap_`). */
170
+ apiKey: string;
171
+ /** Override the base API URL. Defaults to `https://api.isnap.dev`. */
172
+ baseUrl?: string;
173
+ /** Maximum number of automatic retries on 429 responses. Defaults to `3`. */
174
+ maxRetries?: number;
175
+ /** Fallback delay in milliseconds between retries when no `Retry-After` header is present. Defaults to `5000`. */
176
+ retryDelayMs?: number;
177
+ }
178
+ /** Error returned by the iSnap API. */
179
+ export declare class iSnapError extends Error {
180
+ /** HTTP status code. */
181
+ statusCode: number;
182
+ /** Machine-readable error code (e.g. `'RATE_LIMITED'`). */
183
+ code?: string | undefined;
184
+ constructor(message: string,
185
+ /** HTTP status code. */
186
+ statusCode: number,
187
+ /** Machine-readable error code (e.g. `'RATE_LIMITED'`). */
188
+ code?: string | undefined);
189
+ }
190
+ /** Thrown when the API returns 429 Too Many Requests after exhausting retries. */
191
+ export declare class RateLimitError extends iSnapError {
192
+ /** Seconds to wait before retrying. */
193
+ retryAfter: number;
194
+ constructor(message: string,
195
+ /** Seconds to wait before retrying. */
196
+ retryAfter: number);
197
+ }
198
+ export {};
199
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,iDAAiD;AACjD,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ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AQ,gBAAgB;IACxD,KAAK,EAAE,cAAc,CAAC;IACtB,IAAI,EAAE,OAAO,CAAC;CACf;AAED,0CAA0C;AAC1C,MAAM,WAAW,kBAAmB,SAAQ,gBAAgB;IAC1D,KAAK,EAAE,gBAAgB,CAAC;IACxB,IAAI,EAAE,OAAO,CAAC;CACf;AAED,iDAAiD;AACjD,MAAM,WAAW,oBAAqB,SAAQ,gBAAgB;IAC5D,KAAK,EAAE,kBAAkB,CAAC;IAC1B,IAAI,EAAE,OAAO,CAAC;CACf;AAED,kGAAkG;AAClG,MAAM,MAAM,YAAY,GACpB,gBAAgB,GAChB,qBAAqB,GACrB,gBAAgB,GAChB,kBAAkB,GAClB,oBAAoB,CAAC;AAIzB,8CAA8C;AAC9C,MAAM,WAAW,WAAW;IAC1B,iDAAiD;IACjD,MAAM,EAAE,MAAM,CAAC;IACf,sEAAsE;IACtE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,6EAA6E;IAC7E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,kHAAkH;IAClH,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAID,uCAAuC;AACvC,qBAAa,UAAW,SAAQ,KAAK;IAGjC,wBAAwB;IACjB,UAAU,EAAE,MAAM;IACzB,2DAA2D;IACpD,IAAI,CAAC,EAAE,MAAM;gBAJpB,OAAO,EAAE,MAAM;IACf,wBAAwB;IACjB,UAAU,EAAE,MAAM;IACzB,2DAA2D;IACpD,IAAI,CAAC,EAAE,MAAM,YAAA;CAKvB;AAED,kFAAkF;AAClF,qBAAa,cAAe,SAAQ,UAAU;IAG1C,uCAAuC;IAChC,UAAU,EAAE,MAAM;gBAFzB,OAAO,EAAE,MAAM;IACf,uCAAuC;IAChC,UAAU,EAAE,MAAM;CAK5B"}
package/dist/types.js ADDED
@@ -0,0 +1,29 @@
1
+ // ── Message ──────────────────────────────────────────────────────────────────
2
+ // ── Errors ───────────────────────────────────────────────────────────────────
3
+ /** Error returned by the iSnap API. */
4
+ export class iSnapError extends Error {
5
+ statusCode;
6
+ code;
7
+ constructor(message,
8
+ /** HTTP status code. */
9
+ statusCode,
10
+ /** Machine-readable error code (e.g. `'RATE_LIMITED'`). */
11
+ code) {
12
+ super(message);
13
+ this.statusCode = statusCode;
14
+ this.code = code;
15
+ this.name = 'iSnapError';
16
+ }
17
+ }
18
+ /** Thrown when the API returns 429 Too Many Requests after exhausting retries. */
19
+ export class RateLimitError extends iSnapError {
20
+ retryAfter;
21
+ constructor(message,
22
+ /** Seconds to wait before retrying. */
23
+ retryAfter) {
24
+ super(message, 429, 'RATE_LIMITED');
25
+ this.retryAfter = retryAfter;
26
+ this.name = 'RateLimitError';
27
+ }
28
+ }
29
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,gFAAgF;AA8NhF,gFAAgF;AAEhF,uCAAuC;AACvC,MAAM,OAAO,UAAW,SAAQ,KAAK;IAI1B;IAEA;IALT,YACE,OAAe;IACf,wBAAwB;IACjB,UAAkB;IACzB,2DAA2D;IACpD,IAAa;QAEpB,KAAK,CAAC,OAAO,CAAC,CAAC;QAJR,eAAU,GAAV,UAAU,CAAQ;QAElB,SAAI,GAAJ,IAAI,CAAS;QAGpB,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;IAC3B,CAAC;CACF;AAED,kFAAkF;AAClF,MAAM,OAAO,cAAe,SAAQ,UAAU;IAInC;IAHT,YACE,OAAe;IACf,uCAAuC;IAChC,UAAkB;QAEzB,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,cAAc,CAAC,CAAC;QAF7B,eAAU,GAAV,UAAU,CAAQ;QAGzB,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IAC/B,CAAC;CACF"}
@@ -0,0 +1,61 @@
1
+ import type { WebhookEvent } from './types.js';
2
+ /**
3
+ * Parameters for parsing an incoming webhook request.
4
+ */
5
+ export interface ParseWebhookOptions {
6
+ /** The webhook secret (from {@link Webhook.secret}). */
7
+ secret: string;
8
+ /** The `x-isnap-signature` header value. */
9
+ signature: string;
10
+ /** The `x-isnap-timestamp` header value (Unix seconds). */
11
+ timestamp: string;
12
+ /** The raw request body string. */
13
+ body: string;
14
+ }
15
+ /**
16
+ * Result of parsing a webhook request.
17
+ */
18
+ export interface ParseWebhookResult {
19
+ /** Whether the signature was valid and the timestamp was fresh. */
20
+ verified: boolean;
21
+ /** The parsed and typed webhook event. Only meaningful when `verified` is `true`. */
22
+ event: WebhookEvent;
23
+ }
24
+ /**
25
+ * Verify and parse an incoming webhook request in a single call.
26
+ *
27
+ * Combines signature verification with JSON parsing to produce a typed
28
+ * {@link WebhookEvent} that supports discriminated-union narrowing.
29
+ *
30
+ * @param options - Signature, timestamp, secret, and raw body
31
+ * @returns Object with `verified` flag and the parsed `event`
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * import { parseWebhook } from '@isnap/sdk';
36
+ *
37
+ * app.post('/webhooks/isnap', (req, res) => {
38
+ * const { verified, event } = parseWebhook({
39
+ * secret: process.env.WEBHOOK_SECRET!,
40
+ * signature: req.headers['x-isnap-signature'] as string,
41
+ * timestamp: req.headers['x-isnap-timestamp'] as string,
42
+ * body: req.body, // raw string
43
+ * });
44
+ *
45
+ * if (!verified) return res.status(401).send('Invalid signature');
46
+ *
47
+ * switch (event.event) {
48
+ * case 'message.sent':
49
+ * console.log('Sent:', event.data.id);
50
+ * break;
51
+ * case 'message.received':
52
+ * console.log('Received:', event.data.body);
53
+ * break;
54
+ * }
55
+ *
56
+ * res.sendStatus(200);
57
+ * });
58
+ * ```
59
+ */
60
+ export declare function parseWebhook(options: ParseWebhookOptions): ParseWebhookResult;
61
+ //# sourceMappingURL=webhook-events.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhook-events.d.ts","sourceRoot":"","sources":["../src/webhook-events.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,wDAAwD;IACxD,MAAM,EAAE,MAAM,CAAC;IACf,4CAA4C;IAC5C,SAAS,EAAE,MAAM,CAAC;IAClB,2DAA2D;IAC3D,SAAS,EAAE,MAAM,CAAC;IAClB,mCAAmC;IACnC,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,mEAAmE;IACnE,QAAQ,EAAE,OAAO,CAAC;IAClB,qFAAqF;IACrF,KAAK,EAAE,YAAY,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,kBAAkB,CAK7E"}
@@ -0,0 +1,44 @@
1
+ import { verifyWebhookSignature } from './webhook-verify.js';
2
+ /**
3
+ * Verify and parse an incoming webhook request in a single call.
4
+ *
5
+ * Combines signature verification with JSON parsing to produce a typed
6
+ * {@link WebhookEvent} that supports discriminated-union narrowing.
7
+ *
8
+ * @param options - Signature, timestamp, secret, and raw body
9
+ * @returns Object with `verified` flag and the parsed `event`
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { parseWebhook } from '@isnap/sdk';
14
+ *
15
+ * app.post('/webhooks/isnap', (req, res) => {
16
+ * const { verified, event } = parseWebhook({
17
+ * secret: process.env.WEBHOOK_SECRET!,
18
+ * signature: req.headers['x-isnap-signature'] as string,
19
+ * timestamp: req.headers['x-isnap-timestamp'] as string,
20
+ * body: req.body, // raw string
21
+ * });
22
+ *
23
+ * if (!verified) return res.status(401).send('Invalid signature');
24
+ *
25
+ * switch (event.event) {
26
+ * case 'message.sent':
27
+ * console.log('Sent:', event.data.id);
28
+ * break;
29
+ * case 'message.received':
30
+ * console.log('Received:', event.data.body);
31
+ * break;
32
+ * }
33
+ *
34
+ * res.sendStatus(200);
35
+ * });
36
+ * ```
37
+ */
38
+ export function parseWebhook(options) {
39
+ const { secret, signature, timestamp, body } = options;
40
+ const event = JSON.parse(body);
41
+ const verified = verifyWebhookSignature(secret, signature, timestamp, body);
42
+ return { verified, event };
43
+ }
44
+ //# sourceMappingURL=webhook-events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhook-events.js","sourceRoot":"","sources":["../src/webhook-events.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AA2B7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,UAAU,YAAY,CAAC,OAA4B;IACvD,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC;IACvD,MAAM,KAAK,GAAiB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC7C,MAAM,QAAQ,GAAG,sBAAsB,CAAC,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;IAC5E,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;AAC7B,CAAC"}