@putdotio/sdk 9.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) put.io
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,286 @@
1
+ <div align="center">
2
+ <p>
3
+ <img src="https://static.put.io/images/putio-boncuk.png" width="72">
4
+ </p>
5
+
6
+ <h1>putio-sdk-typescript</h1>
7
+
8
+ <p>
9
+ TypeScript SDK for the <a href="https://api.put.io/v2/docs">put.io API</a>
10
+ </p>
11
+ <p>
12
+ Domain-first, schema-validated at the boundary, and designed for all runtimes.
13
+ </p>
14
+
15
+ <p>
16
+ <a href="https://github.com/putdotio/putio-sdk-typescript/actions/workflows/ci.yml?query=branch%3Amain" style="text-decoration:none;"><img src="https://img.shields.io/github/actions/workflow/status/putdotio/putio-sdk-typescript/ci.yml?branch=main&style=flat&label=ci&colorA=000000&colorB=000000" alt="CI"></a>
17
+ <a href="https://www.npmjs.com/package/@putdotio/sdk" style="text-decoration:none;"><img src="https://img.shields.io/npm/v/%40putdotio%2Fsdk?style=flat&colorA=000000&colorB=000000" alt="npm version"></a>
18
+ <a href="https://www.npmjs.com/package/@putdotio/sdk" style="text-decoration:none;"><img src="https://img.shields.io/npm/dm/%40putdotio%2Fsdk?style=flat&colorA=000000&colorB=000000" alt="npm downloads"></a>
19
+ <a href="https://github.com/putdotio/putio-sdk-typescript/blob/main/LICENSE" style="text-decoration:none;"><img src="https://img.shields.io/github/license/putdotio/putio-sdk-typescript?style=flat&colorA=000000&colorB=000000" alt="license"></a>
20
+ </p>
21
+ </div>
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ pnpm install @putdotio/sdk
27
+ ```
28
+
29
+ ## Promise Example
30
+
31
+ ```ts
32
+ import { createPutioSdkPromiseClient } from "@putdotio/sdk";
33
+
34
+ const sdk = createPutioSdkPromiseClient({
35
+ accessToken: process.env.PUTIO_TOKEN_FIRST_PARTY,
36
+ });
37
+
38
+ const info = await sdk.account.getInfo({
39
+ download_token: 1,
40
+ pas: 1,
41
+ });
42
+ ```
43
+
44
+ ## Effect Example
45
+
46
+ ```ts
47
+ import { Effect } from "effect";
48
+ import { createPutioSdkEffectClient, makePutioSdkLayer } from "@putdotio/sdk";
49
+
50
+ const sdk = createPutioSdkEffectClient();
51
+
52
+ const program = sdk.files
53
+ .list(0, {
54
+ per_page: 20,
55
+ total: 1,
56
+ })
57
+ .pipe(
58
+ Effect.provide(
59
+ makePutioSdkLayer({
60
+ accessToken: process.env.PUTIO_TOKEN_FIRST_PARTY!,
61
+ }),
62
+ ),
63
+ );
64
+
65
+ const result = await Effect.runPromise(program);
66
+ ```
67
+
68
+ ## Side-By-Side Usage
69
+
70
+ Both client styles expose the same domain surface:
71
+
72
+ ```ts
73
+ promiseClient.files.list(0, { per_page: 20 });
74
+ effectClient.files.list(0, { per_page: 20 });
75
+ ```
76
+
77
+ Choose the Promise client when you want standard async functions.
78
+ Choose the Effect client when you want the canonical typed error channel and Effect-native composition.
79
+
80
+ ## Client Shapes
81
+
82
+ | Client | Use it for |
83
+ | ------------------------------------- | ------------------------------------------------- |
84
+ | `createPutioSdkPromiseClient(config)` | React apps, scripts, server handlers, React Query |
85
+ | `createPutioSdkEffectClient()` | Effect-native workflows and service composition |
86
+
87
+ Effect is the canonical typed surface. The Promise client is an adapter for environments that want standard async functions.
88
+
89
+ - Effect client: keeps errors in the Effect error channel with operation-specific typing
90
+ - Promise client: throws tagged SDK error objects such as `PutioOperationError`, `PutioApiError`, and `PutioRateLimitError`
91
+
92
+ ## Namespace Surface
93
+
94
+ | Namespace | Purpose |
95
+ | --------------- | -------------------------------------------------------------------------- |
96
+ | `account` | account info, settings, confirmations, destructive account actions |
97
+ | `auth` | token validation, login flows, device/OOB helpers, two-factor flows |
98
+ | `config` | app-owned JSON config storage |
99
+ | `downloadLinks` | download-link bundles |
100
+ | `events` | history events and event torrent payloads |
101
+ | `family` | family members and invites |
102
+ | `files` | file listing, search, move/delete/extract, MP4, direct access URLs, upload |
103
+ | `friendInvites` | friend invitation management |
104
+ | `friends` | friend graph and requests |
105
+ | `ifttt` | IFTTT integration endpoints |
106
+ | `oauth` | OAuth app management |
107
+ | `payment` | plans, vouchers, payment flows, payment history |
108
+ | `rss` | RSS feed management |
109
+ | `sharing` | friend shares, public shares, clone flows |
110
+ | `transfers` | transfer list, add/retry/cancel/clean flows |
111
+ | `trash` | trash listing, restore, delete, empty |
112
+ | `tunnel` | route listing |
113
+ | `zips` | zip creation and lookup |
114
+
115
+ ## Design Rules
116
+
117
+ - schema-first contracts at every external boundary
118
+ - typed errors are first-class
119
+ - parameter-conditioned responses are modeled explicitly
120
+ - no compatibility namespace shims in the public API
121
+ - fetch-native core with runtime-portable Web APIs
122
+
123
+ ## Runtime Requirements
124
+
125
+ The package is designed around standard Web APIs. Host runtimes should provide:
126
+
127
+ - `fetch`
128
+ - `Request`, `Response`, and `Headers`
129
+ - `URL` and `URLSearchParams`
130
+ - `AbortController`
131
+ - `FormData`
132
+
133
+ For upload flows, the host should also provide file-compatible inputs such as `File` or `Blob`.
134
+
135
+ If a target runtime is missing these APIs, provide them with host-level polyfills or adapters instead of patching the SDK surface.
136
+
137
+ ## Error Handling
138
+
139
+ Promise consumers receive tagged SDK error objects:
140
+
141
+ ```ts
142
+ import {
143
+ PutioOperationError,
144
+ PutioRateLimitError,
145
+ createPutioSdkPromiseClient,
146
+ } from "@putdotio/sdk";
147
+
148
+ const sdk = createPutioSdkPromiseClient({
149
+ accessToken: process.env.PUTIO_TOKEN_FIRST_PARTY,
150
+ });
151
+
152
+ try {
153
+ await sdk.files.createFolder({
154
+ name: "",
155
+ parent_id: 0,
156
+ });
157
+ } catch (error) {
158
+ if (error instanceof PutioOperationError) {
159
+ if (error.operation === "createFolder") {
160
+ console.log(error.body.error_type);
161
+ }
162
+ }
163
+
164
+ if (error instanceof PutioRateLimitError) {
165
+ console.log(error.retryAfter);
166
+ }
167
+ }
168
+ ```
169
+
170
+ Effect consumers keep errors in the typed error channel instead of throwing:
171
+
172
+ ```ts
173
+ import { Effect } from "effect";
174
+ import { createPutioSdkEffectClient, makePutioSdkLayer } from "@putdotio/sdk";
175
+
176
+ const sdk = createPutioSdkEffectClient();
177
+
178
+ const handled = sdk.files
179
+ .createFolder({
180
+ name: "",
181
+ parent_id: 0,
182
+ })
183
+ .pipe(
184
+ Effect.catchTag("PutioOperationError", (error) => {
185
+ if (error.operation === "createFolder") {
186
+ return Effect.succeed(error.body.error_type);
187
+ }
188
+
189
+ return Effect.fail(error);
190
+ }),
191
+ Effect.provide(
192
+ makePutioSdkLayer({
193
+ accessToken: process.env.PUTIO_TOKEN_FIRST_PARTY!,
194
+ }),
195
+ ),
196
+ );
197
+ ```
198
+
199
+ ## Direct File Access
200
+
201
+ `files` exposes both JSON contracts and direct route helpers:
202
+
203
+ ```ts
204
+ const playlistUrl = await sdk.files.getHlsStreamUrl(fileId, {
205
+ maxSubtitleCount: 1,
206
+ });
207
+
208
+ const upload = await sdk.files.upload({
209
+ file: new File(["hello"], "hello.txt"),
210
+ parentId: 0,
211
+ });
212
+ ```
213
+
214
+ Upload targets `upload.put.io` internally because `api.put.io/v2/files/upload` is only a redirect shim.
215
+
216
+ ## TanStack Query
217
+
218
+ The Promise client plugs into TanStack Query directly:
219
+
220
+ ```ts
221
+ import { useQuery } from "@tanstack/react-query";
222
+ import { createPutioSdkPromiseClient } from "@putdotio/sdk";
223
+
224
+ const sdk = createPutioSdkPromiseClient({
225
+ accessToken: token,
226
+ });
227
+
228
+ export const useAccountInfo = () =>
229
+ useQuery({
230
+ queryKey: ["account", "info"],
231
+ queryFn: () => sdk.account.getInfo({ download_token: 1 }),
232
+ });
233
+ ```
234
+
235
+ The Effect client also works well when you want the canonical typed API:
236
+
237
+ ```ts
238
+ import { useQuery } from "@tanstack/react-query";
239
+ import { Effect } from "effect";
240
+ import { createPutioSdkEffectClient, makePutioSdkLayer } from "@putdotio/sdk";
241
+
242
+ const sdk = createPutioSdkEffectClient();
243
+ const sdkLayer = makePutioSdkLayer({
244
+ accessToken: token,
245
+ });
246
+
247
+ export const useFiles = (parentId: number) =>
248
+ useQuery({
249
+ queryKey: ["files", parentId],
250
+ queryFn: () =>
251
+ Effect.runPromise(sdk.files.list(parentId, { per_page: 50 }).pipe(Effect.provide(sdkLayer))),
252
+ });
253
+ ```
254
+
255
+ ## Verification
256
+
257
+ Local checks:
258
+
259
+ ```bash
260
+ vp install
261
+ vp check .
262
+ vp pack
263
+ vp test run --passWithNoTests
264
+ vp test run --coverage --passWithNoTests
265
+ ```
266
+
267
+ Live verification:
268
+
269
+ ```bash
270
+ vp run test:live
271
+ ```
272
+
273
+ Custom package scripts:
274
+
275
+ ```bash
276
+ vp run verify
277
+ vp run bootstrap:tokens
278
+ ```
279
+
280
+ ## Release Flow
281
+
282
+ Pushes to `main` verify the repo and then run `semantic-release`.
283
+ Add `NPM_TOKEN` in GitHub secrets and use conventional commits to drive version bumps.
284
+
285
+ Implementation and confidence notes live in `docs/READINESS.md`.
286
+ Target-by-target live verification guidance lives in `docs/TESTING.md`.