@bash-app/bash-common 12.1.1 → 12.3.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": "@bash-app/bash-common",
3
- "version": "12.1.1",
3
+ "version": "12.3.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",
@@ -27,6 +27,7 @@ export const DONATION_CHECKOUT_RETURN_SUCCESS_URL_PAGE = '/donation-checkout-ret
27
27
  export const DONATION_CHECKOUT_RETURN_CANCEL_URL_PAGE = '/donation-checkout-return/cancel/$checkoutSessionId' as const;
28
28
  export const VERIFICATION_RETURN_URL = `/sign-up` as const;
29
29
  export const BASH_DETAIL_URL = `/bash-detail` as const;
30
+ export const LOGIN_URL = `/login` as const;
30
31
  export const TICKET_DETAILS = `/ticket-details` as const;
31
32
 
32
33
  export const SWR_KEY_AUTH_TOKEN = "auth-token" as const;
package/src/index.ts CHANGED
@@ -8,3 +8,5 @@ export * from "./utils/paymentUtils";
8
8
  export * from "./utils/awsS3Utils";
9
9
  export * from "./utils/qrCodeUtils";
10
10
  export * from "./utils/sortUtils";
11
+ export * from "./utils/apiUtils";
12
+ export * from "./utils/urlUtils";
@@ -0,0 +1,76 @@
1
+ import {BashEvent, Contact, User } from "@prisma/client";
2
+ import {ContactOrUser} from "../definitions";
3
+ import {Public_User} from "../extendedSchemas";
4
+
5
+ type CombinedUser = User | Public_User;
6
+ type CombinedUserOrContact = CombinedUser | Contact;
7
+
8
+ export function isContactAndNotUser(contactOrUser: Partial<ContactOrUser>): contactOrUser is Contact {
9
+ return !!(contactOrUser as Contact).contactOwnerId;
10
+ }
11
+
12
+ export function getUserName(contactOrUser: Partial<CombinedUserOrContact> | undefined): string {
13
+ if (!contactOrUser) {
14
+ return '';
15
+ }
16
+ if (isContactAndNotUser(contactOrUser)) {
17
+ return contactOrUser.fullName ?? (contactOrUser as Contact).contactEmail ?? '';
18
+ }
19
+ const user = contactOrUser as CombinedUser;
20
+ const combinedName = user.givenName && user.familyName ? `${user.givenName} ${user.familyName}` : null;
21
+ return combinedName ?? user.email;
22
+ }
23
+
24
+ export function getUserImage(contactOrUser: CombinedUserOrContact | undefined): string {
25
+ if (!contactOrUser) {
26
+ return '';
27
+ }
28
+ const noProfilePicture = '/noProfilePicture.svg';
29
+ if (isContactAndNotUser(contactOrUser)) {
30
+ return noProfilePicture;
31
+ }
32
+ const user = contactOrUser as CombinedUser;
33
+ return user.uploadedImage ?? user.image ?? noProfilePicture;
34
+ }
35
+
36
+ export function getContactOrUserEmail(contactOrUser: Partial<ContactOrUser>): string {
37
+ return isContactAndNotUser(contactOrUser)
38
+ ? (contactOrUser as Contact).contactEmail ?? ''
39
+ : (contactOrUser as User).email;
40
+ }
41
+
42
+ export function getUserIdAndNotContactId(contactOrUser: Partial<ContactOrUser>): string | null {
43
+ return isContactAndNotUser(contactOrUser)
44
+ ? null
45
+ : (contactOrUser as User).id;
46
+ }
47
+
48
+ export function getBashEventDetailUrlParamsFromBashEvent(bashEvent: BashEvent | undefined, coverPhoto?: string): string {
49
+ if (!bashEvent) {
50
+ return '';
51
+ }
52
+ return getBashEventDetailUrlParams(bashEvent.title, bashEvent.description, coverPhoto ?? bashEvent.coverPhoto);
53
+ }
54
+
55
+ function getBashEventDetailUrlParams(bashEventTitle: string | undefined,
56
+ bashEventDescription: string | undefined | null,
57
+ coverPhoto?: string | null): string {
58
+ // Don't let the description be huge
59
+ const desc = bashEventDescription && bashEventDescription.length > 10 ? bashEventDescription?.slice(0, 10) + '...' : bashEventDescription;
60
+ // Need to add characters if any of the args is undefined or else the params get thrown off
61
+ const params: string[] = [];
62
+ if (bashEventTitle) {
63
+ // params.push(`${URL_PARAMS_BASH_EVENT_TITLE}=${encodeURIComponent(bashEventTitle)}`);
64
+ }
65
+ if (desc) {
66
+ // params.push(`${URL_PARAMS_BASH_EVENT_DESC}=${encodeURIComponent(desc)}`);
67
+ }
68
+ if (coverPhoto) {
69
+ // params.push(`${URL_PARAMS_BASH_EVENT_COVER_PHOTO}=${encodeURIComponent(coverPhoto)}`);
70
+ }
71
+ return params.join('&');
72
+ }
73
+
74
+ export function isProduction(): boolean {
75
+ return process.env.NODE_ENV === 'production';
76
+ }
@@ -0,0 +1,29 @@
1
+ import {isProduction} from "./apiUtils";
2
+ import {BASH_DETAIL_URL} from "../definitions";
3
+
4
+
5
+ const API_HOST = process.env.REACT_APP_API ?? "http://localhost:3500";
6
+
7
+
8
+ export function getFrontendHost(): string {
9
+ const host = isProduction()
10
+ ? `${window.location.protocol}//${window.location.host}`
11
+ : `${window.location.protocol}//${window.location.hostname}:3000`;
12
+ return host;
13
+ }
14
+
15
+ export function getBackendHost(): string {
16
+ const host = isProduction()
17
+ ? API_HOST
18
+ : `${window.location.protocol}//${window.location.hostname}:3500`;
19
+ return host;
20
+ }
21
+
22
+ export function getSsrBashDetailUrl(bashEventId: string | undefined): string {
23
+ if (bashEventId) {
24
+ const url = `/api/ssr${BASH_DETAIL_URL}/${bashEventId}`;
25
+ return url;
26
+ }
27
+ console.error(`BashEventId was not specified for the ssr bash detail url`);
28
+ return '';
29
+ }