@quintal/environment 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,162 +0,0 @@
1
- import { z } from 'zod';
2
- import { describe, expect, expectTypeOf, it } from 'vitest';
3
- import { createEnvironment } from '../src';
4
-
5
- describe('environment', () => {
6
- it('returns a strongly-typed environment object', () => {
7
- if (false as boolean) {
8
- const environment = createEnvironment({
9
- values: {
10
- environment: {
11
- value: process.env.NEXT_PUBLIC_ENVIRONMENT,
12
- schema: z
13
- .enum(['DEVELOPMENT', 'PREVIEW', 'PRODUCTION'])
14
- .default('DEVELOPMENT'),
15
- },
16
- port: {
17
- value: process.env.PORT,
18
- schema: z.coerce.number().int().default(4000),
19
- isServerOnly: true,
20
- },
21
- isFeatureEnabled: {
22
- value: process.env.NEXT_PUBLIC_IS_FEATURE_ENABLED,
23
- schema: z.enum(['true', 'false']).transform((s) => s === 'true'),
24
- },
25
- baseUrl: {
26
- self: {
27
- value: process.env.NEXT_PUBLIC_BASE_URL_SELF,
28
- schema: z.string().url().default('http://localhost:3000'),
29
- },
30
- api: {
31
- value: process.env.NEXT_PUBLIC_BASE_URL_API,
32
- schema: z.string().url().default('http://localhost:4000'),
33
- },
34
- },
35
- database: {
36
- url: {
37
- value: process.env.DATABASE_URL,
38
- schema: z.string().url(),
39
- isServerOnly: true,
40
- },
41
- token: {
42
- value: process.env.DATABASE_TOKEN,
43
- isServerOnly: true,
44
- },
45
- },
46
- },
47
- });
48
-
49
- expectTypeOf(environment).toEqualTypeOf<{
50
- environment: 'DEVELOPMENT' | 'PREVIEW' | 'PRODUCTION';
51
- port: number;
52
- isFeatureEnabled: boolean;
53
- baseUrl: {
54
- self: string;
55
- api: string;
56
- };
57
- database: {
58
- url: string;
59
- token: string;
60
- };
61
- }>();
62
- }
63
- });
64
-
65
- it('throws when an environment variable is incorrect', () => {
66
- expect(() =>
67
- createEnvironment({
68
- values: {
69
- undefined: { value: undefined },
70
- nestedUndefined: { undefined: { value: undefined } },
71
- emptyString: { value: '' },
72
- emptyStringToNumber: { value: '', schema: z.number() },
73
- emptyStringWithDefault: {
74
- value: '',
75
- schema: z.string().default('test'),
76
- },
77
- emptyStringToNumberWithDefault: {
78
- value: '',
79
- schema: z.number().default(42),
80
- },
81
- },
82
- }),
83
- ).toThrowErrorMatchingInlineSnapshot(
84
- '"❌ Invalid environment variables: undefined: Required, nestedUndefined.undefined: Required, emptyString: Required, emptyStringToNumber: Required"',
85
- );
86
- });
87
-
88
- it('does not throw when accessing a client environment variable and server variables are missing', () => {
89
- const environment = createEnvironment({
90
- isServer: false,
91
- values: {
92
- correct: { value: 'hello world' },
93
- incorrect: { value: undefined, isServerOnly: true },
94
- },
95
- });
96
-
97
- expect(environment.correct).toBe('hello world');
98
- });
99
-
100
- it('throws when trying to access server-only variables from the client', () => {
101
- const values = {
102
- serverOnly: { value: 'hello', isServerOnly: true },
103
- notServerOnly: { value: 'world', isServerOnly: false },
104
- nested: {
105
- serverOnly: { value: 'helloNested', isServerOnly: true },
106
- notServerOnly: { value: 'worldNested', isServerOnly: false },
107
- },
108
- };
109
-
110
- const clientEnvironment = createEnvironment({ isServer: false, values });
111
- expect(
112
- () => clientEnvironment.serverOnly,
113
- ).toThrowErrorMatchingInlineSnapshot(
114
- '"❌ Attempted to access server-side environment variable \'serverOnly\' on the client"',
115
- );
116
- expect(clientEnvironment.notServerOnly).toBe('world');
117
- expect(
118
- () => clientEnvironment.nested.serverOnly,
119
- ).toThrowErrorMatchingInlineSnapshot(
120
- '"❌ Attempted to access server-side environment variable \'nested.serverOnly\' on the client"',
121
- );
122
- expect(clientEnvironment.nested.notServerOnly).toBe('worldNested');
123
-
124
- const serverEnvironment = createEnvironment({ isServer: true, values });
125
- expect(serverEnvironment.serverOnly).toBe('hello');
126
- expect(serverEnvironment.notServerOnly).toBe('world');
127
- expect(serverEnvironment.nested.serverOnly).toBe('helloNested');
128
- expect(serverEnvironment.nested.notServerOnly).toBe('worldNested');
129
- });
130
-
131
- it('parses (nested) environment variables to be in the correct datatype', () => {
132
- const environment = createEnvironment({
133
- values: {
134
- string: { value: 'hello world', schema: z.string() },
135
- number: { value: '42', schema: z.coerce.number().int() },
136
- boolean: {
137
- value: 'true',
138
- schema: z.enum(['true', 'false']).transform((s) => s === 'true'),
139
- },
140
- nested: {
141
- string: { value: 'nested hello world', schema: z.string() },
142
- number: { value: '4242', schema: z.coerce.number().int() },
143
- boolean: {
144
- value: 'false',
145
- schema: z.enum(['true', 'false']).transform((s) => s === 'true'),
146
- },
147
- },
148
- },
149
- });
150
-
151
- expect(environment.string).toBe('hello world');
152
- expect(environment.number).toBe(42);
153
- expect(environment.boolean).toBe(true);
154
- expect(
155
- // @ts-expect-error -- testing invalid access
156
- environment.never,
157
- ).toBe(undefined);
158
- expect(environment.nested.string).toBe('nested hello world');
159
- expect(environment.nested.number).toBe(4242);
160
- expect(environment.nested.boolean).toBe(false);
161
- });
162
- });
package/tsconfig.json DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "extends": "@quintal/config/tsconfig/base.json",
3
- "compilerOptions": {
4
- "rootDir": "."
5
- },
6
- "include": ["**/*.ts", "vite.config.js"],
7
- "exclude": ["node_modules", "dist"]
8
- }
package/vite.config.js DELETED
@@ -1 +0,0 @@
1
- module.exports = require('@quintal/config/vite');