@aws-amplify/adapter-nextjs
Advanced tools
Comparing version
@@ -5,5 +5,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.runWithAmplifyServerContext = void 0; | ||
var runWithAmplifyServerContext_1 = require("./runWithAmplifyServerContext"); | ||
Object.defineProperty(exports, "runWithAmplifyServerContext", { enumerable: true, get: function () { return runWithAmplifyServerContext_1.runWithAmplifyServerContext; } }); | ||
exports.createServerRunner = void 0; | ||
var createServerRunner_1 = require("./createServerRunner"); | ||
Object.defineProperty(exports, "createServerRunner", { enumerable: true, get: function () { return createServerRunner_1.createServerRunner; } }); | ||
//# sourceMappingURL=index.js.map |
@@ -1,1 +0,1 @@ | ||
export { runWithAmplifyServerContext } from './runWithAmplifyServerContext'; | ||
export { createServerRunner } from './createServerRunner'; |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
export { runWithAmplifyServerContext } from './runWithAmplifyServerContext'; | ||
export { createServerRunner } from './createServerRunner'; | ||
//# sourceMappingURL=index.js.map |
import { GetServerSidePropsContext as NextGetServerSidePropsContext } from 'next'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { cookies } from 'next/headers'; | ||
import { LegacyConfig } from 'aws-amplify/adapter-core'; | ||
import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core'; | ||
import { ResourcesConfig } from '@aws-amplify/core'; | ||
export declare namespace NextServer { | ||
@@ -45,3 +47,9 @@ /** | ||
}; | ||
/** | ||
* The union of possible Next.js app server context types. | ||
*/ | ||
type Context = NextRequestAndNextResponseContext | NextRequestAndResponseContext | ServerComponentContext | GetServerSidePropsContext; | ||
/** | ||
* The interface of the input of {@link RunOperationWithContext}. | ||
*/ | ||
interface RunWithContextInput<OperationResult> { | ||
@@ -54,2 +62,11 @@ nextServerContext: Context | null; | ||
} | ||
interface CreateServerRunnerInput { | ||
config: ResourcesConfig | LegacyConfig; | ||
} | ||
interface CreateServerRunnerOutput { | ||
runWithAmplifyServerContext: RunOperationWithContext; | ||
} | ||
interface CreateServerRunner { | ||
(input: CreateServerRunnerInput): CreateServerRunnerOutput; | ||
} | ||
} |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { NextResponse } from 'next/server'; | ||
import { AmplifyServerContextError, } from '@aws-amplify/core/internals/adapter-core'; | ||
import { IncomingMessage, ServerResponse } from 'http'; | ||
export var DATE_IN_THE_PAST = new Date(0); | ||
export var createCookieStorageAdapterFromNextServerContext = function (context) { | ||
var _a = context, request = _a.request, response = _a.response; | ||
if (request instanceof NextRequest && response) { | ||
export const DATE_IN_THE_PAST = new Date(0); | ||
export const createCookieStorageAdapterFromNextServerContext = (context) => { | ||
const { request: req, response: res } = context; | ||
// When the server context is from `getServerSideProps`, the `req` is an instance | ||
// of IncomingMessage, and the `res` is an instance of ServerResponse. | ||
// We cannot import these two classes here from `http` as it breaks in Next | ||
// Edge Runtime. Hence, we check the methods that we need to use for creating | ||
// cookie adapter. | ||
if (req && | ||
res && | ||
Object.prototype.toString.call(req.cookies) === '[object Object]' && | ||
typeof res.setHeader === 'function') { | ||
return createCookieStorageAdapterFromGetServerSidePropsContext(req, res); | ||
} | ||
const { request, response } = context; | ||
// When the server context is from `middleware`, the `request` is an instance | ||
// of `NextRequest`. | ||
// When the server context is from a route handler, the `request` is an `Proxy` | ||
// wrapped `Request`. | ||
// The `NextRequest` and the `Proxy` are sharing the same interface by Next | ||
// implementation. So we don't need to detect the difference. | ||
if (request && response) { | ||
if (response instanceof NextResponse) { | ||
@@ -28,10 +34,6 @@ return createCookieStorageAdapterFromNextRequestAndNextResponse(request, response); | ||
} | ||
var cookies = context.cookies; | ||
const { cookies } = context; | ||
if (typeof cookies === 'function') { | ||
return createCookieStorageAdapterFromNextCookies(cookies); | ||
} | ||
var _b = context, req = _b.request, res = _b.response; | ||
if (req instanceof IncomingMessage && res instanceof ServerResponse) { | ||
return createCookieStorageAdapterFromGetServerSidePropsContext(req, res); | ||
} | ||
// This should not happen normally. | ||
@@ -42,19 +44,31 @@ throw new AmplifyServerContextError({ | ||
}; | ||
var createCookieStorageAdapterFromNextRequestAndNextResponse = function (request, response) { | ||
var readonlyCookieStore = request.cookies; | ||
var mutableCookieStore = response.cookies; | ||
const createCookieStorageAdapterFromNextRequestAndNextResponse = (request, response) => { | ||
const readonlyCookieStore = request.cookies; | ||
const mutableCookieStore = response.cookies; | ||
return { | ||
get: readonlyCookieStore.get.bind(readonlyCookieStore), | ||
get(name) { | ||
return readonlyCookieStore.get(ensureEncodedForJSCookie(name)); | ||
}, | ||
getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore), | ||
set: mutableCookieStore.set.bind(mutableCookieStore), | ||
delete: mutableCookieStore.delete.bind(mutableCookieStore), | ||
set(name, value, options) { | ||
mutableCookieStore.set(ensureEncodedForJSCookie(name), value, options); | ||
}, | ||
delete(name) { | ||
mutableCookieStore.delete(ensureEncodedForJSCookie(name)); | ||
}, | ||
}; | ||
}; | ||
var createCookieStorageAdapterFromNextRequestAndHttpResponse = function (request, response) { | ||
var readonlyCookieStore = request.cookies; | ||
var mutableCookieStore = createMutableCookieStoreFromHeaders(response.headers); | ||
return __assign({ get: readonlyCookieStore.get.bind(readonlyCookieStore), getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore) }, mutableCookieStore); | ||
const createCookieStorageAdapterFromNextRequestAndHttpResponse = (request, response) => { | ||
const readonlyCookieStore = request.cookies; | ||
const mutableCookieStore = createMutableCookieStoreFromHeaders(response.headers); | ||
return { | ||
get(name) { | ||
return readonlyCookieStore.get(ensureEncodedForJSCookie(name)); | ||
}, | ||
getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore), | ||
...mutableCookieStore, | ||
}; | ||
}; | ||
var createCookieStorageAdapterFromNextCookies = function (cookies) { | ||
var cookieStore = cookies(); | ||
const createCookieStorageAdapterFromNextCookies = (cookies) => { | ||
const cookieStore = cookies(); | ||
// When Next cookies() is called in a server component, it returns a readonly | ||
@@ -65,15 +79,15 @@ // cookie store. Hence calling set and delete throws an error. However, | ||
// and safely ignore the error if it is thrown. | ||
var setFunc = function (name, value, options) { | ||
const setFunc = (name, value, options) => { | ||
try { | ||
cookieStore.set(name, value, options); | ||
cookieStore.set(ensureEncodedForJSCookie(name), value, options); | ||
} | ||
catch (_a) { | ||
catch { | ||
// no-op | ||
} | ||
}; | ||
var deleteFunc = function (name) { | ||
const deleteFunc = name => { | ||
try { | ||
cookieStore.delete(name); | ||
cookieStore.delete(ensureEncodedForJSCookie(name)); | ||
} | ||
catch (_a) { | ||
catch { | ||
// no-op | ||
@@ -83,3 +97,5 @@ } | ||
return { | ||
get: cookieStore.get.bind(cookieStore), | ||
get(name) { | ||
return cookieStore.get(ensureEncodedForJSCookie(name)); | ||
}, | ||
getAll: cookieStore.getAll.bind(cookieStore), | ||
@@ -90,38 +106,35 @@ set: setFunc, | ||
}; | ||
var createCookieStorageAdapterFromGetServerSidePropsContext = function (request, response) { | ||
var cookiesMap = __assign({}, request.cookies); | ||
var allCookies = Object.entries(cookiesMap).map(function (_a) { | ||
var name = _a[0], value = _a[1]; | ||
return ({ | ||
name: name, | ||
value: value, | ||
}); | ||
}); | ||
const createCookieStorageAdapterFromGetServerSidePropsContext = (request, response) => { | ||
const cookiesMap = { ...request.cookies }; | ||
const allCookies = Object.entries(cookiesMap).map(([name, value]) => ({ | ||
name, | ||
value, | ||
})); | ||
return { | ||
get: function (name) { | ||
var value = cookiesMap[name]; | ||
get(name) { | ||
const value = cookiesMap[ensureEncodedForJSCookie(name)]; | ||
return value | ||
? { | ||
name: name, | ||
value: value, | ||
name, | ||
value, | ||
} | ||
: undefined; | ||
}, | ||
getAll: function () { | ||
getAll() { | ||
return allCookies; | ||
}, | ||
set: function (name, value, options) { | ||
response.setHeader('Set-Cookie', "".concat(name, "=").concat(value, ";").concat(options ? serializeSetCookieOptions(options) : '')); | ||
set(name, value, options) { | ||
response.setHeader('Set-Cookie', `${ensureEncodedForJSCookie(name)}=${value};${options ? serializeSetCookieOptions(options) : ''}`); | ||
}, | ||
delete: function (name) { | ||
response.setHeader('Set-Cookie', "".concat(name, "=;Expires=").concat(DATE_IN_THE_PAST.toUTCString())); | ||
delete(name) { | ||
response.setHeader('Set-Cookie', `${ensureEncodedForJSCookie(name)}=;Expires=${DATE_IN_THE_PAST.toUTCString()}`); | ||
}, | ||
}; | ||
}; | ||
var createMutableCookieStoreFromHeaders = function (headers) { | ||
var setFunc = function (name, value, options) { | ||
headers.append('Set-Cookie', "".concat(name, "=").concat(value, ";").concat(options ? serializeSetCookieOptions(options) : '')); | ||
const createMutableCookieStoreFromHeaders = (headers) => { | ||
const setFunc = (name, value, options) => { | ||
headers.append('Set-Cookie', `${ensureEncodedForJSCookie(name)}=${value};${options ? serializeSetCookieOptions(options) : ''}`); | ||
}; | ||
var deleteFunc = function (name) { | ||
headers.append('Set-Cookie', "".concat(name, "=;Expires=").concat(DATE_IN_THE_PAST.toUTCString())); | ||
const deleteFunc = name => { | ||
headers.append('Set-Cookie', `${ensureEncodedForJSCookie(name)}=;Expires=${DATE_IN_THE_PAST.toUTCString()}`); | ||
}; | ||
@@ -133,22 +146,29 @@ return { | ||
}; | ||
var serializeSetCookieOptions = function (options) { | ||
var expires = options.expires, maxAge = options.maxAge, domain = options.domain, httpOnly = options.httpOnly, sameSite = options.sameSite, secure = options.secure; | ||
var serializedOptions = []; | ||
const serializeSetCookieOptions = (options) => { | ||
const { expires, domain, httpOnly, sameSite, secure } = options; | ||
const serializedOptions = []; | ||
if (domain) { | ||
serializedOptions.push("Domain=".concat(domain)); | ||
serializedOptions.push(`Domain=${domain}`); | ||
} | ||
if (expires) { | ||
serializedOptions.push("Expires=".concat(expires.toUTCString())); | ||
serializedOptions.push(`Expires=${expires.toUTCString()}`); | ||
} | ||
if (httpOnly) { | ||
serializedOptions.push("HttpOnly"); | ||
serializedOptions.push(`HttpOnly`); | ||
} | ||
if (sameSite) { | ||
serializedOptions.push("SameSite=".concat(sameSite)); | ||
serializedOptions.push(`SameSite=${sameSite}`); | ||
} | ||
if (secure) { | ||
serializedOptions.push("Secure"); | ||
serializedOptions.push(`Secure`); | ||
} | ||
return serializedOptions.join(';'); | ||
}; | ||
// Ensures the cookie names are encoded in order to look up the cookie store | ||
// that is manipulated by js-cookie on the client side. | ||
// Details of the js-cookie encoding behavior see: | ||
// https://github.com/js-cookie/js-cookie#encoding | ||
// The implementation is borrowed from js-cookie without escaping `[()]` as | ||
// we are not using those chars in the auth keys. | ||
const ensureEncodedForJSCookie = (name) => encodeURIComponent(name).replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent); | ||
//# sourceMappingURL=createCookieStorageAdapterFromNextServerContext.js.map |
@@ -1,2 +0,3 @@ | ||
import { ResourcesConfig } from '@aws-amplify/core'; | ||
export declare const getAmplifyConfig: () => ResourcesConfig; | ||
import { ResourcesConfig } from 'aws-amplify'; | ||
import { LegacyConfig } from 'aws-amplify/adapter-core'; | ||
export declare const getAmplifyConfig: (config: ResourcesConfig | LegacyConfig) => ResourcesConfig; |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { AmplifyServerContextError } from '@aws-amplify/core/internals/adapter-core'; | ||
export var getAmplifyConfig = function () { | ||
var configStr = process.env.amplifyConfig; | ||
if (!configStr) { | ||
throw new AmplifyServerContextError({ | ||
message: 'Amplify configuration is missing from `process.env`.', | ||
recoverySuggestion: 'Ensure to use `withAmplify` function in your `next.config.js`.', | ||
}); | ||
} | ||
var configObject = JSON.parse(configStr); | ||
// TODO(HuiSF): adds ResourcesConfig validation when it has one. | ||
return configObject; | ||
}; | ||
import { parseAWSExports } from '@aws-amplify/core/internals/utils'; | ||
export const getAmplifyConfig = (config) => Object.keys(config).some(key => key.startsWith('aws_')) | ||
? parseAWSExports(config) | ||
: config; | ||
//# sourceMappingURL=getAmplifyConfig.js.map |
export { getAmplifyConfig } from './getAmplifyConfig'; | ||
export { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext'; | ||
export { createRunWithAmplifyServerContext } from './createRunWithAmplifyServerContext'; |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
export { getAmplifyConfig } from './getAmplifyConfig'; | ||
export { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext'; | ||
export { createRunWithAmplifyServerContext } from './createRunWithAmplifyServerContext'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,1 +0,1 @@ | ||
export { runWithAmplifyServerContext } from './runWithAmplifyServerContext'; | ||
export { createServerRunner } from './createServerRunner'; |
@@ -5,5 +5,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.runWithAmplifyServerContext = void 0; | ||
var runWithAmplifyServerContext_1 = require("./runWithAmplifyServerContext"); | ||
Object.defineProperty(exports, "runWithAmplifyServerContext", { enumerable: true, get: function () { return runWithAmplifyServerContext_1.runWithAmplifyServerContext; } }); | ||
exports.createServerRunner = void 0; | ||
var createServerRunner_1 = require("./createServerRunner"); | ||
Object.defineProperty(exports, "createServerRunner", { enumerable: true, get: function () { return createServerRunner_1.createServerRunner; } }); | ||
//# sourceMappingURL=index.js.map |
import { GetServerSidePropsContext as NextGetServerSidePropsContext } from 'next'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { cookies } from 'next/headers'; | ||
import { LegacyConfig } from 'aws-amplify/adapter-core'; | ||
import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core'; | ||
import { ResourcesConfig } from '@aws-amplify/core'; | ||
export declare namespace NextServer { | ||
@@ -45,3 +47,9 @@ /** | ||
}; | ||
/** | ||
* The union of possible Next.js app server context types. | ||
*/ | ||
type Context = NextRequestAndNextResponseContext | NextRequestAndResponseContext | ServerComponentContext | GetServerSidePropsContext; | ||
/** | ||
* The interface of the input of {@link RunOperationWithContext}. | ||
*/ | ||
interface RunWithContextInput<OperationResult> { | ||
@@ -54,2 +62,11 @@ nextServerContext: Context | null; | ||
} | ||
interface CreateServerRunnerInput { | ||
config: ResourcesConfig | LegacyConfig; | ||
} | ||
interface CreateServerRunnerOutput { | ||
runWithAmplifyServerContext: RunOperationWithContext; | ||
} | ||
interface CreateServerRunner { | ||
(input: CreateServerRunnerInput): CreateServerRunnerOutput; | ||
} | ||
} |
"use strict"; | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createCookieStorageAdapterFromNextServerContext = exports.DATE_IN_THE_PAST = void 0; | ||
var server_1 = require("next/server"); | ||
var adapter_core_1 = require("@aws-amplify/core/internals/adapter-core"); | ||
var http_1 = require("http"); | ||
const server_1 = require("next/server"); | ||
const adapter_core_1 = require("@aws-amplify/core/internals/adapter-core"); | ||
exports.DATE_IN_THE_PAST = new Date(0); | ||
var createCookieStorageAdapterFromNextServerContext = function (context) { | ||
var _a = context, request = _a.request, response = _a.response; | ||
if (request instanceof server_1.NextRequest && response) { | ||
const createCookieStorageAdapterFromNextServerContext = (context) => { | ||
const { request: req, response: res } = context; | ||
// When the server context is from `getServerSideProps`, the `req` is an instance | ||
// of IncomingMessage, and the `res` is an instance of ServerResponse. | ||
// We cannot import these two classes here from `http` as it breaks in Next | ||
// Edge Runtime. Hence, we check the methods that we need to use for creating | ||
// cookie adapter. | ||
if (req && | ||
res && | ||
Object.prototype.toString.call(req.cookies) === '[object Object]' && | ||
typeof res.setHeader === 'function') { | ||
return createCookieStorageAdapterFromGetServerSidePropsContext(req, res); | ||
} | ||
const { request, response } = context; | ||
// When the server context is from `middleware`, the `request` is an instance | ||
// of `NextRequest`. | ||
// When the server context is from a route handler, the `request` is an `Proxy` | ||
// wrapped `Request`. | ||
// The `NextRequest` and the `Proxy` are sharing the same interface by Next | ||
// implementation. So we don't need to detect the difference. | ||
if (request && response) { | ||
if (response instanceof server_1.NextResponse) { | ||
@@ -31,10 +37,6 @@ return createCookieStorageAdapterFromNextRequestAndNextResponse(request, response); | ||
} | ||
var cookies = context.cookies; | ||
const { cookies } = context; | ||
if (typeof cookies === 'function') { | ||
return createCookieStorageAdapterFromNextCookies(cookies); | ||
} | ||
var _b = context, req = _b.request, res = _b.response; | ||
if (req instanceof http_1.IncomingMessage && res instanceof http_1.ServerResponse) { | ||
return createCookieStorageAdapterFromGetServerSidePropsContext(req, res); | ||
} | ||
// This should not happen normally. | ||
@@ -46,19 +48,31 @@ throw new adapter_core_1.AmplifyServerContextError({ | ||
exports.createCookieStorageAdapterFromNextServerContext = createCookieStorageAdapterFromNextServerContext; | ||
var createCookieStorageAdapterFromNextRequestAndNextResponse = function (request, response) { | ||
var readonlyCookieStore = request.cookies; | ||
var mutableCookieStore = response.cookies; | ||
const createCookieStorageAdapterFromNextRequestAndNextResponse = (request, response) => { | ||
const readonlyCookieStore = request.cookies; | ||
const mutableCookieStore = response.cookies; | ||
return { | ||
get: readonlyCookieStore.get.bind(readonlyCookieStore), | ||
get(name) { | ||
return readonlyCookieStore.get(ensureEncodedForJSCookie(name)); | ||
}, | ||
getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore), | ||
set: mutableCookieStore.set.bind(mutableCookieStore), | ||
delete: mutableCookieStore.delete.bind(mutableCookieStore), | ||
set(name, value, options) { | ||
mutableCookieStore.set(ensureEncodedForJSCookie(name), value, options); | ||
}, | ||
delete(name) { | ||
mutableCookieStore.delete(ensureEncodedForJSCookie(name)); | ||
}, | ||
}; | ||
}; | ||
var createCookieStorageAdapterFromNextRequestAndHttpResponse = function (request, response) { | ||
var readonlyCookieStore = request.cookies; | ||
var mutableCookieStore = createMutableCookieStoreFromHeaders(response.headers); | ||
return __assign({ get: readonlyCookieStore.get.bind(readonlyCookieStore), getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore) }, mutableCookieStore); | ||
const createCookieStorageAdapterFromNextRequestAndHttpResponse = (request, response) => { | ||
const readonlyCookieStore = request.cookies; | ||
const mutableCookieStore = createMutableCookieStoreFromHeaders(response.headers); | ||
return { | ||
get(name) { | ||
return readonlyCookieStore.get(ensureEncodedForJSCookie(name)); | ||
}, | ||
getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore), | ||
...mutableCookieStore, | ||
}; | ||
}; | ||
var createCookieStorageAdapterFromNextCookies = function (cookies) { | ||
var cookieStore = cookies(); | ||
const createCookieStorageAdapterFromNextCookies = (cookies) => { | ||
const cookieStore = cookies(); | ||
// When Next cookies() is called in a server component, it returns a readonly | ||
@@ -69,15 +83,15 @@ // cookie store. Hence calling set and delete throws an error. However, | ||
// and safely ignore the error if it is thrown. | ||
var setFunc = function (name, value, options) { | ||
const setFunc = (name, value, options) => { | ||
try { | ||
cookieStore.set(name, value, options); | ||
cookieStore.set(ensureEncodedForJSCookie(name), value, options); | ||
} | ||
catch (_a) { | ||
catch { | ||
// no-op | ||
} | ||
}; | ||
var deleteFunc = function (name) { | ||
const deleteFunc = name => { | ||
try { | ||
cookieStore.delete(name); | ||
cookieStore.delete(ensureEncodedForJSCookie(name)); | ||
} | ||
catch (_a) { | ||
catch { | ||
// no-op | ||
@@ -87,3 +101,5 @@ } | ||
return { | ||
get: cookieStore.get.bind(cookieStore), | ||
get(name) { | ||
return cookieStore.get(ensureEncodedForJSCookie(name)); | ||
}, | ||
getAll: cookieStore.getAll.bind(cookieStore), | ||
@@ -94,38 +110,35 @@ set: setFunc, | ||
}; | ||
var createCookieStorageAdapterFromGetServerSidePropsContext = function (request, response) { | ||
var cookiesMap = __assign({}, request.cookies); | ||
var allCookies = Object.entries(cookiesMap).map(function (_a) { | ||
var name = _a[0], value = _a[1]; | ||
return ({ | ||
name: name, | ||
value: value, | ||
}); | ||
}); | ||
const createCookieStorageAdapterFromGetServerSidePropsContext = (request, response) => { | ||
const cookiesMap = { ...request.cookies }; | ||
const allCookies = Object.entries(cookiesMap).map(([name, value]) => ({ | ||
name, | ||
value, | ||
})); | ||
return { | ||
get: function (name) { | ||
var value = cookiesMap[name]; | ||
get(name) { | ||
const value = cookiesMap[ensureEncodedForJSCookie(name)]; | ||
return value | ||
? { | ||
name: name, | ||
value: value, | ||
name, | ||
value, | ||
} | ||
: undefined; | ||
}, | ||
getAll: function () { | ||
getAll() { | ||
return allCookies; | ||
}, | ||
set: function (name, value, options) { | ||
response.setHeader('Set-Cookie', "".concat(name, "=").concat(value, ";").concat(options ? serializeSetCookieOptions(options) : '')); | ||
set(name, value, options) { | ||
response.setHeader('Set-Cookie', `${ensureEncodedForJSCookie(name)}=${value};${options ? serializeSetCookieOptions(options) : ''}`); | ||
}, | ||
delete: function (name) { | ||
response.setHeader('Set-Cookie', "".concat(name, "=;Expires=").concat(exports.DATE_IN_THE_PAST.toUTCString())); | ||
delete(name) { | ||
response.setHeader('Set-Cookie', `${ensureEncodedForJSCookie(name)}=;Expires=${exports.DATE_IN_THE_PAST.toUTCString()}`); | ||
}, | ||
}; | ||
}; | ||
var createMutableCookieStoreFromHeaders = function (headers) { | ||
var setFunc = function (name, value, options) { | ||
headers.append('Set-Cookie', "".concat(name, "=").concat(value, ";").concat(options ? serializeSetCookieOptions(options) : '')); | ||
const createMutableCookieStoreFromHeaders = (headers) => { | ||
const setFunc = (name, value, options) => { | ||
headers.append('Set-Cookie', `${ensureEncodedForJSCookie(name)}=${value};${options ? serializeSetCookieOptions(options) : ''}`); | ||
}; | ||
var deleteFunc = function (name) { | ||
headers.append('Set-Cookie', "".concat(name, "=;Expires=").concat(exports.DATE_IN_THE_PAST.toUTCString())); | ||
const deleteFunc = name => { | ||
headers.append('Set-Cookie', `${ensureEncodedForJSCookie(name)}=;Expires=${exports.DATE_IN_THE_PAST.toUTCString()}`); | ||
}; | ||
@@ -137,22 +150,29 @@ return { | ||
}; | ||
var serializeSetCookieOptions = function (options) { | ||
var expires = options.expires, maxAge = options.maxAge, domain = options.domain, httpOnly = options.httpOnly, sameSite = options.sameSite, secure = options.secure; | ||
var serializedOptions = []; | ||
const serializeSetCookieOptions = (options) => { | ||
const { expires, domain, httpOnly, sameSite, secure } = options; | ||
const serializedOptions = []; | ||
if (domain) { | ||
serializedOptions.push("Domain=".concat(domain)); | ||
serializedOptions.push(`Domain=${domain}`); | ||
} | ||
if (expires) { | ||
serializedOptions.push("Expires=".concat(expires.toUTCString())); | ||
serializedOptions.push(`Expires=${expires.toUTCString()}`); | ||
} | ||
if (httpOnly) { | ||
serializedOptions.push("HttpOnly"); | ||
serializedOptions.push(`HttpOnly`); | ||
} | ||
if (sameSite) { | ||
serializedOptions.push("SameSite=".concat(sameSite)); | ||
serializedOptions.push(`SameSite=${sameSite}`); | ||
} | ||
if (secure) { | ||
serializedOptions.push("Secure"); | ||
serializedOptions.push(`Secure`); | ||
} | ||
return serializedOptions.join(';'); | ||
}; | ||
// Ensures the cookie names are encoded in order to look up the cookie store | ||
// that is manipulated by js-cookie on the client side. | ||
// Details of the js-cookie encoding behavior see: | ||
// https://github.com/js-cookie/js-cookie#encoding | ||
// The implementation is borrowed from js-cookie without escaping `[()]` as | ||
// we are not using those chars in the auth keys. | ||
const ensureEncodedForJSCookie = (name) => encodeURIComponent(name).replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent); | ||
//# sourceMappingURL=createCookieStorageAdapterFromNextServerContext.js.map |
@@ -1,2 +0,3 @@ | ||
import { ResourcesConfig } from '@aws-amplify/core'; | ||
export declare const getAmplifyConfig: () => ResourcesConfig; | ||
import { ResourcesConfig } from 'aws-amplify'; | ||
import { LegacyConfig } from 'aws-amplify/adapter-core'; | ||
export declare const getAmplifyConfig: (config: ResourcesConfig | LegacyConfig) => ResourcesConfig; |
@@ -6,16 +6,7 @@ "use strict"; | ||
exports.getAmplifyConfig = void 0; | ||
var adapter_core_1 = require("@aws-amplify/core/internals/adapter-core"); | ||
var getAmplifyConfig = function () { | ||
var configStr = process.env.amplifyConfig; | ||
if (!configStr) { | ||
throw new adapter_core_1.AmplifyServerContextError({ | ||
message: 'Amplify configuration is missing from `process.env`.', | ||
recoverySuggestion: 'Ensure to use `withAmplify` function in your `next.config.js`.', | ||
}); | ||
} | ||
var configObject = JSON.parse(configStr); | ||
// TODO(HuiSF): adds ResourcesConfig validation when it has one. | ||
return configObject; | ||
}; | ||
const utils_1 = require("@aws-amplify/core/internals/utils"); | ||
const getAmplifyConfig = (config) => Object.keys(config).some(key => key.startsWith('aws_')) | ||
? (0, utils_1.parseAWSExports)(config) | ||
: config; | ||
exports.getAmplifyConfig = getAmplifyConfig; | ||
//# sourceMappingURL=getAmplifyConfig.js.map |
export { getAmplifyConfig } from './getAmplifyConfig'; | ||
export { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext'; | ||
export { createRunWithAmplifyServerContext } from './createRunWithAmplifyServerContext'; |
@@ -5,7 +5,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createCookieStorageAdapterFromNextServerContext = exports.getAmplifyConfig = void 0; | ||
exports.createRunWithAmplifyServerContext = exports.getAmplifyConfig = void 0; | ||
var getAmplifyConfig_1 = require("./getAmplifyConfig"); | ||
Object.defineProperty(exports, "getAmplifyConfig", { enumerable: true, get: function () { return getAmplifyConfig_1.getAmplifyConfig; } }); | ||
var createCookieStorageAdapterFromNextServerContext_1 = require("./createCookieStorageAdapterFromNextServerContext"); | ||
Object.defineProperty(exports, "createCookieStorageAdapterFromNextServerContext", { enumerable: true, get: function () { return createCookieStorageAdapterFromNextServerContext_1.createCookieStorageAdapterFromNextServerContext; } }); | ||
var createRunWithAmplifyServerContext_1 = require("./createRunWithAmplifyServerContext"); | ||
Object.defineProperty(exports, "createRunWithAmplifyServerContext", { enumerable: true, get: function () { return createRunWithAmplifyServerContext_1.createRunWithAmplifyServerContext; } }); | ||
//# sourceMappingURL=index.js.map |
227
package.json
{ | ||
"author": "Amazon Web Services", | ||
"name": "@aws-amplify/adapter-nextjs", | ||
"description": "The adapter for the supporting of using Amplify APIs in Next.js.", | ||
"peerDependencies": { | ||
"aws-amplify": "^6.0.0", | ||
"next": ">=13.4.0 <14.0.0" | ||
}, | ||
"dependencies": { | ||
"cookie": "0.5.0", | ||
"server-only": "^0.0.1" | ||
}, | ||
"devDependencies": { | ||
"@types/cookie": "0.5.1", | ||
"@types/node": "^20.3.1", | ||
"@types/react": "^18.2.13", | ||
"@types/react-dom": "^18.2.6", | ||
"aws-amplify": "6.0.1-ui-preview.1aa6ecb.0+1aa6ecb", | ||
"jest-fetch-mock": "3.0.3", | ||
"next": ">= 13.4.0 < 14.0.0", | ||
"typescript": "5.1.6" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/aws/aws-amplify/issues" | ||
}, | ||
"exports": { | ||
".": { | ||
"types": "./lib-esm/index.d.ts", | ||
"import": "./lib-esm/index.js", | ||
"require": "./lib/index.js" | ||
}, | ||
"./with-amplify": { | ||
"types": "./lib-esm/withAmplify.d.ts", | ||
"import": "./lib-esm/withAmplify.js", | ||
"require": "./lib/withAmplify.js" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"files": [ | ||
"lib", | ||
"lib-esm", | ||
"src", | ||
"withAmplify" | ||
], | ||
"homepage": "https://aws-amplify.github.io/", | ||
"jest": { | ||
"coveragePathIgnorePatterns": [ | ||
"/node_modules/", | ||
"dist", | ||
"lib", | ||
"lib-esm" | ||
], | ||
"coverageThreshold": { | ||
"global": { | ||
"branches": 100, | ||
"functions": 100, | ||
"lines": 100, | ||
"statements": 100 | ||
} | ||
}, | ||
"globals": { | ||
"ts-jest": { | ||
"diagnostics": { | ||
"pathRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$" | ||
}, | ||
"tsConfig": false | ||
} | ||
}, | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"json", | ||
"jsx" | ||
], | ||
"testEnvironment": "node", | ||
"testPathIgnorePatterns": [ | ||
"xmlParser-fixture.ts", | ||
"testUtils", | ||
"cases" | ||
], | ||
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$", | ||
"testURL": "http://localhost/", | ||
"transform": { | ||
"^.+\\.(js|jsx|ts|tsx)$": "ts-jest" | ||
} | ||
}, | ||
"license": "Apache-2.0", | ||
"main": "./lib/index.js", | ||
"module": "./lib-esm/index.js", | ||
"scripts": { | ||
"build": "npm run clean && npm run build:esm && npm run build:cjs", | ||
"build-with-test": "npm test && npm run build", | ||
"build:cjs": "rimraf lib && tsc -m commonjs --outDir lib", | ||
"build:cjs:watch": "rimraf lib && tsc -m commonjs --outDir lib --watch", | ||
"build:esm": "rimraf lib-esm && tsc -m esnext --outDir lib-esm", | ||
"build:esm:watch": "rimraf lib-esm && tsc -m esnext --outDir lib-esm --watch", | ||
"clean": "npm run clean:size && rimraf lib-esm lib", | ||
"clean:size": "rimraf dual-publish-tmp tmp*", | ||
"format": "echo \"Not implemented\"", | ||
"lint": "tslint 'src/**/*.ts' && npm run ts-coverage", | ||
"test": "npm run lint && jest -w 1 --coverage", | ||
"ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 90.31" | ||
}, | ||
"typings": "./lib-esm/index.d.ts", | ||
"version": "0.0.2-ui-preview.1aa6ecb.0+1aa6ecb", | ||
"gitHead": "1aa6ecbefb4ba4c8f591fa7dd9f3453ca0996280" | ||
"author": "Amazon Web Services", | ||
"name": "@aws-amplify/adapter-nextjs", | ||
"description": "The adapter for the supporting of using Amplify APIs in Next.js.", | ||
"peerDependencies": { | ||
"aws-amplify": "6.0.1-api-v6-models.c495d1b.0+c495d1b", | ||
"next": ">=13.4.0 <14.0.0" | ||
}, | ||
"dependencies": { | ||
"cookie": "0.5.0", | ||
"server-only": "^0.0.1" | ||
}, | ||
"devDependencies": { | ||
"@types/cookie": "0.5.1", | ||
"@types/node": "^20.3.1", | ||
"@types/react": "^18.2.13", | ||
"@types/react-dom": "^18.2.6", | ||
"aws-amplify": "6.0.1-api-v6-models.c495d1b.0+c495d1b", | ||
"jest-fetch-mock": "3.0.3", | ||
"next": ">= 13.4.0 < 14.0.0", | ||
"typescript": "5.1.6" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/aws/aws-amplify/issues" | ||
}, | ||
"exports": { | ||
".": { | ||
"types": "./lib-esm/index.d.ts", | ||
"import": "./lib-esm/index.js", | ||
"require": "./lib/index.js" | ||
}, | ||
"./api": { | ||
"types": "./lib-esm/api/index.d.ts", | ||
"import": "./lib-esm/api/index.js", | ||
"require": "./lib/api/index.js" | ||
}, | ||
"./internals": { | ||
"types": "./lib-esm/internals/index.d.ts", | ||
"import": "./lib-esm/internals/index.js", | ||
"require": "./lib/internals/index.js" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"files": [ | ||
"lib", | ||
"lib-esm", | ||
"src", | ||
"with-amplify", | ||
"internals" | ||
], | ||
"homepage": "https://aws-amplify.github.io/", | ||
"jest": { | ||
"coveragePathIgnorePatterns": [ | ||
"/node_modules/", | ||
"dist", | ||
"lib", | ||
"lib-esm", | ||
"__tests__/mocks" | ||
], | ||
"coverageThreshold": { | ||
"global": { | ||
"branches": 88, | ||
"functions": 90, | ||
"lines": 92, | ||
"statements": 93 | ||
} | ||
}, | ||
"globals": { | ||
"ts-jest": { | ||
"diagnostics": { | ||
"pathRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$" | ||
}, | ||
"tsConfig": false | ||
} | ||
}, | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"json", | ||
"jsx" | ||
], | ||
"testEnvironment": "node", | ||
"testPathIgnorePatterns": [ | ||
"xmlParser-fixture.ts", | ||
"testUtils", | ||
"cases", | ||
"mocks" | ||
], | ||
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$", | ||
"testURL": "http://localhost/", | ||
"transform": { | ||
"^.+\\.(js|jsx|ts|tsx)$": "ts-jest" | ||
} | ||
}, | ||
"license": "Apache-2.0", | ||
"main": "./lib/index.js", | ||
"module": "./lib-esm/index.js", | ||
"sideEffects": false, | ||
"scripts": { | ||
"build": "npm run clean && npm run build:esm && npm run build:cjs", | ||
"build-with-test": "npm test && npm run build", | ||
"build:cjs": "rimraf lib && tsc -m commonjs --outDir lib", | ||
"build:cjs:watch": "rimraf lib && tsc -m commonjs --outDir lib --watch", | ||
"build:esm": "rimraf lib-esm && tsc -m esnext --outDir lib-esm", | ||
"build:esm:watch": "rimraf lib-esm && tsc -m esnext --outDir lib-esm --watch", | ||
"clean": "npm run clean:size && rimraf lib-esm lib", | ||
"clean:size": "rimraf dual-publish-tmp tmp*", | ||
"format": "echo \"Not implemented\"", | ||
"lint": "tslint 'src/**/*.ts' && npm run ts-coverage", | ||
"test": "npm run lint && jest -w 1 --coverage", | ||
"ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 90.31" | ||
}, | ||
"typings": "./lib-esm/index.d.ts", | ||
"version": "1.0.1-api-v6-models.c495d1b.0+c495d1b", | ||
"gitHead": "c495d1b00a61dd6c81ea9207037f34599c4be290" | ||
} |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
export { runWithAmplifyServerContext } from './runWithAmplifyServerContext'; | ||
export { createServerRunner } from './createServerRunner'; |
@@ -7,3 +7,5 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
import { cookies } from 'next/headers'; | ||
import { LegacyConfig } from 'aws-amplify/adapter-core'; | ||
import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core'; | ||
import { ResourcesConfig } from '@aws-amplify/core'; | ||
@@ -55,2 +57,5 @@ export namespace NextServer { | ||
/** | ||
* The union of possible Next.js app server context types. | ||
*/ | ||
export type Context = | ||
@@ -62,2 +67,5 @@ | NextRequestAndNextResponseContext | ||
/** | ||
* The interface of the input of {@link RunOperationWithContext}. | ||
*/ | ||
export interface RunWithContextInput<OperationResult> { | ||
@@ -75,2 +83,14 @@ nextServerContext: Context | null; | ||
} | ||
export interface CreateServerRunnerInput { | ||
config: ResourcesConfig | LegacyConfig; | ||
} | ||
export interface CreateServerRunnerOutput { | ||
runWithAmplifyServerContext: RunOperationWithContext; | ||
} | ||
export interface CreateServerRunner { | ||
(input: CreateServerRunnerInput): CreateServerRunnerOutput; | ||
} | ||
} |
@@ -10,3 +10,2 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
} from '@aws-amplify/core/internals/adapter-core'; | ||
import { IncomingMessage, ServerResponse } from 'http'; | ||
@@ -18,7 +17,31 @@ export const DATE_IN_THE_PAST = new Date(0); | ||
): CookieStorage.Adapter => { | ||
const { request, response } = context as | ||
const { request: req, response: res } = | ||
context as Partial<NextServer.GetServerSidePropsContext>; | ||
// When the server context is from `getServerSideProps`, the `req` is an instance | ||
// of IncomingMessage, and the `res` is an instance of ServerResponse. | ||
// We cannot import these two classes here from `http` as it breaks in Next | ||
// Edge Runtime. Hence, we check the methods that we need to use for creating | ||
// cookie adapter. | ||
if ( | ||
req && | ||
res && | ||
Object.prototype.toString.call(req.cookies) === '[object Object]' && | ||
typeof res.setHeader === 'function' | ||
) { | ||
return createCookieStorageAdapterFromGetServerSidePropsContext(req, res); | ||
} | ||
const { request, response } = context as Partial< | ||
| NextServer.NextRequestAndNextResponseContext | ||
| NextServer.NextRequestAndResponseContext; | ||
| NextServer.NextRequestAndResponseContext | ||
>; | ||
if (request instanceof NextRequest && response) { | ||
// When the server context is from `middleware`, the `request` is an instance | ||
// of `NextRequest`. | ||
// When the server context is from a route handler, the `request` is an `Proxy` | ||
// wrapped `Request`. | ||
// The `NextRequest` and the `Proxy` are sharing the same interface by Next | ||
// implementation. So we don't need to detect the difference. | ||
if (request && response) { | ||
if (response instanceof NextResponse) { | ||
@@ -37,5 +60,5 @@ return createCookieStorageAdapterFromNextRequestAndNextResponse( | ||
const { cookies } = context as | ||
| NextServer.ServerComponentContext | ||
| NextServer.ServerActionContext; | ||
const { cookies } = context as Partial< | ||
NextServer.ServerComponentContext | NextServer.ServerActionContext | ||
>; | ||
@@ -46,9 +69,2 @@ if (typeof cookies === 'function') { | ||
const { request: req, response: res } = | ||
context as NextServer.GetServerSidePropsContext; | ||
if (req instanceof IncomingMessage && res instanceof ServerResponse) { | ||
return createCookieStorageAdapterFromGetServerSidePropsContext(req, res); | ||
} | ||
// This should not happen normally. | ||
@@ -69,6 +85,12 @@ throw new AmplifyServerContextError({ | ||
return { | ||
get: readonlyCookieStore.get.bind(readonlyCookieStore), | ||
get(name) { | ||
return readonlyCookieStore.get(ensureEncodedForJSCookie(name)); | ||
}, | ||
getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore), | ||
set: mutableCookieStore.set.bind(mutableCookieStore), | ||
delete: mutableCookieStore.delete.bind(mutableCookieStore), | ||
set(name, value, options) { | ||
mutableCookieStore.set(ensureEncodedForJSCookie(name), value, options); | ||
}, | ||
delete(name) { | ||
mutableCookieStore.delete(ensureEncodedForJSCookie(name)); | ||
}, | ||
}; | ||
@@ -87,3 +109,5 @@ }; | ||
return { | ||
get: readonlyCookieStore.get.bind(readonlyCookieStore), | ||
get(name) { | ||
return readonlyCookieStore.get(ensureEncodedForJSCookie(name)); | ||
}, | ||
getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore), | ||
@@ -106,3 +130,3 @@ ...mutableCookieStore, | ||
try { | ||
cookieStore.set(name, value, options); | ||
cookieStore.set(ensureEncodedForJSCookie(name), value, options); | ||
} catch { | ||
@@ -115,3 +139,3 @@ // no-op | ||
try { | ||
cookieStore.delete(name); | ||
cookieStore.delete(ensureEncodedForJSCookie(name)); | ||
} catch { | ||
@@ -123,3 +147,5 @@ // no-op | ||
return { | ||
get: cookieStore.get.bind(cookieStore), | ||
get(name) { | ||
return cookieStore.get(ensureEncodedForJSCookie(name)); | ||
}, | ||
getAll: cookieStore.getAll.bind(cookieStore), | ||
@@ -143,3 +169,3 @@ set: setFunc, | ||
get(name) { | ||
const value = cookiesMap[name]; | ||
const value = cookiesMap[ensureEncodedForJSCookie(name)]; | ||
return value | ||
@@ -158,3 +184,5 @@ ? { | ||
'Set-Cookie', | ||
`${name}=${value};${options ? serializeSetCookieOptions(options) : ''}` | ||
`${ensureEncodedForJSCookie(name)}=${value};${ | ||
options ? serializeSetCookieOptions(options) : '' | ||
}` | ||
); | ||
@@ -165,3 +193,5 @@ }, | ||
'Set-Cookie', | ||
`${name}=;Expires=${DATE_IN_THE_PAST.toUTCString()}` | ||
`${ensureEncodedForJSCookie( | ||
name | ||
)}=;Expires=${DATE_IN_THE_PAST.toUTCString()}` | ||
); | ||
@@ -178,3 +208,5 @@ }, | ||
'Set-Cookie', | ||
`${name}=${value};${options ? serializeSetCookieOptions(options) : ''}` | ||
`${ensureEncodedForJSCookie(name)}=${value};${ | ||
options ? serializeSetCookieOptions(options) : '' | ||
}` | ||
); | ||
@@ -185,3 +217,5 @@ }; | ||
'Set-Cookie', | ||
`${name}=;Expires=${DATE_IN_THE_PAST.toUTCString()}` | ||
`${ensureEncodedForJSCookie( | ||
name | ||
)}=;Expires=${DATE_IN_THE_PAST.toUTCString()}` | ||
); | ||
@@ -198,3 +232,3 @@ }; | ||
): string => { | ||
const { expires, maxAge, domain, httpOnly, sameSite, secure } = options; | ||
const { expires, domain, httpOnly, sameSite, secure } = options; | ||
const serializedOptions: string[] = []; | ||
@@ -218,1 +252,10 @@ if (domain) { | ||
}; | ||
// Ensures the cookie names are encoded in order to look up the cookie store | ||
// that is manipulated by js-cookie on the client side. | ||
// Details of the js-cookie encoding behavior see: | ||
// https://github.com/js-cookie/js-cookie#encoding | ||
// The implementation is borrowed from js-cookie without escaping `[()]` as | ||
// we are not using those chars in the auth keys. | ||
const ensureEncodedForJSCookie = (name: string): string => | ||
encodeURIComponent(name).replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent); |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { ResourcesConfig } from '@aws-amplify/core'; | ||
import { AmplifyServerContextError } from '@aws-amplify/core/internals/adapter-core'; | ||
import { ResourcesConfig } from 'aws-amplify'; | ||
import { LegacyConfig } from 'aws-amplify/adapter-core'; | ||
import { parseAWSExports } from '@aws-amplify/core/internals/utils'; | ||
export const getAmplifyConfig = (): ResourcesConfig => { | ||
const configStr = process.env.amplifyConfig; | ||
if (!configStr) { | ||
throw new AmplifyServerContextError({ | ||
message: 'Amplify configuration is missing from `process.env`.', | ||
recoverySuggestion: | ||
'Ensure to use `withAmplify` function in your `next.config.js`.', | ||
}); | ||
} | ||
const configObject = JSON.parse(configStr); | ||
// TODO(HuiSF): adds ResourcesConfig validation when it has one. | ||
return configObject; | ||
}; | ||
export const getAmplifyConfig = ( | ||
config: ResourcesConfig | LegacyConfig | ||
): ResourcesConfig => | ||
Object.keys(config).some(key => key.startsWith('aws_')) | ||
? parseAWSExports(config) | ||
: (config as ResourcesConfig); |
@@ -5,2 +5,2 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
export { getAmplifyConfig } from './getAmplifyConfig'; | ||
export { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext'; | ||
export { createRunWithAmplifyServerContext } from './createRunWithAmplifyServerContext'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
99286
27.66%88
49.15%1493
38.75%1
-66.67%2
-33.33%