generated from pagopa/io-functions-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.ts
60 lines (54 loc) · 1.63 KB
/
handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as express from "express";
import * as t from "io-ts";
import { Context } from "@azure/functions";
import { ServiceModel } from "@pagopa/io-functions-commons/dist/src/models/service";
import {
AzureUserAttributesMiddleware,
IAzureUserAttributes
} from "@pagopa/io-functions-commons/dist/src/utils/middlewares/azure_user_attributes";
import { ContextMiddleware } from "@pagopa/io-functions-commons/dist/src/utils/middlewares/context_middleware";
import { RequiredParamMiddleware } from "@pagopa/io-functions-commons/dist/src/utils/middlewares/required_param";
import {
withRequestMiddlewares,
wrapRequestHandler
} from "@pagopa/io-functions-commons/dist/src/utils/request_middleware";
import {
IResponseErrorNotFound,
IResponseSuccessJson,
ResponseSuccessJson
} from "@pagopa/ts-commons/lib/responses";
type IHttpHandler = (
context: Context,
userAttrs: IAzureUserAttributes,
param: unknown
) => Promise<
| IResponseSuccessJson<{
readonly message: string;
}>
| IResponseErrorNotFound
>;
export const HttpHandler = (): IHttpHandler => async (
ctx,
userAttrs,
param
): Promise<
IResponseSuccessJson<{
readonly message: string;
}>
> =>
ResponseSuccessJson({
headers: ctx.req?.headers,
message: `Hello ${param} !`,
user: userAttrs
});
export const HttpCtrl = (
serviceModel: ServiceModel
): express.RequestHandler => {
const handler = HttpHandler();
const middlewaresWrap = withRequestMiddlewares(
ContextMiddleware(),
AzureUserAttributesMiddleware(serviceModel),
RequiredParamMiddleware("someParam", t.string)
);
return wrapRequestHandler(middlewaresWrap(handler));
};