-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenvironment.ts
37 lines (28 loc) · 930 Bytes
/
environment.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
import { getPurgeAPIToken } from "./request.ts";
import { EdgeRequest, getSite } from "./request.ts";
let hasPopulatedEnvironment = false;
export const getEnvironment = () =>
Deno.env.get("DENO_DEPLOYMENT_ID") ? "production" : "local";
export const populateEnvironment = (req: EdgeRequest) => {
// There is some performance cost in setting environment variables on every
// request. We know these values will be the same for the lifecycle of the
// isolate, so we can set them once.
if (hasPopulatedEnvironment) {
return;
}
const site = getSite(req);
if (site.id) {
Deno.env.set("SITE_ID", site.id);
}
if (site.name) {
Deno.env.set("SITE_NAME", site.name);
}
if (site.url) {
Deno.env.set("URL", site.url);
}
const purgeAPIToken = getPurgeAPIToken(req);
if (purgeAPIToken) {
Deno.env.set("NETLIFY_PURGE_API_TOKEN", purgeAPIToken);
}
hasPopulatedEnvironment = true;
};