Skip to content

Commit 71c099c

Browse files
authored
fix: tag로 글 불러오는 api를 v3로 변경 (#35)
* fix: tag로 글 불러오는 api를 v3로 변경 * refactor: 안쓰는 코드 삭제
1 parent 047dd36 commit 71c099c

File tree

7 files changed

+491
-31
lines changed

7 files changed

+491
-31
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"editor.tabSize": 2,
33
"editor.formatOnSave": true,
44
"editor.codeActionsOnSave": {
5-
"source.fixAll.eslint": true
5+
"source.fixAll.eslint": "explicit"
66
}
77
}

api/index.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import createCard from "../src/cards/new-log.js";
2-
import createCardDark from "../src/cards/new-log-black.js";
3-
import fetchPost from "../src/fetchers/post-fetcher.js";
4-
import fetchReadPost from "../src/fetchers/readpost-fetcher.js";
1+
import createCard from '../src/cards/new-log.js';
2+
import createCardDark from '../src/cards/new-log-black.js';
3+
import fetchPost from '../src/fetchers/post-fetcher.js';
4+
import fetchReadPost from '../src/fetchers/readpost-fetcher.js';
55

66
export default async (req, res) => {
77
const { name, tag, color, slug } = req.query;
8-
res.setHeader("Content-Type", "image/svg+xml");
8+
res.setHeader('Content-Type', 'image/svg+xml');
99
try {
10-
const post = !slug
11-
? await fetchPost(name, tag)
12-
: await fetchReadPost(name, slug);
13-
return res.send(color === "dark" ? createCardDark(post) : createCard(post));
10+
const post = !slug ? await fetchPost(name, tag) : await fetchReadPost(name, slug);
11+
return res.send(color === 'dark' ? createCardDark(post) : createCard(post));
1412
} catch (e) {
1513
return res.send(e.message);
1614
}

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import express from 'express';
2+
import post from './api/index.js';
3+
4+
const app = express();
5+
6+
app.get('/', post);
7+
8+
app.listen(3000);

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "./api/index.js",
66
"scripts": {
77
"start": "node ./api/index.js",
8+
"dev:local": "node ./index.js",
89
"dev:vercel": "vercel dev --debug",
910
"test": "echo \"Error: no test specified\" && exit 1"
1011
},
@@ -16,5 +17,7 @@
1617
"graphql-request": "^6.1.0"
1718
},
1819
"type": "module",
19-
"devDependencies": {}
20+
"devDependencies": {
21+
"express": "^4.19.2"
22+
}
2023
}

src/fetchers/post-fetcher.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { request } from "../utils/index.js";
1+
import { request } from '../utils/index.js';
22

33
const fetcher = (variables) => {
4-
return request({
5-
query: `
6-
query Posts($cursor: ID, $username: String, $temp_only: Boolean, $tag: String, $limit: Int) {
7-
posts(cursor: $cursor, username: $username, temp_only: $temp_only, tag: $tag, limit: $limit) {
4+
return request(
5+
{
6+
query: `
7+
query velogPosts($input: GetPostsInput!) {
8+
posts(input: $input) {
89
id
910
title
1011
short_description
@@ -24,13 +25,15 @@ const fetcher = (variables) => {
2425
}
2526
}
2627
`,
27-
variables,
28-
});
28+
variables,
29+
},
30+
3
31+
);
2932
};
3033

3134
async function fetchPost(name, tag) {
3235
try {
33-
const { data } = await fetcher({ username: name, limit: 1, tag: tag });
36+
const { data } = await fetcher({ input: { username: name, limit: 1, tag: tag } });
3437
return data.data.posts[0];
3538
} catch (e) {
3639
throw new Error(e);

src/utils/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import axios from "axios";
2-
import canvas from "@napi-rs/canvas";
1+
import axios from 'axios';
2+
import canvas from '@napi-rs/canvas';
33

4-
export function request(data) {
4+
export function request(data, version = 2) {
55
return axios({
6-
url: "https://v2.velog.io/graphql",
7-
method: "post",
6+
url: `https://v${version}.velog.io/graphql`,
7+
method: 'post',
88
data,
99
});
1010
}
@@ -20,19 +20,19 @@ export function replaceAll(str, searchStr, replaceStr) {
2020

2121
export function escapeHtml(text) {
2222
const map = {
23-
"&": "&",
24-
"<": "&lt;",
25-
">": "&gt;",
26-
'"': "&quot;",
27-
"'": "&#039;",
23+
'&': '&amp;',
24+
'<': '&lt;',
25+
'>': '&gt;',
26+
'"': '&quot;',
27+
"'": '&#039;',
2828
};
2929
return text.replace(/[&<>"']/g, function (m) {
3030
return map[m];
3131
});
3232
}
3333

3434
export function getTextWidth(text, font) {
35-
const ctx = canvas.createCanvas(1, 1).getContext("2d");
35+
const ctx = canvas.createCanvas(1, 1).getContext('2d');
3636
ctx.font = font;
3737
const textMetrics = ctx.measureText(text);
3838

0 commit comments

Comments
 (0)