From 70744d6a336e2d87574a3d358e8c4fc5e1d7134e Mon Sep 17 00:00:00 2001 From: Luca Cavallaro Date: Mon, 8 Apr 2024 08:48:02 +0200 Subject: [PATCH] fix(handler-kit-azure-func): http request is now parsed correctly --- .changeset/clean-walls-march.md | 5 +++ .../src/__test__/function.spec.ts | 34 ++++++------------- .../handler-kit-azure-func/src/function.ts | 29 ++++++++++++---- 3 files changed, 39 insertions(+), 29 deletions(-) create mode 100644 .changeset/clean-walls-march.md diff --git a/.changeset/clean-walls-march.md b/.changeset/clean-walls-march.md new file mode 100644 index 0000000..ade7c92 --- /dev/null +++ b/.changeset/clean-walls-march.md @@ -0,0 +1,5 @@ +--- +"@pagopa/handler-kit-azure-func": patch +--- + +Now HttpRequest from @azure/functions@4 is parsed correctly diff --git a/packages/handler-kit-azure-func/src/__test__/function.spec.ts b/packages/handler-kit-azure-func/src/__test__/function.spec.ts index 0d8137f..8592bdc 100644 --- a/packages/handler-kit-azure-func/src/__test__/function.spec.ts +++ b/packages/handler-kit-azure-func/src/__test__/function.spec.ts @@ -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"; @@ -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; - params?: Record; - headers?: Record; - 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, @@ -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( @@ -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({ diff --git a/packages/handler-kit-azure-func/src/function.ts b/packages/handler-kit-azure-func/src/function.ts index 7c2be29..05dba29 100644 --- a/packages/handler-kit-azure-func/src/function.ts +++ b/packages/handler-kit-azure-func/src/function.ts @@ -1,4 +1,4 @@ -import { InvocationContext, HttpResponse } from "@azure/functions"; +import { InvocationContext, HttpRequest, HttpResponse } from "@azure/functions"; import * as t from "io-ts"; @@ -120,12 +120,29 @@ export const httpAzureFunction = h: H.Handler, R> ) => (deps: Omit) => - (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(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),