( key: HttpApiMiddleware.AnyServiceSecurity, service: HttpApiMiddleware.HttpApiMiddlewareSecurity<any, any, any, any> )
| 1117 | >() |
| 1118 | |
| 1119 | const 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 | |
| 1166 | const HandlerErrorTypeId = "~effect/http-api/HttpApiBuilder/HandlerError" as const |
| 1167 | class HandlerError { |
no test coverage detected