Skip to content

Commit d020cf4

Browse files
Feature: Custom Date Range Windows (from and to parameters) (JhaSourav07#1148)
feat(api): add custom from and to date range parameters Co-authored-by: Ixotic27 <ixotic27@users.noreply.github.com>
2 parents 98ae406 + e73066b commit d020cf4

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

app/api/streak/route.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export async function GET(request: Request) {
3939
radius,
4040
font,
4141
year,
42+
from: customFrom,
43+
to: customTo,
4244
refresh,
4345
hide_title,
4446
hide_background,
@@ -57,8 +59,16 @@ export async function GET(request: Request) {
5759
} = parseResult.data;
5860

5961
const themeName = theme || 'dark';
60-
const from = year ? `${year}-01-01T00:00:00Z` : undefined;
61-
const to = year ? `${year}-12-31T23:59:59Z` : undefined;
62+
const from = customFrom
63+
? new Date(customFrom).toISOString()
64+
: year
65+
? `${year}-01-01T00:00:00Z`
66+
: undefined;
67+
const to = customTo
68+
? new Date(customTo).toISOString()
69+
: year
70+
? `${year}-12-31T23:59:59Z`
71+
: undefined;
6272

6373
const tzParam = searchParams.get('tz');
6474
let timezone = 'UTC';

lib/validations.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ export const streakParamsSchema = z.object({
9595
message: 'GitHub was founded in 2008. Please provide a year of 2008 or later.',
9696
}
9797
),
98+
from: z
99+
.string()
100+
.optional()
101+
.refine(
102+
(val) => {
103+
if (!val) return true;
104+
return !isNaN(Date.parse(val));
105+
},
106+
{ message: 'Invalid "from" date format. Use ISO 8601 (e.g. 2023-01-01).' }
107+
),
108+
to: z
109+
.string()
110+
.optional()
111+
.refine(
112+
(val) => {
113+
if (!val) return true;
114+
return !isNaN(Date.parse(val));
115+
},
116+
{ message: 'Invalid "to" date format. Use ISO 8601 (e.g. 2023-12-31).' }
117+
),
98118
refresh: z
99119
.string()
100120
.optional()

0 commit comments

Comments
 (0)