This repository was archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvite.config.mjs
157 lines (155 loc) · 5.25 KB
/
vite.config.mjs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import path from "path"
import { defineConfig } from "vite"
import react from "@vitejs/plugin-react"
export default defineConfig({
server: {
port: 9000
},
resolve: {
alias: {
constants: path.resolve(__dirname, "./src/constants/"),
domains: path.resolve(__dirname, "./src/domains/"),
adapters: path.resolve(__dirname, "./src/adapters/"),
di: path.resolve(__dirname, "./src/di/"),
hooks: path.resolve(__dirname, "./src/frameworks/hooks/"),
pages: path.resolve(__dirname, "./src/frameworks/pages/"),
providers: path.resolve(__dirname, "./src/frameworks/providers/"),
containers: path.resolve(__dirname, "./src/frameworks/containers/"),
components: path.resolve(__dirname, "./src/frameworks/components/"),
"styled-system": path.resolve(__dirname, "./styled-system/")
},
extensions: [".ts", ".tsx", ".js", ".mjs"]
},
plugins: [
react(),
{
name: "mock-server",
configureServer(server) {
const user = { id: "1", name: "falsy" }
let posts = [
{
id: "1",
title: "Hello, world!",
content: "This is the first post.",
author: user,
createdAt: new Date(),
updatedAt: new Date()
}
]
let comments = []
server.middlewares.use((req, res, next) => {
if (req.method === "GET" && req.url === "/api/users") {
setTimeout(() => {
res.setHeader("Content-Type", "application/json")
res.end(JSON.stringify(user))
}, 400)
} else if (req.method === "GET" && req.url === "/api/posts") {
setTimeout(() => {
res.setHeader("Content-Type", "application/json")
res.end(JSON.stringify(posts))
}, 400)
} else if (req.method === "POST" && req.url === "/api/posts") {
let body = ""
req.on("data", (chunk) => {
body += chunk.toString()
})
req.on("end", () => {
const post = {
...JSON.parse(body),
id: new Date().getTime().toString(),
author: user,
createdAt: new Date(),
updatedAt: new Date()
}
posts.push(post)
setTimeout(() => {
res.setHeader("Content-Type", "application/json")
res.end(JSON.stringify(true))
}, 400)
})
} else if (
req.method === "DELETE" &&
typeof req.url === "string" &&
req.url.startsWith("/api/posts/")
) {
const postId = req.url.split("/").pop()
const index = posts.findIndex((post) => post.id === postId)
if (index !== -1) posts.splice(index, 1)
setTimeout(() => {
res.setHeader("Content-Type", "application/json")
res.end(JSON.stringify(true))
}, 400)
} else if (
req.method === "PUT" &&
typeof req.url === "string" &&
req.url.startsWith("/api/posts/")
) {
let body = ""
req.on("data", (chunk) => {
body += chunk.toString()
})
req.on("end", () => {
const postId = req.url.split("/").pop()
posts = posts.map((post) => {
if (post.id === postId) {
post = {
...post,
...JSON.parse(body),
updatedAt: new Date()
}
}
return post
})
setTimeout(() => {
res.setHeader("Content-Type", "application/json")
res.end(JSON.stringify(true))
}, 400)
})
} else if (
req.method === "GET" &&
typeof req.url === "string" &&
req.url.startsWith("/api/posts/") &&
req.url.endsWith("/comments")
) {
const postId = req.url.split("/")[3]
const postComments = comments.filter(
(comment) => comment.postId === postId
)
setTimeout(() => {
res.setHeader("Content-Type", "application/json")
res.end(JSON.stringify(postComments))
}, 400)
} else if (
req.method === "POST" &&
typeof req.url === "string" &&
req.url.startsWith("/api/posts/") &&
req.url.endsWith("/comments")
) {
let body = ""
const postId = req.url.split("/")[3]
req.on("data", (chunk) => {
body += chunk.toString()
})
req.on("end", () => {
const comment = {
...JSON.parse(body),
id: new Date().getTime(),
postId,
author: user,
createdAt: new Date(),
updatedAt: new Date()
}
comments.push(comment)
setTimeout(() => {
res.setHeader("Content-Type", "application/json")
res.end(JSON.stringify(true))
}, 400)
})
} else {
next()
}
})
}
}
]
})