@lowdefy/plugin-aws 4.1.0 → 4.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/plugin-aws",
3
- "version": "4.1.0",
3
+ "version": "4.2.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -40,9 +40,9 @@
40
40
  "dist/*"
41
41
  ],
42
42
  "dependencies": {
43
- "@lowdefy/block-utils": "4.1.0",
44
- "@lowdefy/blocks-antd": "4.1.0",
45
- "@lowdefy/helpers": "4.1.0",
43
+ "@lowdefy/block-utils": "4.2.0",
44
+ "@lowdefy/blocks-antd": "4.2.0",
45
+ "@lowdefy/helpers": "4.2.0",
46
46
  "antd": "4.24.14",
47
47
  "aws-sdk": "2.1459.0",
48
48
  "react": "18.2.0",
@@ -50,9 +50,9 @@
50
50
  },
51
51
  "devDependencies": {
52
52
  "@emotion/jest": "11.10.5",
53
- "@lowdefy/ajv": "4.1.0",
54
- "@lowdefy/block-dev": "4.1.0",
55
- "@lowdefy/jest-yaml-transform": "4.1.0",
53
+ "@lowdefy/ajv": "4.2.0",
54
+ "@lowdefy/block-dev": "4.2.0",
55
+ "@lowdefy/jest-yaml-transform": "4.2.0",
56
56
  "@swc/cli": "0.1.63",
57
57
  "@swc/core": "1.3.99",
58
58
  "@swc/jest": "0.2.29",
@@ -1,216 +0,0 @@
1
- /*
2
- Copyright 2020-2024 Lowdefy, Inc
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- */ import React, { useEffect, useState } from 'react';
16
- import { blockDefaultProps } from '@lowdefy/block-utils';
17
- import { get } from '@lowdefy/helpers';
18
- import { Button } from '@lowdefy/blocks-antd/blocks';
19
- import { Upload } from 'antd';
20
- const makeFileValue = (file, s3Parameters)=>{
21
- const { lastModified, name, percent, size, status, type, uid } = file;
22
- const { bucket, key } = get(s3Parameters, uid, {
23
- default: {}
24
- });
25
- return {
26
- bucket,
27
- key,
28
- lastModified,
29
- name,
30
- percent,
31
- size,
32
- status,
33
- type,
34
- uid
35
- };
36
- };
37
- const makeOnChangeValue = (s3Parameters, changeEvent)=>{
38
- const { file, fileList } = changeEvent;
39
- return {
40
- file: makeFileValue(file, s3Parameters),
41
- fileList: fileList.map((fl)=>makeFileValue(fl, s3Parameters))
42
- };
43
- };
44
- const getDisabled = ({ properties, value })=>{
45
- if (properties.disabled) return true;
46
- return properties.singleFile && value && (value.fileList || []).length >= 1;
47
- };
48
- const getCustomRequest = ({ methods, setS3Parameters })=>async ({ file, onError, onProgress, onSuccess })=>{
49
- let meta;
50
- try {
51
- const { name, size, type, uid } = file;
52
- const s3PostPolicyResponse = await methods.triggerEvent({
53
- name: '__getS3PostPolicy',
54
- event: {
55
- filename: name,
56
- size,
57
- type,
58
- uid
59
- }
60
- });
61
- if (s3PostPolicyResponse.success !== true) {
62
- throw new Error('S3 post policy request error.');
63
- }
64
- const { url, fields } = s3PostPolicyResponse.responses.__getS3PostPolicy.response[0];
65
- const { bucket, key } = fields;
66
- meta = {
67
- bucket,
68
- key,
69
- filename: name,
70
- size,
71
- type,
72
- uid
73
- };
74
- setS3Parameters((prevState)=>{
75
- const ret = {
76
- ...prevState
77
- };
78
- ret[uid] = {
79
- bucket,
80
- key
81
- };
82
- return ret;
83
- });
84
- // Set 20 % progress on policy is acquired else user waits to long before progress is reported
85
- onProgress({
86
- percent: 20
87
- });
88
- // Create FormData with all required fields in S3 policy
89
- const formData = new FormData();
90
- Object.keys(fields).forEach((field)=>{
91
- formData.append(field, fields[field]);
92
- });
93
- // file needs to be the last field in the form
94
- formData.append('file', file);
95
- const xhr = new XMLHttpRequest();
96
- xhr.upload.onprogress = (event)=>{
97
- if (event.lengthComputable) {
98
- onProgress({
99
- percent: event.loaded / event.total * 80 + 20
100
- });
101
- }
102
- };
103
- xhr.addEventListener('error', async (event)=>{
104
- await methods.triggerEvent({
105
- name: 'onError',
106
- event: {
107
- meta,
108
- event
109
- }
110
- });
111
- onError(event);
112
- });
113
- xhr.addEventListener('load', async (event)=>{
114
- await methods.triggerEvent({
115
- name: 'onSuccess',
116
- event: {
117
- meta,
118
- event
119
- }
120
- });
121
- onSuccess(event);
122
- });
123
- xhr.addEventListener('loadend', async (event)=>{
124
- await methods.triggerEvent({
125
- name: 'onDone',
126
- event: {
127
- meta,
128
- event
129
- }
130
- });
131
- });
132
- xhr.open('post', url);
133
- xhr.send(formData);
134
- } catch (error) {
135
- console.error(error);
136
- await methods.triggerEvent({
137
- name: 'onError',
138
- event: {
139
- meta,
140
- error
141
- }
142
- });
143
- onError(error);
144
- }
145
- };
146
- const S3UploadButtonBlock = ({ blockId, components, events, methods, properties, value })=>{
147
- // Use state here because we need to set s3 bucket and key as block value
148
- // The customRequest function does not have access to the updated block value,
149
- // so it cannot set the value directly. customRequest sets the parameters to s3Parameters state,
150
- // and then onChange updates the block value.
151
- const [s3Parameters, setS3Parameters] = useState(value);
152
- const customRequest = getCustomRequest({
153
- methods,
154
- setS3Parameters
155
- });
156
- useEffect(()=>{
157
- methods.setValue({
158
- file: null,
159
- fileList: []
160
- });
161
- methods.registerEvent({
162
- name: '__getS3PostPolicy',
163
- actions: [
164
- {
165
- id: '__getS3PostPolicy',
166
- type: 'Request',
167
- params: [
168
- properties.s3PostPolicyRequestId
169
- ]
170
- }
171
- ]
172
- });
173
- }, []);
174
- const disabled = getDisabled({
175
- properties,
176
- value
177
- });
178
- return /*#__PURE__*/ React.createElement(Upload, {
179
- accept: properties.accept,
180
- customRequest: customRequest,
181
- disabled: disabled,
182
- id: blockId,
183
- multiple: !properties.singleFile,
184
- showUploadList: properties.showUploadList,
185
- onChange: (event)=>{
186
- methods.setValue(makeOnChangeValue(s3Parameters, event));
187
- methods.triggerEvent({
188
- name: 'onChange'
189
- });
190
- }
191
- }, /*#__PURE__*/ React.createElement(Button, {
192
- blockId: `${blockId}_button`,
193
- components: components,
194
- events: events,
195
- properties: {
196
- disabled,
197
- icon: 'AiOutlineUpload',
198
- title: 'Upload',
199
- type: 'default',
200
- ...properties.button
201
- },
202
- methods: methods
203
- }));
204
- };
205
- S3UploadButtonBlock.defaultProps = blockDefaultProps;
206
- S3UploadButtonBlock.meta = {
207
- valueType: 'object',
208
- category: 'input',
209
- icons: [
210
- 'AiOutlineUpload'
211
- ],
212
- styles: [
213
- 'blocks/S3UploadButton/style.less'
214
- ]
215
- };
216
- export default S3UploadButtonBlock;