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

feat: 장소 상세 api bff로 마이그래이션 #212

Merged
merged 1 commit into from
Oct 24, 2023
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
52 changes: 52 additions & 0 deletions src/app/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { paramsSerializer } from '@/lib/apis';

const BATCH_SIZE = 10;
const DELAY = 1000;

export const fetchNaverSearchBlog = async ({
query, includePost,
}: { query: string; includePost: boolean; }) => {
const response = await fetch(`${process.env.NEXT_PUBLIC_ORIGIN}/api/naver/search?${paramsSerializer({
query,
include_post: includePost,
})}`);

if (response.ok) {
const searchResult = await response.json();

return searchResult;
}

return null;
};

export const fetchAllSettledSearchNaverBlogs = async ({
placeName,
}: {
placeName: string[];
}) => {
const copyPlaceName = [...placeName];
const firstPlaceName = copyPlaceName.splice(0, BATCH_SIZE);

const firstResponse = await Promise
.allSettled([...firstPlaceName.map((query) => fetchNaverSearchBlog({
query,
includePost: false,
}))]);

if (placeName.length <= 10) {
return firstResponse;
}

await new Promise((resolve) => {
setTimeout(resolve, DELAY);
});

const secondResponse = await Promise
.allSettled([...copyPlaceName.map((query) => fetchNaverSearchBlog({
query,
includePost: false,
}))]);

return [...firstResponse, ...secondResponse];
};
77 changes: 77 additions & 0 deletions src/app/api/search/places/detail/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { NextRequest, NextResponse } from 'next/server';

import { PlaceDetailsResponseData } from '@googlemaps/google-maps-services-js';

import { fetchNaverSearchBlog } from '@/app/api';
import { paramsSerializer } from '@/lib/apis';
import { PlaceDetail } from '@/lib/types/google.maps';

export const runtime = 'edge';

const TEN_MINUTES = 600;

const hasPlaceId = (
placeDetail: PlaceDetailsResponseData,
): placeDetail is PlaceDetail => {
if (
!placeDetail?.result
|| !placeDetail.result?.geometry?.location
|| !placeDetail.result?.place_id
|| !placeDetail.result?.name
) {
return false;
}

return true;
};

export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const requestHeaders = new Headers(request.headers);

const placeId = searchParams.get('placeId');

const searchResponse = await fetch(`${process.env.NEXT_PUBLIC_ORIGIN}/api/google/search/detail?${paramsSerializer({
placeId,
})}`, {
method: 'GET',
next: {
revalidate: TEN_MINUTES,
},
headers: requestHeaders,
});

if (!searchResponse.ok) {
return NextResponse.json(null, searchResponse);
}

const placeDetail = await searchResponse.json() as PlaceDetailsResponseData;

if (!hasPlaceId(placeDetail)) {
return NextResponse.json(null, {
headers: requestHeaders,
status: 400,
statusText: 'not found place name',
});
}

const searchBlogPost = await fetchNaverSearchBlog({
query: placeDetail.result.name, includePost: true,
});

return NextResponse.json({
...placeDetail,
result: {
...placeDetail.result,
searchBlogPost,
},
}, {
...searchResponse,
headers: {
...searchResponse.headers,
'Cache-Control': 'public, s-maxage=1',
'CDN-Cache-Control': 'public, s-maxage=60',
'Vercel-CDN-Cache-Control': `public, s-maxage=${TEN_MINUTES}`,
},
});
}
52 changes: 2 additions & 50 deletions src/app/api/search/places/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,11 @@ import { checkEmpty } from '@nf-team/core';
import { paramsSerializer } from '@/lib/apis';
import { filteredPlaces } from '@/utils';

import { fetchAllSettledSearchNaverBlogs } from '../..';

export const runtime = 'edge';

const TEN_MINUTES = 600;
const BATCH_SIZE = 10;
const DELAY = 1000;

const fetchNaverSearchBlog = async ({
query, includePost,
}: { query: string; includePost: boolean; }) => {
const response = await fetch(`${process.env.NEXT_PUBLIC_ORIGIN}/api/naver/search?${paramsSerializer({
query,
include_post: includePost,
})}`);

if (response.ok) {
const searchResult = await response.json();

return searchResult;
}

return null;
};

const fetchAllSettledSearchNaverBlogs = async ({
placeName,
}: {
placeName: string[];
}) => {
const copyPlaceName = [...placeName];
const firstPlaceName = copyPlaceName.splice(0, BATCH_SIZE);

const firstResponse = await Promise
.allSettled([...firstPlaceName.map((query) => fetchNaverSearchBlog({
query,
includePost: false,
}))]);

if (placeName.length <= 10) {
return firstResponse;
}

await new Promise((resolve) => {
setTimeout(resolve, DELAY);
});

const secondResponse = await Promise
.allSettled([...copyPlaceName.map((query) => fetchNaverSearchBlog({
query,
includePost: false,
}))]);

return [...firstResponse, ...secondResponse];
};

export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/apis/search/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { api, paramsSerializer } from '..';

import {
NaverSearchBlogResponse, PlaceDetailResponse, SearchPlacesResponse, TextSearchPlaceResponse,
NaverSearchBlogResponse, PlaceDetailsResponse, SearchPlacesResponse, TextSearchPlaceResponse,
} from './model';

const BATCH_SIZE = 10;
Expand Down Expand Up @@ -92,7 +92,7 @@ export const fetchSearchPlaces = async ({
export const fetchPlaceDetail = async ({
placeId, sessionToken,
}: { placeId: string; sessionToken?: string; }) => {
const response = await api<PlaceDetailResponse>({
const response = await api<PlaceDetailsResponse>({
method: 'GET',
url: '/google/search/detail',
params: {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/apis/search/model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { PlaceDetailsResponseData, TextSearchResponseData } from '@googlemaps/google-maps-services-js';
import { TextSearchResponseData } from '@googlemaps/google-maps-services-js';

import { PlaceDetail } from '@/lib/types/google.maps';
import { NaverSearchBlog, SearchPlaces } from '@/lib/types/search';

export type NaverSearchBlogResponse<T> = NaverSearchBlog<T>;

export type TextSearchPlaceResponse = TextSearchResponseData;

export type PlaceDetailResponse = PlaceDetailsResponseData;
export type PlaceDetailsResponse = PlaceDetail;

export type SearchPlacesResponse = SearchPlaces;
Loading