Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hono openapi type issue #123

Merged
merged 9 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/api-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ jobs:

- name: 🛠️ Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 9

- name: 🛠️ Set up Node.js
uses: actions/setup-node@v4
Expand Down
3 changes: 1 addition & 2 deletions apps/admin/app/(dashboard)/dashboard/posts/details/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { Separator } from '@repo/ui/components/ui/separator';
import { MoreHorizontal } from 'lucide-react';
import { useSearchParams } from 'next/navigation';
import { useRouter } from 'next/navigation';
import React, { Suspense, useEffect, useState } from 'react';
import { Suspense, useEffect, useState } from 'react';

type Post = {
id: number;
Expand Down Expand Up @@ -160,7 +160,6 @@ function ShowReports() {
return response.json();
})
.then((response) => {
console.log(response);
router.push('/dashboard/posts');
});

Expand Down
1 change: 0 additions & 1 deletion apps/admin/app/ui/CollapsedNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Button } from '@repo/ui/components/ui/button';
import { Sheet, SheetContent, SheetTrigger } from '@repo/ui/components/ui/sheet';
import { Home, LineChart, Menu, Package, Package2, ShoppingCart, Users } from 'lucide-react';
import Link from 'next/link';
import React from 'react';

function CollapsedNav() {
return (
Expand Down
2 changes: 0 additions & 2 deletions apps/admin/app/ui/dashboard/activities/AddActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ function AddActivity({ activities, setActivities, addresses, sports }: Props): J
async function submit(values: z.infer<typeof formSchema>) {
const urlApi = process.env.NEXT_PUBLIC_API_URL;

console.log(values);

if (values.recurrence === 'weekly') {
values.start_time = new Date(values.start_time);
values.end_time = new Date(values.end_time);
Expand Down
1 change: 0 additions & 1 deletion apps/admin/app/ui/dashboard/posts/PostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Card, CardFooter, CardHeader, CardTitle } from '@repo/ui/components/ui/
import { Separator } from '@repo/ui/components/ui/separator';
import { CircleAlert, MessageCircle } from 'lucide-react';
import Link from 'next/link';
import React from 'react';

type Post = {
id: number;
Expand Down
1 change: 0 additions & 1 deletion apps/admin/app/ui/dashboard/posts/PostsList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import PostCard from '@/app/ui/dashboard/posts/PostCard';
import React from 'react';

type Post = {
id: number;
Expand Down
2 changes: 1 addition & 1 deletion apps/admin/app/ui/dashboard/posts/details/ReportRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from '@repo/ui/components/ui/dropdown-menu';
import { TableCell, TableRow } from '@repo/ui/components/ui/table';
import { MoreHorizontal } from 'lucide-react';
import React, { useState } from 'react';
import { useState } from 'react';

type Report = {
id: number;
Expand Down
1 change: 0 additions & 1 deletion apps/admin/app/ui/dashboard/posts/details/ReportsList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ReportRow from '@/app/ui/dashboard/posts/details/ReportRow';
import { Table, TableBody, TableHead, TableHeader, TableRow } from '@repo/ui/components/ui/table';
import React from 'react';

type Report = {
id: number;
Expand Down
1 change: 0 additions & 1 deletion apps/admin/app/ui/dashboard/users/EditForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ function EditForm(props: EditFormProps): JSX.Element {
roles.map((role) => ({ id: role.id, name: role.label })).filter((role) => values.roles?.includes(role.id)),
);

console.log('testo');
toast.success('Succès', { duration: 2000, description: 'Utilisateur modifié avec succès' });

props.closeDialog();
Expand Down
6 changes: 3 additions & 3 deletions apps/admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
"types:check": "tsc --noEmit"
},
"dependencies": {
"@hookform/resolvers": "^3.3.4",
"@hookform/resolvers": "^3.4.0",
"@repo/ui": "workspace:*",
"date-fns": "^3.6.0",
"lucide-react": "^0.378.0",
"next": "^14.2.3",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-hook-form": "^7.51.4",
"sharp": "^0.33.3",
"sharp": "^0.33.4",
"zod": "^3.23.8"
},
"devDependencies": {
"@repo/biome-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@types/node": "^20.12.11",
"@types/node": "^20.12.12",
"@types/react": "^18.3.2",
"@types/react-dom": "^18.3.0",
"autoprefixer": "^10.4.19",
Expand Down
12 changes: 6 additions & 6 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
"dependencies": {
"@hono/node-server": "^1.11.1",
"@hono/swagger-ui": "^0.2.2",
"@hono/zod-openapi": "^0.11.1",
"@hono/zod-openapi": "0.12.1",
"@hono/zod-validator": "^0.2.1",
"@repo/types": "workspace:*",
"@supabase/supabase-js": "^2.43.1",
"hono": "^4.3.4",
"@supabase/supabase-js": "^2.43.2",
"hono": "^4.3.7",
"zod": "^3.23.8",
"zod-validation-error": "^3.3.0"
},
"devDependencies": {
"@repo/biome-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@types/node": "^20.12.11",
"@types/node": "^20.12.12",
"@types/swagger-ui-dist": "^3.30.4",
"supabase": "^1.165.0",
"tsx": "^4.9.5",
"supabase": "^1.167.4",
"tsx": "^4.10.4",
"typescript": "^5.4.5",
"vitest": "^1.6.0"
}
Expand Down
12 changes: 4 additions & 8 deletions apps/api/src/handlers/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,11 @@ activities.openapi(getAllActivities, async (c) => {
const { data, error, count } = await query;

if (error) {
return c.json({ error: error.message }, 500);
return c.json({ error: 'Failed to fetch activities' }, 500);
}

const cleanData = data.map((activity) => {
const { id_sport, ...cleaned } = activity;
return cleaned;
});

const responseData = {
data: cleanData || [],
data: data || [],
count: count || 0,
};

Expand All @@ -57,7 +52,8 @@ activities.openapi(getAllActivities, async (c) => {

activities.openapi(getOneActivity, async (c) => {
const { id } = c.req.valid('param');
const { data, error } = await supabase.from('ACTIVITIES').select('*').eq('id', id).single();

const { data, error } = await supabase.from('ACTIVITIES').select('*, sport:SPORTS (id,name)').eq('id', id).single();

if (error || !data) {
return c.json({ error: 'Activity not found' }, 404);
Expand Down
11 changes: 6 additions & 5 deletions apps/api/src/handlers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ auth.openapi(signupUser, async (c) => {

const { data: userExist } = await supabase.from('USERS').select('id').eq('email', email);
if (userExist && userExist.length > 0) {
return c.json({ message: 'Email already exists' }, 400);
return c.json({ error: 'Email already exists' }, 400);
}

const { data, error } = await supabase.auth.signUp({
Expand All @@ -25,7 +25,7 @@ auth.openapi(signupUser, async (c) => {
});

if (error || !data?.user?.email) {
return c.json({ message: 'Error while creating user' }, 400);
return c.json({ error: 'Error while creating user' }, 400);
}

const { data: user, error: insertError } = await supabase
Expand All @@ -39,12 +39,13 @@ auth.openapi(signupUser, async (c) => {
date_validity: null,
id_auth: data.user.id,
created_at: new Date().toISOString(),
deleted_at: null,
})
.select()
.single();

if (insertError) {
return c.json({ message: 'Error while creating user' }, 400);
return c.json({ error: 'Error while creating user' }, 400);
}

return c.json(user, 201);
Expand Down Expand Up @@ -105,7 +106,7 @@ auth.openapi(refreshTokens, async (c) => {
});
}

return c.json({ message: 'Token refreshed' });
return c.json({ message: 'Token refreshed' }, 200);
});

auth.openapi(logoutUser, async (c) => {
Expand All @@ -119,5 +120,5 @@ auth.openapi(logoutUser, async (c) => {
}
}

return c.json({ message: 'Logged out' });
return c.json({ message: 'Logged out' }, 200);
});
21 changes: 12 additions & 9 deletions apps/api/src/handlers/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,20 @@ blog.openapi(getAllPosts, async (c) => {
const finalData = data.map((row) => {
return {
...row,
categories: row.categories.map((category) => ({
id: category.CATEGORIES?.id,
name: category.CATEGORIES?.name,
})),
comments_number: row.comments_number[0]?.count,
views_number: row.views_number[0]?.count,
likes_number: row.likes_number[0]?.count,
author: row.author ? { id: row.author.id, username: row.author.username } : null,
categories: row.categories
? row.categories.map((category) => ({
id: category.CATEGORIES?.id ?? null,
name: category.CATEGORIES?.name ?? null,
}))
: [],
comments_number: row.comments_number[0]?.count || 0,
views_number: row.views_number[0]?.count || 0,
likes_number: row.likes_number[0]?.count || 0,
};
});

return c.json({ data: finalData, count }, 200);
return c.json({ data: finalData, count: count || 0 }, 200);
});

blog.openapi(getPost, async (c) => {
Expand Down Expand Up @@ -243,7 +246,7 @@ blog.openapi(getComments, async (c) => {
return c.json({ error: "Failed to get comments, verify the post's id" }, 400);
}

return c.json({ data, count }, 200);
return c.json({ data, count: count || 0 }, 200);
});

blog.openapi(createResponse, async (c) => {
Expand Down
10 changes: 1 addition & 9 deletions apps/api/src/handlers/health.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { OpenAPIHono } from '@hono/zod-openapi';
import { supabase } from '../libs/supabase.js';
import { dbHealthCheck, healthCheck } from '../routes/health.js';
import { healthCheck } from '../routes/health.js';

export const health = new OpenAPIHono();

health.openapi(healthCheck, (c) => {
return c.json('OK', 200);
});

health.openapi(dbHealthCheck, (c) => {
if (supabase) {
return c.json('OK', 200);
}
return c.json('Failed', 500);
});
2 changes: 1 addition & 1 deletion apps/api/src/handlers/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ location.openapi(deleteAddress, async (c) => {
const { error } = await supabase.from('ADDRESSES').delete().eq('id', id);

if (error) {
return c.json({ error: error.message }, 500);
return c.json({ error: 'Address not found' }, 404);
}

return c.json({ message: 'Address deleted' }, 200);
Expand Down
30 changes: 22 additions & 8 deletions apps/api/src/handlers/matches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ matches.openapi(getAllMatches, async (c) => {

const query = supabase
.from('MATCHES')
.select('*, winner:TEAMS_MATCHES(winner)', { count: 'exact' })
.select('*, winner:TEAMS_MATCHES(winner, id_team)', { count: 'exact' })
.order('id', { ascending: true });

if (search) {
Expand All @@ -42,8 +42,13 @@ matches.openapi(getAllMatches, async (c) => {
return c.json({ error: error.message }, 500);
}

const formattedData = data.map((item) => ({
...item,
winner: item.winner.length > 0 ? (item.winner[0] ? item.winner[0] : null) : null,
}));

const responseData = {
data: data || [],
data: formattedData || [],
count: count || 0,
};

Expand All @@ -52,13 +57,22 @@ matches.openapi(getAllMatches, async (c) => {

matches.openapi(getMatchById, async (c) => {
const { id } = c.req.valid('param');
const { data, error } = await supabase.from('MATCHES').select('*').eq('id', id).single();
const { data, error } = await supabase
.from('MATCHES')
.select('*, winner:TEAMS_MATCHES(winner, id_team)')
.eq('id', id)
.single();

if (error || !data) {
return c.json({ error: 'Match not found' }, 404);
}

return c.json(data, 200);
const formattedData = {
...data,
winner: data.winner.length > 0 ? (data.winner[0] ? data.winner[0] : null) : null,
};

return c.json(formattedData, 200);
});

matches.openapi(createMatch, async (c) => {
Expand All @@ -67,7 +81,8 @@ matches.openapi(createMatch, async (c) => {
const roles = user.roles;
await checkRole(roles, false);

if (start_time >= end_time) return c.json({ error: 'Start time must be before end time' }, 400);
if (start_time && end_time && start_time >= end_time)
return c.json({ error: 'Start time must be before end time' }, 400);

const { data, error } = await supabase.from('MATCHES').insert({ start_time, end_time }).select().single();

Expand All @@ -85,17 +100,16 @@ matches.openapi(updateMatchWinner, async (c) => {
const roles = user.roles;
await checkRole(roles, false);

const { data, error } = await supabase.from('TEAMS_MATCHES').update({ id_team: idTeam, winner }).eq('id_match', id);
const { error } = await supabase.from('TEAMS_MATCHES').update({ id_team: idTeam, winner }).eq('id_match', id);

if (error) {
return c.json({ error: error.message }, 500);
}

return c.json(data, 200);
return c.json({ message: 'Match winner updated' }, 200);
});

matches.openapi(updateMatch, async (c) => {
console.log('GROS NIQUE');
const { id } = c.req.valid('param');
const { start_time, end_time } = c.req.valid('json');
const user = c.get('user');
Expand Down
16 changes: 13 additions & 3 deletions apps/api/src/handlers/reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ reports.openapi(getReports, async (c) => {
const { data, error, count } = await query;

if (error) {
return c.json({ error: 'Failed to get reports' }, 400);
return c.json({ error: 'Failed to get reports' }, 500);
}

return c.json({ data, count }, 200);
const responseData = {
data: data || [],
count: count || 0,
};

return c.json(responseData, 200);
});

reports.openapi(createReport, async (c) => {
Expand Down Expand Up @@ -106,7 +111,12 @@ reports.openapi(getReportComments, async (c) => {
return c.json({ error: 'Failed to get comments' }, 400);
}

return c.json({ data, count }, 200);
const responseData = {
data: data || [],
count: count || 0,
};

return c.json(responseData, 200);
});

reports.openapi(createReportComment, async (c) => {
Expand Down
Loading
Loading