Skip to content

Commit 7624de5

Browse files
authored
Doc Changes made (JhaSourav07#3309)
## Description Fixes JhaSourav07#3308 This PR improves project documentation by adding missing JSDoc comments to exported utility functions and updating middleware documentation to accurately reflect protected routes. ### Changes Made (Documentation Only — No Logic Changes) #### `utils/urls.ts` * Added JSDoc for `getOrigin()` * Documents origin resolution priority chain: * `window.location.origin` * Environment variable * Local fallback * Added JSDoc for `getDashboardUrl()` * Includes `@param` and `@returns` #### `utils/tracking.ts` * Added JSDoc for `trackUser()` * Documents beacon-first / fetch-fallback tracking strategy * Explains no-op conditions * Includes parameter and return documentation #### `utils/dashboardPeriod.ts` * Added JSDoc for `resolveDashboardPeriod()` * Documents period resolution priority chain * Added JSDoc for `shiftDashboardPeriod()` * Documents period shifting behavior for each period type * Added JSDoc for `dashboardPeriodToSearchParams()` * Documents query parameter serialization behavior #### `middleware.ts` * Updated existing JSDoc header * Added `/api/compare` to the protected routes list to match the middleware matcher configuration ### Impact * Improves code readability and maintainability * Enhances IDE autocomplete and hover documentation * Makes onboarding easier for new contributors * Ensures documentation remains consistent with implementation * No functional or behavioral changes --- ## Pillar * [ ] 🎨 Pillar 1 — New Theme Design * [ ] 📐 Pillar 2 — Geometric SVG Improvement * [ ] 🕐 Pillar 3 — Timezone Logic Optimization * [x] 🛠️ Other (Bug fix, refactoring, docs) --- ## Visual Preview N/A – Documentation-only changes with no UI or SVG modifications. --- ## Checklist before requesting a review: * [x] I have read the `CONTRIBUTING.md` file. * [x] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). * [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). * [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). * [x] I have updated `README.md` if I added a new theme or URL parameter. (Not applicable) * [x] I have starred the repo. * [x] I have made sure that I have only one commit to merge in this PR. * [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). * [x] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents c64e010 + a57fc67 commit 7624de5

4 files changed

Lines changed: 71 additions & 0 deletions

File tree

middleware.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getClientIp } from './utils/getClientIp';
1313
* - /api/stats
1414
* - /api/og
1515
* - /api/notify
16+
* - /api/compare
1617
*
1718
* Limit: 60 requests per minute per IP.
1819
*/

utils/dashboardPeriod.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ function formatRollingLabel(start: Date, end: Date): string {
7474
return `${formatter.format(start)} to ${formatter.format(end)}`;
7575
}
7676

77+
/**
78+
* Resolves a {@link DashboardPeriod} from a loose set of URL search-param inputs.
79+
*
80+
* Resolution priority (first match wins):
81+
* 1. **Custom range** — both `input.from` and `input.to` are valid ISO date strings.
82+
* 2. **Month** — `input.month` matches `YYYY-MM` and represents a valid calendar month.
83+
* 3. **Year** — `input.year` matches `YYYY` and falls within the range 2008–(now+5).
84+
* 4. **Rolling 12 months** — default fallback when none of the above match.
85+
*
86+
* @param {DashboardPeriodInput} input - Raw query-string values (year, month, from, to).
87+
* @param {Date} [now=new Date()] - Reference date used for the rolling-window default and year validation.
88+
* @returns {DashboardPeriod} A fully resolved period object with `kind`, `label`, `from`, and `to`.
89+
*/
7790
export function resolveDashboardPeriod(
7891
input: DashboardPeriodInput,
7992
now: Date = new Date()
@@ -145,6 +158,22 @@ export function resolveDashboardPeriod(
145158
};
146159
}
147160

161+
/**
162+
* Shifts a {@link DashboardPeriod} one step forwards or backwards.
163+
*
164+
* Shift semantics vary by period kind:
165+
* - **month** — moves to the previous or next calendar month.
166+
* - **year** — moves to the previous or next calendar year.
167+
* - **range** — shifts by the exact number of days spanned by the current range.
168+
* - **rolling** — shifts the 12-month window by one month in the requested direction.
169+
*
170+
* The function always delegates to {@link resolveDashboardPeriod} so that the returned
171+
* period is normalised and fully hydrated.
172+
*
173+
* @param {DashboardPeriod} period - The currently active period.
174+
* @param {'prev' | 'next'} direction - Direction to shift: `'prev'` for earlier, `'next'` for later.
175+
* @returns {DashboardPeriod} The shifted, fully resolved period.
176+
*/
148177
export function shiftDashboardPeriod(
149178
period: DashboardPeriod,
150179
direction: 'prev' | 'next'
@@ -183,6 +212,18 @@ export function shiftDashboardPeriod(
183212
return resolveDashboardPeriod({ from: shiftedFrom.toISOString(), to: shiftedTo.toISOString() });
184213
}
185214

215+
/**
216+
* Serialises a {@link DashboardPeriod} into a `URLSearchParams` instance suitable for
217+
* appending to a dashboard URL.
218+
*
219+
* Serialisation strategy:
220+
* - **month** — emits a single `month=YYYY-MM` param.
221+
* - **year** — emits a single `year=YYYY` param.
222+
* - **range / rolling** — emits `from` and `to` ISO timestamp params.
223+
*
224+
* @param {DashboardPeriod} period - The period to serialise.
225+
* @returns {URLSearchParams} The query-string representation of the period.
226+
*/
186227
export function dashboardPeriodToSearchParams(period: DashboardPeriod): URLSearchParams {
187228
const params = new URLSearchParams();
188229

utils/tracking.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/**
2+
* Fires a fire-and-forget analytics ping to `/api/track-user` for the given GitHub username.
3+
*
4+
* Uses `navigator.sendBeacon` when available (reliable on page unload), falling back to
5+
* `fetch` with `keepalive: true` for environments that do not support the Beacon API.
6+
*
7+
* The function is a no-op when:
8+
* - Running outside of a browser context (`navigator` or `window` is undefined).
9+
* - `username` is an empty string or falsy.
10+
*
11+
* @param {string} username - The GitHub username to record the visit for.
12+
* @returns {void}
13+
*/
114
export function trackUser(username: string) {
215
if (typeof navigator === 'undefined' || typeof window === 'undefined') return;
316
if (!username) return;

utils/urls.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
const FALLBACK_ORIGIN = 'https://commitpulse.vercel.app';
22

3+
/**
4+
* Resolves the base origin URL for the current environment.
5+
*
6+
* Priority order:
7+
* 1. `window.location.origin` — used when running in the browser.
8+
* 2. `NEXT_PUBLIC_SITE_URL` environment variable — used in server-side contexts.
9+
* 3. Hardcoded fallback (`https://commitpulse.vercel.app`).
10+
*
11+
* @returns {string} The resolved base origin URL (e.g. `https://commitpulse.app` or `http://localhost:3000`).
12+
*/
313
export function getOrigin(): string {
414
const envOrigin = process.env.NEXT_PUBLIC_SITE_URL?.trim() || null;
515
return (
616
(typeof window !== 'undefined' ? window.location.origin : null) ?? envOrigin ?? FALLBACK_ORIGIN
717
);
818
}
919

20+
/**
21+
* Constructs the full absolute URL for a user's dashboard page.
22+
*
23+
* @param {string} username - The GitHub username whose dashboard URL should be generated.
24+
* @returns {string} The absolute dashboard URL (e.g. `https://commitpulse.app/dashboard/octocat`).
25+
*/
1026
export function getDashboardUrl(username: string): string {
1127
return `${getOrigin()}/dashboard/${encodeURIComponent(username)}`;
1228
}

0 commit comments

Comments
 (0)