Skip to content

Commit

Permalink
refactor: split get events for http and httpApi
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Oct 15, 2022
1 parent 312d4f0 commit fdd1699
Showing 1 changed file with 70 additions and 52 deletions.
122 changes: 70 additions & 52 deletions src/ServerlessOffline.js
Expand Up @@ -61,17 +61,22 @@ export default class ServerlessOffline {
async start() {
this.#mergeOptions()

const { httpEvents, lambdas, scheduleEvents, webSocketEvents } =
this.#getEvents()
const {
httpEvents,
httpApiEvents,
lambdas,
scheduleEvents,
webSocketEvents,
} = this.#getEvents()

if (lambdas.length > 0) {
await this.#createLambda(lambdas)
}

const eventModules = []

if (httpEvents.length > 0) {
eventModules.push(this.#createHttp(httpEvents))
if (httpApiEvents.length > 0 || httpEvents.length > 0) {
eventModules.push(this.#createHttp([...httpApiEvents, ...httpEvents]))
}

if (scheduleEvents.length > 0) {
Expand Down Expand Up @@ -166,9 +171,11 @@ export default class ServerlessOffline {
async #createHttp(events, skipStart) {
const { default: Http } = await import('./events/http/index.js')

this.#http = new Http(this.#serverless, this.#options, this.#lambda)
if (this.#http == null) {
this.#http = new Http(this.#serverless, this.#options, this.#lambda)

await this.#http.createServer()
await this.#http.createServer()
}

this.#http.create(events)

Expand Down Expand Up @@ -259,6 +266,7 @@ export default class ServerlessOffline {
const { service } = this.#serverless

const httpEvents = []
const httpApiEvents = []
const lambdas = []
const scheduleEvents = []
const webSocketEvents = []
Expand All @@ -275,66 +283,75 @@ export default class ServerlessOffline {
events.forEach((event) => {
const { http, httpApi, schedule, websocket } = event

if ((http || httpApi) && functionDefinition.handler) {
if (http && functionDefinition.handler) {
const httpEvent = {
functionKey,
handler: functionDefinition.handler,
http: http || httpApi,
http,
}

if (httpApi) {
// Ensure definitions for 'httpApi' events are objects so that they can be marked
// with an 'isHttpApi' property (they are handled differently to 'http' events)
if (typeof httpEvent.http === 'string') {
httpEvent.http = {
routeKey: httpEvent.http === '*' ? '$default' : httpEvent.http,
}
} else if (typeof httpEvent.http === 'object') {
if (!httpEvent.http.method) {
log.warning(
`Event definition is missing a method for function "${functionKey}"`,
)
httpEvent.http.method = ''
}
if (
httpEvent.http.method === '*' &&
httpEvent.http.path === '*'
) {
httpEvent.http.routeKey = '$default'
} else {
const resolvedMethod =
httpEvent.http.method === '*'
? 'ANY'
: httpEvent.http.method.toUpperCase()
httpEvent.http.routeKey = `${resolvedMethod} ${httpEvent.http.path}`
}
// Clear these properties to avoid confusion (they will be derived from the routeKey
// when needed later)
delete httpEvent.http.method
delete httpEvent.http.path
} else {
httpEvents.push(httpEvent)
}

if (httpApi && functionDefinition.handler) {
const httpApiEvent = {
functionKey,
handler: functionDefinition.handler,
http: httpApi,
}

// Ensure definitions for 'httpApi' events are objects so that they can be marked
// with an 'isHttpApi' property (they are handled differently to 'http' events)
if (typeof httpApiEvent.http === 'string') {
httpApiEvent.http = {
routeKey:
httpApiEvent.http === '*' ? '$default' : httpApiEvent.http,
}
} else if (typeof httpApiEvent.http === 'object') {
if (!httpApiEvent.http.method) {
log.warning(
`Event definition must be a string or object but received ${typeof httpEvent.http} for function "${functionKey}"`,
`Event definition is missing a method for function "${functionKey}"`,
)
httpEvent.http.routeKey = ''
httpApiEvent.http.method = ''
}

httpEvent.http.isHttpApi = true

if (
functionDefinition.httpApi &&
functionDefinition.httpApi.payload
httpApiEvent.http.method === '*' &&
httpApiEvent.http.path === '*'
) {
httpEvent.http.payload = functionDefinition.httpApi.payload
httpApiEvent.http.routeKey = '$default'
} else {
httpEvent.http.payload =
service.provider.httpApi && service.provider.httpApi.payload
? service.provider.httpApi.payload
: '2.0'
const resolvedMethod =
httpApiEvent.http.method === '*'
? 'ANY'
: httpApiEvent.http.method.toUpperCase()
httpApiEvent.http.routeKey = `${resolvedMethod} ${httpApiEvent.http.path}`
}
// Clear these properties to avoid confusion (they will be derived from the routeKey
// when needed later)
delete httpApiEvent.http.method
delete httpApiEvent.http.path
} else {
log.warning(
`Event definition must be a string or object but received ${typeof httpApiEvent.http} for function "${functionKey}"`,
)
httpApiEvent.http.routeKey = ''
}

httpEvents.push(httpEvent)
httpApiEvent.http.isHttpApi = true

if (
functionDefinition.httpApi &&
functionDefinition.httpApi.payload
) {
httpApiEvent.http.payload = functionDefinition.httpApi.payload
} else {
httpApiEvent.http.payload =
service.provider.httpApi && service.provider.httpApi.payload
? service.provider.httpApi.payload
: '2.0'
}

httpApiEvents.push(httpApiEvent)
}

if (schedule) {
Expand All @@ -354,6 +371,7 @@ export default class ServerlessOffline {
})

return {
httpApiEvents,
httpEvents,
lambdas,
scheduleEvents,
Expand Down

0 comments on commit fdd1699

Please sign in to comment.