refactor: migrate from @izzyjs/route to @tuyau

This commit is contained in:
Sonny
2024-10-28 01:05:45 +01:00
parent 05c867f44f
commit c308262ee0
44 changed files with 294 additions and 235 deletions

View File

@@ -1,16 +1,12 @@
import { searchTermValidator } from '#validators/search_term';
import type { HttpContext } from '@adonisjs/core/http';
import db from '@adonisjs/lucid/services/db';
export default class SearchesController {
async search({ request, auth }: HttpContext) {
const term = request.qs()?.term;
if (!term) {
console.warn('qs term null');
return { error: 'missing "term" query param' };
}
const { searchTerm } = await request.validateUsing(searchTermValidator);
const { rows } = await db.rawQuery('SELECT * FROM search_text(?, ?)', [
term,
searchTerm,
auth.user!.id,
]);
return { results: rows };

View File

@@ -1,8 +1,8 @@
import User from '#models/user';
import { RouteName } from '#types/tuyau';
import type { HttpContext } from '@adonisjs/core/http';
import logger from '@adonisjs/core/services/logger';
import db from '@adonisjs/lucid/services/db';
import { RouteName } from '@izzyjs/route/types';
export default class UsersController {
private redirectTo: RouteName = 'auth.login';

22
app/lib/tuyau.ts Normal file
View File

@@ -0,0 +1,22 @@
import { api } from '#adonisjs/api';
import { QueryParams } from '#types/query_params';
import { RouteName } from '#types/tuyau';
export const getRoute = (routeName: RouteName, options?: QueryParams) => {
const current = api.routes.find((route) => route.name === routeName);
if (!current) {
throw new Error(`Route ${routeName} not found`);
}
if (options?.qs) {
const searchParams = new URLSearchParams(options?.qs);
return { ...current, path: `${current.path}?${searchParams.toString()}` };
}
return current;
};
export function getPath(routeName: RouteName, options?: QueryParams) {
const current = getRoute(routeName, options);
return current.path;
}

View File

@@ -1,7 +1,7 @@
import { getPath } from '#lib/tuyau';
import type { Authenticators } from '@adonisjs/auth/types';
import type { HttpContext } from '@adonisjs/core/http';
import type { NextFn } from '@adonisjs/core/types/http';
import { route } from '@izzyjs/route/client';
/**
* Auth middleware is used authenticate HTTP requests and deny
@@ -11,7 +11,7 @@ export default class AuthMiddleware {
/**
* The URL to redirect to, when authentication fails
*/
redirectTo = route('auth.login').url;
redirectTo = getPath('auth.login');
async handle(
ctx: HttpContext,

View File

@@ -0,0 +1 @@
export type QueryParams = { qs?: Record<string, any> };

4
app/types/tuyau.ts Normal file
View File

@@ -0,0 +1,4 @@
import { api } from '#adonisjs/api';
export type RouteName = (typeof api)['routes'][number]['name'];
export type RouteParams = (typeof api)['routes'][number]['params'];

View File

@@ -0,0 +1,7 @@
import vine from '@vinejs/vine';
export const searchTermValidator = vine.compile(
vine.object({
searchTerm: vine.string().trim().minLength(1).maxLength(255),
})
);