@bytescale/sdk 1.0.0-alpha.1
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 +21 -0
- package/README.md +178 -0
- package/dist/browser/cjs/_96d8.main.js +10 -0
- package/dist/browser/cjs/_9d47.main.js +10 -0
- package/dist/browser/cjs/main.js +3946 -0
- package/dist/node/cjs/main.js +3793 -0
- package/dist/node/esm/main.mjs +3816 -0
- package/dist/types/gen/apis/FileApi.d.ts +135 -0
- package/dist/types/gen/apis/FolderApi.d.ts +110 -0
- package/dist/types/gen/apis/JobApi.d.ts +56 -0
- package/dist/types/gen/apis/UploadApi.d.ts +81 -0
- package/dist/types/gen/apis/index.d.ts +4 -0
- package/dist/types/gen/index.d.ts +3 -0
- package/dist/types/gen/models/index.d.ts +2856 -0
- package/dist/types/gen/runtime.d.ts +178 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/utils/ChunkedStream.d.ts +29 -0
- package/dist/types/utils/Model.d.ts +35 -0
- package/dist/types/utils/NodeUtils.d.ts +13 -0
- package/dist/types/utils/UploadManager.d.ts +39 -0
- package/dist/types/utils/index.d.ts +3 -0
- package/package.json +97 -0
- package/tests/ChunkedStream.test.ts +49 -0
- package/tests/UploadManager.test.ts +164 -0
- package/tests/utils/RandomStream.ts +76 -0
- package/tests/utils/StreamToBuffer.ts +8 -0
- package/tests/utils/TempUtils.ts +9 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Upload Ltd
|
|
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,178 @@
|
|
|
1
|
+
# Bytescale JavaScript SDK
|
|
2
|
+
|
|
3
|
+
The Bytescale JavaScript SDK allows you to quickly integrate your application with [Bytescale](https://www.bytescale.com).
|
|
4
|
+
|
|
5
|
+
Effortlessly upload, optimize, transform, and host your digital media assets.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @bytescale/sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Additional step for Node.js:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install node-fetch
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Full Documentation
|
|
20
|
+
|
|
21
|
+
**[Bytescale JavaScript SDK Full Documentation »](https://www.bytescale.com/docs/sdks/javascript)**
|
|
22
|
+
|
|
23
|
+
## Quick Guide
|
|
24
|
+
|
|
25
|
+
- [Upload a File](#upload-a-file)
|
|
26
|
+
- [Download a File](#download-a-file)
|
|
27
|
+
- [Process a File](#process-a-file)
|
|
28
|
+
- [Get File Details](#get-file-details)
|
|
29
|
+
- [List Folder](#list-folder)
|
|
30
|
+
- **[See All Methods »](https://www.bytescale.com/docs/sdks/javascript)**
|
|
31
|
+
|
|
32
|
+
### Upload a File
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
import * as Upload from "@bytescale/sdk";
|
|
36
|
+
import fetch from "node-fetch"; // Node.js only.
|
|
37
|
+
|
|
38
|
+
const uploadManager = new Upload.UploadManager(
|
|
39
|
+
new Upload.Configuration({
|
|
40
|
+
fetchApi: fetch, // 'fetch as any' for TypeScript
|
|
41
|
+
apiKey: "YOUR_UPLOAD_API_KEY" // e.g. "secret_xxxxx"
|
|
42
|
+
})
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
uploadManager
|
|
46
|
+
.upload({
|
|
47
|
+
accountId: "YOUR_UPLOAD_ACCOUNT_ID", // e.g. "W142hJk"
|
|
48
|
+
// Supported types for 'data' field:
|
|
49
|
+
// - String
|
|
50
|
+
// - Blob
|
|
51
|
+
// - File (i.e. from a DOM file input element)
|
|
52
|
+
// - Buffer
|
|
53
|
+
// - ReadableStream (Node.js), e.g. fs.createReadStream("file.txt")
|
|
54
|
+
data: "Example Data"
|
|
55
|
+
})
|
|
56
|
+
.then(
|
|
57
|
+
uploadedFile => console.log(uploadedFile),
|
|
58
|
+
error => console.error(error)
|
|
59
|
+
);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Download a File
|
|
63
|
+
|
|
64
|
+
```javascript
|
|
65
|
+
import * as Upload from "@bytescale/sdk";
|
|
66
|
+
import fetch from "node-fetch"; // Node.js only.
|
|
67
|
+
|
|
68
|
+
const fileApi = new Upload.FileApi(
|
|
69
|
+
new Upload.Configuration({
|
|
70
|
+
fetchApi: fetch, // 'fetch as any' for TypeScript
|
|
71
|
+
apiKey: "YOUR_UPLOAD_API_KEY" // e.g. "secret_xxxxx"
|
|
72
|
+
})
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
fileApi
|
|
76
|
+
.downloadFile({
|
|
77
|
+
accountId: "YOUR_UPLOAD_ACCOUNT_ID", // e.g. "W142hJk"
|
|
78
|
+
filePath: "/uploads/2022/12/25/hello_world.txt"
|
|
79
|
+
})
|
|
80
|
+
.then(response => response.text()) // .text() | .json() | .blob() | .stream()
|
|
81
|
+
.then(
|
|
82
|
+
fileContents => console.log(fileContents),
|
|
83
|
+
error => console.error(error)
|
|
84
|
+
);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
**Note:** you can also download files using: `https://upcdn.io/{accountId}/raw/{filePath}`
|
|
88
|
+
|
|
89
|
+
### Process a File
|
|
90
|
+
|
|
91
|
+
```javascript
|
|
92
|
+
import * as Upload from "@bytescale/sdk";
|
|
93
|
+
import fetch from "node-fetch"; // Node.js only.
|
|
94
|
+
|
|
95
|
+
const fileApi = new Upload.FileApi(
|
|
96
|
+
new Upload.Configuration({
|
|
97
|
+
fetchApi: fetch, // 'fetch as any' for TypeScript
|
|
98
|
+
apiKey: "YOUR_UPLOAD_API_KEY" // e.g. "secret_xxxxx"
|
|
99
|
+
})
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
fileApi
|
|
103
|
+
.processFile({
|
|
104
|
+
accountId: "YOUR_UPLOAD_ACCOUNT_ID", // e.g. "W142hJk"
|
|
105
|
+
filePath: "/uploads/2022/12/25/image.jpg",
|
|
106
|
+
transformation: "thumbnail" // Create transformations here: https://www.bytescale.com/dashboard/transformations
|
|
107
|
+
})
|
|
108
|
+
.then(response => response.stream()) // .text() | .json() | .blob() | .stream()
|
|
109
|
+
.then(
|
|
110
|
+
imageByteStream =>
|
|
111
|
+
new Promise((resolve, reject) => {
|
|
112
|
+
const writer = fs.createWriteStream("image-thumbnail.jpg");
|
|
113
|
+
writer.on("close", resolve);
|
|
114
|
+
writer.on("error", reject);
|
|
115
|
+
imageByteStream.pipe(writer);
|
|
116
|
+
})
|
|
117
|
+
)
|
|
118
|
+
.then(
|
|
119
|
+
() => console.log("Thumbnail saved to 'image-thumbnail.jpg'"),
|
|
120
|
+
error => console.error(error)
|
|
121
|
+
);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
**Note:** you can also process files using: `https://upcdn.io/{accountId}/{transformation}/{filePath}`
|
|
125
|
+
|
|
126
|
+
### Get File Details
|
|
127
|
+
|
|
128
|
+
```javascript
|
|
129
|
+
import * as Upload from "@bytescale/sdk";
|
|
130
|
+
import fetch from "node-fetch"; // Node.js only.
|
|
131
|
+
|
|
132
|
+
const fileApi = new Upload.FileApi(
|
|
133
|
+
new Upload.Configuration({
|
|
134
|
+
fetchApi: fetch, // 'fetch as any' for TypeScript
|
|
135
|
+
apiKey: "YOUR_UPLOAD_API_KEY" // e.g. "secret_xxxxx"
|
|
136
|
+
})
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
fileApi
|
|
140
|
+
.getFileDetails({
|
|
141
|
+
accountId: "YOUR_UPLOAD_ACCOUNT_ID", // e.g. "W142hJk"
|
|
142
|
+
filePath: "/uploads/2022/12/25/image.jpg"
|
|
143
|
+
})
|
|
144
|
+
.then(
|
|
145
|
+
fileDetails => console.log(fileDetails),
|
|
146
|
+
error => console.error(error)
|
|
147
|
+
);
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### List Folder
|
|
151
|
+
|
|
152
|
+
```javascript
|
|
153
|
+
import * as Upload from "@bytescale/sdk";
|
|
154
|
+
import fetch from "node-fetch"; // Node.js only.
|
|
155
|
+
|
|
156
|
+
const folderApi = new Upload.FolderApi(
|
|
157
|
+
new Upload.Configuration({
|
|
158
|
+
fetchApi: fetch, // 'fetch as any' for TypeScript
|
|
159
|
+
apiKey: "YOUR_UPLOAD_API_KEY" // e.g. "secret_xxxxx"
|
|
160
|
+
})
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
folderApi
|
|
164
|
+
.listFolder({
|
|
165
|
+
accountId: "YOUR_UPLOAD_ACCOUNT_ID", // e.g. "W142hJk"
|
|
166
|
+
folderPath: "/",
|
|
167
|
+
recursive: false
|
|
168
|
+
})
|
|
169
|
+
.then(
|
|
170
|
+
// Note: operation is paginated, see 'result.cursor' and 'params.cursor'.
|
|
171
|
+
result => console.log(`Items in folder: ${result.items.length}`),
|
|
172
|
+
error => console.error(error)
|
|
173
|
+
);
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## License
|
|
177
|
+
|
|
178
|
+
[MIT](LICENSE)
|