@relayapi/sdk 0.2.2 → 0.3.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.
package/README.md ADDED
@@ -0,0 +1,358 @@
1
+ # Relay TypeScript API Library
2
+
3
+ [![NPM version](<https://img.shields.io/npm/v/@relayapi/sdk.svg?label=npm%20(stable)>)](https://npmjs.org/package/@relayapi/sdk) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/@relayapi/sdk)
4
+
5
+ This library provides convenient access to the Relay REST API from server-side TypeScript or JavaScript.
6
+
7
+ The REST API documentation can be found on [docs.relayapi.dev](https://docs.relayapi.dev). The full API of this library can be found in [api.md](api.md).
8
+
9
+ It is generated with [Stainless](https://www.stainless.com/).
10
+
11
+ ## Installation
12
+
13
+ ```sh
14
+ npm install @relayapi/sdk
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ The full API of this library can be found in [api.md](api.md).
20
+
21
+ <!-- prettier-ignore -->
22
+ ```js
23
+ import Relay from '@relayapi/sdk';
24
+
25
+ const client = new Relay({
26
+ apiKey: process.env['RELAY_API_KEY'], // This is the default and can be omitted
27
+ });
28
+
29
+ const posts = await client.posts.list();
30
+
31
+ console.log(posts.data);
32
+ ```
33
+
34
+ ### Request & Response types
35
+
36
+ This library includes TypeScript definitions for all request params and response fields. You may import and use them like so:
37
+
38
+ <!-- prettier-ignore -->
39
+ ```ts
40
+ import Relay from '@relayapi/sdk';
41
+
42
+ const client = new Relay({
43
+ apiKey: process.env['RELAY_API_KEY'], // This is the default and can be omitted
44
+ });
45
+
46
+ const posts: Relay.PostListResponse = await client.posts.list();
47
+ ```
48
+
49
+ Documentation for each method, request param, and response field are available in docstrings and will appear on hover in most modern editors.
50
+
51
+ ## Handling errors
52
+
53
+ When the library is unable to connect to the API,
54
+ or if the API returns a non-success status code (i.e., 4xx or 5xx response),
55
+ a subclass of `APIError` will be thrown:
56
+
57
+ <!-- prettier-ignore -->
58
+ ```ts
59
+ const posts = await client.posts.list().catch(async (err) => {
60
+ if (err instanceof Relay.APIError) {
61
+ console.log(err.status); // 400
62
+ console.log(err.name); // BadRequestError
63
+ console.log(err.headers); // {server: 'nginx', ...}
64
+ } else {
65
+ throw err;
66
+ }
67
+ });
68
+ ```
69
+
70
+ Error codes are as follows:
71
+
72
+ | Status Code | Error Type |
73
+ | ----------- | -------------------------- |
74
+ | 400 | `BadRequestError` |
75
+ | 401 | `AuthenticationError` |
76
+ | 403 | `PermissionDeniedError` |
77
+ | 404 | `NotFoundError` |
78
+ | 422 | `UnprocessableEntityError` |
79
+ | 429 | `RateLimitError` |
80
+ | >=500 | `InternalServerError` |
81
+ | N/A | `APIConnectionError` |
82
+
83
+ ### Retries
84
+
85
+ Certain errors will be automatically retried 2 times by default, with a short exponential backoff.
86
+ Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict,
87
+ 429 Rate Limit, and >=500 Internal errors will all be retried by default.
88
+
89
+ You can use the `maxRetries` option to configure or disable this:
90
+
91
+ <!-- prettier-ignore -->
92
+ ```js
93
+ // Configure the default for all requests:
94
+ const client = new Relay({
95
+ maxRetries: 0, // default is 2
96
+ });
97
+
98
+ // Or, configure per-request:
99
+ await client.posts.list({
100
+ maxRetries: 5,
101
+ });
102
+ ```
103
+
104
+ ### Timeouts
105
+
106
+ Requests time out after 1 minute by default. You can configure this with a `timeout` option:
107
+
108
+ <!-- prettier-ignore -->
109
+ ```ts
110
+ // Configure the default for all requests:
111
+ const client = new Relay({
112
+ timeout: 20 * 1000, // 20 seconds (default is 1 minute)
113
+ });
114
+
115
+ // Override per-request:
116
+ await client.posts.list({
117
+ timeout: 5 * 1000,
118
+ });
119
+ ```
120
+
121
+ On timeout, an `APIConnectionTimeoutError` is thrown.
122
+
123
+ Note that requests which time out will be [retried twice by default](#retries).
124
+
125
+ ## Advanced Usage
126
+
127
+ ### Accessing raw Response data (e.g., headers)
128
+
129
+ The "raw" `Response` returned by `fetch()` can be accessed through the `.asResponse()` method on the `APIPromise` type that all methods return.
130
+ This method returns as soon as the headers for a successful response are received and does not consume the response body, so you are free to write custom parsing or streaming logic.
131
+
132
+ You can also use the `.withResponse()` method to get the raw `Response` along with the parsed data.
133
+ Unlike `.asResponse()` this method consumes the body, returning once it is parsed.
134
+
135
+ <!-- prettier-ignore -->
136
+ ```ts
137
+ const client = new Relay();
138
+
139
+ const response = await client.posts.list().asResponse();
140
+ console.log(response.headers.get('X-My-Header'));
141
+ console.log(response.statusText); // access the underlying Response object
142
+
143
+ const { data: posts, response: raw } = await client.posts.list().withResponse();
144
+ console.log(raw.headers.get('X-My-Header'));
145
+ console.log(posts.data);
146
+ ```
147
+
148
+ ### Logging
149
+
150
+ > [!IMPORTANT]
151
+ > All log messages are intended for debugging only. The format and content of log messages
152
+ > may change between releases.
153
+
154
+ #### Log levels
155
+
156
+ The log level can be configured in two ways:
157
+
158
+ 1. Via the `RELAY_LOG` environment variable
159
+ 2. Using the `logLevel` client option (overrides the environment variable if set)
160
+
161
+ ```ts
162
+ import Relay from '@relayapi/sdk';
163
+
164
+ const client = new Relay({
165
+ logLevel: 'debug', // Show all log messages
166
+ });
167
+ ```
168
+
169
+ Available log levels, from most to least verbose:
170
+
171
+ - `'debug'` - Show debug messages, info, warnings, and errors
172
+ - `'info'` - Show info messages, warnings, and errors
173
+ - `'warn'` - Show warnings and errors (default)
174
+ - `'error'` - Show only errors
175
+ - `'off'` - Disable all logging
176
+
177
+ At the `'debug'` level, all HTTP requests and responses are logged, including headers and bodies.
178
+ Some authentication-related headers are redacted, but sensitive data in request and response bodies
179
+ may still be visible.
180
+
181
+ #### Custom logger
182
+
183
+ By default, this library logs to `globalThis.console`. You can also provide a custom logger.
184
+ Most logging libraries are supported, including [pino](https://www.npmjs.com/package/pino), [winston](https://www.npmjs.com/package/winston), [bunyan](https://www.npmjs.com/package/bunyan), [consola](https://www.npmjs.com/package/consola), [signale](https://www.npmjs.com/package/signale), and [@std/log](https://jsr.io/@std/log). If your logger doesn't work, please open an issue.
185
+
186
+ When providing a custom logger, the `logLevel` option still controls which messages are emitted, messages
187
+ below the configured level will not be sent to your logger.
188
+
189
+ ```ts
190
+ import Relay from '@relayapi/sdk';
191
+ import pino from 'pino';
192
+
193
+ const logger = pino();
194
+
195
+ const client = new Relay({
196
+ logger: logger.child({ name: 'Relay' }),
197
+ logLevel: 'debug', // Send all messages to pino, allowing it to filter
198
+ });
199
+ ```
200
+
201
+ ### Making custom/undocumented requests
202
+
203
+ This library is typed for convenient access to the documented API. If you need to access undocumented
204
+ endpoints, params, or response properties, the library can still be used.
205
+
206
+ #### Undocumented endpoints
207
+
208
+ To make requests to undocumented endpoints, you can use `client.get`, `client.post`, and other HTTP verbs.
209
+ Options on the client, such as retries, will be respected when making these requests.
210
+
211
+ ```ts
212
+ await client.post('/some/path', {
213
+ body: { some_prop: 'foo' },
214
+ query: { some_query_arg: 'bar' },
215
+ });
216
+ ```
217
+
218
+ #### Undocumented request params
219
+
220
+ To make requests using undocumented parameters, you may use `// @ts-expect-error` on the undocumented
221
+ parameter. This library doesn't validate at runtime that the request matches the type, so any extra values you
222
+ send will be sent as-is.
223
+
224
+ ```ts
225
+ client.posts.list({
226
+ // ...
227
+ // @ts-expect-error baz is not yet public
228
+ baz: 'undocumented option',
229
+ });
230
+ ```
231
+
232
+ For requests with the `GET` verb, any extra params will be in the query, all other requests will send the
233
+ extra param in the body.
234
+
235
+ If you want to explicitly send an extra argument, you can do so with the `query`, `body`, and `headers` request
236
+ options.
237
+
238
+ #### Undocumented response properties
239
+
240
+ To access undocumented response properties, you may access the response object with `// @ts-expect-error` on
241
+ the response object, or cast the response object to the requisite type. Like the request params, we do not
242
+ validate or strip extra properties from the response from the API.
243
+
244
+ ### Customizing the fetch client
245
+
246
+ By default, this library expects a global `fetch` function is defined.
247
+
248
+ If you want to use a different `fetch` function, you can either polyfill the global:
249
+
250
+ ```ts
251
+ import fetch from 'my-fetch';
252
+
253
+ globalThis.fetch = fetch;
254
+ ```
255
+
256
+ Or pass it to the client:
257
+
258
+ ```ts
259
+ import Relay from '@relayapi/sdk';
260
+ import fetch from 'my-fetch';
261
+
262
+ const client = new Relay({ fetch });
263
+ ```
264
+
265
+ ### Fetch options
266
+
267
+ If you want to set custom `fetch` options without overriding the `fetch` function, you can provide a `fetchOptions` object when instantiating the client or making a request. (Request-specific options override client options.)
268
+
269
+ ```ts
270
+ import Relay from '@relayapi/sdk';
271
+
272
+ const client = new Relay({
273
+ fetchOptions: {
274
+ // `RequestInit` options
275
+ },
276
+ });
277
+ ```
278
+
279
+ #### Configuring proxies
280
+
281
+ To modify proxy behavior, you can provide custom `fetchOptions` that add runtime-specific proxy
282
+ options to requests:
283
+
284
+ <img src="https://raw.githubusercontent.com/stainless-api/sdk-assets/refs/heads/main/node.svg" align="top" width="18" height="21"> **Node** <sup>[[docs](https://github.com/nodejs/undici/blob/main/docs/docs/api/ProxyAgent.md#example---proxyagent-with-fetch)]</sup>
285
+
286
+ ```ts
287
+ import Relay from '@relayapi/sdk';
288
+ import * as undici from 'undici';
289
+
290
+ const proxyAgent = new undici.ProxyAgent('http://localhost:8888');
291
+ const client = new Relay({
292
+ fetchOptions: {
293
+ dispatcher: proxyAgent,
294
+ },
295
+ });
296
+ ```
297
+
298
+ <img src="https://raw.githubusercontent.com/stainless-api/sdk-assets/refs/heads/main/bun.svg" align="top" width="18" height="21"> **Bun** <sup>[[docs](https://bun.sh/guides/http/proxy)]</sup>
299
+
300
+ ```ts
301
+ import Relay from '@relayapi/sdk';
302
+
303
+ const client = new Relay({
304
+ fetchOptions: {
305
+ proxy: 'http://localhost:8888',
306
+ },
307
+ });
308
+ ```
309
+
310
+ <img src="https://raw.githubusercontent.com/stainless-api/sdk-assets/refs/heads/main/deno.svg" align="top" width="18" height="21"> **Deno** <sup>[[docs](https://docs.deno.com/api/deno/~/Deno.createHttpClient)]</sup>
311
+
312
+ ```ts
313
+ import Relay from 'npm:@relayapi/sdk';
314
+
315
+ const httpClient = Deno.createHttpClient({ proxy: { url: 'http://localhost:8888' } });
316
+ const client = new Relay({
317
+ fetchOptions: {
318
+ client: httpClient,
319
+ },
320
+ });
321
+ ```
322
+
323
+ ## Frequently Asked Questions
324
+
325
+ ## Semantic versioning
326
+
327
+ This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions:
328
+
329
+ 1. Changes that only affect static types, without breaking runtime behavior.
330
+ 2. Changes to library internals which are technically public but not intended or documented for external use. _(Please open a GitHub issue to let us know if you are relying on such internals.)_
331
+ 3. Changes that we do not expect to impact the vast majority of users in practice.
332
+
333
+ We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience.
334
+
335
+ We are keen for your feedback; please open an [issue](https://www.github.com/relayapi-dev/relay-node/issues) with questions, bugs, or suggestions.
336
+
337
+ ## Requirements
338
+
339
+ TypeScript >= 4.9 is supported.
340
+
341
+ The following runtimes are supported:
342
+
343
+ - Web browsers (Up-to-date Chrome, Firefox, Safari, Edge, and more)
344
+ - Node.js 20 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.
345
+ - Deno v1.28.0 or higher.
346
+ - Bun 1.0 or later.
347
+ - Cloudflare Workers.
348
+ - Vercel Edge Runtime.
349
+ - Jest 28 or greater with the `"node"` environment (`"jsdom"` is not supported at this time).
350
+ - Nitro v2.6 or greater.
351
+
352
+ Note that React Native is not supported at this time.
353
+
354
+ If you are interested in other runtime environments, please open or upvote an issue on GitHub.
355
+
356
+ ## Contributing
357
+
358
+ See [the contributing documentation](./CONTRIBUTING.md).
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.2.2";
1
+ export declare const VERSION = "0.3.0";
package/dist/version.js CHANGED
@@ -2,4 +2,4 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
4
  // Version is managed by release-please. Do not edit manually.
5
- exports.VERSION = '0.2.2'; // x-release-please-version
5
+ exports.VERSION = '0.3.0'; // x-release-please-version
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@relayapi/sdk",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "The official TypeScript library for the Relay API",
5
5
  "author": "Relay <support@relayapi.dev>",
6
6
  "license": "Apache-2.0",