Commit 6d2c799
authored
refactor(streak): introduced zod schema for API parameter validation (JhaSourav07#124)
## Description
Fixes JhaSourav07#67
Introduces zod for strict validation of incoming URL parameters across
all three API routes, replacing scattered manual `searchParams.get()`
calls and if/else fallbacks with a single centralized schema.
- New file `lib/validations.ts`:
Defines `streakParamsSchema`, `githubParamsSchema`, and `ogParamsSchema`
using `Zod`. Default values (theme: "dark", scale: "linear", speed:
"8s") are handled via Zod's `.default()`. Invalid speed and scale values
silently fall back to their defaults using `.catch()` to preserve
existing behavior. The user and username fields are required and return
a structured JSON error if missing.
- Changes in `app/api/streak/route.ts`:
Replaced all individual `searchParams.get()` calls with a single
`streakParamsSchema.safeParse()` call at the top of the handler. If
parsing fails (e.g. missing user), the route returns a clean structured
JSON 400 error immediately before any GitHub API call is made. All
downstream logic remains identical.
- Changes in `app/api/github/route.ts`:
Replaced manual `searchParams.get('username')` and `if (!username)`
check with `githubParamsSchema.safeParse()`. Returns a structured JSON
400 error if username is missing. All error handling and cache logic
remains identical.
- Changes in `app/api/og/route.tsx`:
Replaced `searchParams.get('user')` ?? 'unknown' with
`ogParamsSchema.parse()`. Since user is optional with a default of
'unknown', this route never throws a validation error. All rendering
logic remains identical.
- Added 4 additional optional properties — `radius`, `font`, `year`, and
`refresh` {Other than the requested ones}.
- All Acceptance Criteria are met. 🚀
## Pillar
- 🛠️ Other (Bug fix, refactoring, docs)
## Visual Preview
<img width="457" height="325" alt="Screenshot 2026-05-17 at 1 43 52 PM"
src="https://github.com/user-attachments/assets/8f46c743-60c3-4d9d-98fa-57a160c089d9"
/>
## Checklist before requesting a review:
- I have read the `CONTRIBUTING.md` file.
- I have tested these changes locally (`localhost:3000/api/streak`).
- I have run `npm run format` and `npm run lint` locally and resolved
all errors.
- My commits follow the Conventional Commits format.
- I have started the repo.
- I have made sure that i have only one commit to merge in this PR.
- The SVG output matches the CommitPulse "premium quality" aesthetic
standard (no raw elements, smooth animations, correct fonts).6 files changed
Lines changed: 98 additions & 121 deletions
File tree
- app/api
- github
- og
- streak
- lib
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
6 | | - | |
7 | | - | |
8 | 7 | | |
9 | | - | |
10 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
11 | 15 | | |
12 | 16 | | |
| 17 | + | |
| 18 | + | |
13 | 19 | | |
14 | 20 | | |
15 | 21 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | | - | |
| 9 | + | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| |||
57 | 59 | | |
58 | 60 | | |
59 | 61 | | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
| 62 | + | |
69 | 63 | | |
70 | 64 | | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
| 65 | + | |
| 66 | + | |
88 | 67 | | |
89 | 68 | | |
90 | 69 | | |
| |||
96 | 75 | | |
97 | 76 | | |
98 | 77 | | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
| 78 | + | |
106 | 79 | | |
107 | 80 | | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
| 81 | + | |
118 | 82 | | |
119 | | - | |
120 | 83 | | |
121 | 84 | | |
122 | 85 | | |
| |||
128 | 91 | | |
129 | 92 | | |
130 | 93 | | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
| 94 | + | |
138 | 95 | | |
139 | 96 | | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
| 97 | + | |
148 | 98 | | |
149 | 99 | | |
150 | 100 | | |
151 | | - | |
152 | 101 | | |
153 | 102 | | |
154 | 103 | | |
| |||
160 | 109 | | |
161 | 110 | | |
162 | 111 | | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
| 112 | + | |
170 | 113 | | |
171 | 114 | | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
| 115 | + | |
180 | 116 | | |
181 | 117 | | |
182 | 118 | | |
183 | 119 | | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
| 120 | + | |
193 | 121 | | |
194 | 122 | | |
195 | 123 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | | - | |
14 | 14 | | |
15 | | - | |
16 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
17 | 23 | | |
18 | 24 | | |
19 | | - | |
20 | | - | |
21 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
22 | 30 | | |
23 | | - | |
| 31 | + | |
24 | 32 | | |
25 | 33 | | |
26 | 34 | | |
| |||
33 | 41 | | |
34 | 42 | | |
35 | 43 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
55 | 53 | | |
56 | 54 | | |
57 | 55 | | |
58 | 56 | | |
59 | 57 | | |
60 | 58 | | |
61 | | - | |
62 | | - | |
63 | 59 | | |
64 | 60 | | |
65 | 61 | | |
66 | 62 | | |
67 | 63 | | |
68 | | - | |
| 64 | + | |
69 | 65 | | |
70 | 66 | | |
71 | 67 | | |
72 | 68 | | |
73 | 69 | | |
74 | 70 | | |
75 | | - | |
| 71 | + | |
76 | 72 | | |
77 | 73 | | |
78 | 74 | | |
79 | 75 | | |
80 | | - | |
81 | 76 | | |
82 | 77 | | |
83 | 78 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
0 commit comments