@bytescale/sdk 1.1.0 → 1.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.
Files changed (49) hide show
  1. package/dist/browser/cjs/main.js +1152 -1094
  2. package/dist/node/cjs/main.js +1156 -952
  3. package/dist/node/esm/main.mjs +1167 -933
  4. package/dist/types/index.d.ts +2 -2
  5. package/dist/types/private/AuthSessionState.d.ts +13 -0
  6. package/dist/types/private/ConsoleUtils.d.ts +5 -0
  7. package/dist/types/private/EnvChecker.d.ts +4 -0
  8. package/dist/types/private/Mutex.d.ts +16 -0
  9. package/dist/types/{uploads/ChunkedStream.d.ts → private/NodeChunkedStream.d.ts} +11 -4
  10. package/dist/types/private/StreamUtils.d.ts +8 -0
  11. package/dist/types/private/TypeUtils.d.ts +2 -0
  12. package/dist/types/private/UploadManagerBase.d.ts +43 -0
  13. package/dist/types/private/dtos/SetAccessTokenRequestDto.d.ts +6 -0
  14. package/dist/types/private/dtos/SetAccessTokenResponseDto.d.ts +7 -0
  15. package/dist/types/private/model/AddCancellationHandler.d.ts +1 -0
  16. package/dist/types/private/model/AuthManagerInterface.d.ts +44 -0
  17. package/dist/types/private/model/AuthSession.d.ts +7 -0
  18. package/dist/types/private/model/OnPartProgress.d.ts +1 -0
  19. package/dist/types/private/model/PreUploadInfo.d.ts +6 -0
  20. package/dist/types/private/model/PutUploadPartResult.d.ts +4 -0
  21. package/dist/types/private/model/UploadManagerInterface.d.ts +5 -0
  22. package/dist/types/private/model/UploadSourceProcessed.d.ts +18 -0
  23. package/dist/types/public/browser/AuthManagerBrowser.d.ts +20 -0
  24. package/dist/types/public/browser/UploadManagerBrowser.d.ts +17 -0
  25. package/dist/types/public/browser/index.d.ts +2 -0
  26. package/dist/types/public/node/AuthManagerNode.d.ts +6 -0
  27. package/dist/types/public/node/UploadManagerNode.d.ts +26 -0
  28. package/dist/types/public/node/index.d.ts +2 -0
  29. package/dist/types/{uploads/UploadModel.d.ts → public/shared/CommonTypes.d.ts} +8 -11
  30. package/dist/types/public/shared/UrlBuilder.d.ts +28 -0
  31. package/dist/types/public/shared/UrlBuilderTypes.d.ts +173 -0
  32. package/dist/types/{generated → public/shared/generated}/runtime.d.ts +44 -29
  33. package/dist/types/public/shared/index.d.ts +4 -0
  34. package/package.json +2 -2
  35. package/tests/ChunkedStream.test.ts +3 -3
  36. package/tests/UploadManager.test.ts +1 -1
  37. package/tests/UrlBuilder.test.ts +168 -0
  38. package/dist/browser/cjs/_70b2.main.js +0 -10
  39. package/dist/browser/cjs/_7392.main.js +0 -10
  40. package/dist/types/uploads/NodeUtils.d.ts +0 -13
  41. package/dist/types/uploads/UploadManager.d.ts +0 -39
  42. package/dist/types/uploads/index.d.ts +0 -3
  43. /package/dist/types/{generated → public/shared/generated}/apis/FileApi.d.ts +0 -0
  44. /package/dist/types/{generated → public/shared/generated}/apis/FolderApi.d.ts +0 -0
  45. /package/dist/types/{generated → public/shared/generated}/apis/JobApi.d.ts +0 -0
  46. /package/dist/types/{generated → public/shared/generated}/apis/UploadApi.d.ts +0 -0
  47. /package/dist/types/{generated → public/shared/generated}/apis/index.d.ts +0 -0
  48. /package/dist/types/{generated → public/shared/generated}/index.d.ts +0 -0
  49. /package/dist/types/{generated → public/shared/generated}/models/index.d.ts +0 -0
@@ -10,22 +10,61 @@ import { ErrorResponse } from "./models";
10
10
  * https://openapi-generator.tech
11
11
  * Do not edit the class manually.
12
12
  */
13
- export declare const BASE_PATH: string;
14
13
  export interface BytescaleApiClientConfig {
14
+ /**
15
+ * Required for Node.js: you must provide an instance of requires("node-fetch")
16
+ *
17
+ * Not required for the browser.
18
+ */
15
19
  fetchApi?: FetchAPI;
16
- apiKey?: string | ((name: string) => string);
20
+ /**
21
+ * Required for all environments. Must begin with "public_" or "secret_".
22
+ *
23
+ * Please note: if you require JWT-based auth, you must provide an API key to this field, and then call 'AuthManager.beginAuthSession' to start a JWT-based auth session. The JWT's permissions will be merged with the API key's permissions, with precedence given to the JWT.
24
+ */
25
+ apiKey: string;
26
+ /**
27
+ * The base URL of the Bytescale API.
28
+ */
29
+ apiUrl?: string;
30
+ /**
31
+ * The base URL of the Bytescale CDN.
32
+ */
33
+ cdnUrl?: string;
34
+ /**
35
+ * Headers to include in all API requests.
36
+ *
37
+ * These headers take precedence over any headers automatically added by the SDK (e.g. "Authorization", "Content-Type", etc.).
38
+ */
17
39
  headers?: () => Promise<HTTPHeaders>;
18
40
  }
41
+ export declare class BytescaleApiClientConfigResolver {
42
+ static defaultApiUrl: string;
43
+ static defaultCdnUrl: string;
44
+ static getApiUrl(config: BytescaleApiClientConfig): string;
45
+ static getCdnUrl(config: BytescaleApiClientConfig): string;
46
+ static getFetchApi(config: BytescaleApiClientConfig): FetchAPI;
47
+ }
19
48
  /**
20
49
  * This is the base class for all generated API classes.
21
50
  */
22
51
  export declare class BaseAPI {
23
52
  protected readonly config: BytescaleApiClientConfig;
53
+ protected static readonly specialApiKeys: string[];
54
+ protected static readonly specialApiKeyAccountId = "W142hJk";
55
+ protected static readonly accountIdLength = 7;
24
56
  constructor(config: BytescaleApiClientConfig);
25
- protected request(context: RequestOpts, initOverrides: RequestInit | InitOverrideFunction | undefined, operationBasePathOverride: string | undefined): Promise<Response>;
57
+ protected request(context: RequestOpts, initOverrides: RequestInit | InitOverrideFunction | undefined, baseUrlOverride: string | undefined): Promise<Response>;
58
+ /**
59
+ * Returns a successful response (2**) else throws an error.
60
+ */
61
+ protected doFetch(url: string, init: RequestInit, isBytescaleApi: boolean): Promise<Response>;
26
62
  protected encodeParam(paramName: string, paramValue: string): string;
27
63
  private createFetchParams;
28
- private fetchApi;
64
+ }
65
+ export declare class CancelledError extends Error {
66
+ name: "CancelledError";
67
+ constructor();
29
68
  }
30
69
  export declare class BytescaleApiError extends Error {
31
70
  name: "BytescaleApiError";
@@ -33,21 +72,6 @@ export declare class BytescaleApiError extends Error {
33
72
  readonly details: any | undefined;
34
73
  constructor(response: ErrorResponse);
35
74
  }
36
- export declare class ResponseError extends Error {
37
- response: Response;
38
- name: "ResponseError";
39
- constructor(response: Response, msg?: string);
40
- }
41
- export declare class FetchError extends Error {
42
- cause: Error;
43
- name: "FetchError";
44
- constructor(cause: Error, msg?: string);
45
- }
46
- export declare class RequiredError extends Error {
47
- field: string;
48
- name: "RequiredError";
49
- constructor(field: string, msg?: string);
50
- }
51
75
  export declare type FetchAPI = WindowOrWorkerGlobalScope["fetch"];
52
76
  export declare type Json = any;
53
77
  export declare type HTTPMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS" | "HEAD";
@@ -63,15 +87,10 @@ export declare type HTTPRequestInit = {
63
87
  method: HTTPMethod;
64
88
  body?: HTTPBody;
65
89
  };
66
- export declare type ModelPropertyNaming = "camelCase" | "snake_case" | "PascalCase" | "original";
67
90
  export declare type InitOverrideFunction = (requestContext: {
68
91
  init: HTTPRequestInit;
69
92
  context: RequestOpts;
70
93
  }) => Promise<RequestInit>;
71
- export interface FetchParams {
72
- url: string;
73
- init: RequestInit;
74
- }
75
94
  export interface RequestOpts {
76
95
  path: string;
77
96
  method: HTTPMethod;
@@ -84,13 +103,9 @@ export interface ApiResponse<T> {
84
103
  raw: Response;
85
104
  value(): Promise<T>;
86
105
  }
87
- export interface ResponseTransformer<T> {
88
- (json: any): T;
89
- }
90
106
  export declare class JSONApiResponse<T> {
91
107
  raw: Response;
92
- private transformer;
93
- constructor(raw: Response, transformer?: ResponseTransformer<T>);
108
+ constructor(raw: Response);
94
109
  value(): Promise<T>;
95
110
  }
96
111
  export declare class VoidApiResponse {
@@ -0,0 +1,4 @@
1
+ export * from "./generated";
2
+ export * from "./CommonTypes";
3
+ export * from "./UrlBuilder";
4
+ export * from "./UrlBuilderTypes";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytescale/sdk",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "Bytescale JavaScript SDK",
5
5
  "author": "Bytescale <hello@bytescale.com> (https://www.bytescale.com)",
6
6
  "license": "MIT",
@@ -37,7 +37,7 @@
37
37
  "lint": "eslint . --ext .js,.jsx,.ts,.tsx",
38
38
  "typecheck": "tsc --noEmit",
39
39
  "test": "NODE_OPTIONS=--experimental-vm-modules npx jest --runInBand --silent=false --verbose=false",
40
- "prepack": "npm run clean && webpack && tsc-alias",
40
+ "prepack": "npm run clean && webpack && tsc-alias && rm dist/types/index.browser.d.ts && mv dist/types/index.node.d.ts dist/types/index.d.ts",
41
41
  "postpack": "[ ! -f bytescale-sdk-*.tgz ] || mv bytescale-sdk-*.tgz bytescale-sdk.tgz",
42
42
  "prepare": "husky install",
43
43
  "prepack:cdn": "npm run clean && webpack --config webpack.config.cdn.js && find dist -name \"*.ts\" -type f -delete && for f in dist/*.js; do cp -- \"$f\" \"${f%.js}\"; done",
@@ -1,4 +1,4 @@
1
- import { ChunkedStream } from "../src/uploads";
1
+ import { NodeChunkedStream } from "../src/private/NodeChunkedStream";
2
2
  import { createRandomStreamFactory } from "./utils/RandomStream";
3
3
  import { streamToBuffer } from "./utils/StreamToBuffer";
4
4
 
@@ -9,7 +9,7 @@ describe("ChunkedStream", () => {
9
9
  const expectedSize = Math.pow(1024, 2) * 100; // 100MB
10
10
  const maxPartSize = Math.pow(1024, 2) * 2; // 2MB
11
11
  const expectedData = await createRandomStreamFactory(expectedSize);
12
- const chunkedStream = new ChunkedStream(expectedData());
12
+ const chunkedStream = new NodeChunkedStream(expectedData());
13
13
  const worker = chunkedStream.runChunkPipeline();
14
14
  let remaining = expectedSize;
15
15
  const buffersFromParts = Array<Buffer>();
@@ -23,7 +23,7 @@ describe("ChunkedStream", () => {
23
23
 
24
24
  const partSize = nextPartSize();
25
25
 
26
- const subStream = await chunkedStream.take(partSize);
26
+ const subStream = chunkedStream.take(partSize);
27
27
  const subStreamBuffer = await streamToBuffer(subStream);
28
28
 
29
29
  expect(subStreamBuffer.byteLength).toEqual(partSize);
@@ -1,4 +1,4 @@
1
- import { BytescaleApiClientConfig, FileApi, UploadManager } from "../src";
1
+ import { BytescaleApiClientConfig, FileApi, UploadManager } from "../src/index.node";
2
2
  import fetch from "node-fetch";
3
3
  import * as buffer from "buffer";
4
4
  import { createRandomStreamFactory } from "./utils/RandomStream";
@@ -0,0 +1,168 @@
1
+ import { UrlBuilder } from "../src/public/shared";
2
+
3
+ describe("UrlBuilder", () => {
4
+ test("raw file without params", () => {
5
+ const actual = new UrlBuilder().url({ accountId: "1234abc", filePath: "/example.jpg" });
6
+ const expected = "https://upcdn.io/1234abc/raw/example.jpg";
7
+ expect(actual).toEqual(expected);
8
+ });
9
+
10
+ test("raw file with one param", () => {
11
+ const actual = new UrlBuilder().url({ accountId: "1234abc", filePath: "/example.jpg", options: { auth: true } });
12
+ const expected = "https://upcdn.io/1234abc/raw/example.jpg?auth=true";
13
+ expect(actual).toEqual(expected);
14
+ });
15
+
16
+ test("raw file with multiple params", () => {
17
+ const actual = new UrlBuilder().url({
18
+ accountId: "1234abc",
19
+ filePath: "/example.jpg",
20
+ options: { auth: true, cache: true, version: "42" }
21
+ });
22
+ const expected = "https://upcdn.io/1234abc/raw/example.jpg?auth=true&cache=true&version=42";
23
+ expect(actual).toEqual(expected);
24
+ });
25
+
26
+ test("raw file with rewritten params", () => {
27
+ const actual = new UrlBuilder().url({
28
+ accountId: "1234abc",
29
+ filePath: "/example.jpg",
30
+ options: { auth: true, cache: true, cacheTtl: 100 } // cacheTtl is rewritten to cache_ttl
31
+ });
32
+ const expected = "https://upcdn.io/1234abc/raw/example.jpg?auth=true&cache=true&cache_ttl=100";
33
+ expect(actual).toEqual(expected);
34
+ });
35
+
36
+ test("transformation preset without params", () => {
37
+ const actual = new UrlBuilder().url({
38
+ accountId: "1234abc",
39
+ filePath: "/example.jpg",
40
+ options: { transformation: { type: "preset", preset: "thumbnail" } }
41
+ });
42
+ const expected = "https://upcdn.io/1234abc/thumbnail/example.jpg";
43
+ expect(actual).toEqual(expected);
44
+ });
45
+
46
+ test("transformation preset with one param", () => {
47
+ const actual = new UrlBuilder().url({
48
+ accountId: "1234abc",
49
+ filePath: "/example.jpg",
50
+ options: { transformation: { type: "preset", preset: "thumbnail", params: { artifact: "/foo" } } }
51
+ });
52
+ const expected = "https://upcdn.io/1234abc/thumbnail/example.jpg?artifact=%2Ffoo";
53
+ expect(actual).toEqual(expected);
54
+ });
55
+
56
+ test("transformation preset with multiple params", () => {
57
+ const actual = new UrlBuilder().url({
58
+ accountId: "1234abc",
59
+ filePath: "/example.jpg",
60
+ options: {
61
+ transformation: { type: "preset", preset: "thumbnail", params: { large: false, artifact: "/foo" } }
62
+ }
63
+ });
64
+ const expected = "https://upcdn.io/1234abc/thumbnail/example.jpg?large=false&artifact=%2Ffoo";
65
+ expect(actual).toEqual(expected);
66
+ });
67
+
68
+ test("transformation preset with rewritten params", () => {
69
+ const actual = new UrlBuilder().url({
70
+ accountId: "1234abc",
71
+ filePath: "/example.jpg",
72
+ options: {
73
+ // cachePermanently is rewritten to cache_perm
74
+ transformation: { type: "preset", preset: "thumbnail", params: { cachePermanently: false, artifact: "/foo" } }
75
+ }
76
+ });
77
+ const expected = "https://upcdn.io/1234abc/thumbnail/example.jpg?cache_perm=false&artifact=%2Ffoo";
78
+ expect(actual).toEqual(expected);
79
+ });
80
+
81
+ test("transformation API without params", () => {
82
+ const actual = new UrlBuilder().url({
83
+ accountId: "1234abc",
84
+ filePath: "/example.jpg",
85
+ options: {
86
+ transformation: {
87
+ type: "image"
88
+ }
89
+ }
90
+ });
91
+ const expected = "https://upcdn.io/1234abc/image/example.jpg";
92
+ expect(actual).toEqual(expected);
93
+ });
94
+
95
+ test("transformation API with one param", () => {
96
+ const actual = new UrlBuilder().url({
97
+ accountId: "1234abc",
98
+ filePath: "/example.jpg",
99
+ options: {
100
+ transformation: {
101
+ type: "image",
102
+ params: {
103
+ w: 42
104
+ }
105
+ }
106
+ }
107
+ });
108
+ const expected = "https://upcdn.io/1234abc/image/example.jpg?w=42";
109
+ expect(actual).toEqual(expected);
110
+ });
111
+
112
+ test("transformation API with a mix of params", () => {
113
+ const actual = new UrlBuilder().url({
114
+ accountId: "1234abc",
115
+ filePath: "/example.jpg",
116
+ options: {
117
+ transformation: {
118
+ type: "image",
119
+ params: {
120
+ w: 42,
121
+ h: 50
122
+ }
123
+ },
124
+ auth: true
125
+ }
126
+ });
127
+ const expected = "https://upcdn.io/1234abc/image/example.jpg?auth=true&w=42&h=50";
128
+ expect(actual).toEqual(expected);
129
+ });
130
+
131
+ test("elide null and undefined", () => {
132
+ const actual = new UrlBuilder().url({
133
+ accountId: "1234abc",
134
+ filePath: "/example.jpg",
135
+ options: {
136
+ transformation: {
137
+ type: "image",
138
+ params: {
139
+ w: null,
140
+ h: undefined,
141
+ r: 52
142
+ }
143
+ },
144
+ auth: true
145
+ }
146
+ });
147
+ const expected = "https://upcdn.io/1234abc/image/example.jpg?auth=true&r=52";
148
+ expect(actual).toEqual(expected);
149
+ });
150
+
151
+ test("custom CNAME", () => {
152
+ const actual = new UrlBuilder({ cdnUrl: "https://example.com" }).url({
153
+ accountId: "1234abc",
154
+ filePath: "/example.jpg",
155
+ options: {
156
+ transformation: {
157
+ type: "image",
158
+ params: {
159
+ w: 42,
160
+ h: 50
161
+ }
162
+ }
163
+ }
164
+ });
165
+ const expected = "https://example.com/1234abc/image/example.jpg?w=42&h=50";
166
+ expect(actual).toEqual(expected);
167
+ });
168
+ });
@@ -1,10 +0,0 @@
1
- (self["webpackChunk_bytescale_sdk"] = self["webpackChunk_bytescale_sdk"] || []).push([["_70b2"],{
2
-
3
- /***/ "?70b2":
4
- /***/ (function() {
5
-
6
- /* (ignored) */
7
-
8
- /***/ })
9
-
10
- }]);
@@ -1,10 +0,0 @@
1
- (self["webpackChunk_bytescale_sdk"] = self["webpackChunk_bytescale_sdk"] || []).push([["_7392"],{
2
-
3
- /***/ "?7392":
4
- /***/ (function() {
5
-
6
- /* (ignored) */
7
-
8
- /***/ })
9
-
10
- }]);
@@ -1,13 +0,0 @@
1
- /// <reference types="node" />
2
- import type * as stream from "stream";
3
- import type * as buffer from "buffer";
4
- import { BlobLike } from "./UploadModel";
5
- /**
6
- * Provides access to Node.js built-in libraries. Should only be called when we're certain we're running in Node.js.
7
- *
8
- * We import lazily to support browsers, which don't have these modules, but also won't call these methods so won't trigger the imports.
9
- */
10
- export declare class NodeUtils {
11
- static createStream(): Promise<stream.Readable>;
12
- static createBlob(sources: string[], options?: buffer.BlobOptions): Promise<BlobLike>;
13
- }
@@ -1,39 +0,0 @@
1
- import { FileDetails, BytescaleApiClientConfig } from "../generated";
2
- import { UploadManagerParams } from "./UploadModel";
3
- export declare class UploadManager {
4
- private readonly config;
5
- private readonly stringMimeType;
6
- private readonly defaultMaxConcurrentUploadParts;
7
- private readonly uploadApi;
8
- constructor(config: BytescaleApiClientConfig);
9
- upload(request: UploadManagerParams): Promise<FileDetails>;
10
- private checkIfCancelled;
11
- private beginUpload;
12
- private uploadPart;
13
- /**
14
- * Returns etag for the part.
15
- */
16
- private putUploadPart;
17
- private coerceRequestBody;
18
- private makeRequestBody;
19
- private bufferToStream;
20
- /**
21
- * Only expected to be called in Node.js environments, and as such, we can assume 'BlobLike' is not a DOM 'File' object.
22
- */
23
- private blobToBuffer;
24
- private sliceDataForRequest;
25
- private isNodeJs;
26
- private getUploadPart;
27
- private calculateSize;
28
- private calculateMime;
29
- private calculateOriginalFileName;
30
- private calculateMaxConcurrency;
31
- private getChunkedStream;
32
- private foldData;
33
- private foldDataRaw;
34
- private processUploadSource;
35
- private stringToBlob;
36
- private globalBlob;
37
- private nodeJsBlob;
38
- private mapAsync;
39
- }
@@ -1,3 +0,0 @@
1
- export * from "./ChunkedStream";
2
- export * from "./UploadModel";
3
- export * from "./UploadManager";