Skip to content

Commit

Permalink
refactor: move private http event condition to http class
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Sep 24, 2022
1 parent 462eb87 commit a2d3438
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
34 changes: 1 addition & 33 deletions src/ServerlessOffline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -266,8 +265,6 @@ export default class ServerlessOffline {

const functionKeys = service.getAllFunctions()

let hasPrivateHttpEvent = false

functionKeys.forEach((functionKey) => {
const functionDefinition = service.getFunction(functionKey)

Expand Down Expand Up @@ -337,10 +334,6 @@ export default class ServerlessOffline {
}
}

if (http?.private) {
hasPrivateHttpEvent = true
}

httpEvents.push(httpEvent)
}

Expand All @@ -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,
Expand Down
38 changes: 37 additions & 1 deletion src/events/http/Http.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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()
}

Expand Down

0 comments on commit a2d3438

Please sign in to comment.