Skip to content

Commit 710c819

Browse files
authored
feat: unwrap uauth namespace to flat exports + barrel (anomalyco#22699)
1 parent a1dbfb5 commit 710c819

2 files changed

Lines changed: 91 additions & 91 deletions

File tree

‎packages/opencode/src/auth/auth.ts‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import path from "path"
2+
import { Effect, Layer, Record, Result, Schema, Context } from "effect"
3+
import { zod } from "@/util/effect-zod"
4+
import { Global } from "../global"
5+
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
6+
7+
export const OAUTH_DUMMY_KEY = "opencode-oauth-dummy-key"
8+
9+
const file = path.join(Global.Path.data, "auth.json")
10+
11+
const fail = (message: string) => (cause: unknown) => new AuthError({ message, cause })
12+
13+
export class Oauth extends Schema.Class<Oauth>("OAuth")({
14+
type: Schema.Literal("oauth"),
15+
refresh: Schema.String,
16+
access: Schema.String,
17+
expires: Schema.Number,
18+
accountId: Schema.optional(Schema.String),
19+
enterpriseUrl: Schema.optional(Schema.String),
20+
}) {}
21+
22+
export class Api extends Schema.Class<Api>("ApiAuth")({
23+
type: Schema.Literal("api"),
24+
key: Schema.String,
25+
metadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
26+
}) {}
27+
28+
export class WellKnown extends Schema.Class<WellKnown>("WellKnownAuth")({
29+
type: Schema.Literal("wellknown"),
30+
key: Schema.String,
31+
token: Schema.String,
32+
}) {}
33+
34+
const _Info = Schema.Union([Oauth, Api, WellKnown]).annotate({ discriminator: "type", identifier: "Auth" })
35+
export const Info = Object.assign(_Info, { zod: zod(_Info) })
36+
export type Info = Schema.Schema.Type<typeof _Info>
37+
38+
export class AuthError extends Schema.TaggedErrorClass<AuthError>()("AuthError", {
39+
message: Schema.String,
40+
cause: Schema.optional(Schema.Defect),
41+
}) {}
42+
43+
export interface Interface {
44+
readonly get: (providerID: string) => Effect.Effect<Info | undefined, AuthError>
45+
readonly all: () => Effect.Effect<Record<string, Info>, AuthError>
46+
readonly set: (key: string, info: Info) => Effect.Effect<void, AuthError>
47+
readonly remove: (key: string) => Effect.Effect<void, AuthError>
48+
}
49+
50+
export class Service extends Context.Service<Service, Interface>()("@opencode/Auth") {}
51+
52+
export const layer = Layer.effect(
53+
Service,
54+
Effect.gen(function* () {
55+
const fsys = yield* AppFileSystem.Service
56+
const decode = Schema.decodeUnknownOption(Info)
57+
58+
const all = Effect.fn("Auth.all")(function* () {
59+
const data = (yield* fsys.readJson(file).pipe(Effect.orElseSucceed(() => ({})))) as Record<string, unknown>
60+
return Record.filterMap(data, (value) => Result.fromOption(decode(value), () => undefined))
61+
})
62+
63+
const get = Effect.fn("Auth.get")(function* (providerID: string) {
64+
return (yield* all())[providerID]
65+
})
66+
67+
const set = Effect.fn("Auth.set")(function* (key: string, info: Info) {
68+
const norm = key.replace(/\/+$/, "")
69+
const data = yield* all()
70+
if (norm !== key) delete data[key]
71+
delete data[norm + "/"]
72+
yield* fsys
73+
.writeJson(file, { ...data, [norm]: info }, 0o600)
74+
.pipe(Effect.mapError(fail("Failed to write auth data")))
75+
})
76+
77+
const remove = Effect.fn("Auth.remove")(function* (key: string) {
78+
const norm = key.replace(/\/+$/, "")
79+
const data = yield* all()
80+
delete data[key]
81+
delete data[norm]
82+
yield* fsys.writeJson(file, data, 0o600).pipe(Effect.mapError(fail("Failed to write auth data")))
83+
})
84+
85+
return Service.of({ get, all, set, remove })
86+
}),
87+
)
88+
89+
export const defaultLayer = layer.pipe(Layer.provide(AppFileSystem.defaultLayer))
Lines changed: 2 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,2 @@
1-
import path from "path"
2-
import { Effect, Layer, Record, Result, Schema, Context } from "effect"
3-
import { zod } from "@/util/effect-zod"
4-
import { Global } from "../global"
5-
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
6-
7-
export const OAUTH_DUMMY_KEY = "opencode-oauth-dummy-key"
8-
9-
const file = path.join(Global.Path.data, "auth.json")
10-
11-
const fail = (message: string) => (cause: unknown) => new Auth.AuthError({ message, cause })
12-
13-
export namespace Auth {
14-
export class Oauth extends Schema.Class<Oauth>("OAuth")({
15-
type: Schema.Literal("oauth"),
16-
refresh: Schema.String,
17-
access: Schema.String,
18-
expires: Schema.Number,
19-
accountId: Schema.optional(Schema.String),
20-
enterpriseUrl: Schema.optional(Schema.String),
21-
}) {}
22-
23-
export class Api extends Schema.Class<Api>("ApiAuth")({
24-
type: Schema.Literal("api"),
25-
key: Schema.String,
26-
metadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
27-
}) {}
28-
29-
export class WellKnown extends Schema.Class<WellKnown>("WellKnownAuth")({
30-
type: Schema.Literal("wellknown"),
31-
key: Schema.String,
32-
token: Schema.String,
33-
}) {}
34-
35-
const _Info = Schema.Union([Oauth, Api, WellKnown]).annotate({ discriminator: "type", identifier: "Auth" })
36-
export const Info = Object.assign(_Info, { zod: zod(_Info) })
37-
export type Info = Schema.Schema.Type<typeof _Info>
38-
39-
export class AuthError extends Schema.TaggedErrorClass<AuthError>()("AuthError", {
40-
message: Schema.String,
41-
cause: Schema.optional(Schema.Defect),
42-
}) {}
43-
44-
export interface Interface {
45-
readonly get: (providerID: string) => Effect.Effect<Info | undefined, AuthError>
46-
readonly all: () => Effect.Effect<Record<string, Info>, AuthError>
47-
readonly set: (key: string, info: Info) => Effect.Effect<void, AuthError>
48-
readonly remove: (key: string) => Effect.Effect<void, AuthError>
49-
}
50-
51-
export class Service extends Context.Service<Service, Interface>()("@opencode/Auth") {}
52-
53-
export const layer = Layer.effect(
54-
Service,
55-
Effect.gen(function* () {
56-
const fsys = yield* AppFileSystem.Service
57-
const decode = Schema.decodeUnknownOption(Info)
58-
59-
const all = Effect.fn("Auth.all")(function* () {
60-
const data = (yield* fsys.readJson(file).pipe(Effect.orElseSucceed(() => ({})))) as Record<string, unknown>
61-
return Record.filterMap(data, (value) => Result.fromOption(decode(value), () => undefined))
62-
})
63-
64-
const get = Effect.fn("Auth.get")(function* (providerID: string) {
65-
return (yield* all())[providerID]
66-
})
67-
68-
const set = Effect.fn("Auth.set")(function* (key: string, info: Info) {
69-
const norm = key.replace(/\/+$/, "")
70-
const data = yield* all()
71-
if (norm !== key) delete data[key]
72-
delete data[norm + "/"]
73-
yield* fsys
74-
.writeJson(file, { ...data, [norm]: info }, 0o600)
75-
.pipe(Effect.mapError(fail("Failed to write auth data")))
76-
})
77-
78-
const remove = Effect.fn("Auth.remove")(function* (key: string) {
79-
const norm = key.replace(/\/+$/, "")
80-
const data = yield* all()
81-
delete data[key]
82-
delete data[norm]
83-
yield* fsys.writeJson(file, data, 0o600).pipe(Effect.mapError(fail("Failed to write auth data")))
84-
})
85-
86-
return Service.of({ get, all, set, remove })
87-
}),
88-
)
89-
90-
export const defaultLayer = layer.pipe(Layer.provide(AppFileSystem.defaultLayer))
91-
}
1+
export * as Auth from "./auth"
2+
export { OAUTH_DUMMY_KEY } from "./auth"

0 commit comments

Comments
 (0)