Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
54nd10 committed Dec 21, 2023
1 parent 0a59a5d commit 7b5a274
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 25 deletions.
1 change: 1 addition & 0 deletions apps/www/configs/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Fetcher} from '@/models/fetcher.model'
export const nextAPIToken = process.env.NEXT_API_TOKEN || ''
export const strapiAPI = process.env.STRAPI_API_URL || ''
export const strapiAPIToken = process.env.STRAPI_API_TOKEN || ''
export const strapiFetcher = new Fetcher(strapiAPI, {
Expand Down
1 change: 0 additions & 1 deletion apps/www/src/app/api/og/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {addAbsoluteURLsInObject} from '@/utils/toolbox'
import ogs from 'open-graph-scraper'

export async function GET(request: Request) {

const urlParam = new URL(request.url).searchParams.get('url')
let url: string | undefined | URL = Array.isArray(urlParam) ? urlParam[0] : urlParam

Expand Down
38 changes: 14 additions & 24 deletions apps/www/src/app/api/revalidate/route.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
import {NextResponse} from 'next/server'
import {headers} from '@/utils/api/headers'
import {options} from '@/utils/api/options'
import {protectedAPI} from '@/utils/api/protected-api'
import {revalidateTag} from 'next/cache'

export async function OPTIONS() {
return NextResponse.json(
{},
{
status: 200,
headers: {
'Access-Control-Allow-Origin': 'https://cms.56k.cloud',
'Access-Control-Allow-Methods': 'OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
}
}
)
return options()
}

export async function GET() {
revalidateTag('strapi')
return new Response(
'Strapi tag revalidated',
{
status: 200,
headers: {
'Access-Control-Allow-Origin': 'https://cms.56k.cloud',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
export async function GET(request: Request) {
return protectedAPI(request, () => {
revalidateTag('strapi')
return new Response(
'Strapi tag revalidated',
{
status: 200,
headers: headers({methods: 'GET'})
}
}
)
)
})
}
15 changes: 15 additions & 0 deletions apps/www/src/utils/api/headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {strapiAPI} from '../../../configs/server'

export type HeaderProps = {
origin?: string
methods?: string
headers?: string
}

export function headers(props: HeaderProps) {
return {
'Access-Control-Allow-Origin': props.origin || strapiAPI || '*',
'Access-Control-Allow-Methods': props.methods || 'GET, POST, PUT, PATCH, DELETE, OPTIONS',
'Access-Control-Allow-Headers': props.headers || 'Content-Type, Authorization'
}
}
12 changes: 12 additions & 0 deletions apps/www/src/utils/api/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {NextResponse} from 'next/server'
import {headers} from './headers'

export function options() {
return NextResponse.json(
{},
{
status: 200,
headers: headers({methods: 'OPTIONS'})
}
)
}
16 changes: 16 additions & 0 deletions apps/www/src/utils/api/protected-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {nextAPIToken} from '../../../configs/server'

export function protectedAPI(request: Request, success: () => Response) {
const token = request.headers.get('Authorization')
if (token === nextAPIToken) {
return success()
} else {
return new Response(
'Unauthorized',
{
status: 401,
statusText: 'Unauthorized'
}
)
}
}

0 comments on commit 7b5a274

Please sign in to comment.