This repository was archived by the owner on Jan 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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
e45c933
commit 3484535
Showing
3 changed files
with
130 additions
and
0 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,58 @@ | ||
import { readFile } from "node:fs/promises"; | ||
import { join } from "node:path"; | ||
|
||
import { log } from "@acdh-oeaw/lib"; | ||
import type { NextRequest } from "next/server"; | ||
import { z } from "zod"; | ||
|
||
import type { Resource } from "@/lib/content/types"; | ||
|
||
const filePath = join(process.cwd(), "./public/metadata/curricula.json"); | ||
|
||
const searchParamsSchema = z.object({ | ||
limit: z.coerce.number().int().positive().max(100).optional().default(10), | ||
offset: z.coerce.number().int().nonnegative().optional().default(0), | ||
}); | ||
|
||
export async function GET(request: NextRequest) { | ||
const { searchParams } = request.nextUrl; | ||
|
||
const result = searchParamsSchema.safeParse({ | ||
limit: searchParams.get("limit"), | ||
offset: searchParams.get("offset"), | ||
}); | ||
|
||
if (!result.success) { | ||
return Response.json({ message: "Bad request" }, { status: 400 }); | ||
} | ||
|
||
const { offset, limit } = result.data; | ||
|
||
try { | ||
const curricula = await readCurricula(); | ||
|
||
return Response.json({ | ||
curricula: curricula.slice(offset, offset + limit), | ||
offset, | ||
limit, | ||
total: curricula.length, | ||
}); | ||
} catch (error) { | ||
log.error(error); | ||
return Response.json({ message: "Internal server error" }, { status: 500 }); | ||
} | ||
} | ||
|
||
const cache = { curricula: null as Array<Resource> | null }; | ||
|
||
async function readCurricula() { | ||
if (cache.curricula != null) return cache.curricula; | ||
|
||
const fileContent = await readFile(filePath, { encoding: "utf-8" }); | ||
const { curricula } = JSON.parse(fileContent) as { curricula: Array<Resource> }; | ||
|
||
// eslint-disable-next-line require-atomic-updates | ||
cache.curricula = curricula; | ||
|
||
return curricula; | ||
} |
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,58 @@ | ||
import { readFile } from "node:fs/promises"; | ||
import { join } from "node:path"; | ||
|
||
import { log } from "@acdh-oeaw/lib"; | ||
import type { NextRequest } from "next/server"; | ||
import { z } from "zod"; | ||
|
||
import type { Resource } from "@/lib/content/types"; | ||
|
||
const filePath = join(process.cwd(), "./public/metadata/resources.json"); | ||
|
||
const searchParamsSchema = z.object({ | ||
limit: z.coerce.number().int().positive().max(100).optional().default(10), | ||
offset: z.coerce.number().int().nonnegative().optional().default(0), | ||
}); | ||
|
||
export async function GET(request: NextRequest) { | ||
const { searchParams } = request.nextUrl; | ||
|
||
const result = searchParamsSchema.safeParse({ | ||
limit: searchParams.get("limit"), | ||
offset: searchParams.get("offset"), | ||
}); | ||
|
||
if (!result.success) { | ||
return Response.json({ message: "Bad request" }, { status: 400 }); | ||
} | ||
|
||
const { offset, limit } = result.data; | ||
|
||
try { | ||
const resources = await readResources(); | ||
|
||
return Response.json({ | ||
resources: resources.slice(offset, offset + limit), | ||
offset, | ||
limit, | ||
total: resources.length, | ||
}); | ||
} catch (error) { | ||
log.error(error); | ||
return Response.json({ message: "Internal server error" }, { status: 500 }); | ||
} | ||
} | ||
|
||
const cache = { resources: null as Array<Resource> | null }; | ||
|
||
async function readResources() { | ||
if (cache.resources != null) return cache.resources; | ||
|
||
const fileContent = await readFile(filePath, { encoding: "utf-8" }); | ||
const { resources } = JSON.parse(fileContent) as { resources: Array<Resource> }; | ||
|
||
// eslint-disable-next-line require-atomic-updates | ||
cache.resources = resources; | ||
|
||
return resources; | ||
} |
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,14 @@ | ||
import { log } from "@acdh-oeaw/lib"; | ||
|
||
async function generate() { | ||
// | ||
} | ||
|
||
generate() | ||
.then(() => { | ||
log.success("Successfully generated metadata dump."); | ||
}) | ||
.catch((error) => { | ||
log.error("Failed to generate metadata dump.", String(error)); | ||
process.exitCode = 1; | ||
}); |