nzk-react-components 0.4.1 → 0.4.2

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.
@@ -5,5 +5,6 @@ export interface Props {
5
5
  }
6
6
  declare const useCloudinary: (props?: Props | undefined) => {
7
7
  uploadImage: (file: Blob | string, onComplete?: ((imageUrl: string) => void) | undefined) => void;
8
+ promiseUploadImage: (file: Blob | string) => Promise<unknown>;
8
9
  };
9
10
  export default useCloudinary;
@@ -38,8 +38,50 @@ var useCloudinary = function (props) {
38
38
  });
39
39
  }
40
40
  };
41
+ var promiseUploadImage = function (file) {
42
+ return new Promise(function (resolve, reject) {
43
+ var url = "https://api.cloudinary.com/v1_1/" + cloudName + "/upload";
44
+ var xhr = new XMLHttpRequest();
45
+ var fd = new FormData();
46
+ xhr.addEventListener('error', function () {
47
+ reject(new Error("Couldn't reach cloudinary to upload image"));
48
+ });
49
+ xhr.open('POST', url, true);
50
+ xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
51
+ xhr.onreadystatechange = function () {
52
+ if (xhr.readyState === 4 && xhr.status < 300) {
53
+ var response = JSON.parse(xhr.responseText);
54
+ if (response.secure_url) {
55
+ resolve(response.secure_url);
56
+ }
57
+ else {
58
+ reject(new Error("No secure_url returned by cloudinary"));
59
+ }
60
+ }
61
+ else if (xhr.readyState === 4 && xhr.status >= 300) {
62
+ reject(new Error("Cloudinary returned error code: " + xhr.status));
63
+ }
64
+ };
65
+ fd.append('upload_preset', unsignedUploadPreset);
66
+ if (uploadTag)
67
+ fd.append('tags', uploadTag);
68
+ if (file instanceof Blob) {
69
+ fd.append('file', file);
70
+ xhr.send(fd);
71
+ }
72
+ else {
73
+ fetch(file).then(function (response) {
74
+ response.blob().then(function (blobFile) {
75
+ fd.append('file', blobFile);
76
+ xhr.send(fd);
77
+ });
78
+ });
79
+ }
80
+ });
81
+ };
41
82
  return {
42
- uploadImage: uploadImage
83
+ uploadImage: uploadImage,
84
+ promiseUploadImage: promiseUploadImage
43
85
  };
44
86
  };
45
87
 
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -3,3 +3,4 @@ import { Meta } from "@storybook/react/types-6-0";
3
3
  declare const _default: Meta<import("@storybook/react").Args>;
4
4
  export default _default;
5
5
  export declare const base64ImageUpload: Story;
6
+ export declare const base64ImagePromiseUpload: Story;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nzk-react-components",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "files": [