Skip to content

Commit 5022895

Browse files
authored
refactor: unwrap ExperimentalHttpApiServer namespace + self-reexport (#22979)
1 parent 54046e0 commit 5022895

1 file changed

Lines changed: 91 additions & 91 deletions

File tree

  • packages/opencode/src/server/instance/httpapi

packages/opencode/src/server/instance/httpapi/server.ts

Lines changed: 91 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -25,106 +25,106 @@ const Headers = Schema.Struct({
2525
"x-opencode-directory": Schema.optional(Schema.String),
2626
})
2727

28-
export namespace ExperimentalHttpApiServer {
29-
function decode(input: string) {
30-
try {
31-
return decodeURIComponent(input)
32-
} catch {
33-
return input
34-
}
28+
function decode(input: string) {
29+
try {
30+
return decodeURIComponent(input)
31+
} catch {
32+
return input
3533
}
34+
}
3635

37-
class Unauthorized extends Schema.TaggedErrorClass<Unauthorized>()(
38-
"Unauthorized",
39-
{ message: Schema.String },
40-
{ httpApiStatus: 401 },
41-
) {}
36+
class Unauthorized extends Schema.TaggedErrorClass<Unauthorized>()(
37+
"Unauthorized",
38+
{ message: Schema.String },
39+
{ httpApiStatus: 401 },
40+
) {}
4241

43-
class Authorization extends HttpApiMiddleware.Service<Authorization>()("@opencode/ExperimentalHttpApiAuthorization", {
44-
error: Unauthorized,
45-
security: {
46-
basic: HttpApiSecurity.basic,
47-
},
48-
}) {}
42+
class Authorization extends HttpApiMiddleware.Service<Authorization>()("@opencode/ExperimentalHttpApiAuthorization", {
43+
error: Unauthorized,
44+
security: {
45+
basic: HttpApiSecurity.basic,
46+
},
47+
}) {}
4948

50-
const normalize = HttpRouter.middleware()(
51-
Effect.gen(function* () {
52-
return (effect) =>
53-
Effect.gen(function* () {
54-
const query = yield* HttpServerRequest.schemaSearchParams(Query)
55-
if (!query.auth_token) return yield* effect
56-
const req = yield* HttpServerRequest.HttpServerRequest
57-
const next = req.modify({
58-
headers: {
59-
...req.headers,
60-
authorization: `Basic ${query.auth_token}`,
61-
},
62-
})
63-
return yield* effect.pipe(Effect.provideService(HttpServerRequest.HttpServerRequest, next))
49+
const normalize = HttpRouter.middleware()(
50+
Effect.gen(function* () {
51+
return (effect) =>
52+
Effect.gen(function* () {
53+
const query = yield* HttpServerRequest.schemaSearchParams(Query)
54+
if (!query.auth_token) return yield* effect
55+
const req = yield* HttpServerRequest.HttpServerRequest
56+
const next = req.modify({
57+
headers: {
58+
...req.headers,
59+
authorization: `Basic ${query.auth_token}`,
60+
},
6461
})
65-
}),
66-
).layer
62+
return yield* effect.pipe(Effect.provideService(HttpServerRequest.HttpServerRequest, next))
63+
})
64+
}),
65+
).layer
6766

68-
const auth = Layer.succeed(
69-
Authorization,
70-
Authorization.of({
71-
basic: (effect, { credential }) =>
72-
Effect.gen(function* () {
73-
if (!Flag.OPENCODE_SERVER_PASSWORD) return yield* effect
67+
const auth = Layer.succeed(
68+
Authorization,
69+
Authorization.of({
70+
basic: (effect, { credential }) =>
71+
Effect.gen(function* () {
72+
if (!Flag.OPENCODE_SERVER_PASSWORD) return yield* effect
7473

75-
const user = Flag.OPENCODE_SERVER_USERNAME ?? "opencode"
76-
if (credential.username !== user) {
77-
return yield* new Unauthorized({ message: "Unauthorized" })
78-
}
79-
if (Redacted.value(credential.password) !== Flag.OPENCODE_SERVER_PASSWORD) {
80-
return yield* new Unauthorized({ message: "Unauthorized" })
81-
}
82-
return yield* effect
83-
}),
84-
}),
85-
)
74+
const user = Flag.OPENCODE_SERVER_USERNAME ?? "opencode"
75+
if (credential.username !== user) {
76+
return yield* new Unauthorized({ message: "Unauthorized" })
77+
}
78+
if (Redacted.value(credential.password) !== Flag.OPENCODE_SERVER_PASSWORD) {
79+
return yield* new Unauthorized({ message: "Unauthorized" })
80+
}
81+
return yield* effect
82+
}),
83+
}),
84+
)
8685

87-
const instance = HttpRouter.middleware()(
88-
Effect.gen(function* () {
89-
return (effect) =>
90-
Effect.gen(function* () {
91-
const query = yield* HttpServerRequest.schemaSearchParams(Query)
92-
const headers = yield* HttpServerRequest.schemaHeaders(Headers)
93-
const raw = query.directory || headers["x-opencode-directory"] || process.cwd()
94-
const workspace = query.workspace || undefined
95-
const ctx = yield* Effect.promise(() =>
96-
Instance.provide({
97-
directory: Filesystem.resolve(decode(raw)),
98-
init: () => AppRuntime.runPromise(InstanceBootstrap),
99-
fn: () => Instance.current,
100-
}),
101-
)
86+
const instance = HttpRouter.middleware()(
87+
Effect.gen(function* () {
88+
return (effect) =>
89+
Effect.gen(function* () {
90+
const query = yield* HttpServerRequest.schemaSearchParams(Query)
91+
const headers = yield* HttpServerRequest.schemaHeaders(Headers)
92+
const raw = query.directory || headers["x-opencode-directory"] || process.cwd()
93+
const workspace = query.workspace || undefined
94+
const ctx = yield* Effect.promise(() =>
95+
Instance.provide({
96+
directory: Filesystem.resolve(decode(raw)),
97+
init: () => AppRuntime.runPromise(InstanceBootstrap),
98+
fn: () => Instance.current,
99+
}),
100+
)
102101

103-
const next = workspace ? effect.pipe(Effect.provideService(WorkspaceRef, workspace)) : effect
104-
return yield* next.pipe(Effect.provideService(InstanceRef, ctx))
105-
})
106-
}),
107-
).layer
102+
const next = workspace ? effect.pipe(Effect.provideService(WorkspaceRef, workspace)) : effect
103+
return yield* next.pipe(Effect.provideService(InstanceRef, ctx))
104+
})
105+
}),
106+
).layer
108107

109-
const QuestionSecured = QuestionApi.middleware(Authorization)
110-
const PermissionSecured = PermissionApi.middleware(Authorization)
111-
const ProviderSecured = ProviderApi.middleware(Authorization)
108+
const QuestionSecured = QuestionApi.middleware(Authorization)
109+
const PermissionSecured = PermissionApi.middleware(Authorization)
110+
const ProviderSecured = ProviderApi.middleware(Authorization)
112111

113-
export const routes = Layer.mergeAll(
114-
HttpApiBuilder.layer(QuestionSecured).pipe(Layer.provide(questionHandlers)),
115-
HttpApiBuilder.layer(PermissionSecured).pipe(Layer.provide(permissionHandlers)),
116-
HttpApiBuilder.layer(ProviderSecured).pipe(Layer.provide(providerHandlers)),
117-
).pipe(
118-
Layer.provide(auth),
119-
Layer.provide(normalize),
120-
Layer.provide(instance),
121-
Layer.provide(HttpServer.layerServices),
122-
Layer.provideMerge(Observability.layer),
123-
)
112+
export const routes = Layer.mergeAll(
113+
HttpApiBuilder.layer(QuestionSecured).pipe(Layer.provide(questionHandlers)),
114+
HttpApiBuilder.layer(PermissionSecured).pipe(Layer.provide(permissionHandlers)),
115+
HttpApiBuilder.layer(ProviderSecured).pipe(Layer.provide(providerHandlers)),
116+
).pipe(
117+
Layer.provide(auth),
118+
Layer.provide(normalize),
119+
Layer.provide(instance),
120+
Layer.provide(HttpServer.layerServices),
121+
Layer.provideMerge(Observability.layer),
122+
)
124123

125-
export const webHandler = lazy(() =>
126-
HttpRouter.toWebHandler(routes, {
127-
memoMap,
128-
}),
129-
)
130-
}
124+
export const webHandler = lazy(() =>
125+
HttpRouter.toWebHandler(routes, {
126+
memoMap,
127+
}),
128+
)
129+
130+
export * as ExperimentalHttpApiServer from "./server"

0 commit comments

Comments
 (0)