@indietabletop/appkit 3.2.0-8 → 3.2.0-9
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/lib/append-copy-to-text.test.ts +29 -0
- package/lib/failureMessages.test.ts +138 -0
- package/lib/failureMessages.ts +76 -0
- package/lib/index.ts +1 -1
- package/package.json +1 -1
- package/lib/knownFailure.ts +0 -9
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
appendCopyToText,
|
|
4
|
+
maybeAppendCopyToText,
|
|
5
|
+
} from "./append-copy-to-text.ts";
|
|
6
|
+
|
|
7
|
+
describe("appendCopyToText", () => {
|
|
8
|
+
test("Appends ' (Copy)' to provided string", () => {
|
|
9
|
+
const returnValue = appendCopyToText("Zangrad Raiders");
|
|
10
|
+
expect(returnValue).toBe("Zangrad Raiders (Copy)");
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("Adds a copy count number if string already ends in ' (Copy)'", () => {
|
|
14
|
+
const returnValue = appendCopyToText("Zangrad Raiders (Copy)");
|
|
15
|
+
expect(returnValue).toBe("Zangrad Raiders (Copy 2)");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("Increments a copy count number if one already exists", () => {
|
|
19
|
+
const returnValue = appendCopyToText("Zangrad Raiders (Copy 2)");
|
|
20
|
+
expect(returnValue).toBe("Zangrad Raiders (Copy 3)");
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe("maybeAppendCopyToText", () => {
|
|
25
|
+
test("Ignores empty strings", () => {
|
|
26
|
+
const returnValue = maybeAppendCopyToText("");
|
|
27
|
+
expect(returnValue).toBe("");
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
getFetchFailureMessages,
|
|
4
|
+
getSubmitFailureMessage,
|
|
5
|
+
} from "./failureMessages.ts";
|
|
6
|
+
|
|
7
|
+
describe("getFetchFailureMessages", () => {
|
|
8
|
+
test("Returns correct message for API_ERROR with code 404", () => {
|
|
9
|
+
const result = getFetchFailureMessages({ type: "API_ERROR", code: 404 });
|
|
10
|
+
|
|
11
|
+
expect(result).toMatchInlineSnapshot(`
|
|
12
|
+
{
|
|
13
|
+
"description": "The link you have followed might be broken.",
|
|
14
|
+
"title": "Not found",
|
|
15
|
+
}
|
|
16
|
+
`);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("Returns correct message for API_ERROR with code 500", () => {
|
|
20
|
+
const result = getFetchFailureMessages({ type: "API_ERROR", code: 500 });
|
|
21
|
+
|
|
22
|
+
expect(result).toMatchInlineSnapshot(`
|
|
23
|
+
{
|
|
24
|
+
"description": "This is probably an issue with our servers. You can try refreshing.",
|
|
25
|
+
"title": "Ooops, something went wrong",
|
|
26
|
+
}
|
|
27
|
+
`);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("Returns correct message for API_ERROR with partial override", () => {
|
|
31
|
+
const result = getFetchFailureMessages(
|
|
32
|
+
{ type: "API_ERROR", code: 404 },
|
|
33
|
+
{ 404: { title: `Army not found` } },
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
expect(result).toMatchInlineSnapshot(`
|
|
37
|
+
{
|
|
38
|
+
"description": "The link you have followed might be broken.",
|
|
39
|
+
"title": "Army not found",
|
|
40
|
+
}
|
|
41
|
+
`);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("Returns correct message for API_ERROR with override", () => {
|
|
45
|
+
const result = getFetchFailureMessages(
|
|
46
|
+
{ type: "API_ERROR", code: 404 },
|
|
47
|
+
{
|
|
48
|
+
404: {
|
|
49
|
+
title: `Army not found`,
|
|
50
|
+
description: `It might have been deleted.`,
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
expect(result).toMatchInlineSnapshot(`
|
|
56
|
+
{
|
|
57
|
+
"description": "It might have been deleted.",
|
|
58
|
+
"title": "Army not found",
|
|
59
|
+
}
|
|
60
|
+
`);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("Returns correct message for NETWORK_ERROR", () => {
|
|
64
|
+
const result = getFetchFailureMessages({ type: "NETWORK_ERROR" });
|
|
65
|
+
|
|
66
|
+
expect(result).toMatchInlineSnapshot(`
|
|
67
|
+
{
|
|
68
|
+
"description": "Check your interent connection and try again.",
|
|
69
|
+
"title": "No connection",
|
|
70
|
+
}
|
|
71
|
+
`);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("Returns correct message for UNKNOWN_ERROR", () => {
|
|
75
|
+
const result = getFetchFailureMessages({ type: "UNKNOWN_ERROR" });
|
|
76
|
+
|
|
77
|
+
expect(result).toMatchInlineSnapshot(`
|
|
78
|
+
{
|
|
79
|
+
"description": "This is probably an issue on our side. You can try refreshing.",
|
|
80
|
+
"title": "Ooops, something went wrong",
|
|
81
|
+
}
|
|
82
|
+
`);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("Returns correct message for an unrecognised error type", () => {
|
|
86
|
+
const result = getFetchFailureMessages({ type: "FOO" as any });
|
|
87
|
+
|
|
88
|
+
expect(result).toMatchInlineSnapshot(`
|
|
89
|
+
{
|
|
90
|
+
"description": "This is probably an issue on our side. You can try refreshing.",
|
|
91
|
+
"title": "Ooops, something went wrong",
|
|
92
|
+
}
|
|
93
|
+
`);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe("getSubmitFailureMessage", () => {
|
|
98
|
+
test("Returns correct message for API_ERROR with code 500", () => {
|
|
99
|
+
const message = getSubmitFailureMessage({ type: "API_ERROR", code: 500 });
|
|
100
|
+
expect(message).toMatchInlineSnapshot(
|
|
101
|
+
`"Could not submit form due to an unexpected server error. Please refresh the page and try again."`,
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("Returns correct message for API_ERROR with override", () => {
|
|
106
|
+
const message = getSubmitFailureMessage(
|
|
107
|
+
{ type: "API_ERROR", code: 401 },
|
|
108
|
+
{ 401: `Username and password do not match. Please try again.` },
|
|
109
|
+
);
|
|
110
|
+
expect(message).toMatchInlineSnapshot(
|
|
111
|
+
`"Username and password do not match. Please try again."`,
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("Returns correct message for NETWORK_ERROR", () => {
|
|
116
|
+
const result = getSubmitFailureMessage({ type: "NETWORK_ERROR" });
|
|
117
|
+
|
|
118
|
+
expect(result).toMatchInlineSnapshot(
|
|
119
|
+
`"Could not submit form due to network error. Make sure you are connected to the internet and try again."`,
|
|
120
|
+
);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("Returns correct message for UNKNOWN_ERROR", () => {
|
|
124
|
+
const result = getSubmitFailureMessage({ type: "UNKNOWN_ERROR" });
|
|
125
|
+
|
|
126
|
+
expect(result).toMatchInlineSnapshot(
|
|
127
|
+
`"Could not submit form due to an unexpected error. Please refresh the page and try again."`,
|
|
128
|
+
);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("Returns correct message for an unrecognised error type", () => {
|
|
132
|
+
const result = getSubmitFailureMessage({ type: "FOO" as any });
|
|
133
|
+
|
|
134
|
+
expect(result).toMatchInlineSnapshot(
|
|
135
|
+
`"Could not submit form due to an unexpected error. Please refresh the page and try again."`,
|
|
136
|
+
);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { FailurePayload } from "./types.ts";
|
|
2
|
+
|
|
3
|
+
type OnOverride<T> = (fallback: T, override?: Partial<T>) => T;
|
|
4
|
+
|
|
5
|
+
function createFailureMessageGetter<T>(
|
|
6
|
+
defaults: Record<number | "fallback" | "connection", T>,
|
|
7
|
+
options: { onOverride: OnOverride<T> },
|
|
8
|
+
) {
|
|
9
|
+
return function getMessage(
|
|
10
|
+
failure: FailurePayload,
|
|
11
|
+
overrides: Record<number, Partial<T>> = {},
|
|
12
|
+
) {
|
|
13
|
+
switch (failure.type) {
|
|
14
|
+
case "API_ERROR": {
|
|
15
|
+
return options.onOverride(
|
|
16
|
+
defaults[failure.code] ?? defaults.fallback,
|
|
17
|
+
overrides[failure.code],
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
case "NETWORK_ERROR": {
|
|
22
|
+
return defaults.connection;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
default: {
|
|
26
|
+
return defaults.fallback;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const getFetchFailureMessages = createFailureMessageGetter(
|
|
33
|
+
{
|
|
34
|
+
404: {
|
|
35
|
+
title: `Not found`,
|
|
36
|
+
description: `The link you have followed might be broken.`,
|
|
37
|
+
},
|
|
38
|
+
500: {
|
|
39
|
+
title: `Ooops, something went wrong`,
|
|
40
|
+
description: `This is probably an issue with our servers. You can try refreshing.`,
|
|
41
|
+
},
|
|
42
|
+
connection: {
|
|
43
|
+
title: `No connection`,
|
|
44
|
+
description: `Check your interent connection and try again.`,
|
|
45
|
+
},
|
|
46
|
+
fallback: {
|
|
47
|
+
title: `Ooops, something went wrong`,
|
|
48
|
+
description: `This is probably an issue on our side. You can try refreshing.`,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
onOverride(fallback, override) {
|
|
53
|
+
return { ...fallback, ...override };
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
export const getSubmitFailureMessage = createFailureMessageGetter(
|
|
59
|
+
{
|
|
60
|
+
500: `Could not submit form due to an unexpected server error. Please refresh the page and try again.`,
|
|
61
|
+
connection: `Could not submit form due to network error. Make sure you are connected to the internet and try again.`,
|
|
62
|
+
fallback: `Could not submit form due to an unexpected error. Please refresh the page and try again.`,
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
onOverride(fallback, override) {
|
|
66
|
+
return override ?? fallback;
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @deprecated Use {@link getSubmitFailureMessage} instead.
|
|
73
|
+
*/
|
|
74
|
+
export function toKnownFailureMessage(failure: FailurePayload) {
|
|
75
|
+
return getSubmitFailureMessage(failure);
|
|
76
|
+
}
|
package/lib/index.ts
CHANGED
|
@@ -23,7 +23,7 @@ export * from "./async-op.ts";
|
|
|
23
23
|
export * from "./caught-value.ts";
|
|
24
24
|
export * from "./class-names.ts";
|
|
25
25
|
export * from "./client.ts";
|
|
26
|
-
export * from "./
|
|
26
|
+
export * from "./failureMessages.ts";
|
|
27
27
|
export * from "./media.ts";
|
|
28
28
|
export * from "./structs.ts";
|
|
29
29
|
export * from "./types.ts";
|
package/package.json
CHANGED
package/lib/knownFailure.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { FailurePayload } from "./types.ts";
|
|
2
|
-
|
|
3
|
-
export function toKnownFailureMessage(failure: FailurePayload) {
|
|
4
|
-
if (failure.type === "NETWORK_ERROR") {
|
|
5
|
-
return "Could not submit form due to network error. Make sure you are connected to the internet and try again.";
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
return "Could not submit form due to an unexpected error. Please refresh the page and try again.";
|
|
9
|
-
}
|