rankrunners-cms 0.0.35 → 0.0.37
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
|
@@ -6,7 +6,10 @@ import {
|
|
|
6
6
|
} from "valibot";
|
|
7
7
|
import { CMS_BASE_URL, SITE_ID } from "../constants";
|
|
8
8
|
import type { FormFillSubmissionDTO } from "../types/form-fills";
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
type SendFormFillResult,
|
|
11
|
+
SendFormFillResultType,
|
|
12
|
+
} from "../types/form-fills";
|
|
10
13
|
|
|
11
14
|
export type SendFormFillArgs<
|
|
12
15
|
A,
|
|
@@ -33,14 +36,21 @@ export const sendFormFill = async <
|
|
|
33
36
|
const formToSend = safeParse(args.schema, args.data);
|
|
34
37
|
|
|
35
38
|
if (!formToSend.success) {
|
|
39
|
+
const errors: Record<string, string> = {};
|
|
40
|
+
formToSend.issues.forEach((issue) => {
|
|
41
|
+
if (issue.path && issue.path[0] && issue.path[0].key) {
|
|
42
|
+
errors[issue.path[0].key as string] = issue.message;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
36
46
|
console.error("Form fill validation failed:", formToSend.issues);
|
|
37
|
-
return
|
|
47
|
+
return { response: SendFormFillResultType.ValidationError, errors };
|
|
38
48
|
}
|
|
39
49
|
|
|
40
50
|
const data: FormFillSubmissionDTO = {
|
|
41
51
|
siteId: SITE_ID,
|
|
42
52
|
type: args.type,
|
|
43
|
-
data:
|
|
53
|
+
data: formToSend.output as Record<string, string>,
|
|
44
54
|
schema: {
|
|
45
55
|
schema: JSON.stringify(args.schema),
|
|
46
56
|
fields: args.fieldNames,
|
|
@@ -59,21 +69,26 @@ export const sendFormFill = async <
|
|
|
59
69
|
});
|
|
60
70
|
|
|
61
71
|
if (response.ok) {
|
|
62
|
-
return
|
|
72
|
+
return { response: SendFormFillResultType.Success };
|
|
63
73
|
}
|
|
64
74
|
|
|
65
75
|
console.error("Form fill submission failed with status:", response.status);
|
|
66
|
-
|
|
76
|
+
console.error("Form fill submission:", data);
|
|
77
|
+
return { response: SendFormFillResultType.UnknownError };
|
|
67
78
|
} catch (error) {
|
|
68
79
|
console.error("Error sending form fill:", error);
|
|
69
80
|
if (error instanceof Error) {
|
|
70
81
|
if (error.message.includes("validation")) {
|
|
71
|
-
return
|
|
82
|
+
return { response: SendFormFillResultType.ValidationError };
|
|
72
83
|
}
|
|
73
84
|
if (error.message.includes("captcha")) {
|
|
74
|
-
return
|
|
85
|
+
return { response: SendFormFillResultType.CaptchaError };
|
|
75
86
|
}
|
|
76
87
|
}
|
|
77
|
-
|
|
88
|
+
if (error instanceof TypeError) {
|
|
89
|
+
// fetch throws TypeError on network failure
|
|
90
|
+
return { response: SendFormFillResultType.NetworkError };
|
|
91
|
+
}
|
|
92
|
+
return { response: SendFormFillResultType.UnknownError };
|
|
78
93
|
}
|
|
79
94
|
};
|
|
@@ -46,25 +46,35 @@ export const FormFillSubmissionSchema = object({
|
|
|
46
46
|
|
|
47
47
|
export type FormFillSubmissionDTO = InferInput<typeof FormFillSubmissionSchema>;
|
|
48
48
|
|
|
49
|
-
export
|
|
49
|
+
export type SendFormFillResult = {
|
|
50
|
+
response: SendFormFillResultType;
|
|
51
|
+
errors?: Record<string, string>;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export enum SendFormFillResultType {
|
|
50
55
|
Success = "success",
|
|
51
56
|
ValidationError = "validation_error",
|
|
52
57
|
CaptchaError = "captcha_error",
|
|
58
|
+
NetworkError = "network_error",
|
|
53
59
|
InvalidResponse = "invalid_response",
|
|
54
60
|
UnknownError = "unknown_error",
|
|
55
61
|
}
|
|
56
62
|
|
|
57
|
-
export const formFillResultToMessage = (
|
|
63
|
+
export const formFillResultToMessage = (
|
|
64
|
+
result: SendFormFillResultType,
|
|
65
|
+
): string => {
|
|
58
66
|
switch (result) {
|
|
59
|
-
case
|
|
67
|
+
case SendFormFillResultType.Success:
|
|
60
68
|
return "Form submitted successfully!";
|
|
61
|
-
case
|
|
69
|
+
case SendFormFillResultType.ValidationError:
|
|
62
70
|
return "There was a validation error with your submission. Please check your input and try again.";
|
|
63
|
-
case
|
|
71
|
+
case SendFormFillResultType.CaptchaError:
|
|
64
72
|
return "Captcha verification failed. Please complete the captcha and try again.";
|
|
65
|
-
case
|
|
73
|
+
case SendFormFillResultType.NetworkError:
|
|
74
|
+
return "A network error occurred while submitting the form. Please check your internet connection and try again.";
|
|
75
|
+
case SendFormFillResultType.InvalidResponse:
|
|
66
76
|
return "An internal error occurred while submitting the form. Please call us instead!";
|
|
67
|
-
case
|
|
77
|
+
case SendFormFillResultType.UnknownError:
|
|
68
78
|
default:
|
|
69
79
|
return "An unknown error occurred while submitting the form. Please try again later, or try calling us!";
|
|
70
80
|
}
|