-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from Bear29ers/feature/#122_get_data_from_ins…
…tagram Feature/#122 Get data from instagram(Instagramの情報を取得する)
- Loading branch information
Showing
9 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,9 @@ yarn-error.log* | |
# local env files | ||
.env*.local | ||
|
||
# docker-compose | ||
docker-compose.yml | ||
|
||
# vercel | ||
.vercel | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
import fetchJson from '@/libs/fetchJson'; | ||
|
||
import type { Media } from '@/types/media'; | ||
|
||
export const GET = async () => { | ||
const baseUrl = process.env.GRAPH_API_BASE_URL; | ||
const instagramId = process.env.GRAPH_API_INSTAGRAM_ID; | ||
const accessToken = process.env.GRAPH_API_ACCESS_TOKEN; | ||
const queries = 'media{caption,children{media_url},media_url,permalink,like_count,timestamp,username}'; | ||
const url = `${baseUrl}/${instagramId}?access_token=${accessToken}&fields=${queries}`; | ||
|
||
try { | ||
const data = await fetchJson<Media>(url); | ||
return NextResponse.json(data); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error('Error fetching Instagram media data: ', error); | ||
return NextResponse.json({ error: 'Failed to fetch Instagram data' }, { status: 500 }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// execute fetch api | ||
const fetchJson = async <T>(url: string): Promise<T> => { | ||
const res = await fetch(url); | ||
if (!res.ok) { | ||
throw new Error(`HTTP error! status: ${res.status}`); | ||
} | ||
return (await res.json()) as T; | ||
}; | ||
|
||
export default fetchJson; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { convertToCamelCase } from '@/utils/conversion/convertToCamelCase'; | ||
|
||
import type { Media } from '@/types/media'; | ||
|
||
const fetchMedia = async (): Promise<Media> => { | ||
const res = await fetch('/api/instagram'); | ||
|
||
if (!res.ok) { | ||
throw new Error('Failed to fetch media data'); | ||
} | ||
|
||
const data = (await res.json()) as Media; | ||
return convertToCamelCase(data); | ||
}; | ||
|
||
export default fetchMedia; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
interface ChildrenData { | ||
mediaUrl: string; | ||
id: string; | ||
} | ||
|
||
interface Children { | ||
data: ChildrenData[]; | ||
} | ||
|
||
interface MediaData { | ||
caption: string; | ||
children: Children; | ||
mediaUrl: string; | ||
permalink: string; | ||
likeCount: number; | ||
timestamp: string; | ||
username: string; | ||
id: string; | ||
} | ||
|
||
export interface Media { | ||
media: { | ||
data: MediaData[]; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* eslint-disable no-param-reassign */ | ||
/* eslint-disable no-use-before-define */ | ||
|
||
// convert snake case to camel case (string) | ||
const convertToCamelCaseStr = (str: string): string => { | ||
return str | ||
.split('_') | ||
.map((word, index) => { | ||
if (index === 0) { | ||
return word.toLowerCase(); | ||
} | ||
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); | ||
}) | ||
.join(''); | ||
}; | ||
|
||
// convert snake case to camel case (array of objects) | ||
const convertToCamelCaseArrayOfObjects = <T extends object>(array: T[]): T[] => { | ||
const arrayType = toString.call(array).slice(8, -1).toLowerCase(); | ||
|
||
if (arrayType !== 'array') return []; | ||
return array.map((obj) => convertToCamelCase(obj)); | ||
}; | ||
|
||
// convert snake to camel case (object) | ||
export const convertToCamelCase = <T extends object>(obj: T): T => { | ||
const result = {} as T; | ||
Object.entries(obj).forEach(([key, val]) => { | ||
const valType = toString.call(val).slice(8, -1).toLowerCase(); | ||
|
||
if (valType === 'object') { | ||
val = convertToCamelCase(val as Record<string, unknown>); | ||
} else if (valType === 'array') { | ||
val = convertToCamelCaseArrayOfObjects(val as Record<string, unknown>[]); | ||
} | ||
(result as Record<string, unknown>)[convertToCamelCaseStr(key)] = val; | ||
}); | ||
|
||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters