File tree Expand file tree Collapse file tree 10 files changed +67
-50
lines changed
Expand file tree Collapse file tree 10 files changed +67
-50
lines changed Original file line number Diff line number Diff line change 1- NEXT_PUBLIC_MODE = default
1+ # Config
2+ PUBLIC_SERVER_URL = http://localhost:4000
3+ PUBLIC_MOCK_DATA = true
4+ SERVER_PORT = 4000 # optional, server port (alias: PORT, PUBLIC_SERVER_URL.port) (defaults to 4000 if none are specified)
5+
6+ # Secrets
7+ PUBLIC_X_ANON_KEY = ...
8+ X_API_KEY = ...
Original file line number Diff line number Diff line change 1- # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
1+ node_modules
2+ /.env
23
3- node_modules /
4-
5- .tool-versions
6- bun.lockb
7-
8- # dependencies
9- /node_modules
10- /.pnp
11- .pnp.js
12- .yarn /install-state.gz
13-
14- # testing
15- /coverage
16-
17- # next.js
18- .next
19- out
20-
21- # production
22- build
23-
24- # misc
4+ # OS / Dev Env
255.DS_Store
266* .pem
27- .direnv
28-
29- # debug
30- npm-debug.log *
31- yarn-debug.log *
32- yarn-error.log *
33-
34- # local env files
35- .env * .local
36-
37- # vercel
38- .vercel
7+ /.direnv
398
40- # typescript
9+ # Build Output
10+ dist
4111* .tsbuildinfo
42- next-env.d.ts
Original file line number Diff line number Diff line change 77 "." : " ./app.ts"
88 },
99 "scripts" : {
10- "dev" : " bun run --watch ./serve.ts" ,
11- "dev:mock" : " MOCK=true bun run --watch ./serve.ts"
10+ "dev" : " bun --env-file=../../.env run --watch ./serve.ts"
1211 },
1312 "devDependencies" : {
1413 "@types/bun" : " ^1.2.18"
Original file line number Diff line number Diff line change 11import { app } from "./app.ts" ;
22
3- const port = process . env . SERVER_PORT ?? process . env . PORT ?? 4000 ;
3+ const port =
4+ process . env . SERVER_PORT ??
5+ process . env . PORT ??
6+ parsePublicServerURL ( ) ??
7+ "4000" ;
48
59app . listen ( port , ( ) =>
610 console . log ( `Server started at http://localhost:${ port } ` ) ,
711) ;
12+
13+ function parsePublicServerURL ( ) {
14+ if ( ! process . env . PUBLIC_SERVER_URL ) return undefined ;
15+ const url = new URL ( process . env . PUBLIC_SERVER_URL ) ;
16+ return url . port ;
17+ }
Original file line number Diff line number Diff line change 55 "type" : " module" ,
66 "scripts" : {
77 "dev" : " vite" ,
8- "dev:mock" : " VITE_MODE=mock vite" ,
98 "build" : " tsc && vite build" ,
109 "preview" : " vite preview" ,
1110 "check" : " tsc --noEmit"
Original file line number Diff line number Diff line change 11import type { RegisterType } from "@/app/type" ;
22import { SampleUser } from "@/app/utils/mock_data" ;
3-
4- const MODE = import . meta. env . VITE_MODE ;
3+ import { env } from "../../lib/env.ts" ;
54
65export class User {
76 private user : RegisterType | undefined ;
87
98 constructor ( ) {
109 if ( typeof window !== "undefined" ) {
11- if ( MODE === "mock" ) {
10+ if ( env . mockData ) {
1211 this . user = SampleUser ;
1312 } else {
1413 const storedUser = localStorage . getItem ( "user" ) ;
@@ -36,7 +35,7 @@ export class User {
3635 setUser ( newUser : RegisterType ) : void {
3736 this . user = newUser ;
3837
39- if ( typeof window !== "undefined" && MODE !== "mock" ) {
38+ if ( typeof window !== "undefined" && ! env . mockData ) {
4039 localStorage . setItem ( "user" , JSON . stringify ( newUser ) ) ;
4140 }
4241 }
Original file line number Diff line number Diff line change 11import { edenTreaty } from "@elysiajs/eden" ;
22import type { App } from "@packages/server" ;
3+ import { env } from "./env.ts" ;
34
4- export const api = edenTreaty < App > ( "http://localhost:4000" ) . api ;
5+ export const api = edenTreaty < App > ( env . serverURL ) . api ;
Original file line number Diff line number Diff line change 1+ export const env = {
2+ mockData : boolean ( import . meta. env . PUBLIC_MOCK_DATA ) ,
3+ serverURL : string ( import . meta. env . PUBLIC_SERVER_URL , {
4+ name : "PUBLIC_SERVER_URL" ,
5+ } ) ,
6+ } ;
7+
8+ function boolean ( value : string | undefined ) : boolean {
9+ return value === "true" || value === "1" ;
10+ }
11+
12+ /**
13+ * if value != null -> return value
14+ * if defaultValue != null -> return defaultValue
15+ * default -> throws Error
16+ * @param value 値
17+ * @param param1
18+ * @returns 結果
19+ */
20+ function string (
21+ value : string | undefined ,
22+ { name, defaultValue } : { name ?: string ; defaultValue ?: string } ,
23+ ) : string {
24+ if ( value != null ) {
25+ return value ;
26+ }
27+ if ( defaultValue != null ) {
28+ return defaultValue ;
29+ }
30+ throw new Error ( `${ name } is undefined` ) ;
31+ }
Original file line number Diff line number Diff line change 11/// <reference types="vite/client" />
22
33interface ImportMetaEnv {
4- readonly VITE_MODE : string ;
4+ readonly PUBLIC_SERVER_URL : string ;
5+ readonly PUBLIC_MOCK_DATA : string ;
56}
67
78interface ImportMeta {
Original file line number Diff line number Diff line change 1- import path from "node:path" ;
21import react from "@vitejs/plugin-react" ;
32import { defineConfig } from "vite" ;
43
54export default defineConfig ( {
65 plugins : [ react ( ) ] ,
76 resolve : {
87 alias : {
9- "@" : path . resolve ( __dirname , "./src" ) ,
8+ "@" : "./src" ,
109 } ,
1110 } ,
11+ envDir : "../../" ,
12+ envPrefix : "PUBLIC_" ,
1213 server : {
1314 port : 3000 ,
1415 } ,
You can’t perform that action at this time.
0 commit comments