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

✨ 사용자 검색 모킹 API Handler 추가 #14

Merged
merged 4 commits into from
Oct 31, 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
15 changes: 4 additions & 11 deletions src/app/mocks/data/feed.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { IUser, userMockData } from './user';

interface IImage {
id: number;
imageUrl: string;
}

interface IFeed {
id: number;

user: {
id: number;
profileImage: string;
username: string;
};
user: IUser;

content: string;
images: IImage[];
Expand Down Expand Up @@ -56,11 +53,7 @@ function generateFeedMockData(count: number) {
for (let id = 1; id <= count; id++) {
const feed = {
id,
user: {
id: id,
profileImage: `https://randomuser.me/api/portraits/${Math.random() > 0.5 ? 'men' : 'women'}/${id}.jpg`,
username: `user_${id}`,
},
user: userMockData[id - 1],
content: generateRandomContent(Math.floor(Math.random() * 300)),
images: generateRandomImages(),
likeCount: Math.floor(Math.random() * 1000),
Expand Down
1 change: 1 addition & 0 deletions src/app/mocks/data/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './feed';
export * from './user';
28 changes: 28 additions & 0 deletions src/app/mocks/data/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export interface IUser {
id: number;
profileImage: string;
username: string;
}

function generateRandomUsername(id: number) {
const prefixes = ['user', 'player', 'guest', 'member', 'star'];
return prefixes[Math.floor(Math.random() * prefixes.length)] + id;
}

function generateUserMockData(count: number) {
const mockUsers: IUser[] = [];

for (let id = 1; id <= count; id++) {
const user = {
id,
profileImage: `https://randomuser.me/api/portraits/${Math.random() > 0.5 ? 'men' : 'women'}/${id - 1}.jpg`,
username: generateRandomUsername(id),
};

mockUsers.push(user);
}

return mockUsers;
}

export const userMockData = generateUserMockData(100);
43 changes: 39 additions & 4 deletions src/app/mocks/handlers/search.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import Fuse from 'fuse.js';
import { http } from 'msw';

import { feedMockData } from '../data';
import { feedMockData, userMockData } from '../data';
import { createHttpErrorResponse, createHttpSuccessResponse } from '../lib';

export const searchHandlers = [
// 1️⃣ 피드 검색
http.get('http://api.example.com/v2/feeds', ({ request }) => {
const url = new URL(request.url);

const query = url.searchParams.get('query');
const target = url.searchParams.get('target');
const page = Number(url.searchParams.get('page') || 1);
const size = Number(url.searchParams.get('size') || 15);

if (!query || query.length < 2) {
if (!target || target.length < 2) {
return createHttpErrorResponse('검색어는 최소 2글자 이상이어야 합니다');
}

Expand All @@ -21,7 +22,7 @@ export const searchHandlers = [
});

const contents = fuse
.search(query)
.search(target)
.map((result) => result.item)
.slice((page - 1) * size, page * size);
const totalFeeds = contents.length;
Expand All @@ -37,4 +38,38 @@ export const searchHandlers = [
},
});
}),

// 2️⃣ 사용자 검색
http.get('http://api.example.com/v2/users', ({ request }) => {
const url = new URL(request.url);

const target = url.searchParams.get('target');
const page = Number(url.searchParams.get('page') || 1);
const size = Number(url.searchParams.get('size') || 15);

if (!target || target.length < 2) {
return createHttpErrorResponse('검색어는 최소 2글자 이상이어야 합니다');
}

const fuse = new Fuse(userMockData, {
keys: ['username'],
});

const contents = fuse
.search(target)
.map((result) => result.item)
.slice((page - 1) * size, page * size);
const totalFeeds = contents.length;
const hasNextPage = totalFeeds > page * size;

return createHttpSuccessResponse({
user: {
contents,
currentPageNumber: page,
pageSize: size,
numberOfElements: contents.length,
hasNextPage,
},
});
}),
BangDori marked this conversation as resolved.
Show resolved Hide resolved
];