@oneblink/storage 1.0.0 → 2.0.0-beta.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.
- package/dist/OneBlinkRequestHandler.js +2 -2
- package/dist/OneBlinkRequestHandler.js.map +1 -1
- package/dist/OneBlinkUploader.d.ts +17 -15
- package/dist/OneBlinkUploader.js +33 -16
- package/dist/OneBlinkUploader.js.map +1 -1
- package/dist/types.d.ts +9 -0
- package/dist/types.js.map +1 -1
- package/dist/uploadToS3.js +4 -8
- package/dist/uploadToS3.js.map +1 -1
- package/package.json +6 -6
|
@@ -19,9 +19,9 @@ class OneBlinkRequestHandler {
|
|
|
19
19
|
request.query['key'] = this.oneblinkResponse.s3.key;
|
|
20
20
|
}
|
|
21
21
|
const requestUrl = `${request.method} ${request.protocol}//${request.hostname}${request.path}?${new URLSearchParams(request.query).toString()}`;
|
|
22
|
-
console.log('Starting
|
|
22
|
+
console.log('Starting storage upload request', requestUrl);
|
|
23
23
|
const response = await this.oneBlinkHttpHandler.handleRequest(request);
|
|
24
|
-
console.log('Finished
|
|
24
|
+
console.log('Finished storage upload request', requestUrl);
|
|
25
25
|
const oneblinkResponse = response.headers['x-oneblink-response'];
|
|
26
26
|
if (typeof oneblinkResponse === 'string') {
|
|
27
27
|
this.oneblinkResponse = JSON.parse(oneblinkResponse);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OneBlinkRequestHandler.js","sourceRoot":"","sources":["../src/OneBlinkRequestHandler.ts"],"names":[],"mappings":";;;AASA;;;;GAIG;AACH,MAAa,sBAAsB;IAQjC,YACE,mBAAyC,EACzC,iBAAqC;QAErC,IAAI,CAAC,mBAAmB,GAAG,mBAAmB,CAAA;QAC9C,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAA;IAC5C,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,OAAoB;QAC/B,IAAI,IAAI,CAAC,iBAAiB,EAAE,CAAC;YAC3B,OAAO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AO,CAAC,IAAI,IAAI,IAAI,eAAe,CAAC,OAAO,CAAC,KAA+B,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAA;QACzK,OAAO,CAAC,GAAG,CAAC,iCAAiC,EAAE,UAAU,CAAC,CAAA;QAC1D,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,mBAAmB,CAAC,aAAa,CAAC,OAAO,CAAC,CAAA;QACtE,OAAO,CAAC,GAAG,CAAC,iCAAiC,EAAE,UAAU,CAAC,CAAA;QAE1D,MAAM,gBAAgB,GAAG,QAAQ,CAAC,OAAO,CAAC,qBAAqB,CAAC,CAAA;QAChE,IAAI,OAAO,gBAAgB,KAAK,QAAQ,EAAE,CAAC;YACzC,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAA;QACtD,CAAC;QAED,IAAI,QAAQ,CAAC,UAAU,IAAI,GAAG,EAAE,CAAC;YAC/B,IAAI,CAAC,YAAY;gBACf,MAAM,IAAI,CAAC,mBAAmB,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAA;QAC/D,CAAC;QAED,OAAO;YACL,QAAQ;SACT,CAAA;IACH,CAAC;CACF;AA9CD,wDA8CC","sourcesContent":["import { HttpRequest, HttpResponse } from '@smithy/protocol-http'\nimport { RequestHandler } from '@smithy/types'\nimport {\n RequestBodyHeader,\n IOneBlinkHttpHandler,\n OneBlinkResponse,\n FailResponse,\n} from './http-handlers/types'\n\n/**\n * Our own custom request handler to allow the response header which includes\n * dynamic data from the lambda at edge to be retrieved and held for later when\n * the upload has completed.\n */\nexport class OneBlinkRequestHandler<T>\n implements RequestHandler<HttpRequest, HttpResponse>\n{\n requestBodyHeader?: RequestBodyHeader\n oneBlinkHttpHandler: IOneBlinkHttpHandler\n oneblinkResponse?: OneBlinkResponse<T>\n failResponse?: FailResponse\n\n constructor(\n oneBlinkHttpHandler: IOneBlinkHttpHandler,\n requestBodyHeader?: RequestBodyHeader,\n ) {\n this.oneBlinkHttpHandler = oneBlinkHttpHandler\n this.requestBodyHeader = requestBodyHeader\n }\n\n async handle(request: HttpRequest) {\n if (this.requestBodyHeader) {\n request.headers['x-oneblink-request-body'] = JSON.stringify(\n this.requestBodyHeader,\n )\n }\n\n if (this.oneblinkResponse) {\n request.query['key'] = this.oneblinkResponse.s3.key\n }\n\n const requestUrl = `${request.method} ${request.protocol}//${request.hostname}${request.path}?${new URLSearchParams(request.query as Record<string, string>).toString()}`\n console.log('Starting storage upload request', requestUrl)\n const response = await this.oneBlinkHttpHandler.handleRequest(request)\n console.log('Finished storage upload request', requestUrl)\n\n const oneblinkResponse = response.headers['x-oneblink-response']\n if (typeof oneblinkResponse === 'string') {\n this.oneblinkResponse = JSON.parse(oneblinkResponse)\n }\n\n if (response.statusCode >= 400) {\n this.failResponse =\n await this.oneBlinkHttpHandler.handleFailResponse(response)\n }\n\n return {\n response,\n }\n }\n}\n"]}
|
|
@@ -45,21 +45,14 @@ export default class OneBlinkUploader {
|
|
|
45
45
|
* @param data The submission upload data and options
|
|
46
46
|
* @returns The upload result
|
|
47
47
|
*/
|
|
48
|
-
uploadSubmission({ submission, definition, device, userToken, previousFormSubmissionApprovalId, jobId, formsAppId, externalId, taskId, taskActionId, taskGroupInstanceId, recaptchas, onProgress, abortSignal, }: UploadFormSubmissionOptions & {
|
|
49
|
-
/** The identifier of the task that will be marked as completed */
|
|
50
|
-
taskId?: string;
|
|
51
|
-
/** The identifier of the task action that was used to complete the task */
|
|
52
|
-
taskActionId?: string;
|
|
53
|
-
/**
|
|
54
|
-
* The identifier of the task group instance that the completed task is
|
|
55
|
-
* associated with
|
|
56
|
-
*/
|
|
57
|
-
taskGroupInstanceId?: string;
|
|
48
|
+
uploadSubmission({ submission, definition, device, userToken, previousFormSubmissionApprovalId, jobId, formsAppId, externalId, taskId, taskActionId, taskGroupInstanceId, formSubmissionDraftId, recaptchas, onProgress, abortSignal, }: UploadFormSubmissionOptions & {
|
|
58
49
|
/** The reCAPTCHA tokens to validate the submission */
|
|
59
50
|
recaptchas?: {
|
|
60
51
|
/** A reCAPTCHA token */
|
|
61
52
|
token: string;
|
|
62
53
|
}[];
|
|
54
|
+
/** The identifier of the draft to mark as submitted. */
|
|
55
|
+
formSubmissionDraftId?: string;
|
|
63
56
|
}): Promise<import("./http-handlers/types").OneBlinkResponse<{
|
|
64
57
|
submissionTimestamp: string;
|
|
65
58
|
submissionId: string;
|
|
@@ -120,6 +113,9 @@ export default class OneBlinkUploader {
|
|
|
120
113
|
* // ...
|
|
121
114
|
* },
|
|
122
115
|
* formsAppId: 1,
|
|
116
|
+
* formSubmissionDraftId: '',
|
|
117
|
+
* createdAt: new Date().toISOString(),
|
|
118
|
+
* title: '',
|
|
123
119
|
* onProgress: (progress) => {
|
|
124
120
|
* // ...
|
|
125
121
|
* },
|
|
@@ -129,13 +125,19 @@ export default class OneBlinkUploader {
|
|
|
129
125
|
* @param data The submission upload data and options
|
|
130
126
|
* @returns The upload result
|
|
131
127
|
*/
|
|
132
|
-
|
|
128
|
+
uploadFormSubmissionDraft({ submission, definition, device, userToken, previousFormSubmissionApprovalId, jobId, formsAppId, externalId, taskId, taskActionId, taskGroupInstanceId, formSubmissionDraftId, createdAt, title, lastElementUpdated, onProgress, abortSignal, }: UploadFormSubmissionOptions & {
|
|
129
|
+
/** The identifier of the draft that a new version should be created for. */
|
|
130
|
+
formSubmissionDraftId: string;
|
|
131
|
+
/**
|
|
132
|
+
* The date and time (in ISO format) when the draft data was saved by a
|
|
133
|
+
* user.
|
|
134
|
+
*/
|
|
135
|
+
createdAt: string;
|
|
136
|
+
/** The title input by a user to identify the draft. */
|
|
137
|
+
title: string;
|
|
133
138
|
/** The identifier for the last element that was used before saving draft */
|
|
134
139
|
lastElementUpdated?: SubmissionTypes.NewS3SubmissionData['lastElementUpdated'];
|
|
135
|
-
}): Promise<import("./http-handlers/types").OneBlinkResponse<
|
|
136
|
-
submissionTimestamp: string;
|
|
137
|
-
draftDataId: string;
|
|
138
|
-
}>>;
|
|
140
|
+
}): Promise<import("./http-handlers/types").OneBlinkResponse<SubmissionTypes.FormSubmissionDraftVersion>>;
|
|
139
141
|
/**
|
|
140
142
|
* Upload an asset file. Asset files are always public.
|
|
141
143
|
*
|
package/dist/OneBlinkUploader.js
CHANGED
|
@@ -51,7 +51,7 @@ class OneBlinkUploader {
|
|
|
51
51
|
* @param data The submission upload data and options
|
|
52
52
|
* @returns The upload result
|
|
53
53
|
*/
|
|
54
|
-
async uploadSubmission({ submission, definition, device, userToken, previousFormSubmissionApprovalId, jobId, formsAppId, externalId, taskId, taskActionId, taskGroupInstanceId, recaptchas, onProgress, abortSignal, }) {
|
|
54
|
+
async uploadSubmission({ submission, definition, device, userToken, previousFormSubmissionApprovalId, jobId, formsAppId, externalId, taskId, taskActionId, taskGroupInstanceId, formSubmissionDraftId, recaptchas = [], onProgress, abortSignal, }) {
|
|
55
55
|
const newS3SubmissionData = {
|
|
56
56
|
submission,
|
|
57
57
|
definition,
|
|
@@ -76,9 +76,10 @@ class OneBlinkUploader {
|
|
|
76
76
|
taskId,
|
|
77
77
|
taskActionId,
|
|
78
78
|
taskGroupInstanceId,
|
|
79
|
-
recaptchas: recaptchas || [],
|
|
80
79
|
jobId,
|
|
81
80
|
previousFormSubmissionApprovalId,
|
|
81
|
+
recaptchas,
|
|
82
|
+
formSubmissionDraftId,
|
|
82
83
|
},
|
|
83
84
|
});
|
|
84
85
|
}
|
|
@@ -134,6 +135,9 @@ class OneBlinkUploader {
|
|
|
134
135
|
* // ...
|
|
135
136
|
* },
|
|
136
137
|
* formsAppId: 1,
|
|
138
|
+
* formSubmissionDraftId: '',
|
|
139
|
+
* createdAt: new Date().toISOString(),
|
|
140
|
+
* title: '',
|
|
137
141
|
* onProgress: (progress) => {
|
|
138
142
|
* // ...
|
|
139
143
|
* },
|
|
@@ -143,27 +147,40 @@ class OneBlinkUploader {
|
|
|
143
147
|
* @param data The submission upload data and options
|
|
144
148
|
* @returns The upload result
|
|
145
149
|
*/
|
|
146
|
-
async
|
|
150
|
+
async uploadFormSubmissionDraft({ submission, definition, device, userToken, previousFormSubmissionApprovalId, jobId, formsAppId, externalId, taskId, taskActionId, taskGroupInstanceId, formSubmissionDraftId, createdAt, title, lastElementUpdated, onProgress, abortSignal, }) {
|
|
151
|
+
const newS3SubmissionData = {
|
|
152
|
+
submission,
|
|
153
|
+
definition,
|
|
154
|
+
device,
|
|
155
|
+
lastElementUpdated,
|
|
156
|
+
};
|
|
157
|
+
const tags = (0, generateFormSubmissionTags_1.default)({
|
|
158
|
+
userToken,
|
|
159
|
+
previousFormSubmissionApprovalId,
|
|
160
|
+
jobId,
|
|
161
|
+
});
|
|
162
|
+
tags.append('formSubmissionDraftId', formSubmissionDraftId);
|
|
147
163
|
return await (0, uploadToS3_1.default)({
|
|
148
164
|
...this,
|
|
149
165
|
contentType: 'application/json',
|
|
150
|
-
body: JSON.stringify(
|
|
151
|
-
|
|
152
|
-
|
|
166
|
+
body: JSON.stringify(newS3SubmissionData),
|
|
167
|
+
key: 'form-submission-draft-versions',
|
|
168
|
+
tags,
|
|
169
|
+
abortSignal,
|
|
170
|
+
onProgress,
|
|
171
|
+
requestBodyHeader: {
|
|
153
172
|
formsAppId,
|
|
154
|
-
|
|
173
|
+
formId: definition.id,
|
|
155
174
|
externalId,
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
key: `forms/${definition.id}/drafts`,
|
|
160
|
-
tags: (0, generateFormSubmissionTags_1.default)({
|
|
161
|
-
userToken,
|
|
175
|
+
taskId,
|
|
176
|
+
taskActionId,
|
|
177
|
+
taskGroupInstanceId,
|
|
162
178
|
jobId,
|
|
163
179
|
previousFormSubmissionApprovalId,
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
180
|
+
createdAt,
|
|
181
|
+
title,
|
|
182
|
+
formSubmissionDraftId,
|
|
183
|
+
},
|
|
167
184
|
});
|
|
168
185
|
}
|
|
169
186
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OneBlinkUploader.js","sourceRoot":"","sources":["../src/OneBlinkUploader.ts"],"names":[],"mappings":";;;;;AAAA,8DAAqC;AAWrC,8FAAqE;AACrE;;;GAGG;AACH,MAAqB,gBAAgB;IAKnC;;;;;;;;;;;;OAYG;IACH,YAAY,KAAgC;QAC1C,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC,SAAS,CAAA;QAChC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAA;QAC1B,IAAI,CAAC,cAAc,GAAG,KAAK,CAAC,cAAc,CAAA;IAC5C,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,KAAK,CAAC,gBAAgB,CAAC,EACrB,UAAU,EACV,UAAU,EACV,MAAM,EACN,SAAS,EACT,gCAAgC,EAChC,KAAK,EACL,UAAU,EACV,UAAU,EACV,MAAM,EACN,YAAY,EACZ,mBAAmB,EACnB,UAAU,EACV,UAAU,EACV,WAAW,GAgBZ;QACC,MAAM,mBAAmB,GAAwC;YAC/D,UAAU;YACV,UAAU;YACV,MAAM;SACP,CAAA;QACD,MAAM,IAAI,GAAG,IAAA,oCAA0B,EAAC;YACtC,SAAS;YACT,gCAAgC;YAChC,KAAK;SACN,CAAC,CAAA;QAEF,OAAO,MAAM,IAAA,oBAAU,EAKpB;YACD,GAAG,IAAI;YACP,WAAW,EAAE,kBAAkB;YAC/B,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,mBAAmB,CAAC;YACzC,GAAG,EAAE,SAAS,UAAU,CAAC,EAAE,cAAc;YACzC,IAAI;YACJ,WAAW;YACX,UAAU;YACV,iBAAiB,EAAE;gBACjB,UAAU;gBACV,UAAU;gBACV,MAAM;gBACN,YAAY;gBACZ,mBAAmB;gBACnB,UAAU,EAAE,UAAU,IAAI,EAAE;gBAC5B,KAAK;gBACL,gCAAgC;aACjC;SACF,CAAC,CAAA;IACJ,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,KAAK,CAAC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,iBAAiB,EAAE;gBACjB,QAAQ,EAAE,QAAQ;aACnB;YACD,QAAQ,EAAE,KAAK;SAChB,CAAC,CAAA;IACJ,CAAC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,KAAK,CAAC,mBAAmB,CAAC,EACxB,UAAU,EACV,WAAW,EACX,IAAI,EACJ,MAAM,GACqB;QAC3B,OAAO,MAAM,IAAA,oBAAU,EAAC;YACtB,GAAG,IAAI;YACP,WAAW,EAAE,iBAAiB;YAC9B,IAAI,EAAE,IAAI;YACV,GAAG,EAAE,SAAS,MAAM,iBAAiB;YACrC,WAAW;YACX,UAAU;SACX,CAAC,CAAA;IACJ,CAAC;CACF;AArdD,mCAqdC","sourcesContent":["import uploadToS3 from './uploadToS3'\nimport {\n AttachmentUploadData,\n StorageConstructorOptions,\n UploadAssetOptions,\n UploadFormSubmissionOptions,\n UploadOptions,\n UploadEmailAttachmentOptions,\n UploadPDFConversionOptions,\n} from './types'\nimport { SubmissionTypes } from '@oneblink/types'\nimport generateFormSubmissionTags from './generateFormSubmissionTags'\n/**\n * Used to create an instance of the OneBlinkUploader, exposing methods to\n * upload submissions and other types of files\n */\nexport default class OneBlinkUploader {\n apiOrigin: StorageConstructorOptions['apiOrigin']\n region: StorageConstructorOptions['region']\n getBearerToken: StorageConstructorOptions['getBearerToken']\n\n /**\n * #### Example\n *\n * ```typescript\n * import { OneBlinkUploader } from '@oneblink/uploads'\n *\n * const uploader = new OneBlinkUploader({\n * apiOrigin: 'https://auth-api.blinkm.io',\n * region: 'ap-southeast-2',\n * getBearerToken: () => getAccessToken(),\n * })\n * ```\n */\n constructor(props: StorageConstructorOptions) {\n this.apiOrigin = props.apiOrigin\n this.region = props.region\n this.getBearerToken = props.getBearerToken\n }\n\n /**\n * Upload a submission.\n *\n * #### Example\n *\n * ```ts\n * const result = await uploader.uploadSubmission({\n * submission: {\n * // ...\n * },\n * definition: {\n * // ...\n * },\n * formsAppId: 1,\n * onProgress: (progress) => {\n * // ...\n * },\n * })\n * ```\n *\n * @param data The submission upload data and options\n * @returns The upload result\n */\n async uploadSubmission({\n submission,\n definition,\n device,\n userToken,\n previousFormSubmissionApprovalId,\n jobId,\n formsAppId,\n externalId,\n taskId,\n taskActionId,\n taskGroupInstanceId,\n recaptchas,\n onProgress,\n abortSignal,\n }: UploadFormSubmissionOptions & {\n /** The identifier of the task that will be marked as completed */\n taskId?: string\n /** The identifier of the task action that was used to complete the task */\n taskActionId?: string\n /**\n * The identifier of the task group instance that the completed task is\n * associated with\n */\n taskGroupInstanceId?: string\n /** The reCAPTCHA tokens to validate the submission */\n recaptchas?: {\n /** A reCAPTCHA token */\n token: string\n }[]\n }) {\n const newS3SubmissionData: SubmissionTypes.NewS3SubmissionData = {\n submission,\n definition,\n device,\n }\n const tags = generateFormSubmissionTags({\n userToken,\n previousFormSubmissionApprovalId,\n jobId,\n })\n\n return await uploadToS3<{\n submissionTimestamp: string\n submissionId: string\n pdfAccessToken?: string\n preventPayment: boolean\n }>({\n ...this,\n contentType: 'application/json',\n body: JSON.stringify(newS3SubmissionData),\n key: `forms/${definition.id}/submissions`,\n tags,\n abortSignal,\n onProgress,\n requestBodyHeader: {\n formsAppId,\n externalId,\n taskId,\n taskActionId,\n taskGroupInstanceId,\n recaptchas: recaptchas || [],\n jobId,\n previousFormSubmissionApprovalId,\n },\n })\n }\n\n /**\n * Upload an form submission attachment.\n *\n * #### Example\n *\n * ```ts\n * const abortController = new AbortController()\n * const result = await uploader.uploadAttachment({\n * formId: 1,\n * data: new Blob(['a string of data'], {\n * type: 'text/plain',\n * }),\n * fileName: 'file.txt',\n * contentType: 'text/plain',\n * isPrivate: true,\n * abortSignal: abortController.signal,\n * })\n * ```\n *\n * @param data The attachment upload data and options\n * @returns The upload result\n */\n async uploadAttachment({\n formId,\n fileName,\n contentType,\n isPrivate,\n data,\n username,\n onProgress,\n abortSignal,\n }: UploadOptions & {\n /** The identifier for the form that is being completed */\n formId: number\n /** The name of the file being uploaded */\n fileName: string\n /** A standard MIME type describing the format of the contents */\n contentType: string\n /** Set to `true` to prevent the file from being downloaded publicly */\n isPrivate: boolean\n /** The file data to upload */\n data: AttachmentUploadData\n /** A username to allow a single user to download the attachment file */\n username?: string\n }) {\n return await uploadToS3<{\n url: string\n attachmentDataId: string\n uploadedAt: string\n }>({\n ...this,\n contentType,\n body: data,\n key: `forms/${formId}/attachments`,\n abortSignal,\n onProgress,\n isPublic: !isPrivate,\n contentDisposition: `attachment; filename*=UTF-8''${encodeURIComponent(fileName)}`,\n requestBodyHeader: {\n username,\n fileName,\n },\n })\n }\n\n /**\n * Upload a draft submission.\n *\n * #### Example\n *\n * ```ts\n * const result = await uploader.uploadDraftSubmission({\n * submission: {\n * // ...\n * },\n * definition: {\n * // ...\n * },\n * formsAppId: 1,\n * onProgress: (progress) => {\n * // ...\n * },\n * })\n * ```\n *\n * @param data The submission upload data and options\n * @returns The upload result\n */\n async uploadDraftSubmission({\n submission,\n definition,\n device,\n userToken,\n previousFormSubmissionApprovalId,\n jobId,\n formsAppId,\n externalId,\n lastElementUpdated,\n onProgress,\n abortSignal,\n }: UploadFormSubmissionOptions & {\n /** The identifier for the last element that was used before saving draft */\n lastElementUpdated?: SubmissionTypes.NewS3SubmissionData['lastElementUpdated']\n }) {\n return await uploadToS3<{\n submissionTimestamp: string\n draftDataId: string\n }>({\n ...this,\n contentType: 'application/json',\n body: JSON.stringify({\n definition,\n submission,\n formsAppId,\n lastElementUpdated,\n externalId,\n device,\n submissionTimestamp: new Date().toISOString(),\n }),\n key: `forms/${definition.id}/drafts`,\n tags: generateFormSubmissionTags({\n userToken,\n jobId,\n previousFormSubmissionApprovalId,\n }),\n abortSignal,\n onProgress,\n })\n }\n\n /**\n * Upload an asset file. Asset files are always public.\n *\n * #### Example\n *\n * ```ts\n * const abortController = new AbortController()\n * const result = await uploader.uploadAttachment({\n * onProgress: (progress) => {\n * // ...\n * },\n * data: new Blob(['a string of data'], {\n * type: 'text/plain',\n * }),\n * fileName: 'file.txt',\n * contentType: 'text/plain',\n * abortSignal: abortController.signal,\n * organisatsionId: 'abc123',\n * })\n * ```\n *\n * @param data The asset upload data and options\n * @returns The upload result\n */\n async uploadAsset({\n onProgress,\n abortSignal,\n data,\n contentType,\n fileName,\n organisationId,\n }: UploadOptions &\n UploadAssetOptions & {\n /** The identifier for the organisation that owns the asset */\n organisationId: string\n }) {\n return await uploadToS3<{\n url: string\n }>({\n ...this,\n contentType,\n body: data,\n key: `organisations/${organisationId}/assets`,\n abortSignal,\n onProgress,\n requestBodyHeader: {\n fileName,\n },\n isPublic: true,\n })\n }\n\n /**\n * Upload an asset file for a product service such as Product Notifications.\n * Asset files are always public.\n *\n * #### Example\n *\n * ```ts\n * const abortController = new AbortController()\n * const result = await uploader.uploadAttachment({\n * onProgress: (progress) => {\n * // ...\n * },\n * data: new Blob(['a string of data'], {\n * type: 'text/plain',\n * }),\n * fileName: 'file.txt',\n * contentType: 'text/plain',\n * abortSignal: abortController.signal,\n * })\n * ```\n *\n * @param data The asset upload data and options\n * @returns The upload result\n */\n async uploadProductAsset({\n onProgress,\n abortSignal,\n data,\n contentType,\n fileName,\n }: UploadOptions & UploadAssetOptions) {\n return await uploadToS3<{\n url: string\n }>({\n ...this,\n contentType,\n body: data,\n key: `administration/assets`,\n abortSignal,\n onProgress,\n requestBodyHeader: {\n fileName,\n },\n isPublic: true,\n })\n }\n\n /**\n * Upload form prefill data.\n *\n * #### Example\n *\n * ```ts\n * const abortController = new AbortController()\n * const result = await uploader.uploadPrefillData({\n * onProgress: (progress) => {\n * // ...\n * },\n * data: {\n * field1: 'abc',\n * field2: 123,\n * },\n * formId: 12,\n * abortSignal: abortController.signal,\n * })\n * ```\n *\n * @param data The prefill upload data and options\n * @returns The upload result\n */\n async uploadPrefillData({\n formId,\n prefillData,\n onProgress,\n abortSignal,\n }: UploadOptions & {\n /** The identifier for the form that the prefill data is associated with */\n formId: number\n /** The prefill data to upload */\n prefillData: SubmissionTypes.NewS3SubmissionData['submission']\n }) {\n return await uploadToS3<{\n preFillFormDataId: string\n }>({\n ...this,\n contentType: 'application/json',\n body: JSON.stringify(prefillData),\n key: `forms/${formId}/pre-fill`,\n abortSignal,\n onProgress,\n })\n }\n\n /**\n * Upload an email attachment. Email attachments are always private.\n *\n * #### Example\n *\n * ```ts\n * const abortController = new AbortController()\n * const result = await uploader.uploadEmailAttachment({\n * onProgress: (progress) => {\n * // ...\n * },\n * data: new Blob(['a string of data'], {\n * type: 'text/plain',\n * }),\n * fileName: 'file.txt',\n * contentType: 'text/plain',\n * abortSignal: abortController.signal,\n * })\n * ```\n *\n * @param data The email attachment data and options\n * @returns The upload result\n */\n async uploadEmailAttachment({\n onProgress,\n abortSignal,\n data,\n contentType,\n fileName,\n }: UploadOptions & UploadEmailAttachmentOptions) {\n return await uploadToS3({\n ...this,\n contentType,\n body: data,\n key: 'email-attachments',\n abortSignal,\n onProgress,\n requestBodyHeader: {\n filename: fileName,\n },\n isPublic: false,\n })\n }\n\n /**\n * Upload a PDF for conversion. PDF Conversions are always private.\n *\n * #### Example\n *\n * ```ts\n * const abortController = new AbortController()\n * const result = await uploader.uploadPDFConversion({\n * onProgress: (progress) => {\n * // ...\n * },\n * data: pdfData,\n * formId: 1,\n * abortSignal: abortController.signal,\n * })\n * ```\n *\n * @param data The PDF data and options\n * @returns The upload result\n */\n async uploadPDFConversion({\n onProgress,\n abortSignal,\n data,\n formId,\n }: UploadPDFConversionOptions) {\n return await uploadToS3({\n ...this,\n contentType: 'application/pdf',\n body: data,\n key: `forms/${formId}/pdf-conversion`,\n abortSignal,\n onProgress,\n })\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"OneBlinkUploader.js","sourceRoot":"","sources":["../src/OneBlinkUploader.ts"],"names":[],"mappings":";;;;;AAAA,8DAAqC;AAWrC,8FAAqE;AACrE;;;GAGG;AACH,MAAqB,gBAAgB;IAKnC;;;;;;;;;;;;OAYG;IACH,YAAY,KAAgC;QAC1C,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC,SAA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X,IAAI,EAAE,IAAI;YACV,GAAG,EAAE,mBAAmB;YACxB,WAAW;YACX,UAAU;YACV,iBAAiB,EAAE;gBACjB,QAAQ,EAAE,QAAQ;aACnB;YACD,QAAQ,EAAE,KAAK;SAChB,CAAC,CAAA;IACJ,CAAC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,KAAK,CAAC,mBAAmB,CAAC,EACxB,UAAU,EACV,WAAW,EACX,IAAI,EACJ,MAAM,GACqB;QAC3B,OAAO,MAAM,IAAA,oBAAU,EAAC;YACtB,GAAG,IAAI;YACP,WAAW,EAAE,iBAAiB;YAC9B,IAAI,EAAE,IAAI;YACV,GAAG,EAAE,SAAS,MAAM,iBAAiB;YACrC,WAAW;YACX,UAAU;SACX,CAAC,CAAA;IACJ,CAAC;CACF;AA9eD,mCA8eC","sourcesContent":["import uploadToS3 from './uploadToS3'\nimport {\n AttachmentUploadData,\n StorageConstructorOptions,\n UploadAssetOptions,\n UploadFormSubmissionOptions,\n UploadOptions,\n UploadEmailAttachmentOptions,\n UploadPDFConversionOptions,\n} from './types'\nimport { SubmissionTypes } from '@oneblink/types'\nimport generateFormSubmissionTags from './generateFormSubmissionTags'\n/**\n * Used to create an instance of the OneBlinkUploader, exposing methods to\n * upload submissions and other types of files\n */\nexport default class OneBlinkUploader {\n apiOrigin: StorageConstructorOptions['apiOrigin']\n region: StorageConstructorOptions['region']\n getBearerToken: StorageConstructorOptions['getBearerToken']\n\n /**\n * #### Example\n *\n * ```typescript\n * import { OneBlinkUploader } from '@oneblink/uploads'\n *\n * const uploader = new OneBlinkUploader({\n * apiOrigin: 'https://auth-api.blinkm.io',\n * region: 'ap-southeast-2',\n * getBearerToken: () => getAccessToken(),\n * })\n * ```\n */\n constructor(props: StorageConstructorOptions) {\n this.apiOrigin = props.apiOrigin\n this.region = props.region\n this.getBearerToken = props.getBearerToken\n }\n\n /**\n * Upload a submission.\n *\n * #### Example\n *\n * ```ts\n * const result = await uploader.uploadSubmission({\n * submission: {\n * // ...\n * },\n * definition: {\n * // ...\n * },\n * formsAppId: 1,\n * onProgress: (progress) => {\n * // ...\n * },\n * })\n * ```\n *\n * @param data The submission upload data and options\n * @returns The upload result\n */\n async uploadSubmission({\n submission,\n definition,\n device,\n userToken,\n previousFormSubmissionApprovalId,\n jobId,\n formsAppId,\n externalId,\n taskId,\n taskActionId,\n taskGroupInstanceId,\n formSubmissionDraftId,\n recaptchas = [],\n onProgress,\n abortSignal,\n }: UploadFormSubmissionOptions & {\n /** The reCAPTCHA tokens to validate the submission */\n recaptchas?: {\n /** A reCAPTCHA token */\n token: string\n }[]\n /** The identifier of the draft to mark as submitted. */\n formSubmissionDraftId?: string\n }) {\n const newS3SubmissionData: SubmissionTypes.NewS3SubmissionData = {\n submission,\n definition,\n device,\n }\n const tags = generateFormSubmissionTags({\n userToken,\n previousFormSubmissionApprovalId,\n jobId,\n })\n\n return await uploadToS3<{\n submissionTimestamp: string\n submissionId: string\n pdfAccessToken?: string\n preventPayment: boolean\n }>({\n ...this,\n contentType: 'application/json',\n body: JSON.stringify(newS3SubmissionData),\n key: `forms/${definition.id}/submissions`,\n tags,\n abortSignal,\n onProgress,\n requestBodyHeader: {\n formsAppId,\n externalId,\n taskId,\n taskActionId,\n taskGroupInstanceId,\n jobId,\n previousFormSubmissionApprovalId,\n recaptchas,\n formSubmissionDraftId,\n },\n })\n }\n\n /**\n * Upload an form submission attachment.\n *\n * #### Example\n *\n * ```ts\n * const abortController = new AbortController()\n * const result = await uploader.uploadAttachment({\n * formId: 1,\n * data: new Blob(['a string of data'], {\n * type: 'text/plain',\n * }),\n * fileName: 'file.txt',\n * contentType: 'text/plain',\n * isPrivate: true,\n * abortSignal: abortController.signal,\n * })\n * ```\n *\n * @param data The attachment upload data and options\n * @returns The upload result\n */\n async uploadAttachment({\n formId,\n fileName,\n contentType,\n isPrivate,\n data,\n username,\n onProgress,\n abortSignal,\n }: UploadOptions & {\n /** The identifier for the form that is being completed */\n formId: number\n /** The name of the file being uploaded */\n fileName: string\n /** A standard MIME type describing the format of the contents */\n contentType: string\n /** Set to `true` to prevent the file from being downloaded publicly */\n isPrivate: boolean\n /** The file data to upload */\n data: AttachmentUploadData\n /** A username to allow a single user to download the attachment file */\n username?: string\n }) {\n return await uploadToS3<{\n url: string\n attachmentDataId: string\n uploadedAt: string\n }>({\n ...this,\n contentType,\n body: data,\n key: `forms/${formId}/attachments`,\n abortSignal,\n onProgress,\n isPublic: !isPrivate,\n contentDisposition: `attachment; filename*=UTF-8''${encodeURIComponent(fileName)}`,\n requestBodyHeader: {\n username,\n fileName,\n },\n })\n }\n\n /**\n * Upload a draft submission.\n *\n * #### Example\n *\n * ```ts\n * const result = await uploader.uploadDraftSubmission({\n * submission: {\n * // ...\n * },\n * definition: {\n * // ...\n * },\n * formsAppId: 1,\n * formSubmissionDraftId: '',\n * createdAt: new Date().toISOString(),\n * title: '',\n * onProgress: (progress) => {\n * // ...\n * },\n * })\n * ```\n *\n * @param data The submission upload data and options\n * @returns The upload result\n */\n async uploadFormSubmissionDraft({\n submission,\n definition,\n device,\n userToken,\n previousFormSubmissionApprovalId,\n jobId,\n formsAppId,\n externalId,\n taskId,\n taskActionId,\n taskGroupInstanceId,\n formSubmissionDraftId,\n createdAt,\n title,\n lastElementUpdated,\n onProgress,\n abortSignal,\n }: UploadFormSubmissionOptions & {\n /** The identifier of the draft that a new version should be created for. */\n formSubmissionDraftId: string\n /**\n * The date and time (in ISO format) when the draft data was saved by a\n * user.\n */\n createdAt: string\n /** The title input by a user to identify the draft. */\n title: string\n /** The identifier for the last element that was used before saving draft */\n lastElementUpdated?: SubmissionTypes.NewS3SubmissionData['lastElementUpdated']\n }) {\n const newS3SubmissionData: SubmissionTypes.NewS3SubmissionData = {\n submission,\n definition,\n device,\n lastElementUpdated,\n }\n const tags = generateFormSubmissionTags({\n userToken,\n previousFormSubmissionApprovalId,\n jobId,\n })\n\n tags.append('formSubmissionDraftId', formSubmissionDraftId)\n\n return await uploadToS3<SubmissionTypes.FormSubmissionDraftVersion>({\n ...this,\n contentType: 'application/json',\n body: JSON.stringify(newS3SubmissionData),\n key: 'form-submission-draft-versions',\n tags,\n abortSignal,\n onProgress,\n requestBodyHeader: {\n formsAppId,\n formId: definition.id,\n externalId,\n taskId,\n taskActionId,\n taskGroupInstanceId,\n jobId,\n previousFormSubmissionApprovalId,\n createdAt,\n title,\n formSubmissionDraftId,\n },\n })\n }\n\n /**\n * Upload an asset file. Asset files are always public.\n *\n * #### Example\n *\n * ```ts\n * const abortController = new AbortController()\n * const result = await uploader.uploadAttachment({\n * onProgress: (progress) => {\n * // ...\n * },\n * data: new Blob(['a string of data'], {\n * type: 'text/plain',\n * }),\n * fileName: 'file.txt',\n * contentType: 'text/plain',\n * abortSignal: abortController.signal,\n * organisatsionId: 'abc123',\n * })\n * ```\n *\n * @param data The asset upload data and options\n * @returns The upload result\n */\n async uploadAsset({\n onProgress,\n abortSignal,\n data,\n contentType,\n fileName,\n organisationId,\n }: UploadOptions &\n UploadAssetOptions & {\n /** The identifier for the organisation that owns the asset */\n organisationId: string\n }) {\n return await uploadToS3<{\n url: string\n }>({\n ...this,\n contentType,\n body: data,\n key: `organisations/${organisationId}/assets`,\n abortSignal,\n onProgress,\n requestBodyHeader: {\n fileName,\n },\n isPublic: true,\n })\n }\n\n /**\n * Upload an asset file for a product service such as Product Notifications.\n * Asset files are always public.\n *\n * #### Example\n *\n * ```ts\n * const abortController = new AbortController()\n * const result = await uploader.uploadAttachment({\n * onProgress: (progress) => {\n * // ...\n * },\n * data: new Blob(['a string of data'], {\n * type: 'text/plain',\n * }),\n * fileName: 'file.txt',\n * contentType: 'text/plain',\n * abortSignal: abortController.signal,\n * })\n * ```\n *\n * @param data The asset upload data and options\n * @returns The upload result\n */\n async uploadProductAsset({\n onProgress,\n abortSignal,\n data,\n contentType,\n fileName,\n }: UploadOptions & UploadAssetOptions) {\n return await uploadToS3<{\n url: string\n }>({\n ...this,\n contentType,\n body: data,\n key: `administration/assets`,\n abortSignal,\n onProgress,\n requestBodyHeader: {\n fileName,\n },\n isPublic: true,\n })\n }\n\n /**\n * Upload form prefill data.\n *\n * #### Example\n *\n * ```ts\n * const abortController = new AbortController()\n * const result = await uploader.uploadPrefillData({\n * onProgress: (progress) => {\n * // ...\n * },\n * data: {\n * field1: 'abc',\n * field2: 123,\n * },\n * formId: 12,\n * abortSignal: abortController.signal,\n * })\n * ```\n *\n * @param data The prefill upload data and options\n * @returns The upload result\n */\n async uploadPrefillData({\n formId,\n prefillData,\n onProgress,\n abortSignal,\n }: UploadOptions & {\n /** The identifier for the form that the prefill data is associated with */\n formId: number\n /** The prefill data to upload */\n prefillData: SubmissionTypes.NewS3SubmissionData['submission']\n }) {\n return await uploadToS3<{\n preFillFormDataId: string\n }>({\n ...this,\n contentType: 'application/json',\n body: JSON.stringify(prefillData),\n key: `forms/${formId}/pre-fill`,\n abortSignal,\n onProgress,\n })\n }\n\n /**\n * Upload an email attachment. Email attachments are always private.\n *\n * #### Example\n *\n * ```ts\n * const abortController = new AbortController()\n * const result = await uploader.uploadEmailAttachment({\n * onProgress: (progress) => {\n * // ...\n * },\n * data: new Blob(['a string of data'], {\n * type: 'text/plain',\n * }),\n * fileName: 'file.txt',\n * contentType: 'text/plain',\n * abortSignal: abortController.signal,\n * })\n * ```\n *\n * @param data The email attachment data and options\n * @returns The upload result\n */\n async uploadEmailAttachment({\n onProgress,\n abortSignal,\n data,\n contentType,\n fileName,\n }: UploadOptions & UploadEmailAttachmentOptions) {\n return await uploadToS3({\n ...this,\n contentType,\n body: data,\n key: 'email-attachments',\n abortSignal,\n onProgress,\n requestBodyHeader: {\n filename: fileName,\n },\n isPublic: false,\n })\n }\n\n /**\n * Upload a PDF for conversion. PDF Conversions are always private.\n *\n * #### Example\n *\n * ```ts\n * const abortController = new AbortController()\n * const result = await uploader.uploadPDFConversion({\n * onProgress: (progress) => {\n * // ...\n * },\n * data: pdfData,\n * formId: 1,\n * abortSignal: abortController.signal,\n * })\n * ```\n *\n * @param data The PDF data and options\n * @returns The upload result\n */\n async uploadPDFConversion({\n onProgress,\n abortSignal,\n data,\n formId,\n }: UploadPDFConversionOptions) {\n return await uploadToS3({\n ...this,\n contentType: 'application/pdf',\n body: data,\n key: `forms/${formId}/pdf-conversion`,\n abortSignal,\n onProgress,\n })\n }\n}\n"]}
|
package/dist/types.d.ts
CHANGED
|
@@ -45,6 +45,15 @@ export type UploadFormSubmissionOptions = UploadOptions & {
|
|
|
45
45
|
previousFormSubmissionApprovalId?: string;
|
|
46
46
|
/** The identifier of the job that will be marked as submitted */
|
|
47
47
|
jobId?: string;
|
|
48
|
+
/** The identifier of the task that will be marked as completed */
|
|
49
|
+
taskId?: string;
|
|
50
|
+
/** The identifier of the task action that was used to complete the task */
|
|
51
|
+
taskActionId?: string;
|
|
52
|
+
/**
|
|
53
|
+
* The identifier of the task group instance that the completed task is
|
|
54
|
+
* associated with
|
|
55
|
+
*/
|
|
56
|
+
taskGroupInstanceId?: string;
|
|
48
57
|
};
|
|
49
58
|
export type UploadAssetOptions = UploadOptions & {
|
|
50
59
|
/** The file data to upload */
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import { PutObjectCommandInput } from '@aws-sdk/client-s3'\nimport { SubmissionTypes } from '@oneblink/types'\n\nexport type ProgressListenerEvent = { progress: number; total: number }\nexport type ProgressListener = (progress: ProgressListenerEvent) => void\n\n/** The properties to be passed to the Storage class constructors */\nexport type StorageConstructorOptions = {\n /** The API origin URL used to communicate with the OneBlink API */\n apiOrigin: string\n /** The AWS region to upload the submission to */\n region: string\n /**\n * A function that returns a promise resolving to a Bearer token. If the\n * promise resolves to a truthy value, the `authorization` header will be set\n * with the value.\n */\n getBearerToken: () => Promise<string | undefined>\n}\n\nexport type UploadOptions = {\n /** An optional progress listener for tracking the progress of the upload */\n onProgress?: ProgressListener\n /** An optional AbortSignal that can be used to abort the upload */\n abortSignal?: AbortSignal\n}\n\nexport type AttachmentUploadData = NonNullable<PutObjectCommandInput['Body']>\n\nexport type UploadFormSubmissionOptions = UploadOptions & {\n /** The submission data */\n submission: SubmissionTypes.NewS3SubmissionData['submission']\n /** The form that is being submitted */\n definition: SubmissionTypes.NewS3SubmissionData['definition']\n /** The device the form is being submitted */\n device?: SubmissionTypes.NewS3SubmissionData['device']\n /** The identifier for the forms app that is being submitted from */\n formsAppId: number\n /** An encrypted token that represents the user */\n userToken?: string\n /** The external identifier that represents the submission */\n externalId?: string\n /**\n * The identifier for the previous FormSubmissionApproval that lead to a\n * clarification request\n */\n previousFormSubmissionApprovalId?: string\n /** The identifier of the job that will be marked as submitted */\n jobId?: string\n}\n\nexport type UploadAssetOptions = UploadOptions & {\n /** The file data to upload */\n data: AttachmentUploadData\n /** A standard MIME type describing the format of the contents */\n contentType: string\n /** The name of the file being uploaded */\n fileName: string\n}\n\nexport type UploadPDFConversionOptions = UploadOptions & {\n /** The PDF File to upload */\n data: AttachmentUploadData\n /** The id of the Form that the PDF Conversion is occurring on */\n formId: number\n}\n\nexport type UploadEmailAttachmentOptions = UploadAssetOptions\n"]}
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import { PutObjectCommandInput } from '@aws-sdk/client-s3'\nimport { SubmissionTypes } from '@oneblink/types'\n\nexport type ProgressListenerEvent = { progress: number; total: number }\nexport type ProgressListener = (progress: ProgressListenerEvent) => void\n\n/** The properties to be passed to the Storage class constructors */\nexport type StorageConstructorOptions = {\n /** The API origin URL used to communicate with the OneBlink API */\n apiOrigin: string\n /** The AWS region to upload the submission to */\n region: string\n /**\n * A function that returns a promise resolving to a Bearer token. If the\n * promise resolves to a truthy value, the `authorization` header will be set\n * with the value.\n */\n getBearerToken: () => Promise<string | undefined>\n}\n\nexport type UploadOptions = {\n /** An optional progress listener for tracking the progress of the upload */\n onProgress?: ProgressListener\n /** An optional AbortSignal that can be used to abort the upload */\n abortSignal?: AbortSignal\n}\n\nexport type AttachmentUploadData = NonNullable<PutObjectCommandInput['Body']>\n\nexport type UploadFormSubmissionOptions = UploadOptions & {\n /** The submission data */\n submission: SubmissionTypes.NewS3SubmissionData['submission']\n /** The form that is being submitted */\n definition: SubmissionTypes.NewS3SubmissionData['definition']\n /** The device the form is being submitted */\n device?: SubmissionTypes.NewS3SubmissionData['device']\n /** The identifier for the forms app that is being submitted from */\n formsAppId: number\n /** An encrypted token that represents the user */\n userToken?: string\n /** The external identifier that represents the submission */\n externalId?: string\n /**\n * The identifier for the previous FormSubmissionApproval that lead to a\n * clarification request\n */\n previousFormSubmissionApprovalId?: string\n /** The identifier of the job that will be marked as submitted */\n jobId?: string\n /** The identifier of the task that will be marked as completed */\n taskId?: string\n /** The identifier of the task action that was used to complete the task */\n taskActionId?: string\n /**\n * The identifier of the task group instance that the completed task is\n * associated with\n */\n taskGroupInstanceId?: string\n}\n\nexport type UploadAssetOptions = UploadOptions & {\n /** The file data to upload */\n data: AttachmentUploadData\n /** A standard MIME type describing the format of the contents */\n contentType: string\n /** The name of the file being uploaded */\n fileName: string\n}\n\nexport type UploadPDFConversionOptions = UploadOptions & {\n /** The PDF File to upload */\n data: AttachmentUploadData\n /** The id of the Form that the PDF Conversion is occurring on */\n formId: number\n}\n\nexport type UploadEmailAttachmentOptions = UploadAssetOptions\n"]}
|
package/dist/uploadToS3.js
CHANGED
|
@@ -48,20 +48,16 @@ async function uploadToS3({ region, apiOrigin, key, body, requestBodyHeader, tag
|
|
|
48
48
|
client: s3Client,
|
|
49
49
|
partSize: 5 * 1024 * 1024,
|
|
50
50
|
queueSize: oneBlinkHttpHandler.determineQueueSize(),
|
|
51
|
-
//Related github issue: https://github.com/aws/aws-sdk-js-v3/issues/2311
|
|
52
|
-
//This is a variable that is set to false by default, setting it to true
|
|
53
|
-
//means that it will force the upload to fail when one part fails on
|
|
54
|
-
//an upload. The S3 client has built in retry logic to retry uploads by default
|
|
51
|
+
// Related github issue: https://github.com/aws/aws-sdk-js-v3/issues/2311
|
|
52
|
+
// This is a variable that is set to false by default, setting it to true
|
|
53
|
+
// means that it will force the upload to fail when one part fails on
|
|
54
|
+
// an upload. The S3 client has built in retry logic to retry uploads by default
|
|
55
55
|
leavePartsOnError: true,
|
|
56
56
|
params: {
|
|
57
57
|
Bucket: bucket,
|
|
58
58
|
Key: key,
|
|
59
59
|
Body: body,
|
|
60
60
|
ContentType: contentType,
|
|
61
|
-
ServerSideEncryption: 'AES256',
|
|
62
|
-
Expires: new Date(new Date().setFullYear(new Date().getFullYear() + 1)), // Max 1 year
|
|
63
|
-
CacheControl: 'max-age=31536000', // Max 1 year(365 days),
|
|
64
|
-
ACL: 'bucket-owner-full-control',
|
|
65
61
|
Tagging: tags.toString(),
|
|
66
62
|
},
|
|
67
63
|
});
|
package/dist/uploadToS3.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uploadToS3.js","sourceRoot":"","sources":["../src/uploadToS3.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAoE;AACpE,sDAAuD;AAEvD,kFAAyD;AAEzD,mDAAwD;AACxD,qEAAiE;AAEjE,MAAM,cAAc,GAAG,CAAC,CAAA;AAExB,MAAM,cAAc,GAAG,UAAU,CAAA;AAqBjC,KAAK,UAAU,UAAU,CAAI,EAC3B,MAAM,EACN,SAAS,EACT,GAAG,EACH,IAAI,EACJ,iBAAiB,EACjB,IAAI,GAAG,IAAI,eAAe,EAAE,EAC5B,cAAc,EACd,UAAU,EACV,WAAW,EACX,WAAW,EACX,QAAQ,GACQ;IAChB,MAAM,mBAAmB,GAAG,IAAA,sCAAsB,GAAE,CAAA;IACpD,MAAM,sBAAsB,GAAG,IAAI,+CAAsB,CACvD,mBAAmB,EACnB,iBAAiB,CAClB,CAAA;IAED,4DAA4D;IAC5D,iEAAiE;IACjE,8DAA8D;IAC9D,mEAAmE;IACnE,oEAAoE;IACpE,kEAAkE;IAClE,gEAAgE;IAChE,MA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AC,UAAU;gBAC9D,aAAa,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS;aAC1D,CACF,CAAA;QACH,CAAC;QACD,MAAM,KAAK,CAAA;IACb,CAAC;IAED,IAAI,CAAC,sBAAsB,CAAC,gBAAgB,EAAE,CAAC;QAC7C,MAAM,IAAI,KAAK,CACb,uEAAuE,CACxE,CAAA;IACH,CAAC;IACD,OAAO,sBAAsB,CAAC,gBAAgB,CAAA;AAChD,CAAC;AAED,kBAAe,UAAU,CAAA;AAElB,MAAM,mCAAmC,GAAG,CACjD,QAAqE,EACrE,EAAE;IACF,MAAM,OAAO,GAAG,CAAC,CAAC,QAAQ,CAAC,MAAM,IAAI,CAAC,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,GAAG,CAAA;IAC/D,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;AAC5B,CAAC,CAAA;AALY,QAAA,mCAAmC,uCAK/C","sourcesContent":["import { PutObjectCommandInput, S3Client } from '@aws-sdk/client-s3'\nimport { Upload, Progress } from '@aws-sdk/lib-storage'\nimport { StorageConstructorOptions, UploadOptions } from './types'\nimport OneBlinkStorageError from './OneBlinkStorageError'\nimport { RequestBodyHeader } from './http-handlers/types'\nimport { getOneBlinkHttpHandler } from './http-handlers'\nimport { OneBlinkRequestHandler } from './OneBlinkRequestHandler'\n\nconst RETRY_ATTEMPTS = 3\n\nconst endpointSuffix = '/storage'\n\n/** The properties to be passed to the uploadToS3 function */\ninterface UploadToS3Props extends UploadOptions, StorageConstructorOptions {\n /** The key of the file that is being uploaded. */\n key: string\n /**\n * The body of the request. This can be a string, a Buffer, a Blob, a\n * ReadableStream, or a Readable.\n */\n body: PutObjectCommandInput['Body']\n /** Optional header to be included in the request to the OneBlink API */\n requestBodyHeader?: RequestBodyHeader\n /** An optional set of tags that will be applied to the uploaded file */\n tags?: URLSearchParams\n /** A standard MIME type describing the format of the contents */\n contentType: PutObjectCommandInput['ContentType']\n /** Set to `true` to make the upload available to download publicly */\n isPublic?: boolean\n}\n\nasync function uploadToS3<T>({\n region,\n apiOrigin,\n key,\n body,\n requestBodyHeader,\n tags = new URLSearchParams(),\n getBearerToken,\n onProgress,\n abortSignal,\n contentType,\n isPublic,\n}: UploadToS3Props) {\n const oneBlinkHttpHandler = getOneBlinkHttpHandler()\n const oneBlinkRequestHandler = new OneBlinkRequestHandler<T>(\n oneBlinkHttpHandler,\n requestBodyHeader,\n )\n\n // The endpoint we use instead of the the AWS S3 endpoint is\n // formatted internally by the AWS S3 SDK. It will add the Bucket\n // parameter below as the subdomain to the URL (as long as the\n // bucket does not contain a `.`). The logic below allows the final\n // URL used to upload the object to be the origin that is passed in.\n // The suffix on the end is important as it will allow us to route\n // traffic to S3 via lambda at edge instead of going to our API.\n const url = new URL(endpointSuffix, apiOrigin)\n const [bucket, ...domainParts] = url.hostname.split('.')\n url.hostname = domainParts.join('.')\n\n const s3Client = new S3Client({\n endpoint: url.href,\n region: region,\n maxAttempts: RETRY_ATTEMPTS,\n requestHandler: oneBlinkRequestHandler,\n // Sign requests with our own Authorization header instead\n // of letting AWS SDK attempt to generate credentials\n signer: {\n sign: async (request) => {\n const token = await getBearerToken()\n if (token) {\n request.headers['authorization'] = 'Bearer ' + token\n }\n\n return request\n },\n },\n })\n\n if (isPublic) {\n tags.append('public-read', 'yes')\n }\n\n const managedUpload = new Upload({\n client: s3Client,\n partSize: 5 * 1024 * 1024,\n queueSize: oneBlinkHttpHandler.determineQueueSize(),\n // Related github issue: https://github.com/aws/aws-sdk-js-v3/issues/2311\n // This is a variable that is set to false by default, setting it to true\n // means that it will force the upload to fail when one part fails on\n // an upload. The S3 client has built in retry logic to retry uploads by default\n leavePartsOnError: true,\n params: {\n Bucket: bucket,\n Key: key,\n Body: body,\n ContentType: contentType,\n Tagging: tags.toString(),\n },\n })\n\n managedUpload.on('httpUploadProgress', (progress) => {\n if (onProgress && progress.total) {\n const percent = determineUploadProgressAsPercentage({\n ...progress,\n total: progress.total,\n })\n onProgress({ progress: percent, total: 100 })\n }\n })\n\n abortSignal?.addEventListener('abort', () => {\n managedUpload.abort()\n })\n\n try {\n await managedUpload.done()\n } catch (error) {\n if (oneBlinkRequestHandler.failResponse) {\n throw new OneBlinkStorageError(\n oneBlinkRequestHandler.failResponse.message,\n {\n httpStatusCode: oneBlinkRequestHandler.failResponse.statusCode,\n originalError: error instanceof Error ? error : undefined,\n },\n )\n }\n throw error\n }\n\n if (!oneBlinkRequestHandler.oneblinkResponse) {\n throw new Error(\n 'No response from server. Something went wrong in \"@oneblink/uploads\".',\n )\n }\n return oneBlinkRequestHandler.oneblinkResponse\n}\n\nexport default uploadToS3\n\nexport const determineUploadProgressAsPercentage = (\n progress: Required<Pick<Progress, 'total'>> & Omit<Progress, 'total'>,\n) => {\n const percent = ((progress.loaded || 0) / progress.total) * 100\n return Math.floor(percent)\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oneblink/storage",
|
|
3
3
|
"description": "SDK for managing storage files in the OneBlink ecosystem",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.0-beta.2",
|
|
5
5
|
"author": "OneBlink <developers@oneblink.io> (https://oneblink.io)",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/oneblink/storage/issues"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@aws-sdk/client-s3": "^3.
|
|
11
|
-
"@aws-sdk/lib-storage": "^3.
|
|
10
|
+
"@aws-sdk/client-s3": "^3.569.0",
|
|
11
|
+
"@aws-sdk/lib-storage": "^3.569.0",
|
|
12
12
|
"browser-or-node": "^3.0.0"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@microsoft/eslint-plugin-sdl": "^0.2.2",
|
|
16
16
|
"@oneblink/types": "github:oneblink/types",
|
|
17
17
|
"@types/jest": "^29.5.12",
|
|
18
|
-
"@typescript-eslint/eslint-plugin": "^7.
|
|
19
|
-
"@typescript-eslint/parser": "^7.
|
|
18
|
+
"@typescript-eslint/eslint-plugin": "^7.8.0",
|
|
19
|
+
"@typescript-eslint/parser": "^7.8.0",
|
|
20
20
|
"eslint": "^8.57.0",
|
|
21
21
|
"eslint-config-prettier": "^9.1.0",
|
|
22
22
|
"eslint-plugin-prettier": "^5.1.3",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"ts-jest": "^29.1.2",
|
|
29
29
|
"tsc-watch": "^6.2.0",
|
|
30
30
|
"typedoc": "^0.25.13",
|
|
31
|
-
"typescript": "^5.4.
|
|
31
|
+
"typescript": "^5.4.5"
|
|
32
32
|
},
|
|
33
33
|
"directories": {
|
|
34
34
|
"doc": "docs"
|