@aws-amplify/adapter-nextjs 0.0.2-console-preview.e8aa063.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.
Files changed (58) hide show
  1. package/LICENSE +201 -0
  2. package/lib/index.d.ts +1 -0
  3. package/lib/index.js +8 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/runWithAmplifyServerContext.d.ts +2 -0
  6. package/lib/runWithAmplifyServerContext.js +74 -0
  7. package/lib/runWithAmplifyServerContext.js.map +1 -0
  8. package/lib/types/NextServer.d.ts +53 -0
  9. package/lib/types/NextServer.js +5 -0
  10. package/lib/types/NextServer.js.map +1 -0
  11. package/lib/types/index.d.ts +1 -0
  12. package/lib/types/index.js +5 -0
  13. package/lib/types/index.js.map +1 -0
  14. package/lib/utils/createCookieStorageAdapterFromNextServerContext.d.ts +4 -0
  15. package/lib/utils/createCookieStorageAdapterFromNextServerContext.js +152 -0
  16. package/lib/utils/createCookieStorageAdapterFromNextServerContext.js.map +1 -0
  17. package/lib/utils/getAmplifyConfig.d.ts +2 -0
  18. package/lib/utils/getAmplifyConfig.js +20 -0
  19. package/lib/utils/getAmplifyConfig.js.map +1 -0
  20. package/lib/utils/index.d.ts +2 -0
  21. package/lib/utils/index.js +10 -0
  22. package/lib/utils/index.js.map +1 -0
  23. package/lib/withAmplify.d.ts +9 -0
  24. package/lib/withAmplify.js +33 -0
  25. package/lib/withAmplify.js.map +1 -0
  26. package/lib-esm/index.d.ts +1 -0
  27. package/lib-esm/index.js +4 -0
  28. package/lib-esm/index.js.map +1 -0
  29. package/lib-esm/runWithAmplifyServerContext.d.ts +2 -0
  30. package/lib-esm/runWithAmplifyServerContext.js +70 -0
  31. package/lib-esm/runWithAmplifyServerContext.js.map +1 -0
  32. package/lib-esm/types/NextServer.d.ts +53 -0
  33. package/lib-esm/types/NextServer.js +4 -0
  34. package/lib-esm/types/NextServer.js.map +1 -0
  35. package/lib-esm/types/index.d.ts +1 -0
  36. package/lib-esm/types/index.js +4 -0
  37. package/lib-esm/types/index.js.map +1 -0
  38. package/lib-esm/utils/createCookieStorageAdapterFromNextServerContext.d.ts +4 -0
  39. package/lib-esm/utils/createCookieStorageAdapterFromNextServerContext.js +148 -0
  40. package/lib-esm/utils/createCookieStorageAdapterFromNextServerContext.js.map +1 -0
  41. package/lib-esm/utils/getAmplifyConfig.d.ts +2 -0
  42. package/lib-esm/utils/getAmplifyConfig.js +16 -0
  43. package/lib-esm/utils/getAmplifyConfig.js.map +1 -0
  44. package/lib-esm/utils/index.d.ts +2 -0
  45. package/lib-esm/utils/index.js +5 -0
  46. package/lib-esm/utils/index.js.map +1 -0
  47. package/lib-esm/withAmplify.d.ts +9 -0
  48. package/lib-esm/withAmplify.js +29 -0
  49. package/lib-esm/withAmplify.js.map +1 -0
  50. package/package.json +111 -0
  51. package/src/index.ts +4 -0
  52. package/src/runWithAmplifyServerContext.ts +60 -0
  53. package/src/types/NextServer.ts +72 -0
  54. package/src/types/index.ts +4 -0
  55. package/src/utils/createCookieStorageAdapterFromNextServerContext.ts +202 -0
  56. package/src/utils/getAmplifyConfig.ts +22 -0
  57. package/src/utils/index.ts +5 -0
  58. package/src/withAmplify.ts +28 -0
@@ -0,0 +1 @@
1
+ export { runWithAmplifyServerContext } from './runWithAmplifyServerContext';
@@ -0,0 +1,4 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export { runWithAmplifyServerContext } from './runWithAmplifyServerContext';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AAEtC,OAAO,EAAE,2BAA2B,EAAE,MAAM,+BAA+B,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { NextServer } from './types';
2
+ export declare const runWithAmplifyServerContext: NextServer.RunOperationWithContext;
@@ -0,0 +1,70 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
+ return new (P || (P = Promise))(function (resolve, reject) {
6
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
10
+ });
11
+ };
12
+ var __generator = (this && this.__generator) || function (thisArg, body) {
13
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
14
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
15
+ function verb(n) { return function (v) { return step([n, v]); }; }
16
+ function step(op) {
17
+ if (f) throw new TypeError("Generator is already executing.");
18
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
19
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
20
+ if (y = 0, t) op = [op[0] & 2, t.value];
21
+ switch (op[0]) {
22
+ case 0: case 1: t = op; break;
23
+ case 4: _.label++; return { value: op[1], done: false };
24
+ case 5: _.label++; y = op[1]; op = [0]; continue;
25
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
26
+ default:
27
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
28
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
29
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
30
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
31
+ if (t[2]) _.ops.pop();
32
+ _.trys.pop(); continue;
33
+ }
34
+ op = body.call(thisArg, _);
35
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
36
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
37
+ }
38
+ };
39
+ import { createCookieStorageAdapterFromNextServerContext, getAmplifyConfig, } from './utils';
40
+ import { MemoryKeyValueStorage } from '@aws-amplify/core';
41
+ import { createAWSCredentialsAndIdentityIdProvider, createKeyValueStorageFromCookieStorageAdapter, createUserPoolsTokenProvider, runWithAmplifyServerContext as runWithAmplifyServerContextCore, } from 'aws-amplify/internals/adapter-core';
42
+ export var runWithAmplifyServerContext = function (_a) {
43
+ var nextServerContext = _a.nextServerContext, operation = _a.operation;
44
+ return __awaiter(void 0, void 0, void 0, function () {
45
+ var amplifyConfig, keyValueStorage, credentialsProvider, tokenProvider;
46
+ return __generator(this, function (_b) {
47
+ amplifyConfig = getAmplifyConfig();
48
+ // When the Auth config is presented, attempt to create a Amplify server
49
+ // context with token and credentials provider.
50
+ if (amplifyConfig.Auth) {
51
+ keyValueStorage = nextServerContext === null
52
+ ? // When `null` is passed as the value of `nextServerContext`, opt-in
53
+ // unauthenticated role (primarily for static rendering). It's
54
+ // safe to use the singleton `MemoryKeyValueStorage` here, as the
55
+ // static rendering uses the same unauthenticated role cross-sever.
56
+ MemoryKeyValueStorage
57
+ : createKeyValueStorageFromCookieStorageAdapter(createCookieStorageAdapterFromNextServerContext(nextServerContext));
58
+ credentialsProvider = createAWSCredentialsAndIdentityIdProvider(amplifyConfig.Auth, keyValueStorage);
59
+ tokenProvider = createUserPoolsTokenProvider(amplifyConfig.Auth, keyValueStorage);
60
+ return [2 /*return*/, runWithAmplifyServerContextCore(amplifyConfig, {
61
+ Auth: { credentialsProvider: credentialsProvider, tokenProvider: tokenProvider },
62
+ }, operation)];
63
+ }
64
+ // Otherwise it may be the case that auth is not used, e.g. API key.
65
+ // Omitting the `Auth` in the second parameter.
66
+ return [2 /*return*/, runWithAmplifyServerContextCore(amplifyConfig, {}, operation)];
67
+ });
68
+ });
69
+ };
70
+ //# sourceMappingURL=runWithAmplifyServerContext.js.map
@@ -0,0 +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,GACpB,iBAAiB,KAAK,IAAI;oBACzB,CAAC,CAAC,oEAAoE;wBACpE,8DAA8D;wBAC9D,iEAAiE;wBACjE,mEAAmE;wBACnE,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"}
@@ -0,0 +1,53 @@
1
+ import { GetServerSidePropsContext as NextGetServerSidePropsContext } from 'next';
2
+ import { NextRequest, NextResponse } from 'next/server';
3
+ import { cookies } from 'next/headers';
4
+ import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
5
+ export declare namespace NextServer {
6
+ /**
7
+ * This context is normally available in the following:
8
+ * - Next App Router [middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware)
9
+ * - Next App Router [route handler](https://nextjs.org/docs/app/building-your-application/routing/route-handlers)
10
+ * when using `NextResponse` to create the response of the route handler
11
+ */
12
+ type NextRequestAndNextResponseContext = {
13
+ request: NextRequest;
14
+ response: NextResponse;
15
+ };
16
+ /**
17
+ * This context is normally available in the following:
18
+ * - Next App Router [route handler](https://nextjs.org/docs/app/building-your-application/routing/route-handlers)
19
+ * when using the Web API [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
20
+ * to create the response of the route handler
21
+ */
22
+ type NextRequestAndResponseContext = {
23
+ request: NextRequest;
24
+ response: Response;
25
+ };
26
+ /**
27
+ * This context is normally available in the following:
28
+ * - Next [Server Component](https://nextjs.org/docs/getting-started/react-essentials#server-components)
29
+ * where the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies)
30
+ * function can be imported and called
31
+ */
32
+ type ServerComponentContext = {
33
+ cookies: typeof cookies;
34
+ };
35
+ type ServerActionContext = ServerComponentContext;
36
+ /**
37
+ * This context is normally available in the
38
+ * [`getServerSideProps`](https://nextjs.org/docs/pages/building-your-application/data-fetching/get-server-side-props)
39
+ * function of the Next Pages Router.
40
+ */
41
+ type GetServerSidePropsContext = {
42
+ request: NextGetServerSidePropsContext['req'];
43
+ response: NextGetServerSidePropsContext['res'];
44
+ };
45
+ type Context = NextRequestAndNextResponseContext | NextRequestAndResponseContext | ServerComponentContext | GetServerSidePropsContext;
46
+ interface RunWithContextInput<OperationResult> {
47
+ nextServerContext: Context | null;
48
+ operation: (contextSpec: AmplifyServer.ContextSpec) => OperationResult | Promise<OperationResult>;
49
+ }
50
+ interface RunOperationWithContext {
51
+ <OperationResult>(input: RunWithContextInput<OperationResult>): Promise<OperationResult>;
52
+ }
53
+ }
@@ -0,0 +1,4 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export {};
4
+ //# sourceMappingURL=NextServer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NextServer.js","sourceRoot":"","sources":["../../src/types/NextServer.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC"}
@@ -0,0 +1 @@
1
+ export { NextServer } from './NextServer';
@@ -0,0 +1,4 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export {};
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC"}
@@ -0,0 +1,4 @@
1
+ import { NextServer } from '../types';
2
+ import { CookieStorage } from '@aws-amplify/core/internals/adapter-core';
3
+ export declare const DATE_IN_THE_PAST: Date;
4
+ export declare const createCookieStorageAdapterFromNextServerContext: (context: NextServer.Context) => CookieStorage.Adapter;
@@ -0,0 +1,148 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ var __assign = (this && this.__assign) || function () {
4
+ __assign = Object.assign || function(t) {
5
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
6
+ s = arguments[i];
7
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
8
+ t[p] = s[p];
9
+ }
10
+ return t;
11
+ };
12
+ return __assign.apply(this, arguments);
13
+ };
14
+ import { NextRequest, NextResponse } from 'next/server';
15
+ import { AmplifyServerContextError, } from '@aws-amplify/core/internals/adapter-core';
16
+ import { IncomingMessage, ServerResponse } from 'http';
17
+ export var DATE_IN_THE_PAST = new Date(0);
18
+ export var createCookieStorageAdapterFromNextServerContext = function (context) {
19
+ var _a = context, request = _a.request, response = _a.response;
20
+ if (request instanceof NextRequest && response) {
21
+ if (response instanceof NextResponse) {
22
+ return createCookieStorageAdapterFromNextRequestAndNextResponse(request, response);
23
+ }
24
+ else {
25
+ return createCookieStorageAdapterFromNextRequestAndHttpResponse(request, response);
26
+ }
27
+ }
28
+ var cookies = context.cookies;
29
+ if (typeof cookies === 'function') {
30
+ return createCookieStorageAdapterFromNextCookies(cookies);
31
+ }
32
+ var _b = context, req = _b.request, res = _b.response;
33
+ if (req instanceof IncomingMessage && res instanceof ServerResponse) {
34
+ return createCookieStorageAdapterFromGetServerSidePropsContext(req, res);
35
+ }
36
+ // This should not happen normally.
37
+ throw new AmplifyServerContextError({
38
+ message: 'Attempted to create cookie storage adapter from an unsupported Next.js server context.',
39
+ });
40
+ };
41
+ var createCookieStorageAdapterFromNextRequestAndNextResponse = function (request, response) {
42
+ var readonlyCookieStore = request.cookies;
43
+ var mutableCookieStore = response.cookies;
44
+ return {
45
+ get: readonlyCookieStore.get.bind(readonlyCookieStore),
46
+ getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore),
47
+ set: mutableCookieStore.set.bind(mutableCookieStore),
48
+ delete: mutableCookieStore.delete.bind(mutableCookieStore),
49
+ };
50
+ };
51
+ var createCookieStorageAdapterFromNextRequestAndHttpResponse = function (request, response) {
52
+ var readonlyCookieStore = request.cookies;
53
+ var mutableCookieStore = createMutableCookieStoreFromHeaders(response.headers);
54
+ return __assign({ get: readonlyCookieStore.get.bind(readonlyCookieStore), getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore) }, mutableCookieStore);
55
+ };
56
+ var createCookieStorageAdapterFromNextCookies = function (cookies) {
57
+ var cookieStore = cookies();
58
+ // When Next cookies() is called in a server component, it returns a readonly
59
+ // cookie store. Hence calling set and delete throws an error. However,
60
+ // cookies() returns a mutable cookie store when called in a server action.
61
+ // We have no way to detect which one is returned, so we try to call set and delete
62
+ // and safely ignore the error if it is thrown.
63
+ var setFunc = function (name, value, options) {
64
+ try {
65
+ cookieStore.set(name, value, options);
66
+ }
67
+ catch (_a) {
68
+ // no-op
69
+ }
70
+ };
71
+ var deleteFunc = function (name) {
72
+ try {
73
+ cookieStore.delete(name);
74
+ }
75
+ catch (_a) {
76
+ // no-op
77
+ }
78
+ };
79
+ return {
80
+ get: cookieStore.get.bind(cookieStore),
81
+ getAll: cookieStore.getAll.bind(cookieStore),
82
+ set: setFunc,
83
+ delete: deleteFunc,
84
+ };
85
+ };
86
+ var createCookieStorageAdapterFromGetServerSidePropsContext = function (request, response) {
87
+ var cookiesMap = __assign({}, request.cookies);
88
+ var allCookies = Object.entries(cookiesMap).map(function (_a) {
89
+ var name = _a[0], value = _a[1];
90
+ return ({
91
+ name: name,
92
+ value: value,
93
+ });
94
+ });
95
+ return {
96
+ get: function (name) {
97
+ var value = cookiesMap[name];
98
+ return value
99
+ ? {
100
+ name: name,
101
+ value: value,
102
+ }
103
+ : undefined;
104
+ },
105
+ getAll: function () {
106
+ return allCookies;
107
+ },
108
+ set: function (name, value, options) {
109
+ response.setHeader('Set-Cookie', "".concat(name, "=").concat(value, ";").concat(options ? serializeSetCookieOptions(options) : ''));
110
+ },
111
+ delete: function (name) {
112
+ response.setHeader('Set-Cookie', "".concat(name, "=;Expires=").concat(DATE_IN_THE_PAST.toUTCString()));
113
+ },
114
+ };
115
+ };
116
+ var createMutableCookieStoreFromHeaders = function (headers) {
117
+ var setFunc = function (name, value, options) {
118
+ headers.append('Set-Cookie', "".concat(name, "=").concat(value, ";").concat(options ? serializeSetCookieOptions(options) : ''));
119
+ };
120
+ var deleteFunc = function (name) {
121
+ headers.append('Set-Cookie', "".concat(name, "=;Expires=").concat(DATE_IN_THE_PAST.toUTCString()));
122
+ };
123
+ return {
124
+ set: setFunc,
125
+ delete: deleteFunc,
126
+ };
127
+ };
128
+ var serializeSetCookieOptions = function (options) {
129
+ var expires = options.expires, maxAge = options.maxAge, domain = options.domain, httpOnly = options.httpOnly, sameSite = options.sameSite, secure = options.secure;
130
+ var serializedOptions = [];
131
+ if (domain) {
132
+ serializedOptions.push("Domain=".concat(domain));
133
+ }
134
+ if (expires) {
135
+ serializedOptions.push("Expires=".concat(expires.toUTCString()));
136
+ }
137
+ if (httpOnly) {
138
+ serializedOptions.push("HttpOnly");
139
+ }
140
+ if (sameSite) {
141
+ serializedOptions.push("SameSite=".concat(sameSite));
142
+ }
143
+ if (secure) {
144
+ serializedOptions.push("Secure");
145
+ }
146
+ return serializedOptions.join(';');
147
+ };
148
+ //# sourceMappingURL=createCookieStorageAdapterFromNextServerContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createCookieStorageAdapterFromNextServerContext.js","sourceRoot":"","sources":["../../src/utils/createCookieStorageAdapterFromNextServerContext.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;;;;;;;;;;;;AAEtC,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAExD,OAAO,EACN,yBAAyB,GAEzB,MAAM,0CAA0C,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAEvD,MAAM,CAAC,IAAM,gBAAgB,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC;AAE5C,MAAM,CAAC,IAAM,+CAA+C,GAAG,UAC9D,OAA2B;IAErB,IAAA,KAAwB,OAEa,EAFnC,OAAO,aAAA,EAAE,QAAQ,cAEkB,CAAC;IAE5C,IAAI,OAAO,YAAY,WAAW,IAAI,QAAQ,EAAE;QAC/C,IAAI,QAAQ,YAAY,YAAY,EAAE;YACrC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,OAAO,GAAiD,OAAO,QAAxD,EAAE,MAAM,GAAyC,OAAO,OAAhD,EAAE,MAAM,GAAiC,OAAO,OAAxC,EAAE,QAAQ,GAAuB,OAAO,SAA9B,EAAE,QAAQ,GAAa,OAAO,SAApB,EAAE,MAAM,GAAK,OAAO,OAAZ,CAAa;IACxE,IAAM,iBAAiB,GAAa,EAAE,CAAC;IACvC,IAAI,MAAM,EAAE;QACX,iBAAiB,CAAC,IAAI,CAAC,iBAAU,MAAM,CAAE,CAAC,CAAC;KAC3C;IACD,IAAI,OAAO,EAAE;QACZ,iBAAiB,CAAC,IAAI,CAAC,kBAAW,OAAO,CAAC,WAAW,EAAE,CAAE,CAAC,CAAC;KAC3D;IACD,IAAI,QAAQ,EAAE;QACb,iBAAiB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;KACnC;IACD,IAAI,QAAQ,EAAE;QACb,iBAAiB,CAAC,IAAI,CAAC,mBAAY,QAAQ,CAAE,CAAC,CAAC;KAC/C;IACD,IAAI,MAAM,EAAE;QACX,iBAAiB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;KACjC;IACD,OAAO,iBAAiB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACpC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { ResourcesConfig } from '@aws-amplify/core';
2
+ export declare const getAmplifyConfig: () => ResourcesConfig;
@@ -0,0 +1,16 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { AmplifyServerContextError } from '@aws-amplify/core/internals/adapter-core';
4
+ export var getAmplifyConfig = function () {
5
+ var configStr = process.env.amplifyConfig;
6
+ if (!configStr) {
7
+ throw new AmplifyServerContextError({
8
+ message: 'Amplify configuration is missing from `process.env`.',
9
+ recoverySuggestion: 'Ensure to use `withAmplify` function in your `next.config.js`.',
10
+ });
11
+ }
12
+ var configObject = JSON.parse(configStr);
13
+ // TODO(HuiSF): adds ResourcesConfig validation when it has one.
14
+ return configObject;
15
+ };
16
+ //# sourceMappingURL=getAmplifyConfig.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getAmplifyConfig.js","sourceRoot":"","sources":["../../src/utils/getAmplifyConfig.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AAGtC,OAAO,EAAE,yBAAyB,EAAE,MAAM,0CAA0C,CAAC;AAErF,MAAM,CAAC,IAAM,gBAAgB,GAAG;IAC/B,IAAM,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC;IAE5C,IAAI,CAAC,SAAS,EAAE;QACf,MAAM,IAAI,yBAAyB,CAAC;YACnC,OAAO,EAAE,sDAAsD;YAC/D,kBAAkB,EACjB,gEAAgE;SACjE,CAAC,CAAC;KACH;IAED,IAAM,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IAE3C,gEAAgE;IAChE,OAAO,YAAY,CAAC;AACrB,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { getAmplifyConfig } from './getAmplifyConfig';
2
+ export { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext';
@@ -0,0 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export { getAmplifyConfig } from './getAmplifyConfig';
4
+ export { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AAEtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,+CAA+C,EAAE,MAAM,mDAAmD,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { ResourcesConfig } from '@aws-amplify/core';
2
+ import { NextConfig } from 'next';
3
+ /**
4
+ * Merges the `amplifyConfig` into the `nextConfig.env`.
5
+ * @param nextConfig The next config for a Next.js app.
6
+ * @param amplifyConfig
7
+ * @returns The updated `nextConfig`.
8
+ */
9
+ export declare const withAmplify: (nextConfig: NextConfig, amplifyConfig: ResourcesConfig) => NextConfig;
@@ -0,0 +1,29 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ var __assign = (this && this.__assign) || function () {
4
+ __assign = Object.assign || function(t) {
5
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
6
+ s = arguments[i];
7
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
8
+ t[p] = s[p];
9
+ }
10
+ return t;
11
+ };
12
+ return __assign.apply(this, arguments);
13
+ };
14
+ // NOTE: this function is exported from the subpath `/with-amplify`.
15
+ // The reason is that this function is called in the `next.config.js` which
16
+ // is not being transpiled by Next.js.
17
+ /**
18
+ * Merges the `amplifyConfig` into the `nextConfig.env`.
19
+ * @param nextConfig The next config for a Next.js app.
20
+ * @param amplifyConfig
21
+ * @returns The updated `nextConfig`.
22
+ */
23
+ export var withAmplify = function (nextConfig, amplifyConfig) {
24
+ nextConfig.env = __assign(__assign({}, nextConfig.env), {
25
+ // TODO(Hui): follow up the validation of the amplifyConfig.
26
+ amplifyConfig: JSON.stringify(amplifyConfig) });
27
+ return nextConfig;
28
+ };
29
+ //# sourceMappingURL=withAmplify.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withAmplify.js","sourceRoot":"","sources":["../src/withAmplify.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;;;;;;;;;;;;AAKtC,oEAAoE;AACpE,2EAA2E;AAC3E,sCAAsC;AAEtC;;;;;GAKG;AACH,MAAM,CAAC,IAAM,WAAW,GAAG,UAC1B,UAAsB,EACtB,aAA8B;IAE9B,UAAU,CAAC,GAAG,yBACV,UAAU,CAAC,GAAG;QACjB,4DAA4D;QAC5D,aAAa,EAAE,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,GAC5C,CAAC;IAEF,OAAO,UAAU,CAAC;AACnB,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,111 @@
1
+ {
2
+ "author": "Amazon Web Services",
3
+ "name": "@aws-amplify/adapter-nextjs",
4
+ "description": "The adapter for the supporting of using Amplify APIs in Next.js.",
5
+ "peerDependencies": {
6
+ "aws-amplify": "^6.0.0",
7
+ "next": ">=13.4.0 <14.0.0"
8
+ },
9
+ "dependencies": {
10
+ "cookie": "0.5.0",
11
+ "server-only": "^0.0.1"
12
+ },
13
+ "devDependencies": {
14
+ "@types/cookie": "0.5.1",
15
+ "@types/node": "^20.3.1",
16
+ "@types/react": "^18.2.13",
17
+ "@types/react-dom": "^18.2.6",
18
+ "aws-amplify": "6.0.1-console-preview.e8aa063.0+e8aa063",
19
+ "jest-fetch-mock": "3.0.3",
20
+ "next": ">= 13.4.0 < 14.0.0",
21
+ "typescript": "5.1.6"
22
+ },
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "bugs": {
27
+ "url": "https://github.com/aws/aws-amplify/issues"
28
+ },
29
+ "exports": {
30
+ ".": {
31
+ "types": "./lib-esm/index.d.ts",
32
+ "import": "./lib-esm/index.js",
33
+ "require": "./lib/index.js"
34
+ },
35
+ "./with-amplify": {
36
+ "types": "./lib-esm/withAmplify.d.ts",
37
+ "import": "./lib-esm/withAmplify.js",
38
+ "require": "./lib/withAmplify.js"
39
+ },
40
+ "./package.json": "./package.json"
41
+ },
42
+ "files": [
43
+ "lib",
44
+ "lib-esm",
45
+ "src",
46
+ "withAmplify"
47
+ ],
48
+ "homepage": "https://aws-amplify.github.io/",
49
+ "jest": {
50
+ "coveragePathIgnorePatterns": [
51
+ "/node_modules/",
52
+ "dist",
53
+ "lib",
54
+ "lib-esm"
55
+ ],
56
+ "coverageThreshold": {
57
+ "global": {
58
+ "branches": 100,
59
+ "functions": 100,
60
+ "lines": 100,
61
+ "statements": 100
62
+ }
63
+ },
64
+ "globals": {
65
+ "ts-jest": {
66
+ "diagnostics": {
67
+ "pathRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$"
68
+ },
69
+ "tsConfig": false
70
+ }
71
+ },
72
+ "moduleFileExtensions": [
73
+ "ts",
74
+ "tsx",
75
+ "js",
76
+ "json",
77
+ "jsx"
78
+ ],
79
+ "testEnvironment": "node",
80
+ "testPathIgnorePatterns": [
81
+ "xmlParser-fixture.ts",
82
+ "testUtils",
83
+ "cases"
84
+ ],
85
+ "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$",
86
+ "testURL": "http://localhost/",
87
+ "transform": {
88
+ "^.+\\.(js|jsx|ts|tsx)$": "ts-jest"
89
+ }
90
+ },
91
+ "license": "Apache-2.0",
92
+ "main": "./lib/index.js",
93
+ "module": "./lib-esm/index.js",
94
+ "scripts": {
95
+ "build": "npm run clean && npm run build:esm && npm run build:cjs",
96
+ "build-with-test": "npm test && npm run build",
97
+ "build:cjs": "rimraf lib && tsc -m commonjs --outDir lib",
98
+ "build:cjs:watch": "rimraf lib && tsc -m commonjs --outDir lib --watch",
99
+ "build:esm": "rimraf lib-esm && tsc -m esnext --outDir lib-esm",
100
+ "build:esm:watch": "rimraf lib-esm && tsc -m esnext --outDir lib-esm --watch",
101
+ "clean": "npm run clean:size && rimraf lib-esm lib",
102
+ "clean:size": "rimraf dual-publish-tmp tmp*",
103
+ "format": "echo \"Not implemented\"",
104
+ "lint": "tslint 'src/**/*.ts' && npm run ts-coverage",
105
+ "test": "npm run lint && jest -w 1 --coverage",
106
+ "ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 90.31"
107
+ },
108
+ "typings": "./lib-esm/index.d.ts",
109
+ "version": "0.0.2-console-preview.e8aa063.0+e8aa063",
110
+ "gitHead": "e8aa0638a10b3b894992e157641db2155a03961e"
111
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { runWithAmplifyServerContext } from './runWithAmplifyServerContext';
@@ -0,0 +1,60 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { NextServer } from './types';
5
+ import {
6
+ createCookieStorageAdapterFromNextServerContext,
7
+ getAmplifyConfig,
8
+ } from './utils';
9
+ import { MemoryKeyValueStorage } from '@aws-amplify/core';
10
+ import {
11
+ createAWSCredentialsAndIdentityIdProvider,
12
+ createKeyValueStorageFromCookieStorageAdapter,
13
+ createUserPoolsTokenProvider,
14
+ runWithAmplifyServerContext as runWithAmplifyServerContextCore,
15
+ } from 'aws-amplify/internals/adapter-core';
16
+
17
+ export const runWithAmplifyServerContext: NextServer.RunOperationWithContext =
18
+ async ({ nextServerContext, operation }) => {
19
+ // 1. get amplify config from env vars
20
+ // 2. create key-value storage from nextServerContext
21
+ // 3. create credentials provider
22
+ // 4. create token provider
23
+ // 5. call low level runWithAmplifyServerContext
24
+ const amplifyConfig = getAmplifyConfig();
25
+
26
+ // When the Auth config is presented, attempt to create a Amplify server
27
+ // context with token and credentials provider.
28
+ if (amplifyConfig.Auth) {
29
+ const keyValueStorage =
30
+ nextServerContext === null
31
+ ? // When `null` is passed as the value of `nextServerContext`, opt-in
32
+ // unauthenticated role (primarily for static rendering). It's
33
+ // safe to use the singleton `MemoryKeyValueStorage` here, as the
34
+ // static rendering uses the same unauthenticated role cross-sever.
35
+ MemoryKeyValueStorage
36
+ : createKeyValueStorageFromCookieStorageAdapter(
37
+ createCookieStorageAdapterFromNextServerContext(nextServerContext)
38
+ );
39
+ const credentialsProvider = createAWSCredentialsAndIdentityIdProvider(
40
+ amplifyConfig.Auth,
41
+ keyValueStorage
42
+ );
43
+ const tokenProvider = createUserPoolsTokenProvider(
44
+ amplifyConfig.Auth,
45
+ keyValueStorage
46
+ );
47
+
48
+ return runWithAmplifyServerContextCore(
49
+ amplifyConfig,
50
+ {
51
+ Auth: { credentialsProvider, tokenProvider },
52
+ },
53
+ operation
54
+ );
55
+ }
56
+
57
+ // Otherwise it may be the case that auth is not used, e.g. API key.
58
+ // Omitting the `Auth` in the second parameter.
59
+ return runWithAmplifyServerContextCore(amplifyConfig, {}, operation);
60
+ };