diff --git a/src/ServerlessOffline.js b/src/ServerlessOffline.js index 61d4dffa5..3bf267861 100644 --- a/src/ServerlessOffline.js +++ b/src/ServerlessOffline.js @@ -6,8 +6,7 @@ import { defaultOptions, SERVER_SHUTDOWN_TIMEOUT, } from './config/index.js' -import { gray, orange } from './config/colors.js' -import { createApiKey } from './utils/index.js' +import { gray } from './config/colors.js' export default class ServerlessOffline { #cliOptions = null @@ -266,8 +265,6 @@ export default class ServerlessOffline { const functionKeys = service.getAllFunctions() - let hasPrivateHttpEvent = false - functionKeys.forEach((functionKey) => { const functionDefinition = service.getFunction(functionKey) @@ -337,10 +334,6 @@ export default class ServerlessOffline { } } - if (http?.private) { - hasPrivateHttpEvent = true - } - httpEvents.push(httpEvent) } @@ -360,31 +353,6 @@ export default class ServerlessOffline { }) }) - // for simple API Key authentication model - if (hasPrivateHttpEvent) { - if (this.#options.apiKey) { - log.notice() - log.warning( - orange(`'--apiKey' is deprecated and will be removed in the next major version. - Please define the apiKey value in the 'provider.apiGateway.apiKeys' section of the serverless config. - If you are experiencing any issues please let us know: https://github.com/dherault/serverless-offline/issues`), - ) - log.notice() - } else { - this.#options.apiKey = createApiKey() - } - - log.notice(`Key with token: ${this.#options.apiKey}`) - - if (this.#options.noAuth) { - log.notice( - `Authorizers are turned off. You do not need to use 'x-api-key' header.`, - ) - } else { - log.notice(`Remember to use 'x-api-key' on the request headers.`) - } - } - return { httpEvents, lambdas, diff --git a/src/events/http/Http.js b/src/events/http/Http.js index 3362276ea..248bdf002 100644 --- a/src/events/http/Http.js +++ b/src/events/http/Http.js @@ -1,11 +1,19 @@ +import { log } from '@serverless/utils/log.js' import HttpEventDefinition from './HttpEventDefinition.js' import HttpServer from './HttpServer.js' +import { orange } from '../../config/colors.js' +import { createApiKey } from '../../utils/index.js' export default class Http { + #hasPrivateHttpEvent = false + #httpServer = null + #options = null + constructor(serverless, options, lambda) { this.#httpServer = new HttpServer(serverless, options, lambda) + this.#options = options } start() { @@ -28,10 +36,38 @@ export default class Http { } create(events) { - events.forEach(({ functionKey, handler, http }) => { + events.forEach(({ functionKey, handler, http, private: priv }) => { this.#createEvent(functionKey, http, handler) + + if (priv) { + this.#hasPrivateHttpEvent = true + } }) + if (this.#hasPrivateHttpEvent) { + if (this.#options.apiKey) { + log.notice() + log.warning( + orange(`'--apiKey' is deprecated and will be removed in the next major version. + Please define the apiKey value in the 'provider.apiGateway.apiKeys' section of the serverless config. + If you are experiencing any issues please let us know: https://github.com/dherault/serverless-offline/issues`), + ) + log.notice() + } else { + this.#options.apiKey = createApiKey() + } + + log.notice(`Key with token: ${this.#options.apiKey}`) + + if (this.#options.noAuth) { + log.notice( + `Authorizers are turned off. You do not need to use 'x-api-key' header.`, + ) + } else { + log.notice(`Remember to use 'x-api-key' on the request headers.`) + } + } + this.#httpServer.writeRoutesTerminal() }