Skip to content

Commit

Permalink
fix(handler-kit-azure-func): http request is now parsed correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacavallaro authored Apr 8, 2024
1 parent 7567fda commit 70744d6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-walls-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pagopa/handler-kit-azure-func": patch
---

Now HttpRequest from @azure/functions@4 is parsed correctly
34 changes: 11 additions & 23 deletions packages/handler-kit-azure-func/src/__test__/function.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it, expect, vi } from "vitest";

import * as t from "io-ts";

import { InvocationContext } from "@azure/functions";
import { InvocationContext, HttpRequest } from "@azure/functions";

import * as O from "fp-ts/Option";
import * as RTE from "fp-ts/ReaderTaskEither";
Expand All @@ -14,26 +14,6 @@ import * as H from "@pagopa/handler-kit";

import { azureFunction, httpAzureFunction } from "../function";

const HttpRequest = (req: {
method?: "GET" | "POST";
url?: string;
query?: Record<string, string>;
params?: Record<string, string>;
headers?: Record<string, string>;
body?: unknown;
}) => ({
user: null,
get: () => undefined,
parseFormBody: () => ({} as unknown),
body: undefined,
headers: {},
params: {},
query: {},
url: "https://my-test.url.com/api",
method: "GET",
...req,
});

const ctx = {
error: console.error,
debug: console.debug,
Expand Down Expand Up @@ -61,10 +41,12 @@ describe("httpAzureFunction", () => {
const GreetFunction = httpAzureFunction(GreetHandler)({
lang: "it",
});
const message = HttpRequest({
const message = new HttpRequest({
query: {
name: "luca",
},
url: "https://my-request.pagopa.it",
method: "GET",
});
const response = await GreetFunction(message, ctx);
expect(response.json()).resolves.toEqual(
Expand All @@ -79,7 +61,13 @@ describe("httpAzureFunction", () => {
const ErrorFunction = httpAzureFunction(
H.of((_) => RTE.left(new Error("unhandled error")))
)({});
const response = await ErrorFunction({}, ctx);
const response = await ErrorFunction(
new HttpRequest({
url: "http://my-request.pagopa.it/",
method: "GET",
}),
ctx
);
expect(CtxErrorSpy).toHaveBeenCalled();
expect(response.json()).resolves.toEqual(
expect.objectContaining({
Expand Down
29 changes: 23 additions & 6 deletions packages/handler-kit-azure-func/src/function.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InvocationContext, HttpResponse } from "@azure/functions";
import { InvocationContext, HttpRequest, HttpResponse } from "@azure/functions";

import * as t from "io-ts";

Expand Down Expand Up @@ -120,12 +120,29 @@ export const httpAzureFunction =
h: H.Handler<H.HttpRequest, H.HttpResponse<unknown, H.HttpStatusCode>, R>
) =>
(deps: Omit<R, "logger" | "input">) =>
(input: unknown, ctx: InvocationContext) =>
(request: HttpRequest, ctx: InvocationContext) =>
pipe(
azureFunctionTE(h, {
...deps,
inputDecoder: HttpRequestFromAzure,
})(input, ctx),
TE.tryCatch(
() => request.json(),
() => new Error("The request body is not a valid JSON.")
),
TE.orElse(() => TE.right<Error, unknown>(undefined)),
TE.chain((body) =>
azureFunctionTE(h, {
...deps,
inputDecoder: HttpRequestFromAzure,
})(
{
body,
url: request.url,
params: request.params,
headers: Object.fromEntries(request.headers.entries()),
query: Object.fromEntries(request.query.entries()),
method: request.method,
},
ctx
)
),
TE.getOrElseW((e) =>
logErrorAndReturnHttpResponse(e)({
logger: getLogger(ctx),
Expand Down

0 comments on commit 70744d6

Please sign in to comment.