-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
1,001 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
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,35 @@ | ||
import { NextResponse } from 'next/server' | ||
import type { NextRequest } from 'next/server' | ||
import { RedirectsClient } from 'src/sdk/redirects' | ||
|
||
const redirectsClient = new RedirectsClient() | ||
|
||
export async function middleware(req: NextRequest) { | ||
const { pathname } = req.nextUrl | ||
|
||
const redirect = await redirectsClient.get(pathname) | ||
|
||
if (redirect) { | ||
console.log('redirect matched', redirect) | ||
const url = req.nextUrl.clone() | ||
url.pathname = redirect.to | ||
|
||
const redirectStatusCode = redirect.type === 'permanent' ? 308 : 307 | ||
return NextResponse.redirect(url, redirectStatusCode) | ||
} | ||
|
||
return NextResponse.next() | ||
} | ||
|
||
export const config = { | ||
matcher: [ | ||
/* | ||
* Match all request paths except for the ones starting with: | ||
* - api (API routes) | ||
* - _next/static (static files) | ||
* - _next/image (image optimization files) | ||
* - favicon.ico (favicon file) | ||
*/ | ||
'/((?!api|_next/static|_next/image|favicon.ico).*)', | ||
], | ||
} |
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,49 @@ | ||
/* eslint-disable turbo/no-undeclared-env-vars */ | ||
import { DynamoDBClient, GetItemCommand } from '@aws-sdk/client-dynamodb' | ||
import storeConfig from '../../../discovery.config' | ||
|
||
const dynamoDbClient = new DynamoDBClient({ | ||
region: 'us-east-1', | ||
credentials: { | ||
accessKeyId: process.env.AWS_ACCESS_KEY_ID as string, | ||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY as string, | ||
sessionToken: process.env.AWS_SESSION_TOKEN, | ||
}, | ||
}) | ||
|
||
const TABLE_NAME = 'faststore-redirects' | ||
const ACCOUNT_ID = storeConfig.api.storeId | ||
|
||
export class RedirectsClient { | ||
async get(from: string) { | ||
try { | ||
const command = new GetItemCommand({ | ||
TableName: TABLE_NAME, | ||
Key: { | ||
account_name: { | ||
S: ACCOUNT_ID, | ||
}, | ||
from: { | ||
S: from, | ||
}, | ||
}, | ||
}) | ||
|
||
const { Item } = await dynamoDbClient.send(command) | ||
|
||
if (!Item) { | ||
return | ||
} | ||
|
||
const redirect = { | ||
to: Item.to.S, | ||
from: Item.from.S, | ||
type: Item.type.S, | ||
} | ||
|
||
return redirect | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} | ||
} |
Oops, something went wrong.