MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeSecurityMiddleware

Function makeSecurityMiddleware

packages/effect/src/http-api/HttpApiBuilder.ts:1119–1164  ·  view source on GitHub ↗
(
  key: HttpApiMiddleware.AnyServiceSecurity,
  service: HttpApiMiddleware.HttpApiMiddlewareSecurity<any, any, any, any>
)

Source from the content-addressed store, hash-verified

1117>()
1118
1119const makeSecurityMiddleware = (
1120 key: HttpApiMiddleware.AnyServiceSecurity,
1121 service: HttpApiMiddleware.HttpApiMiddlewareSecurity<any, any, any, any>
1122): (effect: Effect.Effect<any, any, any>, options: any) => Effect.Effect<any, any, any> => {
1123 const cached = securityMiddlewareCache.get(service)
1124 if (cached !== undefined) {
1125 return cached
1126 }
1127
1128 const entries = Object.entries(key.security).map(([securityKey, security]) => ({
1129 decode: securityDecode(security),
1130 middleware: service[securityKey]
1131 }))
1132 if (entries.length === 0) {
1133 return Function.identity
1134 }
1135
1136 const middleware = Effect.fnUntraced(function*(handler: Effect.Effect<any, any, any>, options: {
1137 readonly group: HttpApiGroup.Top
1138 readonly endpoint: HttpApiEndpoint.Top
1139 }) {
1140 handler = Effect.mapError(handler, (error) => new HandlerError(error))
1141 let lastResult: Result.Result<any, any> | undefined
1142 for (let i = 0; i < entries.length; i++) {
1143 const { decode, middleware } = entries[i]
1144 const result = yield* Effect.result(Effect.flatMap(decode, (credential) =>
1145 middleware(handler, {
1146 credential,
1147 endpoint: options.endpoint,
1148 group: options.group
1149 })))
1150 if (Result.isFailure(result)) {
1151 if (isHandlerError(result.failure)) {
1152 return yield* Effect.fail(result.failure.error)
1153 }
1154 lastResult = result
1155 continue
1156 }
1157 return result.success
1158 }
1159 return yield* Effect.fromResult(lastResult!)
1160 })
1161
1162 securityMiddlewareCache.set(service, middleware)
1163 return middleware
1164}
1165
1166const HandlerErrorTypeId = "~effect/http-api/HttpApiBuilder/HandlerError" as const
1167class HandlerError {

Callers 1

applyMiddlewareFunction · 0.85

Calls 8

securityDecodeFunction · 0.85
isHandlerErrorFunction · 0.85
middlewareFunction · 0.70
getMethod · 0.65
resultMethod · 0.65
failMethod · 0.65
setMethod · 0.65
mapMethod · 0.45

Tested by

no test coverage detected