@bytescale/sdk 3.0.0-alpha.10 → 3.0.0-alpha.12

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 CHANGED
@@ -50,6 +50,8 @@
50
50
 
51
51
  <hr/>
52
52
 
53
+ <img alt="Bytescale JavaScript SDK Example" src="https://raw.githubusercontent.com/bytescale/bytescale-javascript-sdk/main/.github/assets/bytescale-javascript-sdk.png">
54
+
53
55
  ## Installation
54
56
 
55
57
  #### For Node.js:
@@ -78,23 +80,16 @@ This library is isomorphic, which means you can upload files from Node.js, or th
78
80
 
79
81
  ```javascript
80
82
  import * as Bytescale from "@bytescale/sdk";
81
- import fetch from "node-fetch";
83
+ import nodeFetch from "node-fetch";
82
84
 
83
85
  const uploadManager = new Bytescale.UploadManager({
84
- fetchApi: fetch, // or 'fetch as any' (please note: you must use 'node-fetch' as errors can occur with other implementations)
85
- apiKey: "YOUR_API_KEY" // e.g. "public_xxxxx"
86
+ fetchApi: nodeFetch, // import nodeFetch from "node-fetch"; // Only required for Node.js // Errors can occur with other implementations // Try 'nodeFetch as any' for TypeScript
87
+ apiKey: "free" // Get API keys from: www.bytescale.com
86
88
  });
87
89
 
88
90
  uploadManager
89
91
  .upload({
90
- // ---------
91
- // Required:
92
- // ---------
93
-
94
- // Your account ID (e.g. "W142hJk")
95
- accountId: "YOUR_ACCOUNT_ID",
96
-
97
- // Supported types for 'data' field:
92
+ // Supported types:
98
93
  // - String
99
94
  // - Buffer
100
95
  // - ReadableStream (Node.js), e.g. fs.createReadStream("file.txt")
@@ -163,7 +158,7 @@ uploadManager
163
158
  <script>
164
159
  // import * as Bytescale from "@bytescale/sdk"
165
160
  const uploadManager = new Bytescale.UploadManager({
166
- apiKey: "YOUR_API_KEY" // e.g. "public_xxxxx"
161
+ apiKey: "free" // Get API keys from: www.bytescale.com
167
162
  });
168
163
 
169
164
  const onFileSelected = async event => {
@@ -171,14 +166,7 @@ uploadManager
171
166
 
172
167
  try {
173
168
  const { fileUrl, filePath } = await uploadManager.upload({
174
- // ---------
175
- // Required:
176
- // ---------
177
-
178
- // Your account ID (e.g. "W142hJk")
179
- accountId: "YOUR_ACCOUNT_ID",
180
-
181
- // Supported types for 'data' field:
169
+ // Supported types:
182
170
  // - String
183
171
  // - Blob
184
172
  // - File (i.e. from a DOM file input element)
@@ -248,16 +236,16 @@ uploadManager
248
236
 
249
237
  ```javascript
250
238
  import * as Bytescale from "@bytescale/sdk";
251
- import fetch from "node-fetch"; // Only required for Node.js
239
+ import nodeFetch from "node-fetch"; // Only required for Node.js
252
240
 
253
241
  const fileApi = new Bytescale.FileApi({
254
- fetchApi: fetch, // 'fetch as any' for TypeScript
255
- apiKey: "YOUR_BYTESCALE_API_KEY" // e.g. "secret_xxxxx"
242
+ fetchApi: nodeFetch, // import nodeFetch from "node-fetch"; // Only required for Node.js // Errors can occur with other implementations // Try 'nodeFetch as any' for TypeScript
243
+ apiKey: "YOUR_API_KEY" // e.g. "secret_xxxxx"
256
244
  });
257
245
 
258
246
  fileApi
259
247
  .downloadFile({
260
- accountId: "YOUR_UPLOAD_ACCOUNT_ID", // e.g. "W142hJk"
248
+ accountId: "YOUR_ACCOUNT_ID", // e.g. "W142hJk"
261
249
  filePath: "/uploads/2022/12/25/hello_world.txt"
262
250
  })
263
251
  .then(response => response.text()) // .text() | .json() | .blob() | .stream()
@@ -274,15 +262,16 @@ Use the [`UrlBuilder`](#urlbuilder) to get a URL to your file (if you need a URL
274
262
  ```javascript
275
263
  import * as Bytescale from "@bytescale/sdk";
276
264
  import fetch from "node-fetch"; // Only required for Node.js
265
+ import fs from "fs";
277
266
 
278
267
  const fileApi = new Bytescale.FileApi({
279
- fetchApi: fetch, // 'fetch as any' for TypeScript
280
- apiKey: "YOUR_BYTESCALE_API_KEY" // e.g. "secret_xxxxx"
268
+ fetchApi: nodeFetch, // import nodeFetch from "node-fetch"; // Only required for Node.js // Errors can occur with other implementations // Try 'nodeFetch as any' for TypeScript
269
+ apiKey: "YOUR_API_KEY" // e.g. "secret_xxxxx"
281
270
  });
282
271
 
283
272
  fileApi
284
273
  .processFile({
285
- accountId: "YOUR_UPLOAD_ACCOUNT_ID", // e.g. "W142hJk"
274
+ accountId: "YOUR_ACCOUNT_ID", // e.g. "W142hJk"
286
275
  filePath: "/uploads/2022/12/25/image.jpg",
287
276
 
288
277
  // See: https://www.bytescale.com/docs/image-processing-api
@@ -317,13 +306,13 @@ import * as Bytescale from "@bytescale/sdk";
317
306
  import fetch from "node-fetch"; // Only required for Node.js
318
307
 
319
308
  const fileApi = new Bytescale.FileApi({
320
- fetchApi: fetch, // 'fetch as any' for TypeScript
321
- apiKey: "YOUR_BYTESCALE_API_KEY" // e.g. "secret_xxxxx"
309
+ fetchApi: nodeFetch, // import nodeFetch from "node-fetch"; // Only required for Node.js // Errors can occur with other implementations // Try 'nodeFetch as any' for TypeScript
310
+ apiKey: "YOUR_API_KEY" // e.g. "secret_xxxxx"
322
311
  });
323
312
 
324
313
  fileApi
325
314
  .getFileDetails({
326
- accountId: "YOUR_UPLOAD_ACCOUNT_ID", // e.g. "W142hJk"
315
+ accountId: "YOUR_ACCOUNT_ID", // e.g. "W142hJk"
327
316
  filePath: "/uploads/2022/12/25/image.jpg"
328
317
  })
329
318
  .then(
@@ -339,13 +328,13 @@ import * as Bytescale from "@bytescale/sdk";
339
328
  import fetch from "node-fetch"; // Only required for Node.js
340
329
 
341
330
  const folderApi = new Bytescale.FolderApi({
342
- fetchApi: fetch, // 'fetch as any' for TypeScript
343
- apiKey: "YOUR_BYTESCALE_API_KEY" // e.g. "secret_xxxxx"
331
+ fetchApi: nodeFetch, // import nodeFetch from "node-fetch"; // Only required for Node.js // Errors can occur with other implementations // Try 'nodeFetch as any' for TypeScript
332
+ apiKey: "YOUR_API_KEY" // e.g. "secret_xxxxx"
344
333
  });
345
334
 
346
335
  folderApi
347
336
  .listFolder({
348
- accountId: "YOUR_UPLOAD_ACCOUNT_ID", // e.g. "W142hJk"
337
+ accountId: "YOUR_ACCOUNT_ID", // e.g. "W142hJk"
349
338
  folderPath: "/",
350
339
  recursive: false
351
340
  })
@@ -356,6 +345,12 @@ folderApi
356
345
  );
357
346
  ```
358
347
 
348
+ ## More Operations
349
+
350
+ For a complete list of operations, please see:
351
+
352
+ **[Bytescale JavaScript SDK Docs »](https://www.bytescale.com/docs/sdks/javascript)**
353
+
359
354
  ## Authorization
360
355
 
361
356
  The Bytescale JavaScript SDK supports two types of authorization:
@@ -376,13 +371,13 @@ Each Public API Key and Secret API Key can have its read/write access limited to
376
371
 
377
372
  JWT cookies are optional.
378
373
 
379
- With JWT cookies, the user can download private files directly via URL, as authorization is performed implicitly via a session cookie. This allows the browser to display private files in `<img>` and `<video>` elements.
374
+ With JWT cookies, the user can download private files directly via the URL, as authorization is performed implicitly via a session cookie. This allows the browser to display private files in `<img>` and `<video>` elements.
380
375
 
381
376
  With JWT cookies, the user can also perform API requests (e.g. file uploads) granted by the [JWT's payload](https://www.bytescale.com/docs/types/BytescaleJwt). This is because the Bytescale JavaScript SDK automatically injects the user's JWT into the `authorization-token` request header for all API requests, assuming the `AuthManager.beginAuthSession` method has been called.
382
377
 
383
378
  _Note: when using JWT cookies to download files, the `?auth=true` query parameter must be added to the URL._
384
379
 
385
- [Learn more about using JWT cookies »](https://www.bytescale.com/docs/authorization#jwt-cookie)
380
+ [Learn more about the `AuthManager` and JWT cookies »](https://www.bytescale.com/docs/authorization#jwt-cookie)
386
381
 
387
382
  ## UrlBuilder
388
383
 
@@ -2034,6 +2034,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
2034
2034
  this.defaultMaxConcurrentUploadParts = 4;
2035
2035
  this.intervalMs = 500;
2036
2036
  this.uploadApi = new UploadApi(config);
2037
+ this.accountId = BytescaleApiClientConfigUtils.getAccountId(config);
2037
2038
  }
2038
2039
  UploadManagerBase_createClass(UploadManagerBase, [{
2039
2040
  key: "upload",
@@ -2151,7 +2152,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
2151
2152
  try {
2152
2153
  var _this5 = this;
2153
2154
  return UploadManagerBase_await(_this5.uploadApi.beginMultipartUpload({
2154
- accountId: request.accountId,
2155
+ accountId: _this5.accountId,
2155
2156
  beginMultipartUploadRequest: {
2156
2157
  metadata: request.metadata,
2157
2158
  mime: mime,
@@ -2172,12 +2173,12 @@ var UploadManagerBase = /*#__PURE__*/function () {
2172
2173
  try {
2173
2174
  var _this7 = this;
2174
2175
  _this7.assertNotCancelled(request);
2175
- return UploadManagerBase_await(_this7.getUploadPart(request, partIndex, uploadInfo), function (part) {
2176
+ return UploadManagerBase_await(_this7.getUploadPart(partIndex, uploadInfo), function (part) {
2176
2177
  _this7.assertNotCancelled(request);
2177
2178
  return UploadManagerBase_await(_this7.putUploadPart(part, source, onProgress, addCancellationHandler), function (etag) {
2178
2179
  _this7.assertNotCancelled(request);
2179
2180
  return UploadManagerBase_awaitIgnored(_this7.uploadApi.completeUploadPart({
2180
- accountId: request.accountId,
2181
+ accountId: _this7.accountId,
2181
2182
  uploadId: uploadInfo.uploadId,
2182
2183
  uploadPartIndex: partIndex,
2183
2184
  completeUploadPartRequest: {
@@ -2216,7 +2217,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
2216
2217
  }
2217
2218
  }, {
2218
2219
  key: "getUploadPart",
2219
- value: function getUploadPart(request, partIndex, uploadInfo) {
2220
+ value: function getUploadPart(partIndex, uploadInfo) {
2220
2221
  try {
2221
2222
  var _this11 = this;
2222
2223
  if (partIndex === 0) {
@@ -2224,7 +2225,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
2224
2225
  }
2225
2226
  return UploadManagerBase_await(_this11.uploadApi.getUploadPart({
2226
2227
  uploadId: uploadInfo.uploadId,
2227
- accountId: request.accountId,
2228
+ accountId: _this11.accountId,
2228
2229
  uploadPartIndex: partIndex
2229
2230
  }));
2230
2231
  } catch (e) {
@@ -2056,6 +2056,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
2056
2056
  this.defaultMaxConcurrentUploadParts = 4;
2057
2057
  this.intervalMs = 500;
2058
2058
  this.uploadApi = new UploadApi(config);
2059
+ this.accountId = BytescaleApiClientConfigUtils.getAccountId(config);
2059
2060
  }
2060
2061
  UploadManagerBase_createClass(UploadManagerBase, [{
2061
2062
  key: "upload",
@@ -2173,7 +2174,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
2173
2174
  try {
2174
2175
  var _this5 = this;
2175
2176
  return UploadManagerBase_await(_this5.uploadApi.beginMultipartUpload({
2176
- accountId: request.accountId,
2177
+ accountId: _this5.accountId,
2177
2178
  beginMultipartUploadRequest: {
2178
2179
  metadata: request.metadata,
2179
2180
  mime: mime,
@@ -2194,12 +2195,12 @@ var UploadManagerBase = /*#__PURE__*/function () {
2194
2195
  try {
2195
2196
  var _this7 = this;
2196
2197
  _this7.assertNotCancelled(request);
2197
- return UploadManagerBase_await(_this7.getUploadPart(request, partIndex, uploadInfo), function (part) {
2198
+ return UploadManagerBase_await(_this7.getUploadPart(partIndex, uploadInfo), function (part) {
2198
2199
  _this7.assertNotCancelled(request);
2199
2200
  return UploadManagerBase_await(_this7.putUploadPart(part, source, onProgress, addCancellationHandler), function (etag) {
2200
2201
  _this7.assertNotCancelled(request);
2201
2202
  return UploadManagerBase_awaitIgnored(_this7.uploadApi.completeUploadPart({
2202
- accountId: request.accountId,
2203
+ accountId: _this7.accountId,
2203
2204
  uploadId: uploadInfo.uploadId,
2204
2205
  uploadPartIndex: partIndex,
2205
2206
  completeUploadPartRequest: {
@@ -2238,7 +2239,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
2238
2239
  }
2239
2240
  }, {
2240
2241
  key: "getUploadPart",
2241
- value: function getUploadPart(request, partIndex, uploadInfo) {
2242
+ value: function getUploadPart(partIndex, uploadInfo) {
2242
2243
  try {
2243
2244
  var _this11 = this;
2244
2245
  if (partIndex === 0) {
@@ -2246,7 +2247,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
2246
2247
  }
2247
2248
  return UploadManagerBase_await(_this11.uploadApi.getUploadPart({
2248
2249
  uploadId: uploadInfo.uploadId,
2249
- accountId: request.accountId,
2250
+ accountId: _this11.accountId,
2250
2251
  uploadPartIndex: partIndex
2251
2252
  }));
2252
2253
  } catch (e) {
@@ -2043,6 +2043,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
2043
2043
  this.defaultMaxConcurrentUploadParts = 4;
2044
2044
  this.intervalMs = 500;
2045
2045
  this.uploadApi = new UploadApi(config);
2046
+ this.accountId = BytescaleApiClientConfigUtils.getAccountId(config);
2046
2047
  }
2047
2048
  UploadManagerBase_createClass(UploadManagerBase, [{
2048
2049
  key: "upload",
@@ -2160,7 +2161,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
2160
2161
  try {
2161
2162
  var _this5 = this;
2162
2163
  return UploadManagerBase_await(_this5.uploadApi.beginMultipartUpload({
2163
- accountId: request.accountId,
2164
+ accountId: _this5.accountId,
2164
2165
  beginMultipartUploadRequest: {
2165
2166
  metadata: request.metadata,
2166
2167
  mime: mime,
@@ -2181,12 +2182,12 @@ var UploadManagerBase = /*#__PURE__*/function () {
2181
2182
  try {
2182
2183
  var _this7 = this;
2183
2184
  _this7.assertNotCancelled(request);
2184
- return UploadManagerBase_await(_this7.getUploadPart(request, partIndex, uploadInfo), function (part) {
2185
+ return UploadManagerBase_await(_this7.getUploadPart(partIndex, uploadInfo), function (part) {
2185
2186
  _this7.assertNotCancelled(request);
2186
2187
  return UploadManagerBase_await(_this7.putUploadPart(part, source, onProgress, addCancellationHandler), function (etag) {
2187
2188
  _this7.assertNotCancelled(request);
2188
2189
  return UploadManagerBase_awaitIgnored(_this7.uploadApi.completeUploadPart({
2189
- accountId: request.accountId,
2190
+ accountId: _this7.accountId,
2190
2191
  uploadId: uploadInfo.uploadId,
2191
2192
  uploadPartIndex: partIndex,
2192
2193
  completeUploadPartRequest: {
@@ -2225,7 +2226,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
2225
2226
  }
2226
2227
  }, {
2227
2228
  key: "getUploadPart",
2228
- value: function getUploadPart(request, partIndex, uploadInfo) {
2229
+ value: function getUploadPart(partIndex, uploadInfo) {
2229
2230
  try {
2230
2231
  var _this11 = this;
2231
2232
  if (partIndex === 0) {
@@ -2233,7 +2234,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
2233
2234
  }
2234
2235
  return UploadManagerBase_await(_this11.uploadApi.getUploadPart({
2235
2236
  uploadId: uploadInfo.uploadId,
2236
- accountId: request.accountId,
2237
+ accountId: _this11.accountId,
2237
2238
  uploadPartIndex: partIndex
2238
2239
  }));
2239
2240
  } catch (e) {
@@ -11,6 +11,7 @@ import { UploadManagerParams, UploadSource } from "../public/shared/CommonTypes"
11
11
  export declare abstract class UploadManagerBase<TSource, TInit> implements UploadManagerInterface {
12
12
  protected readonly config: BytescaleApiClientConfig;
13
13
  protected readonly stringMimeType = "text/plain";
14
+ private readonly accountId;
14
15
  private readonly defaultMaxConcurrentUploadParts;
15
16
  private readonly intervalMs;
16
17
  private readonly uploadApi;
@@ -20,7 +20,6 @@ export interface UploadProgress {
20
20
  }
21
21
  export declare type UploadSource = NodeJS.ReadableStream | BlobLike | Buffer | string;
22
22
  export interface UploadManagerParams extends Omit<BeginMultipartUploadRequest, "size" | "protocol"> {
23
- accountId: string;
24
23
  cancellationToken?: CancellationToken;
25
24
  data: UploadSource;
26
25
  maxConcurrentUploadParts?: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytescale/sdk",
3
- "version": "3.0.0-alpha.10",
3
+ "version": "3.0.0-alpha.12",
4
4
  "description": "Bytescale JavaScript SDK",
5
5
  "author": "Bytescale <hello@bytescale.com> (https://www.bytescale.com)",
6
6
  "license": "MIT",
@@ -13,6 +13,10 @@
13
13
  "require": "./dist/node/cjs/main.js",
14
14
  "import": "./dist/node/esm/main.mjs"
15
15
  },
16
+ "worker": {
17
+ "require": "./dist/node/cjs/main.js",
18
+ "import": "./dist/node/esm/main.mjs"
19
+ },
16
20
  "default": "./dist/browser/cjs/main.js"
17
21
  }
18
22
  },
@@ -27,7 +27,6 @@ const largeFileSize = Math.pow(1024, 2) * 500; // 500MB
27
27
  async function testStreamingUpload(expectedSize: number): Promise<void> {
28
28
  const expectedData = await createRandomStreamFactory(expectedSize);
29
29
  const uploadedFile = await uploadManager.upload({
30
- accountId,
31
30
  data: expectedData(),
32
31
  size: expectedSize
33
32
  });
@@ -56,7 +55,6 @@ describe("UploadManager", () => {
56
55
  const expectedData = "";
57
56
  const expectedSize = 0;
58
57
  const uploadedFile = await uploadManager.upload({
59
- accountId,
60
58
  data: expectedData
61
59
  });
62
60
  const fileDetails = await fileApi.getFileDetails({ accountId, filePath: uploadedFile.filePath });
@@ -70,7 +68,6 @@ describe("UploadManager", () => {
70
68
  const expectedData = "Example Data";
71
69
  const expectedMime = "text/plain";
72
70
  const uploadedFile = await uploadManager.upload({
73
- accountId,
74
71
  data: expectedData
75
72
  });
76
73
  const fileDetails = await fileApi.getFileDetails({ accountId, filePath: uploadedFile.filePath });
@@ -84,7 +81,6 @@ describe("UploadManager", () => {
84
81
  const expectedData = JSON.stringify({ someValue: 42 });
85
82
  const expectedMime = "application/json";
86
83
  const uploadedFile = await uploadManager.upload({
87
- accountId,
88
84
  data: new buffer.Blob([expectedData], { type: expectedMime })
89
85
  });
90
86
  const fileDetails = await fileApi.getFileDetails({ accountId, filePath: uploadedFile.filePath });
@@ -98,7 +94,6 @@ describe("UploadManager", () => {
98
94
  const expectedData = "Example Data";
99
95
  const expectedMime = "text/plain";
100
96
  const uploadedFile = await uploadManager.upload({
101
- accountId,
102
97
  data: Buffer.from(expectedData, "utf8"),
103
98
  mime: expectedMime
104
99
  });
@@ -113,7 +108,6 @@ describe("UploadManager", () => {
113
108
  const expectedData = "Example Data";
114
109
  const expectedMime = "application/octet-stream";
115
110
  const uploadedFile = await uploadManager.upload({
116
- accountId,
117
111
  data: Buffer.from(expectedData, "utf8")
118
112
  });
119
113
  const fileDetails = await fileApi.getFileDetails({ accountId, filePath: uploadedFile.filePath });
@@ -129,7 +123,6 @@ describe("UploadManager", () => {
129
123
  const expectedSize = largeFileSize;
130
124
  const expectedData = await streamToBuffer((await createRandomStreamFactory(expectedSize))());
131
125
  const uploadedFile = await uploadManager.upload({
132
- accountId,
133
126
  data: expectedData
134
127
  });
135
128
  const fileDetails = await fileApi.getFileDetails({ accountId, filePath: uploadedFile.filePath });