Skip to content

Commit f512b65

Browse files
authored
chore(adapter-nextjs): update tsdoc of the public APIs (#14187)
* chore(adapter-nextjs): update tsdoc of the public APIs * chore: add @example
1 parent 825d338 commit f512b65

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

packages/adapter-nextjs/src/types/NextServer.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,85 @@ export declare namespace NextServer {
9191
}
9292

9393
export interface CreateServerRunnerOutput {
94+
/**
95+
* The function to run an operation with the Amplify server context. The operation is a callback function that
96+
* takes a context spec parameter which is used to call the Amplify-side server APIs. The result of the operation
97+
* is returned as a promise.
98+
*
99+
* @example
100+
* ```
101+
* // In `src/amplifyUtils.ts`
102+
* import { createServerRunner } from 'aws-amplify/adapter-nextjs';
103+
* import outputs from '@/amplify_outputs.json';
104+
*
105+
* export const { runWithAmplifyServerContext } = createServerRunner({ config: outputs });
106+
*
107+
* // In `src/app/home/page.tsx` (App router)
108+
* import { cookies } from 'next/headers';
109+
* import { runWithAmplifyServerContext } from '@/amplifyUtils';
110+
*
111+
* export default async function HomePage() {
112+
* const user = await runWithAmplifyServerContext({
113+
* nextServerContext: { cookies },
114+
* operation: (contextSpec) => getCurrentUser(contextSpec),
115+
* });
116+
*
117+
* return <div>{`Hello, ${user.username}`}</div>;
118+
* }
119+
*
120+
* // In `src/pages/home/index.tsx` (Pages router)
121+
* import { runWithAmplifyServerContext } from '@/amplifyUtils';
122+
*
123+
* export const getServerSideProps = async ({ req, res }) => {
124+
* const user = await runWithAmplifyServerContext({
125+
* nextServerContext: { request: req, response: res },
126+
* operation: (contextSpec) => getCurrentUser(contextSpec),
127+
* });
128+
*
129+
* return {
130+
* props: { user },
131+
* }
132+
* }
133+
*
134+
* export default function HomePage(props) {
135+
* return <div>{`Hello, ${props.user.username}`}</div>;
136+
* }
137+
* ```
138+
*/
94139
runWithAmplifyServerContext: RunOperationWithContext;
140+
/**
141+
* The factory function to create the route handlers for the Amplify server-side authentication. You can call this
142+
* function and export the result as the route handlers in the Next.js API routes, to authenticate your end users
143+
* on the server side.
144+
*
145+
* Note: when enabling server-side authentication, Amplify APIs can no longer be used in the client-side.
146+
* @experimental
147+
*
148+
* @example
149+
* ```
150+
* // In `src/amplifyUtils.ts`
151+
* import { createServerRunner } from 'aws-amplify/adapter-nextjs';
152+
* import outputs from '@/amplify_outputs.json';
153+
*
154+
* export const { createAuthRouteHandlers } = createServerRunner({ config: outputs });
155+
*
156+
* // In `src/app/api/auth/[slug]/route.tsx` (App router)
157+
* import { createAuthRouteHandlers } from '@/amplifyUtils';
158+
*
159+
* export const GET = createAuthRouteHandlers({
160+
* redirectOnSignInComplete: "/home",
161+
* redirectOnSignOutComplete: "/sign-in",
162+
* );
163+
*
164+
* // In `src/pages/api/auth/[slug].tsx` (Pages router)
165+
* import { createAuthRouteHandlers } from '@/amplifyUtils';
166+
*
167+
* export default createAuthRouteHandlers({
168+
* redirectOnSignInComplete: "/home",
169+
* redirectOnSignOutComplete: "/sign-in",
170+
* });
171+
* ```
172+
*/
95173
createAuthRouteHandlers: CreateAuthRouteHandlers;
96174
}
97175

0 commit comments

Comments
 (0)