refactor: unwrap ExperimentalHttpApiServer namespace + self-reexport (#22979)
This commit is contained in:
@@ -25,29 +25,28 @@ const Headers = Schema.Struct({
|
|||||||
"x-opencode-directory": Schema.optional(Schema.String),
|
"x-opencode-directory": Schema.optional(Schema.String),
|
||||||
})
|
})
|
||||||
|
|
||||||
export namespace ExperimentalHttpApiServer {
|
function decode(input: string) {
|
||||||
function decode(input: string) {
|
|
||||||
try {
|
try {
|
||||||
return decodeURIComponent(input)
|
return decodeURIComponent(input)
|
||||||
} catch {
|
} catch {
|
||||||
return input
|
return input
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Unauthorized extends Schema.TaggedErrorClass<Unauthorized>()(
|
class Unauthorized extends Schema.TaggedErrorClass<Unauthorized>()(
|
||||||
"Unauthorized",
|
"Unauthorized",
|
||||||
{ message: Schema.String },
|
{ message: Schema.String },
|
||||||
{ httpApiStatus: 401 },
|
{ httpApiStatus: 401 },
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
class Authorization extends HttpApiMiddleware.Service<Authorization>()("@opencode/ExperimentalHttpApiAuthorization", {
|
class Authorization extends HttpApiMiddleware.Service<Authorization>()("@opencode/ExperimentalHttpApiAuthorization", {
|
||||||
error: Unauthorized,
|
error: Unauthorized,
|
||||||
security: {
|
security: {
|
||||||
basic: HttpApiSecurity.basic,
|
basic: HttpApiSecurity.basic,
|
||||||
},
|
},
|
||||||
}) {}
|
}) {}
|
||||||
|
|
||||||
const normalize = HttpRouter.middleware()(
|
const normalize = HttpRouter.middleware()(
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
return (effect) =>
|
return (effect) =>
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
@@ -63,9 +62,9 @@ export namespace ExperimentalHttpApiServer {
|
|||||||
return yield* effect.pipe(Effect.provideService(HttpServerRequest.HttpServerRequest, next))
|
return yield* effect.pipe(Effect.provideService(HttpServerRequest.HttpServerRequest, next))
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
).layer
|
).layer
|
||||||
|
|
||||||
const auth = Layer.succeed(
|
const auth = Layer.succeed(
|
||||||
Authorization,
|
Authorization,
|
||||||
Authorization.of({
|
Authorization.of({
|
||||||
basic: (effect, { credential }) =>
|
basic: (effect, { credential }) =>
|
||||||
@@ -82,9 +81,9 @@ export namespace ExperimentalHttpApiServer {
|
|||||||
return yield* effect
|
return yield* effect
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
const instance = HttpRouter.middleware()(
|
const instance = HttpRouter.middleware()(
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
return (effect) =>
|
return (effect) =>
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
@@ -104,27 +103,28 @@ export namespace ExperimentalHttpApiServer {
|
|||||||
return yield* next.pipe(Effect.provideService(InstanceRef, ctx))
|
return yield* next.pipe(Effect.provideService(InstanceRef, ctx))
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
).layer
|
).layer
|
||||||
|
|
||||||
const QuestionSecured = QuestionApi.middleware(Authorization)
|
const QuestionSecured = QuestionApi.middleware(Authorization)
|
||||||
const PermissionSecured = PermissionApi.middleware(Authorization)
|
const PermissionSecured = PermissionApi.middleware(Authorization)
|
||||||
const ProviderSecured = ProviderApi.middleware(Authorization)
|
const ProviderSecured = ProviderApi.middleware(Authorization)
|
||||||
|
|
||||||
export const routes = Layer.mergeAll(
|
export const routes = Layer.mergeAll(
|
||||||
HttpApiBuilder.layer(QuestionSecured).pipe(Layer.provide(questionHandlers)),
|
HttpApiBuilder.layer(QuestionSecured).pipe(Layer.provide(questionHandlers)),
|
||||||
HttpApiBuilder.layer(PermissionSecured).pipe(Layer.provide(permissionHandlers)),
|
HttpApiBuilder.layer(PermissionSecured).pipe(Layer.provide(permissionHandlers)),
|
||||||
HttpApiBuilder.layer(ProviderSecured).pipe(Layer.provide(providerHandlers)),
|
HttpApiBuilder.layer(ProviderSecured).pipe(Layer.provide(providerHandlers)),
|
||||||
).pipe(
|
).pipe(
|
||||||
Layer.provide(auth),
|
Layer.provide(auth),
|
||||||
Layer.provide(normalize),
|
Layer.provide(normalize),
|
||||||
Layer.provide(instance),
|
Layer.provide(instance),
|
||||||
Layer.provide(HttpServer.layerServices),
|
Layer.provide(HttpServer.layerServices),
|
||||||
Layer.provideMerge(Observability.layer),
|
Layer.provideMerge(Observability.layer),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const webHandler = lazy(() =>
|
export const webHandler = lazy(() =>
|
||||||
HttpRouter.toWebHandler(routes, {
|
HttpRouter.toWebHandler(routes, {
|
||||||
memoMap,
|
memoMap,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
export * as ExperimentalHttpApiServer from "./server"
|
||||||
|
|||||||
Reference in New Issue
Block a user