@buildspacestudio/sdk 0.2.1 → 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/dist/auth/index.cjs +259 -0
- package/dist/auth/index.cjs.map +1 -0
- package/dist/auth/{server.d.ts → index.d.cts} +9 -6
- package/dist/auth/index.d.ts +118 -5
- package/dist/auth/index.js +255 -2
- package/dist/auth/index.js.map +1 -1
- package/dist/client/index.cjs +428 -0
- package/dist/client/index.cjs.map +1 -0
- package/dist/client/index.d.cts +85 -0
- package/dist/client/index.d.ts +8 -7
- package/dist/client/index.js +416 -105
- package/dist/client/index.js.map +1 -1
- package/dist/{auth/client.d.ts → client-BH7LbrKM.d.ts} +7 -5
- package/dist/client-C67hy1kt.d.cts +58 -0
- package/dist/{events/client.d.ts → client-Dlif1JBf.d.ts} +5 -3
- package/dist/{auth/client.js → client-DqWXAwCr.d.cts} +27 -30
- package/dist/events/index.cjs +131 -0
- package/dist/events/index.cjs.map +1 -0
- package/dist/events/{server.d.ts → index.d.cts} +7 -4
- package/dist/events/index.d.ts +59 -4
- package/dist/events/index.js +127 -2
- package/dist/events/index.js.map +1 -1
- package/dist/http-U-zzKmFF.d.cts +100 -0
- package/dist/http-U-zzKmFF.d.ts +100 -0
- package/dist/index.cjs +801 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +87 -0
- package/dist/index.d.ts +16 -15
- package/dist/index.js +785 -106
- package/dist/index.js.map +1 -1
- package/dist/notifications/index.cjs +56 -0
- package/dist/notifications/index.cjs.map +1 -0
- package/dist/notifications/{server.d.ts → index.d.cts} +8 -6
- package/dist/notifications/index.d.ts +79 -3
- package/dist/notifications/index.js +53 -1
- package/dist/notifications/index.js.map +1 -1
- package/dist/storage/index.cjs +213 -0
- package/dist/storage/index.cjs.map +1 -0
- package/dist/storage/index.d.cts +195 -0
- package/dist/storage/index.d.ts +195 -5
- package/dist/storage/index.js +209 -2
- package/dist/storage/index.js.map +1 -1
- package/package.json +75 -16
- package/dist/auth/client.d.ts.map +0 -1
- package/dist/auth/client.js.map +0 -1
- package/dist/auth/index.d.ts.map +0 -1
- package/dist/auth/server.d.ts.map +0 -1
- package/dist/auth/server.js +0 -148
- package/dist/auth/server.js.map +0 -1
- package/dist/client/index.d.ts.map +0 -1
- package/dist/config.d.ts +0 -40
- package/dist/config.d.ts.map +0 -1
- package/dist/config.js +0 -27
- package/dist/config.js.map +0 -1
- package/dist/errors.d.ts +0 -31
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js +0 -30
- package/dist/errors.js.map +0 -1
- package/dist/events/client.d.ts.map +0 -1
- package/dist/events/client.js +0 -97
- package/dist/events/client.js.map +0 -1
- package/dist/events/index.d.ts.map +0 -1
- package/dist/events/server.d.ts.map +0 -1
- package/dist/events/server.js +0 -65
- package/dist/events/server.js.map +0 -1
- package/dist/http.d.ts +0 -39
- package/dist/http.d.ts.map +0 -1
- package/dist/http.js +0 -74
- package/dist/http.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/notifications/index.d.ts.map +0 -1
- package/dist/notifications/server.d.ts.map +0 -1
- package/dist/notifications/server.js +0 -73
- package/dist/notifications/server.js.map +0 -1
- package/dist/storage/client.d.ts +0 -91
- package/dist/storage/client.d.ts.map +0 -1
- package/dist/storage/client.js +0 -117
- package/dist/storage/client.js.map +0 -1
- package/dist/storage/index.d.ts.map +0 -1
- package/dist/storage/server.d.ts +0 -104
- package/dist/storage/server.d.ts.map +0 -1
- package/dist/storage/server.js +0 -104
- package/dist/storage/server.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/errors.ts","../../src/storage/client.ts","../../src/storage/server.ts"],"names":[],"mappings":";;;AAiBO,IAAM,eAAA,GAAN,cAA8B,KAAA,CAAM;AAAA;AAAA,EAEhC,IAAA;AAAA;AAAA,EAEA,OAAA;AAAA;AAAA,EAEA,MAAA;AAAA,EAET,WAAA,CAAY;AAAA,IACV,IAA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;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,OAAO,GAAA,EAA4B;AACvC,IAAA,MAAM,IAAA,CAAK,UAAU,OAAA,CAAc;AAAA,MACjC,OAAA,EAAS,SAAA;AAAA,MACT,IAAA,EAAM,oBAAA;AAAA,MACN,MAAA,EAAQ,QAAA;AAAA,MACR,IAAA,EAAM,EAAE,GAAA;AAAI,KACb,CAAA;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,QAAA,GAIG;AACD,IAAA,OAAO,IAAA,CAAK,UAAU,OAAA,CAInB;AAAA,MACD,OAAA,EAAS,SAAA;AAAA,MACT,IAAA,EAAM;AAAA,KACP,CAAA;AAAA,EACH;AACF","file":"index.cjs","sourcesContent":["/** Services available in the Buildspace platform. */\nexport type BuildspaceService = \"auth\" | \"events\" | \"notifications\" | \"storage\";\n\n/**\n * Error thrown by all Buildspace SDK methods on failure.\n *\n * @example\n * ```ts\n * try {\n * await buildspace.auth.getSession(token);\n * } catch (err) {\n * if (err instanceof BuildspaceError) {\n * console.error(err.service, err.code, err.status, err.message);\n * }\n * }\n * ```\n */\nexport class BuildspaceError extends Error {\n /** Machine-readable error code, e.g. `\"auth/invalid-token\"`. */\n readonly code: string;\n /** Which service produced the error. */\n readonly service: BuildspaceService;\n /** HTTP status code from the API. */\n readonly status: number;\n\n constructor({\n code,\n message,\n service,\n status,\n }: {\n code: string;\n message: string;\n service: BuildspaceService;\n status: number;\n }) {\n super(message);\n this.name = \"BuildspaceError\";\n this.code = code;\n this.service = service;\n this.status = status;\n }\n}\n","import { BuildspaceError } from \"../errors\";\nimport type { HttpTransport } from \"../http\";\nimport type { StorageObject } from \"./server\";\n\n/** Options for uploading a file from the client. */\nexport interface UploadOptions {\n /** MIME type override. If omitted, inferred from the `File` object or defaults to `\"application/octet-stream\"`. */\n contentType?: string;\n /** Storage path/key for the file, e.g. `\"avatars/user-123.png\"`. */\n path: string;\n}\n\n/** Result of a successful client-side file upload. */\nexport interface UploadResponse {\n /** The resolved storage key. */\n key: string;\n /** File size in bytes. */\n size: number;\n /** Signed download URL for the uploaded file. */\n url: string;\n}\n\n/** Result of requesting a signed download URL. */\nexport interface GetUrlResponse {\n /** URL lifetime in seconds, if applicable. */\n expiresIn?: number;\n /** The storage key of the object. */\n key: string;\n /** The signed download URL. */\n url: string;\n}\n\n/**\n * Client-side storage methods for uploading and managing files.\n *\n * Access via `buildspace.storage` on the client SDK.\n *\n * @example\n * ```ts\n * // Upload a file (handles signed URL + PUT automatically)\n * const { key, url } = await buildspace.storage.upload(file, {\n * path: \"avatars/user-123.png\",\n * });\n *\n * // Get a download URL for an existing file\n * const { url } = await buildspace.storage.getUrl(key);\n * ```\n */\nexport class StorageClientNamespace {\n private readonly transport: HttpTransport;\n\n constructor(transport: HttpTransport) {\n this.transport = transport;\n }\n\n /**\n * Upload a `File` or `Blob` to Buildspace storage.\n *\n * This method handles the full upload flow:\n * 1. Requests a pre-signed upload URL from the API\n * 2. Uploads the file directly to the storage provider\n * 3. Returns a signed download URL for the uploaded file\n *\n * @param file - The file or blob to upload.\n * @param opts - Upload options including the storage path.\n * @returns The storage key, file size, and a signed download URL.\n *\n * @throws {BuildspaceError} If the upload URL request or the direct upload fails.\n */\n async upload(file: File | Blob, opts: UploadOptions): Promise<UploadResponse> {\n const contentType =\n opts.contentType ?? (file instanceof File ? file.type : \"application/octet-stream\");\n\n const signed = await this.transport.request<{ key: string; upload_url: string }>({\n service: \"storage\",\n path: \"/v1/storage/upload\",\n method: \"POST\",\n body: {\n path: opts.path,\n content_type: contentType,\n size: file.size,\n },\n });\n\n const response = await this.transport.fetcher(signed.upload_url, {\n method: \"PUT\",\n headers: { \"Content-Type\": contentType },\n body: file,\n });\n\n if (!response.ok) {\n throw new BuildspaceError({\n service: \"storage\",\n status: response.status,\n code: \"storage/upload-failed\",\n message: `Direct upload failed: ${response.statusText}`,\n });\n }\n\n const { url } = await this.getUrl(signed.key);\n\n return {\n key: signed.key,\n size: file.size,\n url,\n };\n }\n\n /**\n * Get a time-limited signed URL for downloading a stored file.\n *\n * @param key - The storage key of the object.\n * @param opts - Optional settings.\n * @param opts.expiresIn - URL lifetime in seconds.\n */\n getUrl(key: string, opts?: { expiresIn?: number }): Promise<GetUrlResponse> {\n return this.transport.request<GetUrlResponse>({\n service: \"storage\",\n path: \"/v1/storage/url\",\n query: {\n key,\n expires_in: opts?.expiresIn,\n },\n });\n }\n\n /**\n * List stored objects, optionally filtered by key prefix.\n *\n * @param prefix - Only return objects whose key starts with this prefix.\n * @param opts - Pagination options.\n */\n list(\n prefix?: string,\n opts?: { limit?: number; offset?: number }\n ): Promise<{ objects: StorageObject[] }> {\n return this.transport.request<{ objects: StorageObject[] }>({\n service: \"storage\",\n path: \"/v1/storage/objects\",\n query: {\n prefix,\n limit: opts?.limit,\n offset: opts?.offset,\n },\n });\n }\n\n /**\n * Delete a stored object by key.\n *\n * @param key - The storage key of the object to delete.\n */\n async delete(key: string): Promise<void> {\n await this.transport.request<void>({\n service: \"storage\",\n path: \"/v1/storage/object\",\n method: \"DELETE\",\n body: { key },\n });\n }\n}\n","import type { HttpTransport } from \"../http\";\n\n/** Options for requesting a pre-signed upload URL on the server. */\nexport interface ServerUploadOptions {\n /** MIME type of the file, e.g. `\"image/png\"`. */\n contentType: string;\n /** Storage path/key for the file, e.g. `\"avatars/user-123.png\"`. */\n key: string;\n /** File size in bytes. */\n size: number;\n}\n\n/** Metadata for a stored object. */\nexport interface StorageObject {\n /** MIME type of the stored file. */\n contentType: string;\n /** ISO 8601 timestamp of when the object was created. */\n createdAt: string;\n /** Unique object identifier. */\n id: string;\n /** Storage key/path of the object. */\n key: string;\n /** File size in bytes. */\n size: number;\n /** ID of the user who uploaded the object, or `null` if uploaded via server key. */\n uploadedBy: string | null;\n}\n\n/**\n * Server-side storage methods for managing file uploads and downloads.\n *\n * Access via `buildspace.storage` on the server SDK.\n *\n * @example\n * ```ts\n * // Get a pre-signed upload URL\n * const { upload_url, key } = await buildspace.storage.getUploadUrl({\n * key: \"avatars/user-123.png\",\n * contentType: \"image/png\",\n * size: file.size,\n * });\n *\n * // Upload directly to the signed URL\n * await fetch(upload_url, { method: \"PUT\", body: file });\n *\n * // Get a signed download URL\n * const { url } = await buildspace.storage.getSignedUrl(key);\n * ```\n */\nexport class StorageServerNamespace {\n private readonly transport: HttpTransport;\n\n constructor(transport: HttpTransport) {\n this.transport = transport;\n }\n\n /**\n * Request a pre-signed URL for direct file upload.\n *\n * @param opts - Upload options including the storage key, content type, and file size.\n * @returns The signed upload URL, the resolved key, and expiry time in seconds.\n */\n getUploadUrl(\n opts: ServerUploadOptions\n ): Promise<{ expires_in: number; key: string; upload_url: string }> {\n return this.transport.request<{ expires_in: number; key: string; upload_url: string }>({\n service: \"storage\",\n path: \"/v1/storage/upload\",\n method: \"POST\",\n body: {\n path: opts.key,\n content_type: opts.contentType,\n size: opts.size,\n },\n });\n }\n\n /**\n * Get a time-limited signed URL for downloading a stored file.\n *\n * @param key - The storage key of the object.\n * @param opts - Optional settings.\n * @param opts.expiresIn - URL lifetime in seconds. Uses server default if omitted.\n */\n getSignedUrl(\n key: string,\n opts?: { expiresIn?: number }\n ): Promise<{ expiresIn?: number; key: string; url: string }> {\n return this.transport.request<{ expiresIn?: number; key: string; url: string }>({\n service: \"storage\",\n path: \"/v1/storage/url\",\n query: {\n key,\n expires_in: opts?.expiresIn,\n },\n });\n }\n\n /**\n * List stored objects, optionally filtered by key prefix.\n *\n * @param prefix - Only return objects whose key starts with this prefix, e.g. `\"avatars/\"`.\n * @param opts - Pagination options.\n */\n list(\n prefix?: string,\n opts?: { limit?: number; offset?: number }\n ): Promise<{ objects: StorageObject[] }> {\n return this.transport.request<{ objects: StorageObject[] }>({\n service: \"storage\",\n path: \"/v1/storage/objects\",\n query: {\n prefix,\n limit: opts?.limit,\n offset: opts?.offset,\n },\n });\n }\n\n /**\n * Delete a stored object by key.\n *\n * @param key - The storage key of the object to delete.\n */\n async delete(key: string): Promise<void> {\n await this.transport.request<void>({\n service: \"storage\",\n path: \"/v1/storage/object\",\n method: \"DELETE\",\n body: { key },\n });\n }\n\n /**\n * Get current storage usage statistics for the app.\n *\n * @returns Byte counts and object count.\n */\n getUsage(): Promise<{\n maxStorageBytes: number;\n objectCount: number;\n storageBytes: number;\n }> {\n return this.transport.request<{\n maxStorageBytes: number;\n objectCount: number;\n storageBytes: number;\n }>({\n service: \"storage\",\n path: \"/v1/storage/usage\",\n });\n }\n}\n"]}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { H as HttpTransport } from '../http-U-zzKmFF.cjs';
|
|
2
|
+
|
|
3
|
+
/** Options for requesting a pre-signed upload URL on the server. */
|
|
4
|
+
interface ServerUploadOptions {
|
|
5
|
+
/** MIME type of the file, e.g. `"image/png"`. */
|
|
6
|
+
contentType: string;
|
|
7
|
+
/** Storage path/key for the file, e.g. `"avatars/user-123.png"`. */
|
|
8
|
+
key: string;
|
|
9
|
+
/** File size in bytes. */
|
|
10
|
+
size: number;
|
|
11
|
+
}
|
|
12
|
+
/** Metadata for a stored object. */
|
|
13
|
+
interface StorageObject {
|
|
14
|
+
/** MIME type of the stored file. */
|
|
15
|
+
contentType: string;
|
|
16
|
+
/** ISO 8601 timestamp of when the object was created. */
|
|
17
|
+
createdAt: string;
|
|
18
|
+
/** Unique object identifier. */
|
|
19
|
+
id: string;
|
|
20
|
+
/** Storage key/path of the object. */
|
|
21
|
+
key: string;
|
|
22
|
+
/** File size in bytes. */
|
|
23
|
+
size: number;
|
|
24
|
+
/** ID of the user who uploaded the object, or `null` if uploaded via server key. */
|
|
25
|
+
uploadedBy: string | null;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Server-side storage methods for managing file uploads and downloads.
|
|
29
|
+
*
|
|
30
|
+
* Access via `buildspace.storage` on the server SDK.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* // Get a pre-signed upload URL
|
|
35
|
+
* const { upload_url, key } = await buildspace.storage.getUploadUrl({
|
|
36
|
+
* key: "avatars/user-123.png",
|
|
37
|
+
* contentType: "image/png",
|
|
38
|
+
* size: file.size,
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* // Upload directly to the signed URL
|
|
42
|
+
* await fetch(upload_url, { method: "PUT", body: file });
|
|
43
|
+
*
|
|
44
|
+
* // Get a signed download URL
|
|
45
|
+
* const { url } = await buildspace.storage.getSignedUrl(key);
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare class StorageServerNamespace {
|
|
49
|
+
private readonly transport;
|
|
50
|
+
constructor(transport: HttpTransport);
|
|
51
|
+
/**
|
|
52
|
+
* Request a pre-signed URL for direct file upload.
|
|
53
|
+
*
|
|
54
|
+
* @param opts - Upload options including the storage key, content type, and file size.
|
|
55
|
+
* @returns The signed upload URL, the resolved key, and expiry time in seconds.
|
|
56
|
+
*/
|
|
57
|
+
getUploadUrl(opts: ServerUploadOptions): Promise<{
|
|
58
|
+
expires_in: number;
|
|
59
|
+
key: string;
|
|
60
|
+
upload_url: string;
|
|
61
|
+
}>;
|
|
62
|
+
/**
|
|
63
|
+
* Get a time-limited signed URL for downloading a stored file.
|
|
64
|
+
*
|
|
65
|
+
* @param key - The storage key of the object.
|
|
66
|
+
* @param opts - Optional settings.
|
|
67
|
+
* @param opts.expiresIn - URL lifetime in seconds. Uses server default if omitted.
|
|
68
|
+
*/
|
|
69
|
+
getSignedUrl(key: string, opts?: {
|
|
70
|
+
expiresIn?: number;
|
|
71
|
+
}): Promise<{
|
|
72
|
+
expiresIn?: number;
|
|
73
|
+
key: string;
|
|
74
|
+
url: string;
|
|
75
|
+
}>;
|
|
76
|
+
/**
|
|
77
|
+
* List stored objects, optionally filtered by key prefix.
|
|
78
|
+
*
|
|
79
|
+
* @param prefix - Only return objects whose key starts with this prefix, e.g. `"avatars/"`.
|
|
80
|
+
* @param opts - Pagination options.
|
|
81
|
+
*/
|
|
82
|
+
list(prefix?: string, opts?: {
|
|
83
|
+
limit?: number;
|
|
84
|
+
offset?: number;
|
|
85
|
+
}): Promise<{
|
|
86
|
+
objects: StorageObject[];
|
|
87
|
+
}>;
|
|
88
|
+
/**
|
|
89
|
+
* Delete a stored object by key.
|
|
90
|
+
*
|
|
91
|
+
* @param key - The storage key of the object to delete.
|
|
92
|
+
*/
|
|
93
|
+
delete(key: string): Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* Get current storage usage statistics for the app.
|
|
96
|
+
*
|
|
97
|
+
* @returns Byte counts and object count.
|
|
98
|
+
*/
|
|
99
|
+
getUsage(): Promise<{
|
|
100
|
+
maxStorageBytes: number;
|
|
101
|
+
objectCount: number;
|
|
102
|
+
storageBytes: number;
|
|
103
|
+
}>;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Options for uploading a file from the client. */
|
|
107
|
+
interface UploadOptions {
|
|
108
|
+
/** MIME type override. If omitted, inferred from the `File` object or defaults to `"application/octet-stream"`. */
|
|
109
|
+
contentType?: string;
|
|
110
|
+
/** Storage path/key for the file, e.g. `"avatars/user-123.png"`. */
|
|
111
|
+
path: string;
|
|
112
|
+
}
|
|
113
|
+
/** Result of a successful client-side file upload. */
|
|
114
|
+
interface UploadResponse {
|
|
115
|
+
/** The resolved storage key. */
|
|
116
|
+
key: string;
|
|
117
|
+
/** File size in bytes. */
|
|
118
|
+
size: number;
|
|
119
|
+
/** Signed download URL for the uploaded file. */
|
|
120
|
+
url: string;
|
|
121
|
+
}
|
|
122
|
+
/** Result of requesting a signed download URL. */
|
|
123
|
+
interface GetUrlResponse {
|
|
124
|
+
/** URL lifetime in seconds, if applicable. */
|
|
125
|
+
expiresIn?: number;
|
|
126
|
+
/** The storage key of the object. */
|
|
127
|
+
key: string;
|
|
128
|
+
/** The signed download URL. */
|
|
129
|
+
url: string;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Client-side storage methods for uploading and managing files.
|
|
133
|
+
*
|
|
134
|
+
* Access via `buildspace.storage` on the client SDK.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* // Upload a file (handles signed URL + PUT automatically)
|
|
139
|
+
* const { key, url } = await buildspace.storage.upload(file, {
|
|
140
|
+
* path: "avatars/user-123.png",
|
|
141
|
+
* });
|
|
142
|
+
*
|
|
143
|
+
* // Get a download URL for an existing file
|
|
144
|
+
* const { url } = await buildspace.storage.getUrl(key);
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
declare class StorageClientNamespace {
|
|
148
|
+
private readonly transport;
|
|
149
|
+
constructor(transport: HttpTransport);
|
|
150
|
+
/**
|
|
151
|
+
* Upload a `File` or `Blob` to Buildspace storage.
|
|
152
|
+
*
|
|
153
|
+
* This method handles the full upload flow:
|
|
154
|
+
* 1. Requests a pre-signed upload URL from the API
|
|
155
|
+
* 2. Uploads the file directly to the storage provider
|
|
156
|
+
* 3. Returns a signed download URL for the uploaded file
|
|
157
|
+
*
|
|
158
|
+
* @param file - The file or blob to upload.
|
|
159
|
+
* @param opts - Upload options including the storage path.
|
|
160
|
+
* @returns The storage key, file size, and a signed download URL.
|
|
161
|
+
*
|
|
162
|
+
* @throws {BuildspaceError} If the upload URL request or the direct upload fails.
|
|
163
|
+
*/
|
|
164
|
+
upload(file: File | Blob, opts: UploadOptions): Promise<UploadResponse>;
|
|
165
|
+
/**
|
|
166
|
+
* Get a time-limited signed URL for downloading a stored file.
|
|
167
|
+
*
|
|
168
|
+
* @param key - The storage key of the object.
|
|
169
|
+
* @param opts - Optional settings.
|
|
170
|
+
* @param opts.expiresIn - URL lifetime in seconds.
|
|
171
|
+
*/
|
|
172
|
+
getUrl(key: string, opts?: {
|
|
173
|
+
expiresIn?: number;
|
|
174
|
+
}): Promise<GetUrlResponse>;
|
|
175
|
+
/**
|
|
176
|
+
* List stored objects, optionally filtered by key prefix.
|
|
177
|
+
*
|
|
178
|
+
* @param prefix - Only return objects whose key starts with this prefix.
|
|
179
|
+
* @param opts - Pagination options.
|
|
180
|
+
*/
|
|
181
|
+
list(prefix?: string, opts?: {
|
|
182
|
+
limit?: number;
|
|
183
|
+
offset?: number;
|
|
184
|
+
}): Promise<{
|
|
185
|
+
objects: StorageObject[];
|
|
186
|
+
}>;
|
|
187
|
+
/**
|
|
188
|
+
* Delete a stored object by key.
|
|
189
|
+
*
|
|
190
|
+
* @param key - The storage key of the object to delete.
|
|
191
|
+
*/
|
|
192
|
+
delete(key: string): Promise<void>;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export { type GetUrlResponse, type ServerUploadOptions, StorageClientNamespace, type StorageObject, StorageServerNamespace, type UploadOptions, type UploadResponse };
|
package/dist/storage/index.d.ts
CHANGED
|
@@ -1,5 +1,195 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { H as HttpTransport } from '../http-U-zzKmFF.js';
|
|
2
|
+
|
|
3
|
+
/** Options for requesting a pre-signed upload URL on the server. */
|
|
4
|
+
interface ServerUploadOptions {
|
|
5
|
+
/** MIME type of the file, e.g. `"image/png"`. */
|
|
6
|
+
contentType: string;
|
|
7
|
+
/** Storage path/key for the file, e.g. `"avatars/user-123.png"`. */
|
|
8
|
+
key: string;
|
|
9
|
+
/** File size in bytes. */
|
|
10
|
+
size: number;
|
|
11
|
+
}
|
|
12
|
+
/** Metadata for a stored object. */
|
|
13
|
+
interface StorageObject {
|
|
14
|
+
/** MIME type of the stored file. */
|
|
15
|
+
contentType: string;
|
|
16
|
+
/** ISO 8601 timestamp of when the object was created. */
|
|
17
|
+
createdAt: string;
|
|
18
|
+
/** Unique object identifier. */
|
|
19
|
+
id: string;
|
|
20
|
+
/** Storage key/path of the object. */
|
|
21
|
+
key: string;
|
|
22
|
+
/** File size in bytes. */
|
|
23
|
+
size: number;
|
|
24
|
+
/** ID of the user who uploaded the object, or `null` if uploaded via server key. */
|
|
25
|
+
uploadedBy: string | null;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Server-side storage methods for managing file uploads and downloads.
|
|
29
|
+
*
|
|
30
|
+
* Access via `buildspace.storage` on the server SDK.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* // Get a pre-signed upload URL
|
|
35
|
+
* const { upload_url, key } = await buildspace.storage.getUploadUrl({
|
|
36
|
+
* key: "avatars/user-123.png",
|
|
37
|
+
* contentType: "image/png",
|
|
38
|
+
* size: file.size,
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* // Upload directly to the signed URL
|
|
42
|
+
* await fetch(upload_url, { method: "PUT", body: file });
|
|
43
|
+
*
|
|
44
|
+
* // Get a signed download URL
|
|
45
|
+
* const { url } = await buildspace.storage.getSignedUrl(key);
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare class StorageServerNamespace {
|
|
49
|
+
private readonly transport;
|
|
50
|
+
constructor(transport: HttpTransport);
|
|
51
|
+
/**
|
|
52
|
+
* Request a pre-signed URL for direct file upload.
|
|
53
|
+
*
|
|
54
|
+
* @param opts - Upload options including the storage key, content type, and file size.
|
|
55
|
+
* @returns The signed upload URL, the resolved key, and expiry time in seconds.
|
|
56
|
+
*/
|
|
57
|
+
getUploadUrl(opts: ServerUploadOptions): Promise<{
|
|
58
|
+
expires_in: number;
|
|
59
|
+
key: string;
|
|
60
|
+
upload_url: string;
|
|
61
|
+
}>;
|
|
62
|
+
/**
|
|
63
|
+
* Get a time-limited signed URL for downloading a stored file.
|
|
64
|
+
*
|
|
65
|
+
* @param key - The storage key of the object.
|
|
66
|
+
* @param opts - Optional settings.
|
|
67
|
+
* @param opts.expiresIn - URL lifetime in seconds. Uses server default if omitted.
|
|
68
|
+
*/
|
|
69
|
+
getSignedUrl(key: string, opts?: {
|
|
70
|
+
expiresIn?: number;
|
|
71
|
+
}): Promise<{
|
|
72
|
+
expiresIn?: number;
|
|
73
|
+
key: string;
|
|
74
|
+
url: string;
|
|
75
|
+
}>;
|
|
76
|
+
/**
|
|
77
|
+
* List stored objects, optionally filtered by key prefix.
|
|
78
|
+
*
|
|
79
|
+
* @param prefix - Only return objects whose key starts with this prefix, e.g. `"avatars/"`.
|
|
80
|
+
* @param opts - Pagination options.
|
|
81
|
+
*/
|
|
82
|
+
list(prefix?: string, opts?: {
|
|
83
|
+
limit?: number;
|
|
84
|
+
offset?: number;
|
|
85
|
+
}): Promise<{
|
|
86
|
+
objects: StorageObject[];
|
|
87
|
+
}>;
|
|
88
|
+
/**
|
|
89
|
+
* Delete a stored object by key.
|
|
90
|
+
*
|
|
91
|
+
* @param key - The storage key of the object to delete.
|
|
92
|
+
*/
|
|
93
|
+
delete(key: string): Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* Get current storage usage statistics for the app.
|
|
96
|
+
*
|
|
97
|
+
* @returns Byte counts and object count.
|
|
98
|
+
*/
|
|
99
|
+
getUsage(): Promise<{
|
|
100
|
+
maxStorageBytes: number;
|
|
101
|
+
objectCount: number;
|
|
102
|
+
storageBytes: number;
|
|
103
|
+
}>;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Options for uploading a file from the client. */
|
|
107
|
+
interface UploadOptions {
|
|
108
|
+
/** MIME type override. If omitted, inferred from the `File` object or defaults to `"application/octet-stream"`. */
|
|
109
|
+
contentType?: string;
|
|
110
|
+
/** Storage path/key for the file, e.g. `"avatars/user-123.png"`. */
|
|
111
|
+
path: string;
|
|
112
|
+
}
|
|
113
|
+
/** Result of a successful client-side file upload. */
|
|
114
|
+
interface UploadResponse {
|
|
115
|
+
/** The resolved storage key. */
|
|
116
|
+
key: string;
|
|
117
|
+
/** File size in bytes. */
|
|
118
|
+
size: number;
|
|
119
|
+
/** Signed download URL for the uploaded file. */
|
|
120
|
+
url: string;
|
|
121
|
+
}
|
|
122
|
+
/** Result of requesting a signed download URL. */
|
|
123
|
+
interface GetUrlResponse {
|
|
124
|
+
/** URL lifetime in seconds, if applicable. */
|
|
125
|
+
expiresIn?: number;
|
|
126
|
+
/** The storage key of the object. */
|
|
127
|
+
key: string;
|
|
128
|
+
/** The signed download URL. */
|
|
129
|
+
url: string;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Client-side storage methods for uploading and managing files.
|
|
133
|
+
*
|
|
134
|
+
* Access via `buildspace.storage` on the client SDK.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* // Upload a file (handles signed URL + PUT automatically)
|
|
139
|
+
* const { key, url } = await buildspace.storage.upload(file, {
|
|
140
|
+
* path: "avatars/user-123.png",
|
|
141
|
+
* });
|
|
142
|
+
*
|
|
143
|
+
* // Get a download URL for an existing file
|
|
144
|
+
* const { url } = await buildspace.storage.getUrl(key);
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
declare class StorageClientNamespace {
|
|
148
|
+
private readonly transport;
|
|
149
|
+
constructor(transport: HttpTransport);
|
|
150
|
+
/**
|
|
151
|
+
* Upload a `File` or `Blob` to Buildspace storage.
|
|
152
|
+
*
|
|
153
|
+
* This method handles the full upload flow:
|
|
154
|
+
* 1. Requests a pre-signed upload URL from the API
|
|
155
|
+
* 2. Uploads the file directly to the storage provider
|
|
156
|
+
* 3. Returns a signed download URL for the uploaded file
|
|
157
|
+
*
|
|
158
|
+
* @param file - The file or blob to upload.
|
|
159
|
+
* @param opts - Upload options including the storage path.
|
|
160
|
+
* @returns The storage key, file size, and a signed download URL.
|
|
161
|
+
*
|
|
162
|
+
* @throws {BuildspaceError} If the upload URL request or the direct upload fails.
|
|
163
|
+
*/
|
|
164
|
+
upload(file: File | Blob, opts: UploadOptions): Promise<UploadResponse>;
|
|
165
|
+
/**
|
|
166
|
+
* Get a time-limited signed URL for downloading a stored file.
|
|
167
|
+
*
|
|
168
|
+
* @param key - The storage key of the object.
|
|
169
|
+
* @param opts - Optional settings.
|
|
170
|
+
* @param opts.expiresIn - URL lifetime in seconds.
|
|
171
|
+
*/
|
|
172
|
+
getUrl(key: string, opts?: {
|
|
173
|
+
expiresIn?: number;
|
|
174
|
+
}): Promise<GetUrlResponse>;
|
|
175
|
+
/**
|
|
176
|
+
* List stored objects, optionally filtered by key prefix.
|
|
177
|
+
*
|
|
178
|
+
* @param prefix - Only return objects whose key starts with this prefix.
|
|
179
|
+
* @param opts - Pagination options.
|
|
180
|
+
*/
|
|
181
|
+
list(prefix?: string, opts?: {
|
|
182
|
+
limit?: number;
|
|
183
|
+
offset?: number;
|
|
184
|
+
}): Promise<{
|
|
185
|
+
objects: StorageObject[];
|
|
186
|
+
}>;
|
|
187
|
+
/**
|
|
188
|
+
* Delete a stored object by key.
|
|
189
|
+
*
|
|
190
|
+
* @param key - The storage key of the object to delete.
|
|
191
|
+
*/
|
|
192
|
+
delete(key: string): Promise<void>;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export { type GetUrlResponse, type ServerUploadOptions, StorageClientNamespace, type StorageObject, StorageServerNamespace, type UploadOptions, type UploadResponse };
|
package/dist/storage/index.js
CHANGED
|
@@ -1,3 +1,210 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var BuildspaceError = class extends Error {
|
|
3
|
+
/** Machine-readable error code, e.g. `"auth/invalid-token"`. */
|
|
4
|
+
code;
|
|
5
|
+
/** Which service produced the error. */
|
|
6
|
+
service;
|
|
7
|
+
/** HTTP status code from the API. */
|
|
8
|
+
status;
|
|
9
|
+
constructor({
|
|
10
|
+
code,
|
|
11
|
+
message,
|
|
12
|
+
service,
|
|
13
|
+
status
|
|
14
|
+
}) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "BuildspaceError";
|
|
17
|
+
this.code = code;
|
|
18
|
+
this.service = service;
|
|
19
|
+
this.status = status;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// src/storage/client.ts
|
|
24
|
+
var StorageClientNamespace = class {
|
|
25
|
+
transport;
|
|
26
|
+
constructor(transport) {
|
|
27
|
+
this.transport = transport;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Upload a `File` or `Blob` to Buildspace storage.
|
|
31
|
+
*
|
|
32
|
+
* This method handles the full upload flow:
|
|
33
|
+
* 1. Requests a pre-signed upload URL from the API
|
|
34
|
+
* 2. Uploads the file directly to the storage provider
|
|
35
|
+
* 3. Returns a signed download URL for the uploaded file
|
|
36
|
+
*
|
|
37
|
+
* @param file - The file or blob to upload.
|
|
38
|
+
* @param opts - Upload options including the storage path.
|
|
39
|
+
* @returns The storage key, file size, and a signed download URL.
|
|
40
|
+
*
|
|
41
|
+
* @throws {BuildspaceError} If the upload URL request or the direct upload fails.
|
|
42
|
+
*/
|
|
43
|
+
async upload(file, opts) {
|
|
44
|
+
const contentType = opts.contentType ?? (file instanceof File ? file.type : "application/octet-stream");
|
|
45
|
+
const signed = await this.transport.request({
|
|
46
|
+
service: "storage",
|
|
47
|
+
path: "/v1/storage/upload",
|
|
48
|
+
method: "POST",
|
|
49
|
+
body: {
|
|
50
|
+
path: opts.path,
|
|
51
|
+
content_type: contentType,
|
|
52
|
+
size: file.size
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
const response = await this.transport.fetcher(signed.upload_url, {
|
|
56
|
+
method: "PUT",
|
|
57
|
+
headers: { "Content-Type": contentType },
|
|
58
|
+
body: file
|
|
59
|
+
});
|
|
60
|
+
if (!response.ok) {
|
|
61
|
+
throw new BuildspaceError({
|
|
62
|
+
service: "storage",
|
|
63
|
+
status: response.status,
|
|
64
|
+
code: "storage/upload-failed",
|
|
65
|
+
message: `Direct upload failed: ${response.statusText}`
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const { url } = await this.getUrl(signed.key);
|
|
69
|
+
return {
|
|
70
|
+
key: signed.key,
|
|
71
|
+
size: file.size,
|
|
72
|
+
url
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Get a time-limited signed URL for downloading a stored file.
|
|
77
|
+
*
|
|
78
|
+
* @param key - The storage key of the object.
|
|
79
|
+
* @param opts - Optional settings.
|
|
80
|
+
* @param opts.expiresIn - URL lifetime in seconds.
|
|
81
|
+
*/
|
|
82
|
+
getUrl(key, opts) {
|
|
83
|
+
return this.transport.request({
|
|
84
|
+
service: "storage",
|
|
85
|
+
path: "/v1/storage/url",
|
|
86
|
+
query: {
|
|
87
|
+
key,
|
|
88
|
+
expires_in: opts?.expiresIn
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* List stored objects, optionally filtered by key prefix.
|
|
94
|
+
*
|
|
95
|
+
* @param prefix - Only return objects whose key starts with this prefix.
|
|
96
|
+
* @param opts - Pagination options.
|
|
97
|
+
*/
|
|
98
|
+
list(prefix, opts) {
|
|
99
|
+
return this.transport.request({
|
|
100
|
+
service: "storage",
|
|
101
|
+
path: "/v1/storage/objects",
|
|
102
|
+
query: {
|
|
103
|
+
prefix,
|
|
104
|
+
limit: opts?.limit,
|
|
105
|
+
offset: opts?.offset
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Delete a stored object by key.
|
|
111
|
+
*
|
|
112
|
+
* @param key - The storage key of the object to delete.
|
|
113
|
+
*/
|
|
114
|
+
async delete(key) {
|
|
115
|
+
await this.transport.request({
|
|
116
|
+
service: "storage",
|
|
117
|
+
path: "/v1/storage/object",
|
|
118
|
+
method: "DELETE",
|
|
119
|
+
body: { key }
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// src/storage/server.ts
|
|
125
|
+
var StorageServerNamespace = class {
|
|
126
|
+
transport;
|
|
127
|
+
constructor(transport) {
|
|
128
|
+
this.transport = transport;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Request a pre-signed URL for direct file upload.
|
|
132
|
+
*
|
|
133
|
+
* @param opts - Upload options including the storage key, content type, and file size.
|
|
134
|
+
* @returns The signed upload URL, the resolved key, and expiry time in seconds.
|
|
135
|
+
*/
|
|
136
|
+
getUploadUrl(opts) {
|
|
137
|
+
return this.transport.request({
|
|
138
|
+
service: "storage",
|
|
139
|
+
path: "/v1/storage/upload",
|
|
140
|
+
method: "POST",
|
|
141
|
+
body: {
|
|
142
|
+
path: opts.key,
|
|
143
|
+
content_type: opts.contentType,
|
|
144
|
+
size: opts.size
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Get a time-limited signed URL for downloading a stored file.
|
|
150
|
+
*
|
|
151
|
+
* @param key - The storage key of the object.
|
|
152
|
+
* @param opts - Optional settings.
|
|
153
|
+
* @param opts.expiresIn - URL lifetime in seconds. Uses server default if omitted.
|
|
154
|
+
*/
|
|
155
|
+
getSignedUrl(key, opts) {
|
|
156
|
+
return this.transport.request({
|
|
157
|
+
service: "storage",
|
|
158
|
+
path: "/v1/storage/url",
|
|
159
|
+
query: {
|
|
160
|
+
key,
|
|
161
|
+
expires_in: opts?.expiresIn
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* List stored objects, optionally filtered by key prefix.
|
|
167
|
+
*
|
|
168
|
+
* @param prefix - Only return objects whose key starts with this prefix, e.g. `"avatars/"`.
|
|
169
|
+
* @param opts - Pagination options.
|
|
170
|
+
*/
|
|
171
|
+
list(prefix, opts) {
|
|
172
|
+
return this.transport.request({
|
|
173
|
+
service: "storage",
|
|
174
|
+
path: "/v1/storage/objects",
|
|
175
|
+
query: {
|
|
176
|
+
prefix,
|
|
177
|
+
limit: opts?.limit,
|
|
178
|
+
offset: opts?.offset
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Delete a stored object by key.
|
|
184
|
+
*
|
|
185
|
+
* @param key - The storage key of the object to delete.
|
|
186
|
+
*/
|
|
187
|
+
async delete(key) {
|
|
188
|
+
await this.transport.request({
|
|
189
|
+
service: "storage",
|
|
190
|
+
path: "/v1/storage/object",
|
|
191
|
+
method: "DELETE",
|
|
192
|
+
body: { key }
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Get current storage usage statistics for the app.
|
|
197
|
+
*
|
|
198
|
+
* @returns Byte counts and object count.
|
|
199
|
+
*/
|
|
200
|
+
getUsage() {
|
|
201
|
+
return this.transport.request({
|
|
202
|
+
service: "storage",
|
|
203
|
+
path: "/v1/storage/usage"
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
export { StorageClientNamespace, StorageServerNamespace };
|
|
209
|
+
//# sourceMappingURL=index.js.map
|
|
3
210
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/storage/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAElD,OAAO,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"sources":["../../src/errors.ts","../../src/storage/client.ts","../../src/storage/server.ts"],"names":[],"mappings":";AAiBO,IAAM,eAAA,GAAN,cAA8B,KAAA,CAAM;AAAA;AAAA,EAEhC,IAAA;AAAA;AAAA,EAEA,OAAA;AAAA;AAAA,EAEA,MAAA;AAAA,EAET,WAAA,CAAY;AAAA,IACV,IAAA;AAAA,IACA,OAAA;AAAA,IACA,OAAA;AAAA,IACA;AAAA,GACF,EAKG;AACD,IAAA,KAAA,CAAM,OAAO,CAAA;AACb,IAAA,IAAA,CAAK,IAAA,GAAO,iBAAA;AACZ,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AACZ,IAAA,IAAA,CAAK,OAAA,GAAU,OAAA;AACf,IAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AAAA,EAChB;AACF,CAAA;;;ACMO,IAAM,yBAAN,M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AACD,IAAA,OAAO,IAAA,CAAK,UAAU,OAAA,CAInB;AAAA,MACD,OAAA,EAAS,SAAA;AAAA,MACT,IAAA,EAAM;AAAA,KACP,CAAA;AAAA,EACH;AACF","file":"index.js","sourcesContent":["/** Services available in the Buildspace platform. */\nexport type BuildspaceService = \"auth\" | \"events\" | \"notifications\" | \"storage\";\n\n/**\n * Error thrown by all Buildspace SDK methods on failure.\n *\n * @example\n * ```ts\n * try {\n * await buildspace.auth.getSession(token);\n * } catch (err) {\n * if (err instanceof BuildspaceError) {\n * console.error(err.service, err.code, err.status, err.message);\n * }\n * }\n * ```\n */\nexport class BuildspaceError extends Error {\n /** Machine-readable error code, e.g. `\"auth/invalid-token\"`. */\n readonly code: string;\n /** Which service produced the error. */\n readonly service: BuildspaceService;\n /** HTTP status code from the API. */\n readonly status: number;\n\n constructor({\n code,\n message,\n service,\n status,\n }: {\n code: string;\n message: string;\n service: BuildspaceService;\n status: number;\n }) {\n super(message);\n this.name = \"BuildspaceError\";\n this.code = code;\n this.service = service;\n this.status = status;\n }\n}\n","import { BuildspaceError } from \"../errors\";\nimport type { HttpTransport } from \"../http\";\nimport type { StorageObject } from \"./server\";\n\n/** Options for uploading a file from the client. */\nexport interface UploadOptions {\n /** MIME type override. If omitted, inferred from the `File` object or defaults to `\"application/octet-stream\"`. */\n contentType?: string;\n /** Storage path/key for the file, e.g. `\"avatars/user-123.png\"`. */\n path: string;\n}\n\n/** Result of a successful client-side file upload. */\nexport interface UploadResponse {\n /** The resolved storage key. */\n key: string;\n /** File size in bytes. */\n size: number;\n /** Signed download URL for the uploaded file. */\n url: string;\n}\n\n/** Result of requesting a signed download URL. */\nexport interface GetUrlResponse {\n /** URL lifetime in seconds, if applicable. */\n expiresIn?: number;\n /** The storage key of the object. */\n key: string;\n /** The signed download URL. */\n url: string;\n}\n\n/**\n * Client-side storage methods for uploading and managing files.\n *\n * Access via `buildspace.storage` on the client SDK.\n *\n * @example\n * ```ts\n * // Upload a file (handles signed URL + PUT automatically)\n * const { key, url } = await buildspace.storage.upload(file, {\n * path: \"avatars/user-123.png\",\n * });\n *\n * // Get a download URL for an existing file\n * const { url } = await buildspace.storage.getUrl(key);\n * ```\n */\nexport class StorageClientNamespace {\n private readonly transport: HttpTransport;\n\n constructor(transport: HttpTransport) {\n this.transport = transport;\n }\n\n /**\n * Upload a `File` or `Blob` to Buildspace storage.\n *\n * This method handles the full upload flow:\n * 1. Requests a pre-signed upload URL from the API\n * 2. Uploads the file directly to the storage provider\n * 3. Returns a signed download URL for the uploaded file\n *\n * @param file - The file or blob to upload.\n * @param opts - Upload options including the storage path.\n * @returns The storage key, file size, and a signed download URL.\n *\n * @throws {BuildspaceError} If the upload URL request or the direct upload fails.\n */\n async upload(file: File | Blob, opts: UploadOptions): Promise<UploadResponse> {\n const contentType =\n opts.contentType ?? (file instanceof File ? file.type : \"application/octet-stream\");\n\n const signed = await this.transport.request<{ key: string; upload_url: string }>({\n service: \"storage\",\n path: \"/v1/storage/upload\",\n method: \"POST\",\n body: {\n path: opts.path,\n content_type: contentType,\n size: file.size,\n },\n });\n\n const response = await this.transport.fetcher(signed.upload_url, {\n method: \"PUT\",\n headers: { \"Content-Type\": contentType },\n body: file,\n });\n\n if (!response.ok) {\n throw new BuildspaceError({\n service: \"storage\",\n status: response.status,\n code: \"storage/upload-failed\",\n message: `Direct upload failed: ${response.statusText}`,\n });\n }\n\n const { url } = await this.getUrl(signed.key);\n\n return {\n key: signed.key,\n size: file.size,\n url,\n };\n }\n\n /**\n * Get a time-limited signed URL for downloading a stored file.\n *\n * @param key - The storage key of the object.\n * @param opts - Optional settings.\n * @param opts.expiresIn - URL lifetime in seconds.\n */\n getUrl(key: string, opts?: { expiresIn?: number }): Promise<GetUrlResponse> {\n return this.transport.request<GetUrlResponse>({\n service: \"storage\",\n path: \"/v1/storage/url\",\n query: {\n key,\n expires_in: opts?.expiresIn,\n },\n });\n }\n\n /**\n * List stored objects, optionally filtered by key prefix.\n *\n * @param prefix - Only return objects whose key starts with this prefix.\n * @param opts - Pagination options.\n */\n list(\n prefix?: string,\n opts?: { limit?: number; offset?: number }\n ): Promise<{ objects: StorageObject[] }> {\n return this.transport.request<{ objects: StorageObject[] }>({\n service: \"storage\",\n path: \"/v1/storage/objects\",\n query: {\n prefix,\n limit: opts?.limit,\n offset: opts?.offset,\n },\n });\n }\n\n /**\n * Delete a stored object by key.\n *\n * @param key - The storage key of the object to delete.\n */\n async delete(key: string): Promise<void> {\n await this.transport.request<void>({\n service: \"storage\",\n path: \"/v1/storage/object\",\n method: \"DELETE\",\n body: { key },\n });\n }\n}\n","import type { HttpTransport } from \"../http\";\n\n/** Options for requesting a pre-signed upload URL on the server. */\nexport interface ServerUploadOptions {\n /** MIME type of the file, e.g. `\"image/png\"`. */\n contentType: string;\n /** Storage path/key for the file, e.g. `\"avatars/user-123.png\"`. */\n key: string;\n /** File size in bytes. */\n size: number;\n}\n\n/** Metadata for a stored object. */\nexport interface StorageObject {\n /** MIME type of the stored file. */\n contentType: string;\n /** ISO 8601 timestamp of when the object was created. */\n createdAt: string;\n /** Unique object identifier. */\n id: string;\n /** Storage key/path of the object. */\n key: string;\n /** File size in bytes. */\n size: number;\n /** ID of the user who uploaded the object, or `null` if uploaded via server key. */\n uploadedBy: string | null;\n}\n\n/**\n * Server-side storage methods for managing file uploads and downloads.\n *\n * Access via `buildspace.storage` on the server SDK.\n *\n * @example\n * ```ts\n * // Get a pre-signed upload URL\n * const { upload_url, key } = await buildspace.storage.getUploadUrl({\n * key: \"avatars/user-123.png\",\n * contentType: \"image/png\",\n * size: file.size,\n * });\n *\n * // Upload directly to the signed URL\n * await fetch(upload_url, { method: \"PUT\", body: file });\n *\n * // Get a signed download URL\n * const { url } = await buildspace.storage.getSignedUrl(key);\n * ```\n */\nexport class StorageServerNamespace {\n private readonly transport: HttpTransport;\n\n constructor(transport: HttpTransport) {\n this.transport = transport;\n }\n\n /**\n * Request a pre-signed URL for direct file upload.\n *\n * @param opts - Upload options including the storage key, content type, and file size.\n * @returns The signed upload URL, the resolved key, and expiry time in seconds.\n */\n getUploadUrl(\n opts: ServerUploadOptions\n ): Promise<{ expires_in: number; key: string; upload_url: string }> {\n return this.transport.request<{ expires_in: number; key: string; upload_url: string }>({\n service: \"storage\",\n path: \"/v1/storage/upload\",\n method: \"POST\",\n body: {\n path: opts.key,\n content_type: opts.contentType,\n size: opts.size,\n },\n });\n }\n\n /**\n * Get a time-limited signed URL for downloading a stored file.\n *\n * @param key - The storage key of the object.\n * @param opts - Optional settings.\n * @param opts.expiresIn - URL lifetime in seconds. Uses server default if omitted.\n */\n getSignedUrl(\n key: string,\n opts?: { expiresIn?: number }\n ): Promise<{ expiresIn?: number; key: string; url: string }> {\n return this.transport.request<{ expiresIn?: number; key: string; url: string }>({\n service: \"storage\",\n path: \"/v1/storage/url\",\n query: {\n key,\n expires_in: opts?.expiresIn,\n },\n });\n }\n\n /**\n * List stored objects, optionally filtered by key prefix.\n *\n * @param prefix - Only return objects whose key starts with this prefix, e.g. `\"avatars/\"`.\n * @param opts - Pagination options.\n */\n list(\n prefix?: string,\n opts?: { limit?: number; offset?: number }\n ): Promise<{ objects: StorageObject[] }> {\n return this.transport.request<{ objects: StorageObject[] }>({\n service: \"storage\",\n path: \"/v1/storage/objects\",\n query: {\n prefix,\n limit: opts?.limit,\n offset: opts?.offset,\n },\n });\n }\n\n /**\n * Delete a stored object by key.\n *\n * @param key - The storage key of the object to delete.\n */\n async delete(key: string): Promise<void> {\n await this.transport.request<void>({\n service: \"storage\",\n path: \"/v1/storage/object\",\n method: \"DELETE\",\n body: { key },\n });\n }\n\n /**\n * Get current storage usage statistics for the app.\n *\n * @returns Byte counts and object count.\n */\n getUsage(): Promise<{\n maxStorageBytes: number;\n objectCount: number;\n storageBytes: number;\n }> {\n return this.transport.request<{\n maxStorageBytes: number;\n objectCount: number;\n storageBytes: number;\n }>({\n service: \"storage\",\n path: \"/v1/storage/usage\",\n });\n }\n}\n"]}
|