@gigadrive/sdk 0.1.2 → 0.2.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,177 @@
1
+ # @gigadrive/sdk
2
+
3
+ The official TypeScript SDK for the [Gigadrive Network](https://gigadrive.de) cloud
4
+ platform — organizations, applications, deployments, storage with resumable file
5
+ uploads, and an OpenAI-compatible AI Gateway.
6
+
7
+ Works in Node.js 18+, browsers, and edge runtimes (anything with `fetch`).
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install @gigadrive/sdk
13
+ # or: pnpm add @gigadrive/sdk
14
+ ```
15
+
16
+ ## Quick start
17
+
18
+ ```ts
19
+ import { GigadriveClient } from '@gigadrive/sdk';
20
+
21
+ // Credentials are auto-detected from the environment (see below),
22
+ // or pass them explicitly.
23
+ const client = new GigadriveClient({
24
+ clientId: process.env.GIGADRIVE_CLIENT_ID,
25
+ clientSecret: process.env.GIGADRIVE_CLIENT_SECRET,
26
+ });
27
+
28
+ const { items: organizations } = await client.organizations.list();
29
+ const { items: applications } = await client.applications.list();
30
+ ```
31
+
32
+ ## Authentication
33
+
34
+ Authentication is handled for you — tokens are fetched, cached, and refreshed
35
+ behind the scenes. Provide credentials via the constructor or environment
36
+ variables (constructor values take precedence):
37
+
38
+ | Method | Constructor | Environment |
39
+ | ---------------------------- | --------------------------------- | ------------------------------------------------- |
40
+ | API key (machine-to-machine) | `clientId` + `clientSecret` | `GIGADRIVE_CLIENT_ID` + `GIGADRIVE_CLIENT_SECRET` |
41
+ | Pre-obtained bearer token | `bearerToken` | `GIGADRIVE_BEARER_TOKEN` |
42
+ | Refresh token | `clientId` + `refreshToken` | `GIGADRIVE_CLIENT_ID` + `GIGADRIVE_REFRESH_TOKEN` |
43
+ | Authorization code + PKCE | `clientId` + `onAuthorizationUrl` | — |
44
+
45
+ ```ts
46
+ // Custom fetch / base URL (e.g. for tests or non-standard runtimes)
47
+ const client = new GigadriveClient({ bearerToken: 'eyJ...', fetch: myFetch });
48
+ ```
49
+
50
+ ## File uploads
51
+
52
+ The high-level `upload()` computes the required SHA-256 checksum, infers the
53
+ content type from the key, creates the upload session, and uploads the bytes
54
+ resumably — in one call.
55
+
56
+ ```ts
57
+ // Node.js — upload straight from a file path (size, checksum, type inferred)
58
+ const { url } = await client.applications.storage.upload({
59
+ applicationId,
60
+ bucketId,
61
+ key: 'reports/q1.pdf',
62
+ path: './q1-report.pdf',
63
+ });
64
+
65
+ // Browser — upload a File with progress and cancellation
66
+ const controller = new AbortController();
67
+ const { url } = await client.applications.storage.upload({
68
+ applicationId,
69
+ bucketId,
70
+ key: `uploads/${file.name}`,
71
+ data: file,
72
+ onProgress: (sent, total) => console.log(`${Math.round((sent / total) * 100)}%`),
73
+ signal: controller.signal,
74
+ });
75
+
76
+ // Wait until the object is finalized server-side, then read it back
77
+ const { object } = await client.applications.storage.upload({
78
+ applicationId,
79
+ bucketId,
80
+ key: 'avatars/user-1.png',
81
+ data: bytes,
82
+ waitForCompletion: true,
83
+ });
84
+ console.log(object?.contentLength, 'bytes stored');
85
+ ```
86
+
87
+ Accepted inputs: browser `File`/`Blob`, Node `Buffer`/`Uint8Array`/`ArrayBuffer`,
88
+ a Node filesystem `path`, or a Node readable `stream` (with `contentLength` and
89
+ `checksumSha256`).
90
+
91
+ ### Many files at once
92
+
93
+ ```ts
94
+ const results = await client.applications.storage.uploadBatch(
95
+ files.map((f) => ({ applicationId, bucketId, key: `uploads/${f.name}`, data: f })),
96
+ { concurrency: 6, onProgress: (done, total) => console.log(`${done}/${total}`) }
97
+ );
98
+ const failed = results.filter((r) => r.error);
99
+ ```
100
+
101
+ ### Working with objects
102
+
103
+ ```ts
104
+ // List a "folder" one level deep
105
+ const { items, commonPrefixes } = await client.applications.storage.objects.list(applicationId, bucketId, {
106
+ prefix: 'images/',
107
+ limit: 100,
108
+ });
109
+
110
+ // Signed download URL for a private object
111
+ const { url } = await client.applications.storage.objects.getAccessUrl(applicationId, bucketId, objectId, {
112
+ expiresInSeconds: 3600,
113
+ });
114
+ ```
115
+
116
+ ## AI Gateway
117
+
118
+ OpenAI-compatible chat completions, responses, audio, video, and model discovery.
119
+
120
+ ```ts
121
+ // Chat completion
122
+ const res = await client.aiGateway.chatCompletions({
123
+ model: 'openai/gpt-4o',
124
+ messages: [{ role: 'user', content: 'Hello!' }],
125
+ });
126
+ console.log(res.choices[0].message.content);
127
+
128
+ // Streaming
129
+ for await (const chunk of client.aiGateway.chatCompletionsStream({
130
+ model: 'openai/gpt-4o',
131
+ messages: [{ role: 'user', content: 'Write a haiku about the sea.' }],
132
+ })) {
133
+ process.stdout.write(chunk.choices[0]?.delta.content ?? '');
134
+ }
135
+
136
+ // Models
137
+ const { items: models } = await client.aiGateway.listModels();
138
+ ```
139
+
140
+ Organization-scoped governance (usage analytics, budgets, policies) lives under
141
+ `client.organizations.aiGateway`.
142
+
143
+ ## Pagination
144
+
145
+ List endpoints accept `page` / `perPage` / `cursor` and return `{ items, total }`
146
+ (cursor-paginated endpoints also return `nextCursor`). Iterate everything with
147
+ the `paginate` helper:
148
+
149
+ ```ts
150
+ import { paginate } from '@gigadrive/sdk';
151
+
152
+ for await (const object of paginate((cursor) =>
153
+ client.applications.storage.objects.list(applicationId, bucketId, { cursor })
154
+ )) {
155
+ console.log(object.key);
156
+ }
157
+ ```
158
+
159
+ ## Errors
160
+
161
+ All errors extend `GigadriveError`. Notable subclasses: `ApiError` (with `status`
162
+ and optional `code`), `AuthenticationError`, `UploadError`, and
163
+ `UploadSessionExpiredError`.
164
+
165
+ ```ts
166
+ import { ApiError } from '@gigadrive/sdk';
167
+
168
+ try {
169
+ await client.deployments.get('missing');
170
+ } catch (err) {
171
+ if (err instanceof ApiError) console.error(err.status, err.message);
172
+ }
173
+ ```
174
+
175
+ ## License
176
+
177
+ Apache-2.0