forked from effect-app/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiddlewares.ts
More file actions
102 lines (94 loc) · 2.55 KB
/
Middlewares.ts
File metadata and controls
102 lines (94 loc) · 2.55 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Mechanism for extendning behaviour of all handlers on the server.
*
* @since 1.0.0
*/
import type { NotLoggedInError } from "@effect-app/infra/errors"
import type * as App from "@effect/platform/Http/App"
import type { Effect } from "effect-app"
import * as internal from "./internal/middlewares"
/**
* Add access logs for handled requests. The log runs before each request.
* Optionally configure log level using the first argument. The default log level
* is `Debug`.
*
* @category logging
* @since 1.0.0
*/
export const accessLog: (
level?: "Info" | "Warning" | "Debug"
) => <R, E>(app: App.Default<R, E>) => App.Default<R, E> = internal.accessLog
/**
* Annotate request logs using generated UUID. The default annotation key is `requestId`.
* The annotation key is configurable using the first argument.
*
* Note that in order to apply the annotation also for access logging, you should
* make sure the `accessLog` middleware is plugged after the `uuidLogAnnotation`.
*
* @category logging
* @since 1.0.0
*/
export const uuidLogAnnotation: (
logAnnotationKey?: string
) => <R, E>(app: App.Default<R, E>) => App.Default<R, E> = internal.uuidLogAnnotation
/**
* Measure how many times each endpoint was called in a
* `server.endpoint_calls` counter metrics.
*
* @category metrics
* @since 1.0.0
*/
export const endpointCallsMetric: () => <R, E>(
app: App.Default<R, E>
) => App.Default<R, E> = internal.endpointCallsMetric
/**
* Logs out a handler failure.
*
* @category logging
* @since 1.0.0
*/
export const errorLog: <R, E>(app: App.Default<R, E>) => App.Default<R, E> = internal.errorLog
/**
* @category models
* @since 1.0.0
*/
export interface BasicAuthCredentials {
user: string
password: string
}
/**
* Basic auth middleware.
*
* @category authorization
* @since 1.0.0
*/
export const basicAuth: <R2, _>(
checkCredentials: (
credentials: BasicAuthCredentials
) => Effect<_, NotLoggedInError, R2>,
options?: Partial<{
headerName: string
skipPaths: readonly string[]
}>
) => <R1, E>(app: App.Default<R1, E>) => App.Default<R1 | R2, E> = internal.basicAuth
/**
* @category models
* @since 1.0.0
*/
export interface CorsOptions {
allowedOrigins: readonly string[]
allowedMethods: readonly string[]
allowedHeaders: readonly string[]
exposedHeaders: readonly string[]
maxAge: number
credentials: boolean
}
/**
* Basic auth middleware.
*
* @category authorization
* @since 1.0.0
*/
export const cors: (
options?: Partial<CorsOptions>
) => <R, E>(app: App.Default<R, E>) => App.Default<R, E> = internal.cors