/
integration.ts
59 lines (53 loc) · 1.68 KB
/
integration.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
import { IntentTagVector, IntentVector } from "@uniformdev/optimize-common";
import fetch from "node-fetch";
export async function fetchIntents(apiKey: string, projectId: string) {
if (!apiKey || !projectId) {
console.error(
"You need to pass an api key and a project id for Uniform. Make sure that your enviroment variables are set correctly."
);
return Promise.resolve({
statusCode: 400,
body: "Missing Project ID or API Key",
});
}
try {
const res = await fetch(
`https://uniform.app/api/v1/manifest?projectId=${projectId}`,
{ method: "get", headers: { "x-api-key": apiKey } }
);
const data = (await res.json()) as { site: { intents: IntentVector[] } };
const results = mapIntents(data.site.intents);
return {
statusCode: 200,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
results_size: results.length,
results,
}),
};
} catch (error) {
console.error("Error fetching Uniform intents", error);
return Promise.resolve({ statusCode: 500, body: String(error) });
}
}
/**
* This function maps over the intents coming from the Uniform API
* and turns them into a format Prismic will be happy with.
* @param intents
*/
function mapIntents(intents: IntentTagVector[]) {
return intents.map((intent) => {
const id = String(intent.id);
return {
id,
title: `${id}`,
description: `This content is visible for users with the intent ${intent.id}`,
image_url:
"https://pbs.twimg.com/profile_images/1235674864649830400/kd3pN6iU_400x400.jpg",
last_update: Date.now(),
blob: { [id]: intent },
};
});
}