@aws-amplify/adapter-nextjs 0.0.2-console-preview.814dea6.0 → 0.0.2-preview-testing.64b45b1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/runWithAmplifyServerContext.d.ts +43 -0
- package/lib/runWithAmplifyServerContext.js +43 -0
- package/lib/runWithAmplifyServerContext.js.map +1 -1
- package/lib/types/NextServer.d.ts +6 -0
- package/lib-esm/runWithAmplifyServerContext.d.ts +43 -0
- package/lib-esm/runWithAmplifyServerContext.js +43 -0
- package/lib-esm/runWithAmplifyServerContext.js.map +1 -1
- package/lib-esm/types/NextServer.d.ts +6 -0
- package/package.json +3 -3
- package/src/runWithAmplifyServerContext.ts +43 -0
- package/src/types/NextServer.ts +6 -0
|
@@ -1,2 +1,45 @@
|
|
|
1
1
|
import { NextServer } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Runs the {@link operation} with the the context created from the {@link nextServerContext}.
|
|
4
|
+
*
|
|
5
|
+
* @param input The input to call the {@link runWithAmplifyServerContext}.
|
|
6
|
+
* @param input.nextServerContext The Next.js server context. It varies depends
|
|
7
|
+
* where the {@link runWithAmplifyServerContext} is being called.
|
|
8
|
+
* - In the [middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware):
|
|
9
|
+
* the context consists of an instance of the `NextRequest` and an instance
|
|
10
|
+
* of the `NextResponse`.
|
|
11
|
+
* - In a [Page server component](https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts#pages):
|
|
12
|
+
* the context is the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
|
|
13
|
+
* function provided by Next.js.
|
|
14
|
+
* - In a [Route Handler](https://nextjs.org/docs/app/building-your-application/routing/route-handlers):
|
|
15
|
+
* the context can be the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
|
|
16
|
+
* function or a combination of an instance of the `NextRequest` and an instance
|
|
17
|
+
* of the `NextResponse`.
|
|
18
|
+
* - In a [Server Action](https://nextjs.org/docs/app/building-your-application/data-fetching/forms-and-mutations#how-server-actions-work):
|
|
19
|
+
* the context is the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
|
|
20
|
+
* function provided by Next.js.
|
|
21
|
+
* @param input.operation The function that contains the business logic calling
|
|
22
|
+
* Amplify APIs. It expects a `contextSpec` parameter.
|
|
23
|
+
* @returns The result returned by the {@link operation}.
|
|
24
|
+
* @example
|
|
25
|
+
* // Use the `fetchAuthSession` API in the Next.js `middleware`.
|
|
26
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
27
|
+
* import { fetchAuthSession } from "aws-amplify/auth/server";
|
|
28
|
+
* import { runWithAmplifyServerContext } from "@aws-amplify/adapter-nextjs";
|
|
29
|
+
|
|
30
|
+
* export async function middleware(request: NextRequest) {
|
|
31
|
+
* const response = NextResponse.next();
|
|
32
|
+
* const authenticated = await runWithAmplifyServerContext({
|
|
33
|
+
* nextServerContext: { request, response },
|
|
34
|
+
* operation: async (contextSpec) => {
|
|
35
|
+
* const session = await fetchAuthSession(contextSpec);
|
|
36
|
+
* return session.tokens !== undefined;
|
|
37
|
+
* }
|
|
38
|
+
* });
|
|
39
|
+
* if (authenticated) {
|
|
40
|
+
* return response;
|
|
41
|
+
* }
|
|
42
|
+
* return NextResponse.redirect(new URL('/sign-in', request.url));
|
|
43
|
+
* }
|
|
44
|
+
*/
|
|
2
45
|
export declare const runWithAmplifyServerContext: NextServer.RunOperationWithContext;
|
|
@@ -42,6 +42,49 @@ exports.runWithAmplifyServerContext = void 0;
|
|
|
42
42
|
var utils_1 = require("./utils");
|
|
43
43
|
var core_1 = require("@aws-amplify/core");
|
|
44
44
|
var adapter_core_1 = require("aws-amplify/internals/adapter-core");
|
|
45
|
+
/**
|
|
46
|
+
* Runs the {@link operation} with the the context created from the {@link nextServerContext}.
|
|
47
|
+
*
|
|
48
|
+
* @param input The input to call the {@link runWithAmplifyServerContext}.
|
|
49
|
+
* @param input.nextServerContext The Next.js server context. It varies depends
|
|
50
|
+
* where the {@link runWithAmplifyServerContext} is being called.
|
|
51
|
+
* - In the [middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware):
|
|
52
|
+
* the context consists of an instance of the `NextRequest` and an instance
|
|
53
|
+
* of the `NextResponse`.
|
|
54
|
+
* - In a [Page server component](https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts#pages):
|
|
55
|
+
* the context is the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
|
|
56
|
+
* function provided by Next.js.
|
|
57
|
+
* - In a [Route Handler](https://nextjs.org/docs/app/building-your-application/routing/route-handlers):
|
|
58
|
+
* the context can be the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
|
|
59
|
+
* function or a combination of an instance of the `NextRequest` and an instance
|
|
60
|
+
* of the `NextResponse`.
|
|
61
|
+
* - In a [Server Action](https://nextjs.org/docs/app/building-your-application/data-fetching/forms-and-mutations#how-server-actions-work):
|
|
62
|
+
* the context is the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
|
|
63
|
+
* function provided by Next.js.
|
|
64
|
+
* @param input.operation The function that contains the business logic calling
|
|
65
|
+
* Amplify APIs. It expects a `contextSpec` parameter.
|
|
66
|
+
* @returns The result returned by the {@link operation}.
|
|
67
|
+
* @example
|
|
68
|
+
* // Use the `fetchAuthSession` API in the Next.js `middleware`.
|
|
69
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
70
|
+
* import { fetchAuthSession } from "aws-amplify/auth/server";
|
|
71
|
+
* import { runWithAmplifyServerContext } from "@aws-amplify/adapter-nextjs";
|
|
72
|
+
|
|
73
|
+
* export async function middleware(request: NextRequest) {
|
|
74
|
+
* const response = NextResponse.next();
|
|
75
|
+
* const authenticated = await runWithAmplifyServerContext({
|
|
76
|
+
* nextServerContext: { request, response },
|
|
77
|
+
* operation: async (contextSpec) => {
|
|
78
|
+
* const session = await fetchAuthSession(contextSpec);
|
|
79
|
+
* return session.tokens !== undefined;
|
|
80
|
+
* }
|
|
81
|
+
* });
|
|
82
|
+
* if (authenticated) {
|
|
83
|
+
* return response;
|
|
84
|
+
* }
|
|
85
|
+
* return NextResponse.redirect(new URL('/sign-in', request.url));
|
|
86
|
+
* }
|
|
87
|
+
*/
|
|
45
88
|
var runWithAmplifyServerContext = function (_a) {
|
|
46
89
|
var nextServerContext = _a.nextServerContext, operation = _a.operation;
|
|
47
90
|
return __awaiter(void 0, void 0, void 0, function () {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runWithAmplifyServerContext.js","sourceRoot":"","sources":["../src/runWithAmplifyServerContext.ts"],"names":[],"mappings":";AAAA,qEAAqE;AACrE,sCAAsC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGtC,iCAGiB;AACjB,0CAA0D;AAC1D,mEAK4C;
|
|
1
|
+
{"version":3,"file":"runWithAmplifyServerContext.js","sourceRoot":"","sources":["../src/runWithAmplifyServerContext.ts"],"names":[],"mappings":";AAAA,qEAAqE;AACrE,sCAAsC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGtC,iCAGiB;AACjB,0CAA0D;AAC1D,mEAK4C;AAE5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACI,IAAM,2BAA2B,GACvC,UAAO,EAAgC;QAA9B,iBAAiB,uBAAA,EAAE,SAAS,eAAA;;;;YAM9B,aAAa,GAAG,IAAA,wBAAgB,GAAE,CAAC;YAEzC,wEAAwE;YACxE,+CAA+C;YAC/C,IAAI,aAAa,CAAC,IAAI,EAAE;gBACjB,eAAe;gBACpB,oEAAoE;gBACpE,8DAA8D;gBAC9D,iEAAiE;gBACjE,mEAAmE;gBACnE,iBAAiB,KAAK,IAAI;oBACzB,CAAC,CAAC,4BAAqB;oBACvB,CAAC,CAAC,IAAA,4DAA6C,EAC7C,IAAA,uDAA+C,EAAC,iBAAiB,CAAC,CACjE,CAAC;gBACA,mBAAmB,GAAG,IAAA,wDAAyC,EACpE,aAAa,CAAC,IAAI,EAClB,eAAe,CACf,CAAC;gBACI,aAAa,GAAG,IAAA,2CAA4B,EACjD,aAAa,CAAC,IAAI,EAClB,eAAe,CACf,CAAC;gBAEF,sBAAO,IAAA,0CAA+B,EACrC,aAAa,EACb;wBACC,IAAI,EAAE,EAAE,mBAAmB,qBAAA,EAAE,aAAa,eAAA,EAAE;qBAC5C,EACD,SAAS,CACT,EAAC;aACF;YAED,oEAAoE;YACpE,+CAA+C;YAC/C,sBAAO,IAAA,0CAA+B,EAAC,aAAa,EAAE,EAAE,EAAE,SAAS,CAAC,EAAC;;;CACrE,CAAC;AA3CU,QAAA,2BAA2B,+BA2CrC"}
|
|
@@ -42,7 +42,13 @@ export declare namespace NextServer {
|
|
|
42
42
|
request: NextGetServerSidePropsContext['req'];
|
|
43
43
|
response: NextGetServerSidePropsContext['res'];
|
|
44
44
|
};
|
|
45
|
+
/**
|
|
46
|
+
* The union of possible Next.js app server context types.
|
|
47
|
+
*/
|
|
45
48
|
type Context = NextRequestAndNextResponseContext | NextRequestAndResponseContext | ServerComponentContext | GetServerSidePropsContext;
|
|
49
|
+
/**
|
|
50
|
+
* The interface of the input of {@link RunOperationWithContext}.
|
|
51
|
+
*/
|
|
46
52
|
interface RunWithContextInput<OperationResult> {
|
|
47
53
|
nextServerContext: Context | null;
|
|
48
54
|
operation: (contextSpec: AmplifyServer.ContextSpec) => OperationResult | Promise<OperationResult>;
|
|
@@ -1,2 +1,45 @@
|
|
|
1
1
|
import { NextServer } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Runs the {@link operation} with the the context created from the {@link nextServerContext}.
|
|
4
|
+
*
|
|
5
|
+
* @param input The input to call the {@link runWithAmplifyServerContext}.
|
|
6
|
+
* @param input.nextServerContext The Next.js server context. It varies depends
|
|
7
|
+
* where the {@link runWithAmplifyServerContext} is being called.
|
|
8
|
+
* - In the [middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware):
|
|
9
|
+
* the context consists of an instance of the `NextRequest` and an instance
|
|
10
|
+
* of the `NextResponse`.
|
|
11
|
+
* - In a [Page server component](https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts#pages):
|
|
12
|
+
* the context is the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
|
|
13
|
+
* function provided by Next.js.
|
|
14
|
+
* - In a [Route Handler](https://nextjs.org/docs/app/building-your-application/routing/route-handlers):
|
|
15
|
+
* the context can be the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
|
|
16
|
+
* function or a combination of an instance of the `NextRequest` and an instance
|
|
17
|
+
* of the `NextResponse`.
|
|
18
|
+
* - In a [Server Action](https://nextjs.org/docs/app/building-your-application/data-fetching/forms-and-mutations#how-server-actions-work):
|
|
19
|
+
* the context is the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
|
|
20
|
+
* function provided by Next.js.
|
|
21
|
+
* @param input.operation The function that contains the business logic calling
|
|
22
|
+
* Amplify APIs. It expects a `contextSpec` parameter.
|
|
23
|
+
* @returns The result returned by the {@link operation}.
|
|
24
|
+
* @example
|
|
25
|
+
* // Use the `fetchAuthSession` API in the Next.js `middleware`.
|
|
26
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
27
|
+
* import { fetchAuthSession } from "aws-amplify/auth/server";
|
|
28
|
+
* import { runWithAmplifyServerContext } from "@aws-amplify/adapter-nextjs";
|
|
29
|
+
|
|
30
|
+
* export async function middleware(request: NextRequest) {
|
|
31
|
+
* const response = NextResponse.next();
|
|
32
|
+
* const authenticated = await runWithAmplifyServerContext({
|
|
33
|
+
* nextServerContext: { request, response },
|
|
34
|
+
* operation: async (contextSpec) => {
|
|
35
|
+
* const session = await fetchAuthSession(contextSpec);
|
|
36
|
+
* return session.tokens !== undefined;
|
|
37
|
+
* }
|
|
38
|
+
* });
|
|
39
|
+
* if (authenticated) {
|
|
40
|
+
* return response;
|
|
41
|
+
* }
|
|
42
|
+
* return NextResponse.redirect(new URL('/sign-in', request.url));
|
|
43
|
+
* }
|
|
44
|
+
*/
|
|
2
45
|
export declare const runWithAmplifyServerContext: NextServer.RunOperationWithContext;
|
|
@@ -39,6 +39,49 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
39
39
|
import { createCookieStorageAdapterFromNextServerContext, getAmplifyConfig, } from './utils';
|
|
40
40
|
import { MemoryKeyValueStorage } from '@aws-amplify/core';
|
|
41
41
|
import { createAWSCredentialsAndIdentityIdProvider, createKeyValueStorageFromCookieStorageAdapter, createUserPoolsTokenProvider, runWithAmplifyServerContext as runWithAmplifyServerContextCore, } from 'aws-amplify/internals/adapter-core';
|
|
42
|
+
/**
|
|
43
|
+
* Runs the {@link operation} with the the context created from the {@link nextServerContext}.
|
|
44
|
+
*
|
|
45
|
+
* @param input The input to call the {@link runWithAmplifyServerContext}.
|
|
46
|
+
* @param input.nextServerContext The Next.js server context. It varies depends
|
|
47
|
+
* where the {@link runWithAmplifyServerContext} is being called.
|
|
48
|
+
* - In the [middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware):
|
|
49
|
+
* the context consists of an instance of the `NextRequest` and an instance
|
|
50
|
+
* of the `NextResponse`.
|
|
51
|
+
* - In a [Page server component](https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts#pages):
|
|
52
|
+
* the context is the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
|
|
53
|
+
* function provided by Next.js.
|
|
54
|
+
* - In a [Route Handler](https://nextjs.org/docs/app/building-your-application/routing/route-handlers):
|
|
55
|
+
* the context can be the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
|
|
56
|
+
* function or a combination of an instance of the `NextRequest` and an instance
|
|
57
|
+
* of the `NextResponse`.
|
|
58
|
+
* - In a [Server Action](https://nextjs.org/docs/app/building-your-application/data-fetching/forms-and-mutations#how-server-actions-work):
|
|
59
|
+
* the context is the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
|
|
60
|
+
* function provided by Next.js.
|
|
61
|
+
* @param input.operation The function that contains the business logic calling
|
|
62
|
+
* Amplify APIs. It expects a `contextSpec` parameter.
|
|
63
|
+
* @returns The result returned by the {@link operation}.
|
|
64
|
+
* @example
|
|
65
|
+
* // Use the `fetchAuthSession` API in the Next.js `middleware`.
|
|
66
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
67
|
+
* import { fetchAuthSession } from "aws-amplify/auth/server";
|
|
68
|
+
* import { runWithAmplifyServerContext } from "@aws-amplify/adapter-nextjs";
|
|
69
|
+
|
|
70
|
+
* export async function middleware(request: NextRequest) {
|
|
71
|
+
* const response = NextResponse.next();
|
|
72
|
+
* const authenticated = await runWithAmplifyServerContext({
|
|
73
|
+
* nextServerContext: { request, response },
|
|
74
|
+
* operation: async (contextSpec) => {
|
|
75
|
+
* const session = await fetchAuthSession(contextSpec);
|
|
76
|
+
* return session.tokens !== undefined;
|
|
77
|
+
* }
|
|
78
|
+
* });
|
|
79
|
+
* if (authenticated) {
|
|
80
|
+
* return response;
|
|
81
|
+
* }
|
|
82
|
+
* return NextResponse.redirect(new URL('/sign-in', request.url));
|
|
83
|
+
* }
|
|
84
|
+
*/
|
|
42
85
|
export var runWithAmplifyServerContext = function (_a) {
|
|
43
86
|
var nextServerContext = _a.nextServerContext, operation = _a.operation;
|
|
44
87
|
return __awaiter(void 0, void 0, void 0, function () {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runWithAmplifyServerContext.js","sourceRoot":"","sources":["../src/runWithAmplifyServerContext.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGtC,OAAO,EACN,+CAA+C,EAC/C,gBAAgB,GAChB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EACN,yCAAyC,EACzC,6CAA6C,EAC7C,4BAA4B,EAC5B,2BAA2B,IAAI,+BAA+B,GAC9D,MAAM,oCAAoC,CAAC;AAE5C,MAAM,CAAC,IAAM,2BAA2B,GACvC,UAAO,EAAgC;QAA9B,iBAAiB,uBAAA,EAAE,SAAS,eAAA;;;;YAM9B,aAAa,GAAG,gBAAgB,EAAE,CAAC;YAEzC,wEAAwE;YACxE,+CAA+C;YAC/C,IAAI,aAAa,CAAC,IAAI,EAAE;gBACjB,eAAe;gBACpB,oEAAoE;gBACpE,8DAA8D;gBAC9D,iEAAiE;gBACjE,mEAAmE;gBACnE,iBAAiB,KAAK,IAAI;oBACzB,CAAC,CAAC,qBAAqB;oBACvB,CAAC,CAAC,6CAA6C,CAC7C,+CAA+C,CAAC,iBAAiB,CAAC,CACjE,CAAC;gBACA,mBAAmB,GAAG,yCAAyC,CACpE,aAAa,CAAC,IAAI,EAClB,eAAe,CACf,CAAC;gBACI,aAAa,GAAG,4BAA4B,CACjD,aAAa,CAAC,IAAI,EAClB,eAAe,CACf,CAAC;gBAEF,sBAAO,+BAA+B,CACrC,aAAa,EACb;wBACC,IAAI,EAAE,EAAE,mBAAmB,qBAAA,EAAE,aAAa,eAAA,EAAE;qBAC5C,EACD,SAAS,CACT,EAAC;aACF;YAED,oEAAoE;YACpE,+CAA+C;YAC/C,sBAAO,+BAA+B,CAAC,aAAa,EAAE,EAAE,EAAE,SAAS,CAAC,EAAC;;;CACrE,CAAC"}
|
|
1
|
+
{"version":3,"file":"runWithAmplifyServerContext.js","sourceRoot":"","sources":["../src/runWithAmplifyServerContext.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGtC,OAAO,EACN,+CAA+C,EAC/C,gBAAgB,GAChB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EACN,yCAAyC,EACzC,6CAA6C,EAC7C,4BAA4B,EAC5B,2BAA2B,IAAI,+BAA+B,GAC9D,MAAM,oCAAoC,CAAC;AAE5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,MAAM,CAAC,IAAM,2BAA2B,GACvC,UAAO,EAAgC;QAA9B,iBAAiB,uBAAA,EAAE,SAAS,eAAA;;;;YAM9B,aAAa,GAAG,gBAAgB,EAAE,CAAC;YAEzC,wEAAwE;YACxE,+CAA+C;YAC/C,IAAI,aAAa,CAAC,IAAI,EAAE;gBACjB,eAAe;gBACpB,oEAAoE;gBACpE,8DAA8D;gBAC9D,iEAAiE;gBACjE,mEAAmE;gBACnE,iBAAiB,KAAK,IAAI;oBACzB,CAAC,CAAC,qBAAqB;oBACvB,CAAC,CAAC,6CAA6C,CAC7C,+CAA+C,CAAC,iBAAiB,CAAC,CACjE,CAAC;gBACA,mBAAmB,GAAG,yCAAyC,CACpE,aAAa,CAAC,IAAI,EAClB,eAAe,CACf,CAAC;gBACI,aAAa,GAAG,4BAA4B,CACjD,aAAa,CAAC,IAAI,EAClB,eAAe,CACf,CAAC;gBAEF,sBAAO,+BAA+B,CACrC,aAAa,EACb;wBACC,IAAI,EAAE,EAAE,mBAAmB,qBAAA,EAAE,aAAa,eAAA,EAAE;qBAC5C,EACD,SAAS,CACT,EAAC;aACF;YAED,oEAAoE;YACpE,+CAA+C;YAC/C,sBAAO,+BAA+B,CAAC,aAAa,EAAE,EAAE,EAAE,SAAS,CAAC,EAAC;;;CACrE,CAAC"}
|
|
@@ -42,7 +42,13 @@ export declare namespace NextServer {
|
|
|
42
42
|
request: NextGetServerSidePropsContext['req'];
|
|
43
43
|
response: NextGetServerSidePropsContext['res'];
|
|
44
44
|
};
|
|
45
|
+
/**
|
|
46
|
+
* The union of possible Next.js app server context types.
|
|
47
|
+
*/
|
|
45
48
|
type Context = NextRequestAndNextResponseContext | NextRequestAndResponseContext | ServerComponentContext | GetServerSidePropsContext;
|
|
49
|
+
/**
|
|
50
|
+
* The interface of the input of {@link RunOperationWithContext}.
|
|
51
|
+
*/
|
|
46
52
|
interface RunWithContextInput<OperationResult> {
|
|
47
53
|
nextServerContext: Context | null;
|
|
48
54
|
operation: (contextSpec: AmplifyServer.ContextSpec) => OperationResult | Promise<OperationResult>;
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"@types/node": "^20.3.1",
|
|
16
16
|
"@types/react": "^18.2.13",
|
|
17
17
|
"@types/react-dom": "^18.2.6",
|
|
18
|
-
"aws-amplify": "6.0.1-
|
|
18
|
+
"aws-amplify": "6.0.1-preview-testing.64b45b1.0+64b45b1",
|
|
19
19
|
"jest-fetch-mock": "3.0.3",
|
|
20
20
|
"next": ">= 13.4.0 < 14.0.0",
|
|
21
21
|
"typescript": "5.1.6"
|
|
@@ -107,6 +107,6 @@
|
|
|
107
107
|
"ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 90.31"
|
|
108
108
|
},
|
|
109
109
|
"typings": "./lib-esm/index.d.ts",
|
|
110
|
-
"version": "0.0.2-
|
|
111
|
-
"gitHead": "
|
|
110
|
+
"version": "0.0.2-preview-testing.64b45b1.0+64b45b1",
|
|
111
|
+
"gitHead": "64b45b1680d18b8befa569ea69752179bea8c2ac"
|
|
112
112
|
}
|
|
@@ -14,6 +14,49 @@ import {
|
|
|
14
14
|
runWithAmplifyServerContext as runWithAmplifyServerContextCore,
|
|
15
15
|
} from 'aws-amplify/internals/adapter-core';
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Runs the {@link operation} with the the context created from the {@link nextServerContext}.
|
|
19
|
+
*
|
|
20
|
+
* @param input The input to call the {@link runWithAmplifyServerContext}.
|
|
21
|
+
* @param input.nextServerContext The Next.js server context. It varies depends
|
|
22
|
+
* where the {@link runWithAmplifyServerContext} is being called.
|
|
23
|
+
* - In the [middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware):
|
|
24
|
+
* the context consists of an instance of the `NextRequest` and an instance
|
|
25
|
+
* of the `NextResponse`.
|
|
26
|
+
* - In a [Page server component](https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts#pages):
|
|
27
|
+
* the context is the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
|
|
28
|
+
* function provided by Next.js.
|
|
29
|
+
* - In a [Route Handler](https://nextjs.org/docs/app/building-your-application/routing/route-handlers):
|
|
30
|
+
* the context can be the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
|
|
31
|
+
* function or a combination of an instance of the `NextRequest` and an instance
|
|
32
|
+
* of the `NextResponse`.
|
|
33
|
+
* - In a [Server Action](https://nextjs.org/docs/app/building-your-application/data-fetching/forms-and-mutations#how-server-actions-work):
|
|
34
|
+
* the context is the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
|
|
35
|
+
* function provided by Next.js.
|
|
36
|
+
* @param input.operation The function that contains the business logic calling
|
|
37
|
+
* Amplify APIs. It expects a `contextSpec` parameter.
|
|
38
|
+
* @returns The result returned by the {@link operation}.
|
|
39
|
+
* @example
|
|
40
|
+
* // Use the `fetchAuthSession` API in the Next.js `middleware`.
|
|
41
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
42
|
+
* import { fetchAuthSession } from "aws-amplify/auth/server";
|
|
43
|
+
* import { runWithAmplifyServerContext } from "@aws-amplify/adapter-nextjs";
|
|
44
|
+
|
|
45
|
+
* export async function middleware(request: NextRequest) {
|
|
46
|
+
* const response = NextResponse.next();
|
|
47
|
+
* const authenticated = await runWithAmplifyServerContext({
|
|
48
|
+
* nextServerContext: { request, response },
|
|
49
|
+
* operation: async (contextSpec) => {
|
|
50
|
+
* const session = await fetchAuthSession(contextSpec);
|
|
51
|
+
* return session.tokens !== undefined;
|
|
52
|
+
* }
|
|
53
|
+
* });
|
|
54
|
+
* if (authenticated) {
|
|
55
|
+
* return response;
|
|
56
|
+
* }
|
|
57
|
+
* return NextResponse.redirect(new URL('/sign-in', request.url));
|
|
58
|
+
* }
|
|
59
|
+
*/
|
|
17
60
|
export const runWithAmplifyServerContext: NextServer.RunOperationWithContext =
|
|
18
61
|
async ({ nextServerContext, operation }) => {
|
|
19
62
|
// 1. get amplify config from env vars
|
package/src/types/NextServer.ts
CHANGED
|
@@ -51,12 +51,18 @@ export namespace NextServer {
|
|
|
51
51
|
response: NextGetServerSidePropsContext['res'];
|
|
52
52
|
};
|
|
53
53
|
|
|
54
|
+
/**
|
|
55
|
+
* The union of possible Next.js app server context types.
|
|
56
|
+
*/
|
|
54
57
|
export type Context =
|
|
55
58
|
| NextRequestAndNextResponseContext
|
|
56
59
|
| NextRequestAndResponseContext
|
|
57
60
|
| ServerComponentContext
|
|
58
61
|
| GetServerSidePropsContext;
|
|
59
62
|
|
|
63
|
+
/**
|
|
64
|
+
* The interface of the input of {@link RunOperationWithContext}.
|
|
65
|
+
*/
|
|
60
66
|
export interface RunWithContextInput<OperationResult> {
|
|
61
67
|
nextServerContext: Context | null;
|
|
62
68
|
operation: (
|