Readme

Set an env var

In val.town, Deno.env.set does nothing. This val fixes it.

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
import process from "node:process";
/**
* Given an object of environment variables, create a stub
* that simulates the same interface as Deno.env
*/
export function createDenoEnvStub(
input: Record<string, string>,
): typeof Deno.env {
return {
get(key: string) {
return input[key];
},
has(key: string) {
return input[key] !== undefined;
},
toObject() {
return { ...input };
},
set(_key: string, _value: string) {
// Stub
},
delete(_key: string) {
// Stub
},
};
}
export function setEnv(key: string, value: string) {
const env = Deno.env.toObject();
// @ts-ignore
process.env = { ...env, [key]: value };
Object.defineProperty(Deno, "env", {
value: createDenoEnvStub({ ...env, [key]: value }),
});
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v12
June 4, 2024