api.ts
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import type {
PrelearningApiError,
PrelearningActionPayload,
PrelearningActionRequest,
PrelearningActionResponse,
PrelearningBuriedPointPayload,
PrelearningBuriedPointRequest,
PrelearningBuriedPointResponse,
PrelearningInitPayload,
PrelearningInitRequest,
PrelearningInitResponse,
PrelearningActionType,
} from '@single-pages/contracts';
import { buildBackendApiPath } from '@single-pages/config';
const backendApiPath = (pathname: string) => buildBackendApiPath(pathname, {
hostname: globalThis.location?.hostname,
envBasePath: (import.meta as ImportMeta & { env: { VITE_BACKEND_API_BASE_PATH?: string } }).env
.VITE_BACKEND_API_BASE_PATH,
});
export class PrelearningRequestError extends Error {
readonly code: string;
readonly details?: unknown;
constructor(code: string, message: string, details?: unknown) {
super(message);
this.name = 'PrelearningRequestError';
this.code = code;
this.details = details;
}
}
export function findMissingPrelearningFields(payload: Partial<PrelearningInitPayload>): string[] {
return ['entrySource', 'activityId', 'activityProductId', 'staffUuid', 'merchantId'].filter((field) => {
const value = payload[field as keyof PrelearningInitPayload];
if (typeof value !== 'string') {
return true;
}
return value.trim().length === 0;
});
}
function isPrelearningError(value: unknown): value is PrelearningApiError {
return Boolean(value)
&& typeof value === 'object'
&& value !== null
&& 'error' in value
&& Boolean((value as PrelearningApiError).error)
&& typeof (value as PrelearningApiError).error.code === 'string';
}
export async function fetchPrelearningSession(payload: PrelearningInitPayload): Promise<PrelearningInitResponse> {
const response = await fetch(backendApiPath('/api/prelearning/init'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ payload } satisfies PrelearningInitRequest),
});
const json = (await response.json().catch(() => null)) as unknown;
if (!response.ok) {
if (isPrelearningError(json)) {
throw new PrelearningRequestError(json.error.code, json.error.message, json.error.details);
}
throw new PrelearningRequestError('PRELEARNING_REQUEST_FAILED', '初始化失败,请稍后重试');
}
return json as PrelearningInitResponse;
}
export async function fetchPrelearningAction(
actionType: PrelearningActionType,
payload: PrelearningActionPayload,
): Promise<PrelearningActionResponse> {
const response = await fetch(backendApiPath('/api/prelearning/action'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ actionType, payload } satisfies PrelearningActionRequest),
});
const json = (await response.json().catch(() => null)) as unknown;
if (!response.ok) {
if (isPrelearningError(json)) {
throw new PrelearningRequestError(json.error.code, json.error.message, json.error.details);
}
throw new PrelearningRequestError('PRELEARNING_REQUEST_FAILED', '学习动作失败,请稍后重试');
}
return json as PrelearningActionResponse;
}
export async function fetchPrelearningBuriedPoint(
payload: PrelearningBuriedPointPayload,
): Promise<PrelearningBuriedPointResponse> {
const response = await fetch(backendApiPath('/api/prelearning/buried-point'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ payload } satisfies PrelearningBuriedPointRequest),
});
const json = (await response.json().catch(() => null)) as unknown;
if (!response.ok) {
if (isPrelearningError(json)) {
throw new PrelearningRequestError(json.error.code, json.error.message, json.error.details);
}
throw new PrelearningRequestError('PRELEARNING_REQUEST_FAILED', '埋点上报失败,请稍后重试');
}
return json as PrelearningBuriedPointResponse;
}