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: add support for Azure Functions v4 #738

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
41 changes: 27 additions & 14 deletions src/convenience/frameworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,15 @@ export type LambdaAsyncAdapter = (
_context: unknown,
) => ReqResHandler;

export type AzureAdapter = (request: {
body?: unknown;
}, context: {
export type AzureAdapter = (context: {
res?: {
status: number;
body: string;
headers?: Record<string, string>;
set?: (key: string, value: string) => void;
send?: {
(body: unknown): void;
(status: number, body: unknown): void;
};
// deno-lint-ignore no-explicit-any
[key: string]: any;
KnorpelSenf marked this conversation as resolved.
Show resolved Hide resolved
};
}) => ReqResHandler;
}, request: { body?: unknown }) => ReqResHandler;
export type AzureAdapterV4 = (
request: { headers: Headers; json(): Promise<Update> },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I comment the wrong place, here is what I mentioned in the comment below.

) => ReqResHandler<{ status: number; body?: string } | { jsonBody: string }>;

export type BunAdapter = (request: {
headers: Headers;
Expand Down Expand Up @@ -273,8 +268,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 +284,23 @@ const azure: AzureAdapter = (request, context) => ({
context.res?.send?.(401, WRONG_TOKEN_ERROR);
},
});
const azureV4: AzureAdapterV4 = (request) => {
type Res = NonNullable<
Awaited<ReturnType<AzureAdapterV4>["handlerReturn"]>
>;
Comment on lines +290 to +293
Copy link

@mildronize mildronize Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CleanShot 2568-01-17 at 19 14 36

@KnorpelSenf After I've check out the new code from the latest commit from #738, (Commit id: 8f9e16e5114421dc0dacab51f899f7d4538f0184)

you checkout the code from here: https://github.com/mildronize/grammy-azure-functions-test/tree/44221d1b5b0f0589347403d4636b9d19fa504874/func-v4

the request type error:

Argument of type 'HttpRequest' is not assignable to parameter of type '{ headers: Headers; json(): Promise<Update>; }'.
  The types returned by 'headers[Symbol.iterator]()' are incompatible between these types.
    Property '[Symbol.iterator]' is missing in type 'SpecIterator<[string, string], any, undefined>' but required in type 'HeadersIterator<[string, string]>

let resolveResponse: (response: Res) => void;
return {
update: Promise.resolve(request.json()) as Promise<Update>,
header: request.headers.get(SECRET_HEADER) || undefined,
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 +556,7 @@ export const adapters = {
"aws-lambda": awsLambda,
"aws-lambda-async": awsLambdaAsync,
azure,
"azure-v4": azureV4,
bun,
cloudflare,
"cloudflare-mod": cloudflareModule,
Expand Down
Loading