@pagouai/api-sdk 0.1.1-next.7
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.
- package/README.md +231 -0
- package/dist/client.d.ts +50 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +448 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +41 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +65 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/pagination.d.ts +10 -0
- package/dist/pagination.d.ts.map +1 -0
- package/dist/pagination.js +32 -0
- package/dist/pagination.js.map +1 -0
- package/dist/resources/transactions.d.ts +34 -0
- package/dist/resources/transactions.d.ts.map +1 -0
- package/dist/resources/transactions.js +85 -0
- package/dist/resources/transactions.js.map +1 -0
- package/dist/types.d.ts +181 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +3 -0
- package/dist/version.js.map +1 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# Pagou.ai TypeScript SDK (`/v2`)
|
|
2
|
+
|
|
3
|
+
Cross-runtime SDK template for Node.js 18+, Bun, and Deno.
|
|
4
|
+
|
|
5
|
+
- Resource-first API (`client.transactions.*`)
|
|
6
|
+
- `fetch`-based (no axios)
|
|
7
|
+
- Typed errors
|
|
8
|
+
- Retries + timeout + idempotency
|
|
9
|
+
- Native `/v2` page/limit pagination
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
bun add @pagouai/api-sdk
|
|
15
|
+
# or npm i / pnpm add
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { Client } from "@pagouai/api-sdk";
|
|
22
|
+
|
|
23
|
+
const client = new Client({
|
|
24
|
+
apiKey: "YOUR_API_KEY",
|
|
25
|
+
timeoutMs: 30_000,
|
|
26
|
+
maxRetries: 2,
|
|
27
|
+
telemetry: true,
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Configuration
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
new Client({
|
|
35
|
+
apiKey: "YOUR_API_KEY",
|
|
36
|
+
environment: "production", // default: "production"
|
|
37
|
+
// environment: "sandbox",
|
|
38
|
+
// baseUrl: "https://api.sandbox.pagou.ai", // explicit override if needed
|
|
39
|
+
timeoutMs: 30_000,
|
|
40
|
+
maxRetries: 2,
|
|
41
|
+
telemetry: true,
|
|
42
|
+
userAgent: "PagouTS-SDK/0.1.0 (Transactions; +https://pagou.ai)",
|
|
43
|
+
fetch: customFetch,
|
|
44
|
+
auth: { scheme: "bearer" }, // default
|
|
45
|
+
// apiVersion: "2026-02-01",
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Auth schemes:
|
|
50
|
+
|
|
51
|
+
- `bearer` (default) -> `Authorization: Bearer <apiKey>`
|
|
52
|
+
- `basic` -> `Authorization: Basic <base64(apiKey:x)>`
|
|
53
|
+
- `api_key_header` -> `apikey: <apiKey>` (or custom `headerName`)
|
|
54
|
+
|
|
55
|
+
Default base URLs:
|
|
56
|
+
|
|
57
|
+
- production: `https://api.pagou.ai`
|
|
58
|
+
- sandbox/test: `https://api.sandbox.pagou.ai`
|
|
59
|
+
|
|
60
|
+
## API Coverage (`/v2`)
|
|
61
|
+
|
|
62
|
+
- `POST /v2/transactions`
|
|
63
|
+
- `GET /v2/transactions`
|
|
64
|
+
- `GET /v2/transactions/{id}`
|
|
65
|
+
- `PUT /v2/transactions/{id}` (test/sandbox-only behavior)
|
|
66
|
+
- `PUT /v2/transactions/{id}/refund`
|
|
67
|
+
|
|
68
|
+
## Usage Examples
|
|
69
|
+
|
|
70
|
+
### 1) Create a transaction
|
|
71
|
+
|
|
72
|
+
For `method: "pix"`, `buyer.document` is required and `buyer.document.type` must be uppercase:
|
|
73
|
+
- `CPF`
|
|
74
|
+
- `CNPJ`
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
const created = await client.transactions.create({
|
|
78
|
+
amount: 1500,
|
|
79
|
+
method: "pix",
|
|
80
|
+
currency: "BRL",
|
|
81
|
+
buyer: {
|
|
82
|
+
name: "Jane Doe",
|
|
83
|
+
email: "jane@example.com",
|
|
84
|
+
document: {
|
|
85
|
+
type: "CPF",
|
|
86
|
+
number: "12345678901",
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
products: [{ name: "Pro Plan", price: 1500, quantity: 1 }],
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
console.log(created.data.id, created.meta.requestId);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### 2) Retrieve a transaction
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
const tx = await client.transactions.retrieve("tr_123");
|
|
99
|
+
console.log(tx.data.status);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### 3) List transactions with page/limit + filters
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
const page = await client.transactions.list({
|
|
106
|
+
page: 1,
|
|
107
|
+
limit: 20,
|
|
108
|
+
status: ["pending", "paid"],
|
|
109
|
+
paymentMethods: ["pix"],
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
console.log(page.data.metadata.total);
|
|
113
|
+
for (const item of page.data.data) {
|
|
114
|
+
console.log(item.id, item.status);
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### 4) Update a transaction (test/sandbox only)
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
const updated = await client.transactions.update(
|
|
122
|
+
"tr_123",
|
|
123
|
+
{ status: "paid" },
|
|
124
|
+
{ idempotencyKey: "idem_update_tr_123_1" },
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
console.log(updated.data.status);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### 5) Auto-paging iterator
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
for await (const item of client.transactions.listAutoPagingIterator({ limit: 100 })) {
|
|
134
|
+
console.log(item.id);
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### 6) Refund a transaction
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
const refunded = await client.transactions.refund(
|
|
142
|
+
"tr_123",
|
|
143
|
+
{ amount: 500, reason: "requested_by_customer" },
|
|
144
|
+
{ idempotencyKey: "idem_refund_tr_123_1" },
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
console.log(refunded.data);
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Request Options
|
|
151
|
+
|
|
152
|
+
All resource methods accept `opts?`:
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
{
|
|
156
|
+
idempotencyKey?: string;
|
|
157
|
+
requestId?: string;
|
|
158
|
+
timeoutMs?: number;
|
|
159
|
+
signal?: AbortSignal;
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Retries and Timeout
|
|
164
|
+
|
|
165
|
+
Retries happen for:
|
|
166
|
+
|
|
167
|
+
- Network errors
|
|
168
|
+
- `429`, `500`, `502`, `503`, `504`
|
|
169
|
+
|
|
170
|
+
Rules:
|
|
171
|
+
|
|
172
|
+
- Always retry for `GET`/`HEAD`
|
|
173
|
+
- Retry `POST`/`PUT` only when `idempotencyKey` is set
|
|
174
|
+
- Respect `Retry-After` header when available
|
|
175
|
+
|
|
176
|
+
Timeout uses `AbortController`.
|
|
177
|
+
|
|
178
|
+
## Error Handling
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
import {
|
|
182
|
+
AuthenticationError,
|
|
183
|
+
InvalidRequestError,
|
|
184
|
+
NotFoundError,
|
|
185
|
+
RateLimitError,
|
|
186
|
+
ServerError,
|
|
187
|
+
NetworkError,
|
|
188
|
+
} from "@pagouai/api-sdk";
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
await client.transactions.retrieve("tr_404");
|
|
192
|
+
} catch (error) {
|
|
193
|
+
if (error instanceof NotFoundError) {
|
|
194
|
+
console.error("Missing transaction", error.requestId);
|
|
195
|
+
} else if (error instanceof RateLimitError) {
|
|
196
|
+
console.error("Back off", error.status, error.code);
|
|
197
|
+
} else if (error instanceof NetworkError) {
|
|
198
|
+
console.error("Network issue", error.message);
|
|
199
|
+
} else if (error instanceof InvalidRequestError || error instanceof AuthenticationError || error instanceof ServerError) {
|
|
200
|
+
console.error(error.message);
|
|
201
|
+
} else {
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Response Shapes
|
|
208
|
+
|
|
209
|
+
Data endpoints:
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
{
|
|
213
|
+
success: boolean;
|
|
214
|
+
requestId: string;
|
|
215
|
+
data: T;
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
List endpoints:
|
|
220
|
+
|
|
221
|
+
```ts
|
|
222
|
+
{
|
|
223
|
+
success: boolean;
|
|
224
|
+
requestId: string;
|
|
225
|
+
metadata: { page: number; limit: number; total: number };
|
|
226
|
+
data: T[];
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
Notes:
|
|
231
|
+
- This SDK uses `/v2` endpoint paths.
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { TransactionsResource } from "./resources/transactions";
|
|
2
|
+
import type { ApiResponse, ClientOptions, PaginatedEnvelope, RequestOptions } from "./types";
|
|
3
|
+
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD";
|
|
4
|
+
interface RawResponse {
|
|
5
|
+
status: number;
|
|
6
|
+
headers: Headers;
|
|
7
|
+
body: unknown;
|
|
8
|
+
requestId?: string;
|
|
9
|
+
}
|
|
10
|
+
interface RequestInput {
|
|
11
|
+
method: HttpMethod;
|
|
12
|
+
path: string;
|
|
13
|
+
query?: Record<string, unknown>;
|
|
14
|
+
body?: unknown;
|
|
15
|
+
options?: RequestOptions;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Main SDK client for Pagou.ai API.
|
|
19
|
+
*/
|
|
20
|
+
export declare class Client {
|
|
21
|
+
private readonly apiKey;
|
|
22
|
+
private readonly baseUrl;
|
|
23
|
+
private readonly timeoutMs;
|
|
24
|
+
private readonly maxRetries;
|
|
25
|
+
private readonly telemetry;
|
|
26
|
+
private readonly userAgent;
|
|
27
|
+
private readonly fetchImpl;
|
|
28
|
+
private readonly auth;
|
|
29
|
+
private readonly apiVersion?;
|
|
30
|
+
private readonly retryBaseDelayMs;
|
|
31
|
+
private readonly maxRetryDelayMs;
|
|
32
|
+
readonly transactions: TransactionsResource;
|
|
33
|
+
constructor(options: ClientOptions);
|
|
34
|
+
/**
|
|
35
|
+
* Sends a request and unwraps a data envelope.
|
|
36
|
+
*/
|
|
37
|
+
requestData<T>(input: RequestInput): Promise<ApiResponse<T>>;
|
|
38
|
+
/**
|
|
39
|
+
* Sends a request and unwraps a paginated envelope.
|
|
40
|
+
*/
|
|
41
|
+
requestPaginated<T>(input: RequestInput): Promise<ApiResponse<PaginatedEnvelope<T>>>;
|
|
42
|
+
requestRaw(input: RequestInput): Promise<RawResponse>;
|
|
43
|
+
private prepareRequest;
|
|
44
|
+
private applyAuthHeader;
|
|
45
|
+
private applyVersionHeader;
|
|
46
|
+
private applyTelemetryHeaders;
|
|
47
|
+
private toApiError;
|
|
48
|
+
}
|
|
49
|
+
export {};
|
|
50
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|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