ref: better type support in server side script

fix: a typo in return message at api/database/song/[id]
This commit is contained in:
alikia2x 2024-07-25 02:36:17 +08:00
parent d0f562452a
commit a23a52f5e7
5 changed files with 20 additions and 17 deletions

View File

@ -1,6 +1,6 @@
{
"name": "aquavox",
"version": "1.10.2",
"version": "1.12.13",
"private": false,
"scripts": {
"dev": "vite dev",

View File

@ -1,27 +1,28 @@
import { songNameCache } from '$lib/server/cache.js';
import { loadData } from '$lib/server/database/loadData';
import { json, error } from '@sveltejs/kit';
import { error, json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
export async function GET({ url }) {
const keyword = url.searchParams.get("keyword");
export const GET: RequestHandler = async ({ url }) => {
const keyword = url.searchParams.get('keyword');
loadData();
await loadData();
if (keyword === null) {
return error(400, {
"message": "Miss parameter: keyword"
})
'message': 'Miss parameter: keyword'
});
}
const resultList: MusicMetadata[] = [];
for (const songName of songNameCache.keys()){
for (const songName of songNameCache.keys()) {
if (songName.toLocaleLowerCase().includes(keyword.toLocaleLowerCase())) {
resultList.push(songNameCache.get(songName)!);
}
}
return json({
"result": resultList
return json({
'result': resultList
});
}
};

View File

@ -1,19 +1,20 @@
import { getCurrentFormattedDateTime } from '$lib/songUpdateTime';
import { json, error } from '@sveltejs/kit';
import fs from 'fs';
import type { RequestHandler } from './$types';
export async function GET({ params }) {
export const GET: RequestHandler = async ({ params }) => {
const filePath = `./data/song/${params.id}.json`;
if (!fs.existsSync(filePath)) {
return error(404, {
message: "No correspoding song."
message: "No corresponding song."
})
}
const data = fs.readFileSync(filePath);
return json(JSON.parse(data.toString()));
}
export async function POST({ params, request }) {
export const POST: RequestHandler = async ({ request, params }) => {
const timeStamp = new Date().getTime();
if (!fs.existsSync("./data/pending/")) {
fs.mkdirSync("./data/pending");

View File

@ -1,8 +1,9 @@
import { songData } from '$lib/server/cache.js';
import { loadData } from '$lib/server/database/loadData.js';
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
export async function GET({ url }) {
export const GET: RequestHandler = async ({ url }) => {
const limit = parseInt(url.searchParams.get("limit") ?? "20");
const offset = parseInt(url.searchParams.get("offset") ?? "0");
loadData();

View File

@ -1,8 +1,8 @@
/** @type {import('./$types').PageLoad} */
import type { PageServerLoad } from './$types';
import fs from 'fs';
export function load({ params }) {
export const load: PageServerLoad = ({ params }) => {
const filePath = `./data/song/${params.id}.json`;
if (!fs.existsSync(filePath)) {
return {