@bash-app/bash-common 1.10.0 → 1.12.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 +4 -1
- package/src/extendedSchemas.ts +27 -1
- package/src/index.ts +1 -0
- package/src/utils/awsS3Utils.ts +82 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bash-app/bash-common",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.0",
|
|
4
4
|
"description": "Common data and scripts to use on the frontend and backend",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -17,6 +17,9 @@
|
|
|
17
17
|
"author": "Frank Nielson",
|
|
18
18
|
"license": "ISC",
|
|
19
19
|
"dependencies": {
|
|
20
|
+
"@aws-sdk/client-s3": "^3.590.0",
|
|
21
|
+
"@aws-sdk/client-textract": "^3.590.0",
|
|
22
|
+
"@aws-sdk/s3-request-presigner": "^3.590.0",
|
|
20
23
|
"ts-node": "^10.9.1",
|
|
21
24
|
"typescript": "^5.2.2"
|
|
22
25
|
},
|
package/src/extendedSchemas.ts
CHANGED
|
@@ -7,7 +7,8 @@ import {
|
|
|
7
7
|
TargetAudience,
|
|
8
8
|
Ticket,
|
|
9
9
|
User,
|
|
10
|
-
TicketTier, Service, Review, Media, BashComment, Recurrence, Contact
|
|
10
|
+
TicketTier, Service, Review, Media, BashComment, Recurrence, Contact,
|
|
11
|
+
BashNotification
|
|
11
12
|
} from "@prisma/client";
|
|
12
13
|
import {RecordKey} from "./definitions";
|
|
13
14
|
|
|
@@ -27,6 +28,31 @@ export interface BashEventExt extends BashEvent {
|
|
|
27
28
|
invitations: InvitationExt[];
|
|
28
29
|
}
|
|
29
30
|
|
|
31
|
+
export interface BashNotificationsExt extends BashNotification {
|
|
32
|
+
creator?: {
|
|
33
|
+
id: string;
|
|
34
|
+
name: string;
|
|
35
|
+
image?: string;
|
|
36
|
+
};
|
|
37
|
+
bashEvent?: {
|
|
38
|
+
id: string;
|
|
39
|
+
name: string;
|
|
40
|
+
coverPhoto?: string;
|
|
41
|
+
};
|
|
42
|
+
eventTask?: {
|
|
43
|
+
id: string;
|
|
44
|
+
title: string;
|
|
45
|
+
};
|
|
46
|
+
invitation?: {
|
|
47
|
+
id: string;
|
|
48
|
+
eventName: string;
|
|
49
|
+
};
|
|
50
|
+
reminders?: Array<{
|
|
51
|
+
id: string;
|
|
52
|
+
reminderText: string;
|
|
53
|
+
}>;
|
|
54
|
+
}
|
|
55
|
+
|
|
30
56
|
export const BASH_EVENT_DATA_TO_INCLUDE = {
|
|
31
57
|
targetAudience: true,
|
|
32
58
|
amountOfGuests: true,
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {DeleteObjectCommand, PutObjectCommand, S3Client} from "@aws-sdk/client-s3";
|
|
2
|
+
import {getSignedUrl} from "@aws-sdk/s3-request-presigner";
|
|
3
|
+
import {DeleteObjectCommandOutput} from "@aws-sdk/client-s3/dist-types/commands";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export class AwsS3Utils {
|
|
7
|
+
static async uploadFile(file: Blob, preSignedUrl: string | undefined, mimetype: string): Promise<boolean> {
|
|
8
|
+
try {
|
|
9
|
+
if (!file) {
|
|
10
|
+
throw new Error('The file data buffer was undefined; no data to upload.');
|
|
11
|
+
}
|
|
12
|
+
if (!preSignedUrl) {
|
|
13
|
+
throw new Error('The pre-signed url for uploading the file was undefined');
|
|
14
|
+
}
|
|
15
|
+
console.log(`Uploading with url: ${preSignedUrl}`);
|
|
16
|
+
const res = await fetch(preSignedUrl, {
|
|
17
|
+
method: 'PUT',
|
|
18
|
+
headers: {
|
|
19
|
+
'Content-Type': mimetype,
|
|
20
|
+
},
|
|
21
|
+
body: file
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
if (!res.ok) {
|
|
25
|
+
throw new Error(`Response was not ok when uploading media file.\nStatus, text: ${res.status}, ${res.statusText}`);
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
console.error(`Error when uploading file to: ${preSignedUrl}\n${e}`);
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static createUrlFromAssetKey(assetKey: string): string {
|
|
36
|
+
if (!assetKey) {
|
|
37
|
+
throw new Error(`Cannot create a url with an assetKey that is undefined`);
|
|
38
|
+
}
|
|
39
|
+
return `https://${process.env.CLOUDFRONT_ASSETS_URL}/${assetKey}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static async createPreSignedUrl(key: string | undefined, mimetype: string | undefined): Promise<string> {
|
|
43
|
+
if (!key || !mimetype) {
|
|
44
|
+
throw new Error(`Cannot create a pre-signed url without a key or a mimetype`);
|
|
45
|
+
}
|
|
46
|
+
const client = new S3Client({
|
|
47
|
+
credentials: {
|
|
48
|
+
accessKeyId: String(process.env.AWS_S3_ACCESS_KEY_ID),
|
|
49
|
+
secretAccessKey: String(process.env.AWS_S3_SECRET_ACCESS_KEY),
|
|
50
|
+
},
|
|
51
|
+
region: String(process.env.AWS_S3_REGION),
|
|
52
|
+
});
|
|
53
|
+
const command = new PutObjectCommand({
|
|
54
|
+
Bucket: String(process.env.AWS_S3_ASSETS_BUCKET_NAME),
|
|
55
|
+
Key: key,
|
|
56
|
+
ContentType: mimetype
|
|
57
|
+
});
|
|
58
|
+
return getSignedUrl(client, command, {
|
|
59
|
+
expiresIn: 3600
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static async deleteFile(key: string): Promise<DeleteObjectCommandOutput | undefined> {
|
|
64
|
+
try {
|
|
65
|
+
const command = new DeleteObjectCommand({
|
|
66
|
+
Bucket: String(process.env.AWS_S3_BUCKET_NAME),
|
|
67
|
+
Key: key,
|
|
68
|
+
});
|
|
69
|
+
const client = new S3Client({
|
|
70
|
+
credentials: {
|
|
71
|
+
accessKeyId: String(process.env.AWS_S3_ACCESS_KEY_ID),
|
|
72
|
+
secretAccessKey: String(process.env.AWS_S3_SECRET_ACCESS_KEY),
|
|
73
|
+
},
|
|
74
|
+
region: String(process.env.AWS_S3_REGION),
|
|
75
|
+
});
|
|
76
|
+
return client.send(command);
|
|
77
|
+
}
|
|
78
|
+
catch (e: any) {
|
|
79
|
+
console.error(e);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|