I'm trying to process this url...
http://localhost:3000/api/search?term=stuff
I want to use getQuery to retrieve the search term. The below code works fine but it is massively clumsy. Is there a more elegant way to just get a string out of the query string and have it be type safe?
server/api/search.ts
export default defineEventHandler(async (event: H3Event) => {
const queryParams = getQuery(event)
let term: string = '';
if(queryParams.term){
if (Array.isArray( queryParams.term)) {
term = queryParams.term[0];
} else {
term = queryParams.term.toString();
}
}
//.... use the term variable to do stuff...
});