Skip to content

Commit

Permalink
feat: add support for Azure Functions v4
Browse files Browse the repository at this point in the history
  • Loading branch information
KnorpelSenf committed Jan 15, 2025
1 parent b04bfee commit f5e6e85
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/convenience/frameworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ export type LambdaAsyncAdapter = (
_context: unknown,
) => ReqResHandler;

export type AzureAdapter = (request: {
body?: unknown;
}, context: {
export type AzureAdapter = (context: {
res?: {
status: number;
body: string;
Expand All @@ -93,7 +91,14 @@ export type AzureAdapter = (request: {
(status: number, body: unknown): void;
};
};
}) => ReqResHandler;
}, request: { body?: unknown }) => ReqResHandler;
export type AzureAdapterV4 = (request: Request, context: {
res?: {
status: number;
body: string;
headers?: Record<string, string>;
};
}) => ReqResHandler<{ status: number; body?: string } | { jsonBody: string }>;

export type BunAdapter = (request: {
headers: Headers;
Expand Down Expand Up @@ -273,8 +278,8 @@ const awsLambdaAsync: LambdaAsyncAdapter = (event, _context) => {
};
};

/** Azure Functions */
const azure: AzureAdapter = (request, context) => ({
/** Azure Functions v3 and v4 */
const azure: AzureAdapter = (context, request) => ({
update: Promise.resolve(request.body as Update),
header: context.res?.headers?.[SECRET_HEADER],
end: () => (context.res = {
Expand All @@ -289,6 +294,23 @@ const azure: AzureAdapter = (request, context) => ({
context.res?.send?.(401, WRONG_TOKEN_ERROR);
},
});
const azureV4: AzureAdapterV4 = (request, context) => {
type Res = NonNullable<
Awaited<ReturnType<AzureAdapterV4>["handlerReturn"]>
>;
let resolveResponse: (response: Res) => void;
return {
update: Promise.resolve(request.json()) as Promise<Update>,
header: context.res?.headers?.[SECRET_HEADER],
end: () => resolveResponse({ status: 204 }),
respond: (json) => resolveResponse({ jsonBody: json }),
unauthorized: () =>
resolveResponse({ status: 401, body: WRONG_TOKEN_ERROR }),
handlerReturn: new Promise<Res>((resolve) => {
resolveResponse = resolve;
}),
};
};

/** Bun.serve */
const bun: BunAdapter = (request) => {
Expand Down Expand Up @@ -544,6 +566,7 @@ export const adapters = {
"aws-lambda": awsLambda,
"aws-lambda-async": awsLambdaAsync,
azure,
"azure-v4": azureV4,
bun,
cloudflare,
"cloudflare-mod": cloudflareModule,
Expand Down

0 comments on commit f5e6e85

Please sign in to comment.