@koloseum/utils 0.1.12 → 0.1.13
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/utils.d.ts +17 -2
- package/dist/utils.js +85 -9
- package/package.json +1 -1
package/dist/utils.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import type { Database } from "@koloseum/types/database";
|
|
1
2
|
import type { CustomError, Microservice, MicroserviceGroup, MicroserviceObject, UserWithAppMetadata } from "@koloseum/types/general";
|
|
2
3
|
import type { PronounsCheckboxes, SocialMediaPlatform } from "@koloseum/types/public-auth";
|
|
3
|
-
import type { SubscriberResponseDto } from "@novu/api/models/components/subscriberresponsedto.js";
|
|
4
|
-
import type { PostgrestError } from "@supabase/supabase-js";
|
|
5
4
|
import type { Page } from "@sveltejs/kit";
|
|
5
|
+
import type { SupabaseClient, PostgrestError } from "@supabase/supabase-js";
|
|
6
|
+
import type { SubscriberResponseDto } from "@novu/api/models/components/subscriberresponsedto.js";
|
|
6
7
|
import type { MobilePhoneLocale } from "validator";
|
|
7
8
|
import { Novu } from "@novu/api";
|
|
8
9
|
export declare const Status: {
|
|
@@ -20,6 +21,20 @@ export declare const Status: {
|
|
|
20
21
|
PASSWORD_RESET_REQUESTED: string;
|
|
21
22
|
};
|
|
22
23
|
export declare const Utility: {
|
|
24
|
+
/**
|
|
25
|
+
* Calls a Supabase Edge function.
|
|
26
|
+
* @param {boolean} browser - Whether the function is being called in the browser
|
|
27
|
+
* @param {SupabaseClient} supabase - The Supabase client
|
|
28
|
+
* @param {string} path - The path to the Edge function
|
|
29
|
+
* @param {"GET" | "POST"} [method="GET"] - The HTTP method to use
|
|
30
|
+
* @returns The response from the Edge function
|
|
31
|
+
*/
|
|
32
|
+
callEdgeFunction: (browser: boolean, supabase: SupabaseClient<Database>, path: string, method?: "GET" | "POST") => Promise<{
|
|
33
|
+
code: number;
|
|
34
|
+
data?: any;
|
|
35
|
+
error?: string;
|
|
36
|
+
context?: Record<string, any>;
|
|
37
|
+
}>;
|
|
23
38
|
/**
|
|
24
39
|
* Capitalises a given word.
|
|
25
40
|
* @param {string} word - The word to be capitalised
|
package/dist/utils.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { error as svelteError } from "@sveltejs/kit";
|
|
2
|
+
import { FunctionsFetchError, FunctionsHttpError, FunctionsRelayError } from "@supabase/supabase-js";
|
|
2
3
|
import { ErrorDto } from "@novu/api/models/errors/errordto.js";
|
|
3
4
|
import sanitize from "sanitize-html";
|
|
4
5
|
import validator from "validator";
|
|
@@ -21,6 +22,76 @@ export const Status = {
|
|
|
21
22
|
};
|
|
22
23
|
/* Utility functions */
|
|
23
24
|
export const Utility = {
|
|
25
|
+
/**
|
|
26
|
+
* Calls a Supabase Edge function.
|
|
27
|
+
* @param {boolean} browser - Whether the function is being called in the browser
|
|
28
|
+
* @param {SupabaseClient} supabase - The Supabase client
|
|
29
|
+
* @param {string} path - The path to the Edge function
|
|
30
|
+
* @param {"GET" | "POST"} [method="GET"] - The HTTP method to use
|
|
31
|
+
* @returns The response from the Edge function
|
|
32
|
+
*/
|
|
33
|
+
callEdgeFunction: async (browser, supabase, path, method = "GET") => {
|
|
34
|
+
try {
|
|
35
|
+
// Set up headers with safe origin handling for SSR
|
|
36
|
+
const headers = {
|
|
37
|
+
"Content-Type": "application/json",
|
|
38
|
+
Accept: "application/json"
|
|
39
|
+
};
|
|
40
|
+
// Only add Origin header in browser environment
|
|
41
|
+
if (browser)
|
|
42
|
+
headers["Origin"] = window.location.origin;
|
|
43
|
+
// Fetch response with additional options for better cross-origin handling
|
|
44
|
+
const { data, error } = await supabase.functions.invoke(path, {
|
|
45
|
+
method,
|
|
46
|
+
headers
|
|
47
|
+
});
|
|
48
|
+
// Handle different error types
|
|
49
|
+
if (error) {
|
|
50
|
+
// Define context
|
|
51
|
+
const context = {};
|
|
52
|
+
// Define error code and message
|
|
53
|
+
const code = Number(error.context.status) || 500;
|
|
54
|
+
let message = Status.ERROR;
|
|
55
|
+
// Handle HTTP errors
|
|
56
|
+
if (error instanceof FunctionsHttpError) {
|
|
57
|
+
// Get error data
|
|
58
|
+
try {
|
|
59
|
+
// Get content type header
|
|
60
|
+
const contentType = error.context.headers.get("content-type");
|
|
61
|
+
// If content type is JSON, get error data
|
|
62
|
+
if (contentType.includes("application/json")) {
|
|
63
|
+
// Get error data
|
|
64
|
+
const data = await error.context.json();
|
|
65
|
+
// Assign error message
|
|
66
|
+
message = data.message;
|
|
67
|
+
}
|
|
68
|
+
// If content type is plain text, get error message
|
|
69
|
+
else if (contentType.includes("text/plain"))
|
|
70
|
+
message = await error.context.text();
|
|
71
|
+
// If any other content type, use the status text
|
|
72
|
+
else
|
|
73
|
+
message = error.context.statusText;
|
|
74
|
+
// Assign attempt ID to context if present
|
|
75
|
+
if (path.includes("verify-id") && data.attemptId)
|
|
76
|
+
context.attemptId = data.attemptId;
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
console.error(error);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// Handle relay and fetch errors
|
|
83
|
+
else if (error instanceof FunctionsRelayError || error instanceof FunctionsFetchError)
|
|
84
|
+
message = error.message;
|
|
85
|
+
// Return error
|
|
86
|
+
return { code: code, error: message, context };
|
|
87
|
+
}
|
|
88
|
+
// Return response
|
|
89
|
+
return { code: 200, data };
|
|
90
|
+
}
|
|
91
|
+
catch (_err) {
|
|
92
|
+
return { code: 500, error: Status.ERROR };
|
|
93
|
+
}
|
|
94
|
+
},
|
|
24
95
|
/**
|
|
25
96
|
* Capitalises a given word.
|
|
26
97
|
* @param {string} word - The word to be capitalised
|
|
@@ -134,15 +205,20 @@ export const Utility = {
|
|
|
134
205
|
// Get person data from user metadata
|
|
135
206
|
const { first_name: firstName, last_name: lastName, pseudonym } = user.app_metadata.person_data;
|
|
136
207
|
// Create subscriber
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
208
|
+
try {
|
|
209
|
+
({ result: subscriber } = await novu.subscribers.create({
|
|
210
|
+
subscriberId: user.id,
|
|
211
|
+
firstName,
|
|
212
|
+
lastName,
|
|
213
|
+
phone: user.phone,
|
|
214
|
+
timezone: "Africa/Nairobi",
|
|
215
|
+
locale: "en_KE",
|
|
216
|
+
data: { pseudonym }
|
|
217
|
+
}));
|
|
218
|
+
}
|
|
219
|
+
catch (error) {
|
|
220
|
+
console.error(error);
|
|
221
|
+
}
|
|
146
222
|
}
|
|
147
223
|
// Otherwise, log error
|
|
148
224
|
else
|