-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a1fa1f
commit 4cf9dcf
Showing
4 changed files
with
75 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { NextResponse } from "next/server"; | ||
import version from "@/version.json"; | ||
import { z } from "zod"; | ||
|
||
const queryParamsSchema = z.object({ | ||
format: z.union([z.literal("json"), z.literal("text")]).default("text"), | ||
}); | ||
|
||
type QueryParams = z.infer<typeof queryParamsSchema>; | ||
|
||
/** | ||
* TODO: Accept any zod schema and validate the query parameters | ||
* @param url | ||
* @returns | ||
*/ | ||
|
||
function parseSearchParam(url: string): QueryParams { | ||
const urlObj = new URL(url); | ||
const searchParams = Object.fromEntries(urlObj.searchParams.entries()); | ||
|
||
// Validate the parameters according to the schema | ||
const parsedParams = queryParamsSchema.parse(searchParams); | ||
return parsedParams; | ||
} | ||
|
||
export async function GET(req: Request) { | ||
const format = parseSearchParam(req.url)?.format; | ||
if (format === "json") { | ||
return NextResponse.json({ | ||
format, | ||
version: version, | ||
}); | ||
} | ||
const formattedVersion = | ||
version.tag !== "latest" | ||
? `v${version.major}.${version.minor}.${version.patch}-${version.tag}.${version.revision}` | ||
: `v${version.major}.${version.minor}.${version.patch}`; | ||
return NextResponse.json({ | ||
format, | ||
version: formattedVersion, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import type * as VersionGet from "@/app/api/version/route"; | ||
import { InferRouteResponse } from "@/types"; | ||
|
||
export type VersionGetResponse = InferRouteResponse<typeof VersionGet.GET>; | ||
|
||
export function useVersion() { | ||
const version = useQuery<VersionGetResponse>({ | ||
queryKey: ["version"], | ||
queryFn: async () => { | ||
const response = await fetch("/api/version"); | ||
return response.json(); | ||
}, | ||
}); | ||
|
||
return { | ||
value: version.data?.format === "text" ? version.data.version : "", | ||
isLoading: version.isLoading, | ||
isError: version.isError, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"major": 1, | ||
"minor": 5, | ||
"patch": 0, | ||
"tag": "canary", | ||
"revision": 0 | ||
} |