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

feat: implement azure-functions adapter #1797

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
59 changes: 59 additions & 0 deletions src/adapter/azure-functions/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type {
Context as AzureFunctionsContext,
HttpRequest as AzureFunctionsHttpRequest,
} from '@azure/functions'
ytnobody marked this conversation as resolved.
Show resolved Hide resolved
import type { Hono } from '../../hono'

export interface AzureFunctionsHTTPEvent {
context: AzureFunctionsContext
req: AzureFunctionsHttpRequest
}

export const handle = (app: Hono) => {
return async (event: AzureFunctionsHTTPEvent): Promise<AzureFunctionsContext> => {
const req = createRequest(event)
const res = await app.fetch(req)

return createResult(event.context, res)
}
}

const createRequest = (event: AzureFunctionsHTTPEvent): Request => {
const urlPath = event.req.url
const url = urlPath

const headersKV = {}
for (const [k, v] of Object.entries(event.req.headers)) {
if (v) headersKV[k] = v
}

const headers = new Headers(headersKV)

const method = event.req.method
const requestInit: RequestInit = {
headers,
method,
}

if (event.req.body) {
requestInit.body = JSON.stringify(event.req.body)
}

return new Request(url, requestInit)
}

const createResult = async (
context: AzureFunctionsContext,
res: Response
): Promise<AzureFunctionsContext> => {
const contentType = res.headers.get('content-type')

res.headers.forEach((value, key) => {
context.res.headers[key] = value
})

context.res.headers['Content-Type'] = contentType ?? 'text/plain'
context.res.body = await res.text()

return context
}
1 change: 1 addition & 0 deletions src/adapter/azure-functions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { handle } from './handler'