@leancodepl/api-binary-blob 8.5.0 → 8.5.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/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # @leancodepl/api-binary-blob
2
+
3
+ Blob conversion utilities for ApiBinary with file handling support.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @leancodepl/api-binary-blob
9
+ # or
10
+ yarn add @leancodepl/api-binary-blob
11
+ ```
12
+
13
+ ## API
14
+
15
+ ### `fromBlob(blob)`
16
+
17
+ Converts Blob to ApiBinary asynchronously.
18
+
19
+ **Parameters:**
20
+
21
+ - `blob: Blob` - The Blob to convert to ApiBinary
22
+
23
+ **Returns:** Promise resolving to ApiBinary or undefined if blob is undefined
24
+
25
+ ### `toBlob(apiBinary, contentType)`
26
+
27
+ Converts ApiBinary to Blob with optional content type.
28
+
29
+ **Parameters:**
30
+
31
+ - `apiBinary: ApiBinary` - The ApiBinary to convert to Blob
32
+ - `contentType?: string` - Optional MIME type for the Blob
33
+
34
+ **Returns:** Blob instance or undefined if apiBinary is null/undefined
35
+
36
+ ## Usage Examples
37
+
38
+ ### File Upload
39
+
40
+ ```typescript
41
+ import { fromBlob, toBlob } from "@leancodepl/api-binary-blob"
42
+
43
+ const handleFileUpload = async (file: File) => {
44
+ const binary = await fromBlob(file)
45
+
46
+ await api.post("/upload", {
47
+ fileName: file.name,
48
+ content: binary,
49
+ })
50
+ }
51
+ ```
52
+
53
+ ### File Download
54
+
55
+ ```typescript
56
+ import { toBlob } from "@leancodepl/api-binary-blob"
57
+
58
+ const downloadFile = async (binary: ApiBinary, filename: string) => {
59
+ const blob = toBlob(binary, "application/octet-stream")
60
+ const url = URL.createObjectURL(blob)
61
+
62
+ const link = document.createElement("a")
63
+ link.href = url
64
+ link.download = filename
65
+ link.click()
66
+
67
+ URL.revokeObjectURL(url)
68
+ }
69
+ ```
70
+
71
+ ### Image Processing
72
+
73
+ ```typescript
74
+ import { fromBlob, toBlob } from "@leancodepl/api-binary-blob"
75
+
76
+ const processImage = async (imageFile: File) => {
77
+ const binary = await fromBlob(imageFile)
78
+
79
+ const processedBlob = toBlob(binary, "image/jpeg")
80
+ const preview = URL.createObjectURL(processedBlob)
81
+
82
+ const img = document.createElement("img")
83
+ img.src = preview
84
+ document.body.appendChild(img)
85
+ }
86
+ ```
@@ -0,0 +1 @@
1
+ exports._default = require('./index.cjs.js').default;
package/index.cjs.js ADDED
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ var apiBinary = require('@leancodepl/api-binary');
4
+
5
+ function fromBlob(blob) {
6
+ if (!blob) return Promise.resolve(undefined);
7
+ return new Promise((resolve, reject)=>{
8
+ try {
9
+ const reader = new FileReader();
10
+ reader.onloadend = ()=>{
11
+ const result = reader.result;
12
+ if (typeof result === "string") {
13
+ // we need to strip the `data:*/*;base64,` from the beginning
14
+ resolve(apiBinary.fromRaw(result.substring(1 + result.indexOf(",", result.indexOf(";")))));
15
+ return;
16
+ }
17
+ throw new Error("Unknown blob result received for ApiBinary creation");
18
+ };
19
+ reader.onerror = reject;
20
+ reader.readAsDataURL(blob);
21
+ } catch (e) {
22
+ reject(e);
23
+ }
24
+ });
25
+ }
26
+
27
+ function base64toBlob(base64Data, contentType, sliceSize = 512) {
28
+ const byteCharacters = atob(base64Data);
29
+ const byteArrays = [];
30
+ for(let offset = 0; offset < byteCharacters.length; offset += sliceSize){
31
+ const slice = byteCharacters.slice(offset, offset + sliceSize);
32
+ const byteNumbers = new Array(slice.length);
33
+ for(let i = 0; i < slice.length; i++){
34
+ byteNumbers[i] = slice.charCodeAt(i);
35
+ }
36
+ byteArrays.push(new Uint8Array(byteNumbers));
37
+ }
38
+ const blob = new Blob(byteArrays, {
39
+ type: contentType
40
+ });
41
+ return blob;
42
+ }
43
+
44
+ function toBlob(apiBinary$1, contentType) {
45
+ if (apiBinary$1 === undefined || apiBinary$1 === null) {
46
+ return undefined;
47
+ }
48
+ return base64toBlob(apiBinary.toRaw(apiBinary$1), contentType);
49
+ }
50
+
51
+ exports.fromBlob = fromBlob;
52
+ exports.toBlob = toBlob;
53
+ Object.keys(apiBinary).forEach(function (k) {
54
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
55
+ enumerable: true,
56
+ get: function () { return apiBinary[k]; }
57
+ });
58
+ });
package/index.cjs.mjs ADDED
@@ -0,0 +1,2 @@
1
+ export * from './index.cjs.js';
2
+ export { _default as default } from './index.cjs.default.js';
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
package/index.esm.js ADDED
@@ -0,0 +1,50 @@
1
+ import { fromRaw, toRaw } from '@leancodepl/api-binary';
2
+ export * from '@leancodepl/api-binary';
3
+
4
+ function fromBlob(blob) {
5
+ if (!blob) return Promise.resolve(undefined);
6
+ return new Promise((resolve, reject)=>{
7
+ try {
8
+ const reader = new FileReader();
9
+ reader.onloadend = ()=>{
10
+ const result = reader.result;
11
+ if (typeof result === "string") {
12
+ // we need to strip the `data:*/*;base64,` from the beginning
13
+ resolve(fromRaw(result.substring(1 + result.indexOf(",", result.indexOf(";")))));
14
+ return;
15
+ }
16
+ throw new Error("Unknown blob result received for ApiBinary creation");
17
+ };
18
+ reader.onerror = reject;
19
+ reader.readAsDataURL(blob);
20
+ } catch (e) {
21
+ reject(e);
22
+ }
23
+ });
24
+ }
25
+
26
+ function base64toBlob(base64Data, contentType, sliceSize = 512) {
27
+ const byteCharacters = atob(base64Data);
28
+ const byteArrays = [];
29
+ for(let offset = 0; offset < byteCharacters.length; offset += sliceSize){
30
+ const slice = byteCharacters.slice(offset, offset + sliceSize);
31
+ const byteNumbers = new Array(slice.length);
32
+ for(let i = 0; i < slice.length; i++){
33
+ byteNumbers[i] = slice.charCodeAt(i);
34
+ }
35
+ byteArrays.push(new Uint8Array(byteNumbers));
36
+ }
37
+ const blob = new Blob(byteArrays, {
38
+ type: contentType
39
+ });
40
+ return blob;
41
+ }
42
+
43
+ function toBlob(apiBinary, contentType) {
44
+ if (apiBinary === undefined || apiBinary === null) {
45
+ return undefined;
46
+ }
47
+ return base64toBlob(toRaw(apiBinary), contentType);
48
+ }
49
+
50
+ export { fromBlob, toBlob };
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@leancodepl/api-binary-blob",
3
- "version": "8.5.0",
3
+ "version": "8.5.1",
4
4
  "license": "Apache-2.0",
5
5
  "dependencies": {
6
- "@leancodepl/api-binary": "8.5.0"
6
+ "@leancodepl/api-binary": "8.5.1"
7
7
  },
8
8
  "devDependencies": {
9
9
  "blob-polyfill": "^9.0.0"
@@ -39,11 +39,6 @@
39
39
  "name": "LeanCode",
40
40
  "url": "https://leancode.co"
41
41
  },
42
- "files": [
43
- "dist",
44
- "README.md",
45
- "CHANGELOG.md"
46
- ],
47
42
  "sideEffects": false,
48
43
  "exports": {
49
44
  "./package.json": "./package.json",
package/src/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from "@leancodepl/api-binary";
2
+ export * from "./lib/fromBlob";
3
+ export * from "./lib/toBlob";
@@ -0,0 +1 @@
1
+ export declare function base64toBlob(base64Data: string, contentType?: string, sliceSize?: number): Blob;
@@ -0,0 +1,17 @@
1
+ import { ApiBinary } from "@leancodepl/api-binary";
2
+ /**
3
+ * Converts Blob to ApiBinary asynchronously.
4
+ *
5
+ * Reads the Blob content as base64 data URL and converts it to ApiBinary format.
6
+ * Handles both required and optional blob parameters with proper overloads.
7
+ *
8
+ * @param blob - The Blob to convert to ApiBinary
9
+ * @returns Promise resolving to ApiBinary or undefined if blob is undefined
10
+ * @example
11
+ * ```typescript
12
+ * const file = new File(['hello world'], 'test.txt', { type: 'text/plain' });
13
+ * const binary = await fromBlob(file);
14
+ * ```
15
+ */
16
+ export declare function fromBlob(blob: Blob): Promise<ApiBinary>;
17
+ export declare function fromBlob(blob?: Blob): Promise<ApiBinary | undefined>;
@@ -0,0 +1,18 @@
1
+ import { ApiBinary } from "@leancodepl/api-binary";
2
+ /**
3
+ * Converts ApiBinary to Blob with optional content type.
4
+ *
5
+ * Transforms ApiBinary base64 data to a Blob object for file operations.
6
+ * Supports both required and optional parameters.
7
+ *
8
+ * @param apiBinary - The ApiBinary to convert to Blob
9
+ * @param contentType - Optional MIME type for the Blob
10
+ * @returns Blob instance or undefined if apiBinary is null/undefined
11
+ * @example
12
+ * ```typescript
13
+ * const blob = toBlob(binary, 'image/jpeg');
14
+ * const url = URL.createObjectURL(blob);
15
+ * ```
16
+ */
17
+ export declare function toBlob(apiBinary: ApiBinary, contentType?: string): Blob;
18
+ export declare function toBlob(apiBinary?: ApiBinary | null, contentType?: string): Blob | undefined;