mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-11 08:43:04 +00:00
refactor: migrate from @izzyjs/route to @tuyau
This commit is contained in:
@@ -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 };
|
||||
|
||||
@@ -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
22
app/lib/tuyau.ts
Normal 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;
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
1
app/types/query_params.ts
Normal file
1
app/types/query_params.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type QueryParams = { qs?: Record<string, any> };
|
||||
4
app/types/tuyau.ts
Normal file
4
app/types/tuyau.ts
Normal 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'];
|
||||
7
app/validators/search_term.ts
Normal file
7
app/validators/search_term.ts
Normal 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),
|
||||
})
|
||||
);
|
||||
Reference in New Issue
Block a user