@@ -2,26 +2,19 @@ import { getDefaultClientConfig } from "./utils";
2
2
3
3
describe ( "getDefaultClientConfig" , ( ) => {
4
4
afterEach ( ( ) => {
5
- vi . unstubAllGlobals ( ) ;
5
+ vi . unstubAllEnvs ( ) ;
6
6
} ) ;
7
7
8
8
it ( "should return the default config" , ( ) => {
9
- vi . stubGlobal ( "process" , {
10
- env : { } ,
11
- } ) ;
12
9
expect ( getDefaultClientConfig ( ) ) . toEqual ( {
13
10
url : "http://localhost:4242/api/frontend" ,
14
11
appName : "nextjs" ,
15
12
clientKey : "default:development.unleash-insecure-frontend-api-token" ,
16
13
} ) ;
17
14
} ) ;
18
15
19
- it ( "should use BASE_URL" , ( ) => {
20
- vi . stubGlobal ( "process" , {
21
- env : {
22
- UNLEASH_SERVER_API_URL : "http://example.com/api" ,
23
- } ,
24
- } ) ;
16
+ it ( "should use NEXT_PUBLIC_UNLEASH_SERVER_API_URL" , ( ) => {
17
+ vi . stubEnv ( "NEXT_PUBLIC_UNLEASH_SERVER_API_URL" , "http://example.com/api" ) ;
25
18
26
19
expect ( getDefaultClientConfig ( ) ) . toEqual ( {
27
20
url : "http://example.com/api/frontend" ,
@@ -30,52 +23,99 @@ describe("getDefaultClientConfig", () => {
30
23
} ) ;
31
24
} ) ;
32
25
33
- it ( "should use set appName" , ( ) => {
34
- vi . stubGlobal ( "process" , {
35
- env : {
36
- NEXT_PUBLIC_UNLEASH_APP_NAME : "my-app" ,
37
- } ,
26
+ it ( "should use `UNLEASH_SERVER_API_URL` and prioritize it over `NEXT_PUBLIC_UNLEASH_SERVER_API_URL` when available" , ( ) => {
27
+ vi . stubEnv ( "NEXT_PUBLIC_UNLEASH_SERVER_API_URL" , "http://example.com/api" ) ;
28
+ vi . stubEnv ( "UNLEASH_SERVER_API_URL" , "http://example.org/api" ) ;
29
+
30
+ expect ( getDefaultClientConfig ( ) ) . toEqual ( {
31
+ url : "http://example.org/api/frontend" ,
32
+ appName : "nextjs" ,
33
+ clientKey : "default:development.unleash-insecure-frontend-api-token" ,
38
34
} ) ;
35
+ } ) ;
36
+
37
+ it ( "should use NEXT_PUBLIC_UNLEASH_FRONTEND_API_URL" , ( ) => {
38
+ vi . stubEnv (
39
+ "NEXT_PUBLIC_UNLEASH_FRONTEND_API_URL" ,
40
+ "http://example.com/proxy"
41
+ ) ;
42
+
43
+ expect ( getDefaultClientConfig ( ) ) . toEqual ( {
44
+ url : "http://example.com/proxy" ,
45
+ appName : "nextjs" ,
46
+ clientKey : "default:development.unleash-insecure-frontend-api-token" ,
47
+ } ) ;
48
+ } ) ;
49
+
50
+ it ( "should use UNLEASH_FRONTEND_API_URL and prioritize it over `NEXT_PUBLIC_UNLEASH_FRONTEND_API_URL` when available" , ( ) => {
51
+ vi . stubEnv (
52
+ "NEXT_PUBLIC_UNLEASH_FRONTEND_API_URL" ,
53
+ "http://example.com/proxy"
54
+ ) ;
55
+ vi . stubEnv ( "UNLEASH_FRONTEND_API_URL" , "http://example.org/api/frontend" ) ;
56
+
57
+ expect ( getDefaultClientConfig ( ) ) . toEqual ( {
58
+ url : "http://example.org/api/frontend" ,
59
+ appName : "nextjs" ,
60
+ clientKey : "default:development.unleash-insecure-frontend-api-token" ,
61
+ } ) ;
62
+ } ) ;
63
+
64
+ it ( "should use NEXT_PUBLIC_UNLEASH_APP_NAME" , ( ) => {
65
+ vi . stubEnv ( "NEXT_PUBLIC_UNLEASH_APP_NAME" , "my-app" ) ;
39
66
40
67
expect ( getDefaultClientConfig ( ) ) . toEqual ( {
41
68
url : "http://localhost:4242/api/frontend" ,
42
69
appName : "my-app" ,
43
70
clientKey : "default:development.unleash-insecure-frontend-api-token" ,
44
71
} ) ;
72
+ } ) ;
45
73
46
- vi . stubGlobal ( "process" , {
47
- env : {
48
- NEXT_PUBLIC_UNLEASH_APP_NAME : "my-app" ,
49
- UNLEASH_APP_NAME : "my-app-override" ,
50
- } ,
51
- } ) ;
74
+ it ( "should use `UNLEASH_APP_NAME` and prioritize it over `NEXT_PUBLIC_UNLEASH_APP_NAME` when available" , ( ) => {
75
+ vi . stubEnv ( "NEXT_PUBLIC_UNLEASH_APP_NAME" , "my-app" ) ;
76
+ vi . stubEnv ( "UNLEASH_APP_NAME" , "my-app-override" ) ;
52
77
53
78
expect ( getDefaultClientConfig ( ) ) . toEqual ( {
54
79
url : "http://localhost:4242/api/frontend" ,
55
80
appName : "my-app-override" ,
56
81
clientKey : "default:development.unleash-insecure-frontend-api-token" ,
57
82
} ) ;
58
83
} ) ;
59
- } ) ;
60
84
61
- // UNLEASH_SERVER_API_URL
62
- // UNLEASH_FRONTEND_API_URL
63
- // UNLEASH_SERVER_API_TOKEN
64
- // UNLEASH_FRONTEND_API_TOKEN
65
- // UNLEASH_APP_NAME
66
-
67
- // export const getDefaultClientConfig = {
68
- // url: `${
69
- // process.env.UNLEASH_BASE_URL ||
70
- // process.env.NEXT_PUBLIC_UNLEASH_BASE_URL ||
71
- // "http://localhost:4242/api"
72
- // }/frontend`,
73
- // appName:
74
- // process.env.UNLEASH_APP_NAME ||
75
- // process.env.NEXT_PUBLIC_UNLEASH_APP_NAME ||
76
- // "nextjs",
77
- // clientKey:
78
- // process.env.UNLEASH_FRONTEND_API_TOKEN ||
79
- // process.env.NEXT_PUBLIC_UNLEASH_FRONTEND_API_TOKEN ||
80
- // "default:development.unleash-insecure-frontend-api-token",
81
- // };
85
+ it ( "should use NEXT_PUBLIC_UNLEASH_FRONTEND_API_TOKEN" , ( ) => {
86
+ vi . stubEnv ( "NEXT_PUBLIC_UNLEASH_FRONTEND_API_TOKEN" , "my-token" ) ;
87
+
88
+ expect ( getDefaultClientConfig ( ) ) . toEqual ( {
89
+ url : "http://localhost:4242/api/frontend" ,
90
+ clientKey : "my-token" ,
91
+ appName : "nextjs" ,
92
+ } ) ;
93
+ } ) ;
94
+
95
+ it ( "should use `UNLEASH_FRONTEND_API_TOKEN` and prioritize it over `NEXT_PUBLIC_UNLEASH_FRONTEND_API_TOKEN` when available" , ( ) => {
96
+ vi . stubEnv ( "NEXT_PUBLIC_UNLEASH_FRONTEND_API_TOKEN" , "my-token" ) ;
97
+ vi . stubEnv ( "UNLEASH_FRONTEND_API_TOKEN" , "my-token-override" ) ;
98
+
99
+ expect ( getDefaultClientConfig ( ) ) . toEqual ( {
100
+ url : "http://localhost:4242/api/frontend" ,
101
+ clientKey : "my-token-override" ,
102
+ appName : "nextjs" ,
103
+ } ) ;
104
+ } ) ;
105
+
106
+ it ( "should use warn about using NEXT_PUBLIC_UNLEASH_SERVER_API_TOKEN" , ( ) => {
107
+ vi . stubEnv ( "NEXT_PUBLIC_UNLEASH_SERVER_API_TOKEN" , "insecure-token" ) ;
108
+
109
+ const warnSpy = vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => { } ) ;
110
+
111
+ expect ( getDefaultClientConfig ( ) ) . toEqual ( {
112
+ url : "http://localhost:4242/api/frontend" ,
113
+ appName : "nextjs" ,
114
+ clientKey : "default:development.unleash-insecure-frontend-api-token" ,
115
+ } ) ;
116
+
117
+ expect ( warnSpy ) . toHaveBeenCalledWith (
118
+ "You are trying to set `NEXT_PUBLIC_UNLEASH_SERVER_API_TOKEN`. Server keys shouldn't be public. Use frontend keys or skip `NEXT_PUBLIC_ prefix."
119
+ ) ;
120
+ } ) ;
121
+ } ) ;
0 commit comments