This repository has been archived by the owner on Sep 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyext.ts
144 lines (132 loc) · 3.29 KB
/
yext.ts
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
export interface EntityProfile {
[field: string]: ProfileValue;
meta?: {
accountId: string;
uid: string;
id: string;
timestamp: string;
folderId: string;
language: string;
countryCode: string;
entityType: string;
};
name?: string;
address?: {
line1?: string;
line2?: string;
city?: string;
region?: string;
postalCode?: string;
countryCode?: string;
};
}
export type ProfileValue =
| string
| number
| boolean
| null
| undefined
| ProfileValue[]
| { [k: string]: ProfileValue };
export interface EntityWebhookData {
meta: {
eventType: string;
uuid: string;
timestamp: number;
accountId: string;
actor: string;
appSpecificAccountId: string;
};
entityId: string;
primaryProfile: EntityProfile;
languageProfiles: EntityProfile[];
changedFields: {
language: string;
fieldNames: string[];
};
}
export interface ApiResponse<T> {
meta: {
uuid: string;
errors: {
code: number;
message: string;
type: string;
}[];
};
response: T;
}
declare var YEXT_API_KEY: string;
const API_BASE = "https://qa.yext.com/v2/accounts/me/";
const VER = "20210714";
function buildUrl(path: string, params?: Record<string, string>) {
const result = new URL(path, API_BASE);
result.searchParams.append("api_key", YEXT_API_KEY);
result.searchParams.append("v", VER);
for (const k in params) {
result.searchParams.append(k, params[k]);
}
return result.toString();
}
export async function getEntity<T extends EntityProfile>(
id: string,
): Promise<T | null> {
const url = buildUrl(`entities/${id}`);
const response = await fetch(url);
if (response.status === 404) {
return null;
} else if (response.status < 200 || response.status >= 300) {
throw response;
}
const body = await response.json() as ApiResponse<T>;
return body.response;
}
export async function updateEntity<T extends EntityProfile>(
id: string,
body: EntityProfile,
): Promise<T> {
const req = new Request(buildUrl(`entities/${id}`), {
method: "PUT",
body: JSON.stringify(body),
headers: { "Content-Type": "application/json; charset=utf-8" },
});
const response = await fetch(req);
if (response.status < 200 || response.status >= 300) {
throw response;
}
const responseBody = await response.json() as ApiResponse<T>;
return responseBody.response;
}
export async function createEntity<T extends EntityProfile>(
id: string,
entityType: string,
body: EntityProfile,
): Promise<T> {
const req = new Request(buildUrl(`entities`, { entityType }), {
method: "POST",
body: JSON.stringify({
...body,
meta: { ...body.meta, id },
}),
headers: { "Content-Type": "application/json; charset=utf-8" },
});
const response = await fetch(req);
if (response.status < 200 || response.status >= 300) {
throw response;
}
const responseBody = await response.json() as ApiResponse<T>;
return responseBody.response;
}
export async function deleteEntity(id: string): Promise<boolean> {
const req = new Request(buildUrl(`entities/${id}`), {
method: "DELETE",
});
const response = await fetch(req);
if (response.status === 404) {
return false;
}
if (response.status < 200 || response.status >= 300) {
throw response;
}
return true;
}