Skip to content

Commit

Permalink
Merge pull request #5 from waonpad/4-add-api-endpoints
Browse files Browse the repository at this point in the history
4 add api endpoints
  • Loading branch information
waonpad authored Nov 28, 2023
2 parents c569506 + 80de556 commit e1abf90
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 9 deletions.
20 changes: 20 additions & 0 deletions apps/web/src/app/api/channels/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';
import { youtubeApi } from '@/lib/youtube-ts';
import { reqSearchParams } from '@/utils/req-search-params';

export const GetChannelByURLSchema = z.object({
url: z.string().startsWith('https://www.youtube.com/'),
});

export async function GET(req: NextRequest) {
const parsed = GetChannelByURLSchema.safeParse(reqSearchParams(req));

if (!parsed.success) {
return NextResponse.json({ error: parsed.error }, { status: 400 });
}

const channel = await youtubeApi.channels.get(parsed.data.url);

return NextResponse.json(channel);
}
20 changes: 20 additions & 0 deletions apps/web/src/app/api/videos/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';
import { youtubeApi } from '@/lib/youtube-ts';
import { reqSearchParams } from '@/utils/req-search-params';

export const GetVideoByURLSchema = z.object({
url: z.string().startsWith('https://www.youtube.com/watch?v='),
});

export async function GET(req: NextRequest) {
const parsed = GetVideoByURLSchema.safeParse(reqSearchParams(req));

if (!parsed.success) {
return NextResponse.json({ error: parsed.error }, { status: 400 });
}

const video = await youtubeApi.videos.get(parsed.data.url);

return NextResponse.json(video);
}
28 changes: 28 additions & 0 deletions apps/web/src/app/api/videos/search/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';
import { youtubeApi } from '@/lib/youtube-ts';
import { reqSearchParams } from '@/utils/req-search-params';

/**
* @see https://github.com/Tenpi/youtube.ts/blob/master/types/SearchTypes.ts#L3
* @description
* This schema ensures only the necessary elements from the above link types \
* If you edit this schema, please make sure to meet the above link types
*/
export const SearchVideoSchema = z.object({
q: z.string().optional(),
maxResults: z.number().max(50).default(10),
pageToken: z.string().optional(),
});

export async function GET(req: NextRequest) {
const parsed = SearchVideoSchema.safeParse(reqSearchParams(req));

if (!parsed.success) {
return NextResponse.json({ error: parsed.error }, { status: 400 });
}

const channel = await youtubeApi.videos.search(parsed.data);

return NextResponse.json(channel);
}
14 changes: 7 additions & 7 deletions apps/web/src/services/fetcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { HttpResponse } from './types';
export const fetcher = <T>(input: RequestInfo, init?: RequestInit) => {
return fetch(input, init)
.then(transformResponse<T>())
.catch((err) => {
console.log('fetcher: err', err);
throw err;
.catch((error) => {
console.log('fetcher: error', error);
throw error;
});
};

Expand All @@ -18,14 +18,14 @@ export const transformResponse = <T>() => {

if (res.status === 422) {
const json: {
err: ZodError;
error: ZodError;
} = await res.json(); // ZodErrorで一時的に型を固定

const response: HttpResponse<T> = {
data: null,
err: {
error: {
...errors['VALIDATION'],
errors: json.err.issues.map((issue) => ({
errors: json.error.issues.map((issue) => ({
code: issue.code,
name: `${issue.path[0]}`,
message: issue.message,
Expand All @@ -50,7 +50,7 @@ export const transformResponse = <T>() => {

return {
data: json,
err: null,
error: null,
status: res.status,
};
};
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/services/fetcher/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type HttpResponse<T> = {
data: T | null;
err: Err | null;
error: Err | null;
status: number;
};

Expand All @@ -18,6 +18,6 @@ export type Err = {

export type ErrResponse = {
data: null;
err: Err;
error: Err;
status: number;
};
6 changes: 6 additions & 0 deletions apps/web/src/utils/req-search-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { NextRequest } from 'next/server';

export const reqSearchParams = (req: NextRequest) => {
const { searchParams } = new URL(req.url);
return Object.fromEntries(searchParams.entries());
};

0 comments on commit e1abf90

Please sign in to comment.