Skip to content

Commit

Permalink
use update fetchToken()
Browse files Browse the repository at this point in the history
  • Loading branch information
gregrickaby committed Jan 29, 2024
1 parent 135360c commit 65cc0a6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 136 deletions.
55 changes: 10 additions & 45 deletions app/api/preSearch/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import config from '@/lib/config'
import {fetchToken} from '@/lib/functions'

/**
* Route segment config.
Expand All @@ -19,59 +20,17 @@ export const runtime = 'edge'
*/
export async function GET() {
try {
// Try and fetch a new access token.
const tokenResponse = await fetch(
`https://www.reddit.com/api/v1/access_token?grant_type=client_credentials&device_id=${config.deviceId}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json charset=UTF-8',
'User-Agent': config.userAgent,
Authorization: `Basic ${btoa(
`${process.env.REDDIT_CLIENT_ID}:${process.env.REDDIT_CLIENT_SECRET}`
)}`
},
next: {revalidate: 3600}
}
)

// Bad response? Bail...
if (tokenResponse.status != 200) {
return new Response(
JSON.stringify({
error: `${tokenResponse.statusText}`
}),
{
status: tokenResponse.status,
statusText: tokenResponse.statusText
}
)
}

// Get the access token.
const token = await tokenResponse.json()

// Issue with token? Bail...
if (token.error) {
return new Response(
JSON.stringify({
error: token.error
}),
{
status: token.status,
statusText: token.statusText
}
)
}
const {token} = await fetchToken()

// Attempt to fetch subreddits.
const response = await fetch(
`https://oauth.reddit.com/subreddits/popular?limit=${config.redditApi.preSearchLimit}`,
{
headers: {
authorization: `Bearer ${token.access_token}`
authorization: `Bearer ${token}`
},
next: {revalidate: 3600}
next: {revalidate: config.cacheTtl}
}
)

Expand All @@ -82,6 +41,9 @@ export async function GET() {
error: `${response.statusText}`
}),
{
headers: {
'X-Robots-Tag': 'noindex'
},
status: response.status,
statusText: response.statusText
}
Expand All @@ -98,6 +60,9 @@ export async function GET() {
error: `No data returned from Reddit.`
}),
{
headers: {
'X-Robots-Tag': 'noindex'
},
status: 400,
statusText: 'Bad Request'
}
Expand Down
47 changes: 3 additions & 44 deletions app/api/reddit/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import config from '@/lib/config'
import {getMediumImage} from '@/lib/functions'
import {fetchToken, getMediumImage} from '@/lib/functions'
import {Posts} from '@/lib/types'

interface RedditAPIResponse {
Expand Down Expand Up @@ -63,56 +63,15 @@ export async function GET(request: Request) {
: ''

try {
// Try and fetch a new access token.
const tokenResponse = await fetch(
`https://www.reddit.com/api/v1/access_token?grant_type=client_credentials&device_id=${config.deviceId}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json charset=UTF-8',
'User-Agent': config.userAgent,
Authorization: `Basic ${btoa(
`${process.env.REDDIT_CLIENT_ID}:${process.env.REDDIT_CLIENT_SECRET}`
)}`
}
}
)

// Bad response? Bail...
if (tokenResponse.status != 200) {
return new Response(
JSON.stringify({
error: `${tokenResponse.statusText}`
}),
{
status: tokenResponse.status,
statusText: tokenResponse.statusText
}
)
}

// Get the access token.
const token = await tokenResponse.json()

// Issue with token? Bail...
if (token.error) {
return new Response(
JSON.stringify({
error: token.error
}),
{
status: token.status,
statusText: token.statusText
}
)
}
const {token} = await fetchToken()

// Attempt to fetch posts.
const response = await fetch(
`https://oauth.reddit.com/r/${subReddit}/${sortBy}/.json?limit=${postLimit}&after=${lastPost}&raw_json=1`,
{
headers: {
Authorization: `Bearer: ${token.access_token}`
Authorization: `Bearer: ${token}`
}
}
)
Expand Down
55 changes: 8 additions & 47 deletions app/api/search/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import config from '@/lib/config'
import {fetchToken} from '@/lib/functions'

/**
* Route segment config.
Expand Down Expand Up @@ -30,56 +31,15 @@ export async function GET(request: Request) {
: config.redditApi.subReddit

try {
// Try and fetch a new access token.
const tokenResponse = await fetch(
`https://www.reddit.com/api/v1/access_token?grant_type=client_credentials&device_id=${config.deviceId}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json charset=UTF-8',
'User-Agent': config.userAgent,
Authorization: `Basic ${btoa(
`${process.env.REDDIT_CLIENT_ID}:${process.env.REDDIT_CLIENT_SECRET}`
)}`
}
}
)

// Bad response? Bail...
if (tokenResponse.status != 200) {
return new Response(
JSON.stringify({
error: `${tokenResponse.statusText}`
}),
{
status: tokenResponse.status,
statusText: tokenResponse.statusText
}
)
}

// Get the access token.
const token = await tokenResponse.json()

// Issue with token? Bail...
if (token.error) {
return new Response(
JSON.stringify({
error: token.error
}),
{
status: token.status,
statusText: token.statusText
}
)
}
const {token} = await fetchToken()

// Attempt to fetch subreddits.
const response = await fetch(
`https://oauth.reddit.com/api/subreddit_autocomplete_v2?query=${term}&limit=10&include_over_18=true&include_profiles=true&typeahead_active=true&search_query_id=${config.deviceId}`,
`https://oauth.reddit.com/api/subreddit_autocomplete_v2?query=${term}&limit=10&include_over_18=true&include_profiles=true&typeahead_active=true`,
{
headers: {
authorization: `Bearer ${token.access_token}`
authorization: `Bearer ${token}`
}
}
)
Expand Down Expand Up @@ -126,14 +86,15 @@ export async function GET(request: Request) {

// Send the response.
return new Response(JSON.stringify(filtered), {
status: 200,
statusText: 'OK',
headers: {
'X-Robots-Tag': 'noindex',
'Content-Type': 'application/json',
'Cache-Control': `public, s-maxage=${config.cacheTtl}`,
'CDN-Cache-Control': `public, s-maxage=${config.cacheTtl}`,
'Vercel-CDN-Cache-Control': `public, s-maxage=${config.cacheTtl}`
},
status: 200,
statusText: 'OK'
}
})
} catch (error) {
// Issue? Leave a message and bail.
Expand Down

1 comment on commit 65cc0a6

@vercel
Copy link

@vercel vercel bot commented on 65cc0a6 Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.