Skip to content

Commit

Permalink
Merge pull request #994 from chardos/master
Browse files Browse the repository at this point in the history
refactor: Extract util for generating hapiPath
  • Loading branch information
dherault committed May 28, 2020
2 parents 13f0275 + 297b3a5 commit 3ca8048
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 3 additions & 41 deletions src/events/http/HttpServer.js
Expand Up @@ -19,6 +19,7 @@ import {
detectEncoding,
jsonPath,
splitHandlerPathAndName,
generateHapiPath,
} from '../../utils/index.js'

const { parse, stringify } = JSON
Expand Down Expand Up @@ -245,29 +246,8 @@ export default class HttpServer {
)

const { path } = httpEvent
// path must start with '/'
let hapiPath = path.startsWith('/') ? path : `/${path}`

// prepend stage to path
const hapiPath = generateHapiPath(path, this.#options, this.#serverless)
const stage = this.#options.stage || this.#serverless.service.provider.stage

if (!this.#options.noPrependStageInUrl) {
// prepend stage to path
hapiPath = `/${stage}${hapiPath}`
}

// add prefix to path
if (this.#options.prefix) {
hapiPath = `/${this.#options.prefix}${hapiPath}`
}

// but must not end with '/'
if (hapiPath !== '/' && hapiPath.endsWith('/')) {
hapiPath = hapiPath.slice(0, -1)
}

hapiPath = hapiPath.replace(/\+}/g, '*}')

const protectedRoutes = []

if (httpEvent.private) {
Expand Down Expand Up @@ -886,25 +866,7 @@ export default class HttpServer {
return
}

let hapiPath = path.startsWith('/') ? path : `/${path}`

if (!this.#options.noPrependStageInUrl) {
const stage =
this.#options.stage || this.#serverless.service.provider.stage
// prepend stage to path
hapiPath = `/${stage}${hapiPath}`
}

if (this.#options.prefix) {
hapiPath = `/${this.#options.prefix}${hapiPath}`
}

if (hapiPath !== '/' && hapiPath.endsWith('/')) {
hapiPath = hapiPath.slice(0, -1)
}

hapiPath = hapiPath.replace(/\+}/g, '*}')

const hapiPath = generateHapiPath(path, this.#options, this.#serverless)
const proxyUriOverwrite = resourceRoutesOptions[methodId] || {}
const proxyUriInUse = proxyUriOverwrite.Uri || proxyUri

Expand Down
22 changes: 22 additions & 0 deletions src/utils/generateHapiPath.js
@@ -0,0 +1,22 @@
export default function generateHapiPath(path, options, serverless) {
// path must start with '/'
let hapiPath = path.startsWith('/') ? path : `/${path}`

if (!options.noPrependStageInUrl) {
const stage = options.stage || serverless.service.provider.stage
// prepend stage to path
hapiPath = `/${stage}${hapiPath}`
}

if (options.prefix) {
hapiPath = `/${options.prefix}${hapiPath}`
}

if (hapiPath !== '/' && hapiPath.endsWith('/')) {
hapiPath = hapiPath.slice(0, -1)
}

hapiPath = hapiPath.replace(/\+}/g, '*}')

return hapiPath
}
1 change: 1 addition & 0 deletions src/utils/index.js
Expand Up @@ -13,6 +13,7 @@ export { default as parseQueryStringParameters } from './parseQueryStringParamet
export { default as satisfiesVersionRange } from './satisfiesVersionRange.js'
export { default as splitHandlerPathAndName } from './splitHandlerPathAndName.js'
export { default as checkDockerDaemon } from './checkDockerDaemon.js'
export { default as generateHapiPath } from './generateHapiPath.js'
// export { default as baseImage } from './baseImage.js'

// Detect the toString encoding from the request headers content-type
Expand Down

0 comments on commit 3ca8048

Please sign in to comment.