-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHtmlEditorApiClientImpl.ts
More file actions
98 lines (86 loc) · 2.69 KB
/
HtmlEditorApiClientImpl.ts
File metadata and controls
98 lines (86 loc) · 2.69 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
import { Result } from "../abstractions/result-types";
import {
AppConfiguration,
AppSessionStateAccessor,
} from "../abstractions/application";
import { HtmlEditorApiClient } from "../abstractions/html-editor-api-client";
import { AxiosStatic, Method } from "axios";
import { Content } from "../abstractions/domain/content";
export class HtmlEditorApiClientImpl implements HtmlEditorApiClient {
private axios;
private appSessionStateAccessor;
constructor({
axiosStatic,
appSessionStateAccessor,
appConfiguration: { htmlEditorApiBaseUrl },
}: {
axiosStatic: AxiosStatic;
appSessionStateAccessor: AppSessionStateAccessor;
appConfiguration: Partial<AppConfiguration>;
}) {
this.axios = axiosStatic.create({
baseURL: htmlEditorApiBaseUrl,
});
this.appSessionStateAccessor = appSessionStateAccessor;
}
private getConnectionData() {
const connectionData = this.appSessionStateAccessor.getSessionAuthData();
if (connectionData.status !== "authenticated") {
throw new Error("Authenticated session required");
}
return {
accountName: connectionData.dopplerAccountName,
jwtToken: connectionData.jwtToken,
};
}
private request<T>(method: Method, url: string, data: unknown = undefined) {
const { accountName, jwtToken } = this.getConnectionData();
return this.axios.request<T>({
method,
url: `/accounts/${accountName}${url}`,
headers: { Authorization: `Bearer ${jwtToken}` },
data,
});
}
private GET<T>(url: string) {
return this.request<T>("GET", url);
}
private PUT(url: string, data: unknown) {
return this.request<any>("PUT", url, data);
}
async getCampaignContent(campaignId: string): Promise<Result<Content>> {
const response = await this.GET<any>(`/campaigns/${campaignId}/content`);
if (response.data.type === "html") {
return {
success: true,
value: {
htmlContent: response.data.htmlContent,
previewImage: response.data.previewImage || "",
type: "html",
},
};
}
// TODO: validate the type for unlayer design responses
return {
success: true,
value: {
// TODO: consider to sanitize and validate this response
htmlContent: response.data.htmlContent,
previewImage: response.data.previewImage || "",
type: "html",
},
};
}
async updateCampaignContent(
campaignId: string,
content: Content,
): Promise<Result> {
const body = {
htmlContent: content.htmlContent,
previewImage: content.previewImage,
type: "html",
};
await this.PUT(`/campaigns/${campaignId}/content`, body);
return { success: true };
}
}