Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Extract util for generating hapiPath #994

Merged
merged 1 commit into from May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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