-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathindex.ts
86 lines (81 loc) · 2.56 KB
/
index.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
import type {
CreateEnv,
ServerClientOptions,
StrictOptions,
} from "@t3-oss/env-core";
import { createEnv as createEnvCore } from "@t3-oss/env-core";
import type { ZodType } from "zod";
const CLIENT_PREFIX = "NEXT_PUBLIC_" as const;
type ClientPrefix = typeof CLIENT_PREFIX;
type Options<
TServer extends Record<string, ZodType>,
TClient extends Record<`${ClientPrefix}${string}`, ZodType>,
TShared extends Record<string, ZodType>,
TExtends extends Array<Record<string, unknown>>,
> = Omit<
StrictOptions<ClientPrefix, TServer, TClient, TShared, TExtends> &
ServerClientOptions<ClientPrefix, TServer, TClient>,
"runtimeEnvStrict" | "runtimeEnv" | "clientPrefix"
> &
(
| {
/**
* Manual destruction of `process.env`. Required for Next.js < 13.4.4.
*/
runtimeEnv: StrictOptions<
ClientPrefix,
TServer,
TClient,
TShared,
TExtends
>["runtimeEnvStrict"];
experimental__runtimeEnv?: never;
}
| {
runtimeEnv?: never;
/**
* Can be used for Next.js ^13.4.4 since they stopped static analysis of server side `process.env`.
* Only client side `process.env` is statically analyzed and needs to be manually destructured.
*/
experimental__runtimeEnv: Record<
| {
[TKey in keyof TClient]: TKey extends `${ClientPrefix}${string}`
? TKey
: never;
}[keyof TClient]
| {
[TKey in keyof TShared]: TKey extends string ? TKey : never;
}[keyof TShared],
string | boolean | number | undefined
>;
}
);
export function createEnv<
TServer extends Record<string, ZodType> = NonNullable<unknown>,
TClient extends Record<
`${ClientPrefix}${string}`,
ZodType
> = NonNullable<unknown>,
TShared extends Record<string, ZodType> = NonNullable<unknown>,
const TExtends extends Array<Record<string, unknown>> = [],
>(
opts: Options<TServer, TClient, TShared, TExtends>,
): CreateEnv<TServer, TClient, TShared, TExtends> {
const client = typeof opts.client === "object" ? opts.client : {};
const server = typeof opts.server === "object" ? opts.server : {};
const shared = opts.shared;
const runtimeEnv = opts.runtimeEnv
? opts.runtimeEnv
: {
...process.env,
...opts.experimental__runtimeEnv,
};
return createEnvCore<ClientPrefix, TServer, TClient, TShared, TExtends>({
...opts,
shared,
client,
server,
clientPrefix: CLIENT_PREFIX,
runtimeEnv,
});
}