Skip to content

Commit

Permalink
chore: add @example
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed Feb 11, 2025
1 parent 343289b commit 4fe728f
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions packages/adapter-nextjs/src/types/NextServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,46 @@ export declare namespace NextServer {
* The function to run an operation with the Amplify server context. The operation is a callback function that
* takes a context spec parameter which is used to call the Amplify-side server APIs. The result of the operation
* is returned as a promise.
*
* @example
* ```
* // In `src/amplifyUtils.ts`
* import { createServerRunner } from 'aws-amplify/adapter-nextjs';
* import outputs from '@/amplify_outputs.json';
*
* export const { runWithAmplifyServerContext } = createServerRunner({ config: outputs });
*
* // In `src/app/home/page.tsx` (App router)
* import { cookies } from 'next/headers';
* import { runWithAmplifyServerContext } from '@/amplifyUtils';
*
* export default async function HomePage() {
* const user = await runWithAmplifyServerContext({
* nextServerContext: { cookies },
* operation: (contextSpec) => getCurrentUser(contextSpec),
* });
*
* return <div>{`Hello, ${user.username}`}</div>;
* }
*
* // In `src/pages/home/index.tsx` (Pages router)
* import { runWithAmplifyServerContext } from '@/amplifyUtils';
*
* export const getServerSideProps = async ({ req, res }) => {
* const user = await runWithAmplifyServerContext({
* nextServerContext: { request: req, response: res },
* operation: (contextSpec) => getCurrentUser(contextSpec),
* });
*
* return {
* props: { user },
* }
* }
*
* export default function HomePage(props) {
* return <div>{`Hello, ${props.user.username}`}</div>;
* }
* ```
*/
runWithAmplifyServerContext: RunOperationWithContext;
/**
Expand All @@ -104,6 +144,31 @@ export declare namespace NextServer {
*
* Note: when enabling server-side authentication, Amplify APIs can no longer be used in the client-side.
* @experimental
*
* @example
* ```
* // In `src/amplifyUtils.ts`
* import { createServerRunner } from 'aws-amplify/adapter-nextjs';
* import outputs from '@/amplify_outputs.json';
*
* export const { createAuthRouteHandlers } = createServerRunner({ config: outputs });
*
* // In `src/app/api/auth/[slug]/route.tsx` (App router)
* import { createAuthRouteHandlers } from '@/amplifyUtils';
*
* export const GET = createAuthRouteHandlers({
* redirectOnSignInComplete: "/home",
* redirectOnSignOutComplete: "/sign-in",
* );
*
* // In `src/pages/api/auth/[slug].tsx` (Pages router)
* import { createAuthRouteHandlers } from '@/amplifyUtils';
*
* export default createAuthRouteHandlers({
* redirectOnSignInComplete: "/home",
* redirectOnSignOutComplete: "/sign-in",
* });
* ```
*/
createAuthRouteHandlers: CreateAuthRouteHandlers;
}
Expand Down

0 comments on commit 4fe728f

Please sign in to comment.