Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
ajoulib-phrase
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
화균 김
ajoulib-phrase
Commits
1c66d8b3
Commit
1c66d8b3
authored
1 month ago
by
화균 김
Browse files
Options
Downloads
Patches
Plain Diff
feat: add api request code
parent
aae24dd9
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/api/request.ts
+23
-21
23 additions, 21 deletions
src/api/request.ts
src/components/ContentWrap/index.tsx
+4
-1
4 additions, 1 deletion
src/components/ContentWrap/index.tsx
with
27 additions
and
22 deletions
src/api/request.ts
+
23
−
21
View file @
1c66d8b3
...
...
@@ -3,14 +3,14 @@
import
{
cache
}
from
'
react
'
;
import
{
BookInfo
}
from
'
@/types
'
;
export
const
getSentence
=
cache
(
async
():
Promise
<
BookInfo
>
=>
{
re
turn
{
sentence
:
"
It isn't what you have or who you are or where you are or what you are doing that makes you happy or unhappy.
"
,
title
:
"
인간관계론
"
,
author
:
"
데일 카네기
"
,
location
:
"
1층, 신간자료실
"
,
code
:
"
158.1 C289hK
"
,
}
export
const
getSentence
=
cache
(
async
():
Promise
<
{
re
sult
:
true
;
data
:
BookInfo
;
}
|
{
result
:
false
;
data
?:
BookInfo
;
error
?:
string
;
}
>
=>
{
try
{
const
spreadsheetId
=
process
.
env
.
SHEET_ID
;
...
...
@@ -23,9 +23,9 @@ export const getSentence = cache(async (): Promise<BookInfo> => {
'
Content-Type
'
:
'
application/json
'
,
},
body
:
JSON
.
stringify
({
client_id
:
process
.
env
.
GOOGLE_CLIENT_ID
,
client_secret
:
process
.
env
.
GOOGLE_CLIENT_SECRET
,
refresh_token
:
process
.
env
.
GOOGLE_REFRESH_TOKEN
,
client_id
:
process
.
env
.
NEXT_
GOOGLE_CLIENT_ID
,
client_secret
:
process
.
env
.
NEXT_
GOOGLE_CLIENT_SECRET
,
refresh_token
:
process
.
env
.
NEXT_
GOOGLE_REFRESH_TOKEN
,
grant_type
:
'
refresh_token
'
,
}),
});
...
...
@@ -53,17 +53,19 @@ export const getSentence = cache(async (): Promise<BookInfo> => {
throw
new
Error
(
'
데이터가 없습니다.
'
);
}
// 데이터 가공
return
rows
.
slice
(
1
).
map
((
row
:
string
[])
=>
({
columnC
:
row
[
0
]
||
''
,
columnD
:
row
[
1
]
||
''
,
columnE
:
row
[
2
]
||
''
,
columnF
:
row
[
3
]
||
''
,
columnG
:
row
[
4
]
||
''
,
}));
const
randomIndex
=
Math
.
floor
(
Math
.
random
()
*
rows
.
length
)
%
rows
.
length
;
const
[
sentence
,
title
,
author
,
location
,
code
]
=
rows
[
randomIndex
];
return
{
result
:
true
,
data
:
{
sentence
,
title
,
author
,
location
,
code
}
}
}
catch
(
error
)
{
}
catch
(
error
:
any
)
{
console
.
error
(
'
Google Sheets API 요청 실패:
'
,
error
);
throw
error
;
return
{
result
:
false
,
error
:
error
?.
message
}
}
});
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/components/ContentWrap/index.tsx
+
4
−
1
View file @
1c66d8b3
...
...
@@ -16,7 +16,10 @@ export const ContentWrap = () => {
const
fetchSentence
=
async
()
=>
{
try
{
const
response
=
await
axios
.
get
(
'
/api/sentence
'
);
setBookInfo
(
response
?.
data
||
null
);
// 첫 번째 문장만 표시
const
{
result
,
data
,
error
}
=
response
?.
data
;
if
(
!
result
)
throw
new
Error
(
error
);
setBookInfo
(
data
);
// 첫 번째 문장만 표시
setLoading
(
false
);
}
catch
(
err
)
{
setError
(
'
문장을 불러오는데 실패했습니다.
'
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment