diff --git a/packages/adapter-nextjs/package.json b/packages/adapter-nextjs/package.json index ad2590a2255..b45d55fa90c 100644 --- a/packages/adapter-nextjs/package.json +++ b/packages/adapter-nextjs/package.json @@ -66,7 +66,8 @@ "clean": "npm run clean:size && rimraf dist", "clean:size": "rimraf dual-publish-tmp tmp*", "format": "echo \"Not implemented\"", - "lint": "tslint 'src/**/*.ts' && npm run ts-coverage", + "lint": "eslint '**/*.{ts,tsx}' && npm run ts-coverage", + "lint:fix": "eslint '**/*.{ts,tsx}' --fix", "test": "npm run lint && jest -w 1 --coverage --logHeapUsage", "ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 90.31" } diff --git a/packages/adapter-nextjs/src/createServerRunner.ts b/packages/adapter-nextjs/src/createServerRunner.ts index 76d238cdd15..3887054c4f7 100644 --- a/packages/adapter-nextjs/src/createServerRunner.ts +++ b/packages/adapter-nextjs/src/createServerRunner.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { ResourcesConfig } from 'aws-amplify'; + import { createRunWithAmplifyServerContext, getAmplifyConfig } from './utils'; import { NextServer } from './types'; diff --git a/packages/adapter-nextjs/src/types/NextServer.ts b/packages/adapter-nextjs/src/types/NextServer.ts index a727204bc00..107bab823e7 100644 --- a/packages/adapter-nextjs/src/types/NextServer.ts +++ b/packages/adapter-nextjs/src/types/NextServer.ts @@ -8,17 +8,17 @@ import { LegacyConfig } from 'aws-amplify/adapter-core'; import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core'; import { ResourcesConfig } from '@aws-amplify/core'; -export namespace NextServer { +export declare namespace NextServer { /** * This context is normally available in the following: * - Next App Router [middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware) * - Next App Router [route handler](https://nextjs.org/docs/app/building-your-application/routing/route-handlers) * when using `NextResponse` to create the response of the route handler */ - export type NextRequestAndNextResponseContext = { + export interface NextRequestAndNextResponseContext { request: NextRequest; response: NextResponse; - }; + } /** * This context is normally available in the following: @@ -26,10 +26,10 @@ export namespace NextServer { * when using the Web API [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) * to create the response of the route handler */ - export type NextRequestAndResponseContext = { + export interface NextRequestAndResponseContext { request: NextRequest; response: Response; - }; + } /** * This context is normally available in the following: @@ -37,9 +37,9 @@ export namespace NextServer { * where the [`cookies`](https://nextjs.org/docs/app/api-reference/functions/cookies) * function can be imported and called */ - export type ServerComponentContext = { + export interface ServerComponentContext { cookies: typeof cookies; - }; + } export type ServerActionContext = ServerComponentContext; @@ -48,10 +48,10 @@ export namespace NextServer { * [`getServerSideProps`](https://nextjs.org/docs/pages/building-your-application/data-fetching/get-server-side-props) * function of the Next Pages Router. */ - export type GetServerSidePropsContext = { + export interface GetServerSidePropsContext { request: NextGetServerSidePropsContext['req']; response: NextGetServerSidePropsContext['res']; - }; + } /** * The union of possible Next.js app server context types. @@ -62,21 +62,16 @@ export namespace NextServer { | ServerComponentContext | GetServerSidePropsContext; - /** - * The interface of the input of {@link RunOperationWithContext}. - */ export interface RunWithContextInput { nextServerContext: Context | null; - operation: ( - contextSpec: AmplifyServer.ContextSpec - ) => OperationResult | Promise; + operation( + contextSpec: AmplifyServer.ContextSpec, + ): OperationResult | Promise; } - export interface RunOperationWithContext { - ( - input: RunWithContextInput - ): Promise; - } + export type RunOperationWithContext = ( + input: RunWithContextInput, + ) => Promise; export interface CreateServerRunnerInput { config: ResourcesConfig | LegacyConfig; @@ -86,7 +81,7 @@ export namespace NextServer { runWithAmplifyServerContext: RunOperationWithContext; } - export interface CreateServerRunner { - (input: CreateServerRunnerInput): CreateServerRunnerOutput; - } + export type CreateServerRunner = ( + input: CreateServerRunnerInput, + ) => CreateServerRunnerOutput; } diff --git a/packages/adapter-nextjs/src/utils/createCookieStorageAdapterFromNextServerContext.ts b/packages/adapter-nextjs/src/utils/createCookieStorageAdapterFromNextServerContext.ts index beffc7ae31e..0b6ac3f8726 100644 --- a/packages/adapter-nextjs/src/utils/createCookieStorageAdapterFromNextServerContext.ts +++ b/packages/adapter-nextjs/src/utils/createCookieStorageAdapterFromNextServerContext.ts @@ -2,16 +2,17 @@ // SPDX-License-Identifier: Apache-2.0 import { NextRequest, NextResponse } from 'next/server.js'; -import { NextServer } from '../types'; import { AmplifyServerContextError, CookieStorage, } from '@aws-amplify/core/internals/adapter-core'; +import { NextServer } from '../types'; + export const DATE_IN_THE_PAST = new Date(0); export const createCookieStorageAdapterFromNextServerContext = ( - context: NextServer.Context + context: NextServer.Context, ): CookieStorage.Adapter => { const { request: req, response: res } = context as Partial; @@ -45,12 +46,12 @@ export const createCookieStorageAdapterFromNextServerContext = ( if (response instanceof NextResponse) { return createCookieStorageAdapterFromNextRequestAndNextResponse( request, - response + response, ); } else { return createCookieStorageAdapterFromNextRequestAndHttpResponse( request, - response + response, ); } } @@ -72,7 +73,7 @@ export const createCookieStorageAdapterFromNextServerContext = ( const createCookieStorageAdapterFromNextRequestAndNextResponse = ( request: NextRequest, - response: NextResponse + response: NextResponse, ): CookieStorage.Adapter => { const readonlyCookieStore = request.cookies; const mutableCookieStore = response.cookies; @@ -93,11 +94,11 @@ const createCookieStorageAdapterFromNextRequestAndNextResponse = ( const createCookieStorageAdapterFromNextRequestAndHttpResponse = ( request: NextRequest, - response: Response + response: Response, ): CookieStorage.Adapter => { const readonlyCookieStore = request.cookies; const mutableCookieStore = createMutableCookieStoreFromHeaders( - response.headers + response.headers, ); return { @@ -110,7 +111,7 @@ const createCookieStorageAdapterFromNextRequestAndHttpResponse = ( }; const createCookieStorageAdapterFromNextCookies = ( - cookies: NextServer.ServerComponentContext['cookies'] + cookies: NextServer.ServerComponentContext['cookies'], ): CookieStorage.Adapter => { const cookieStore = cookies(); @@ -147,7 +148,7 @@ const createCookieStorageAdapterFromNextCookies = ( const createCookieStorageAdapterFromGetServerSidePropsContext = ( request: NextServer.GetServerSidePropsContext['request'], - response: NextServer.GetServerSidePropsContext['response'] + response: NextServer.GetServerSidePropsContext['response'], ): CookieStorage.Adapter => { const cookiesMap = { ...request.cookies }; const allCookies = Object.entries(cookiesMap).map(([name, value]) => ({ @@ -158,6 +159,7 @@ const createCookieStorageAdapterFromGetServerSidePropsContext = ( return { get(name) { const value = cookiesMap[ensureEncodedForJSCookie(name)]; + return value ? { name, @@ -173,39 +175,40 @@ const createCookieStorageAdapterFromGetServerSidePropsContext = ( 'Set-Cookie', `${ensureEncodedForJSCookie(name)}=${value};${ options ? serializeSetCookieOptions(options) : '' - }` + }`, ); }, delete(name) { response.setHeader( 'Set-Cookie', `${ensureEncodedForJSCookie( - name - )}=;Expires=${DATE_IN_THE_PAST.toUTCString()}` + name, + )}=;Expires=${DATE_IN_THE_PAST.toUTCString()}`, ); }, }; }; const createMutableCookieStoreFromHeaders = ( - headers: Headers + headers: Headers, ): Pick => { const setFunc: CookieStorage.Adapter['set'] = (name, value, options) => { headers.append( 'Set-Cookie', `${ensureEncodedForJSCookie(name)}=${value};${ options ? serializeSetCookieOptions(options) : '' - }` + }`, ); }; const deleteFunc: CookieStorage.Adapter['delete'] = name => { headers.append( 'Set-Cookie', `${ensureEncodedForJSCookie( - name - )}=;Expires=${DATE_IN_THE_PAST.toUTCString()}` + name, + )}=;Expires=${DATE_IN_THE_PAST.toUTCString()}`, ); }; + return { set: setFunc, delete: deleteFunc, @@ -213,7 +216,7 @@ const createMutableCookieStoreFromHeaders = ( }; const serializeSetCookieOptions = ( - options: CookieStorage.SetCookieOptions + options: CookieStorage.SetCookieOptions, ): string => { const { expires, domain, httpOnly, sameSite, secure } = options; const serializedOptions: string[] = []; @@ -232,6 +235,7 @@ const serializeSetCookieOptions = ( if (secure) { serializedOptions.push(`Secure`); } + return serializedOptions.join(';'); }; diff --git a/packages/adapter-nextjs/src/utils/createRunWithAmplifyServerContext.ts b/packages/adapter-nextjs/src/utils/createRunWithAmplifyServerContext.ts index 6362d1149a8..1797a1be0ca 100644 --- a/packages/adapter-nextjs/src/utils/createRunWithAmplifyServerContext.ts +++ b/packages/adapter-nextjs/src/utils/createRunWithAmplifyServerContext.ts @@ -9,9 +9,10 @@ import { runWithAmplifyServerContext as runWithAmplifyServerContextCore, } from 'aws-amplify/adapter-core'; -import { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext'; import { NextServer } from '../types'; +import { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext'; + export const createRunWithAmplifyServerContext = ({ config: resourcesConfig, }: { @@ -31,16 +32,16 @@ export const createRunWithAmplifyServerContext = ({ ? sharedInMemoryStorage : createKeyValueStorageFromCookieStorageAdapter( createCookieStorageAdapterFromNextServerContext( - nextServerContext - ) + nextServerContext, + ), ); const credentialsProvider = createAWSCredentialsAndIdentityIdProvider( resourcesConfig.Auth, - keyValueStorage + keyValueStorage, ); const tokenProvider = createUserPoolsTokenProvider( resourcesConfig.Auth, - keyValueStorage + keyValueStorage, ); return runWithAmplifyServerContextCore( @@ -48,7 +49,7 @@ export const createRunWithAmplifyServerContext = ({ { Auth: { credentialsProvider, tokenProvider }, }, - operation + operation, ); } diff --git a/packages/adapter-nextjs/src/utils/getAmplifyConfig.ts b/packages/adapter-nextjs/src/utils/getAmplifyConfig.ts index f3f16dbb868..a8ab9f1d22b 100644 --- a/packages/adapter-nextjs/src/utils/getAmplifyConfig.ts +++ b/packages/adapter-nextjs/src/utils/getAmplifyConfig.ts @@ -6,7 +6,7 @@ import { LegacyConfig } from 'aws-amplify/adapter-core'; import { parseAWSExports } from '@aws-amplify/core/internals/utils'; export const getAmplifyConfig = ( - config: ResourcesConfig | LegacyConfig + config: ResourcesConfig | LegacyConfig, ): ResourcesConfig => Object.keys(config).some(key => key.startsWith('aws_')) ? parseAWSExports(config) diff --git a/packages/adapter-nextjs/tslint.json b/packages/adapter-nextjs/tslint.json deleted file mode 100644 index fcca611fccf..00000000000 --- a/packages/adapter-nextjs/tslint.json +++ /dev/null @@ -1,56 +0,0 @@ -{ - "defaultSeverity": "error", - "plugins": ["prettier"], - "extends": [], - "jsRules": {}, - "rules": { - "prefer-const": true, - "max-line-length": [ - true, - { - "limit": 120, - "ignore-pattern": "^//|^ *" - } - ], - "no-empty-interface": true, - "no-var-keyword": true, - "object-literal-shorthand": true, - "no-eval": true, - "space-before-function-paren": [ - true, - { - "anonymous": "never", - "named": "never" - } - ], - "no-parameter-reassignment": true, - "align": [true, "parameters"], - "no-duplicate-imports": true, - "one-variable-per-declaration": [false, "ignore-for-loop"], - "triple-equals": [true, "allow-null-check"], - "comment-format": [true, "check-space"], - "indent": [false], - "whitespace": [ - false, - "check-branch", - "check-decl", - "check-operator", - "check-preblock" - ], - "eofline": true, - "variable-name": [ - true, - "check-format", - "allow-pascal-case", - "allow-snake-case", - "allow-leading-underscore" - ], - "semicolon": [ - true, - "always", - "ignore-interfaces", - "ignore-bound-class-methods" - ] - }, - "rulesDirectory": [] -}