diff options
| author | Frank <[email protected]> | 2025-08-08 13:22:54 -0400 |
|---|---|---|
| committer | Frank <[email protected]> | 2025-08-08 13:24:32 -0400 |
| commit | 183e0911b76025a1f2a82e979d9834fec2131d0e (patch) | |
| tree | 9987c1753bd64d1ce1d174ab397f1a8c681f642c /cloud/core/src/util | |
| parent | c7bb19ad0712469063eab35589aa5d3602b0c5b1 (diff) | |
| download | opencode-183e0911b76025a1f2a82e979d9834fec2131d0e.tar.gz opencode-183e0911b76025a1f2a82e979d9834fec2131d0e.zip | |
wip: gateway
Diffstat (limited to 'cloud/core/src/util')
| -rw-r--r-- | cloud/core/src/util/fn.ts | 14 | ||||
| -rw-r--r-- | cloud/core/src/util/log.ts | 55 | ||||
| -rw-r--r-- | cloud/core/src/util/price.ts | 3 |
3 files changed, 72 insertions, 0 deletions
diff --git a/cloud/core/src/util/fn.ts b/cloud/core/src/util/fn.ts new file mode 100644 index 000000000..038a50719 --- /dev/null +++ b/cloud/core/src/util/fn.ts @@ -0,0 +1,14 @@ +import { z } from "zod" + +export function fn<T extends z.ZodType, Result>( + schema: T, + cb: (input: z.output<T>) => Result, +) { + const result = (input: z.input<T>) => { + const parsed = schema.parse(input) + return cb(parsed) + } + result.force = (input: z.input<T>) => cb(input) + result.schema = schema + return result +} diff --git a/cloud/core/src/util/log.ts b/cloud/core/src/util/log.ts new file mode 100644 index 000000000..4f2d25c13 --- /dev/null +++ b/cloud/core/src/util/log.ts @@ -0,0 +1,55 @@ +import { Context } from "../context" + +export namespace Log { + const ctx = Context.create<{ + tags: Record<string, any> + }>() + + export function create(tags?: Record<string, any>) { + tags = tags || {} + + const result = { + info(message?: any, extra?: Record<string, any>) { + const prefix = Object.entries({ + ...use().tags, + ...tags, + ...extra, + }) + .map(([key, value]) => `${key}=${value}`) + .join(" ") + console.log(prefix, message) + return result + }, + tag(key: string, value: string) { + if (tags) tags[key] = value + return result + }, + clone() { + return Log.create({ ...tags }) + }, + } + + return result + } + + export function provide<R>(tags: Record<string, any>, cb: () => R) { + const existing = use() + return ctx.provide( + { + tags: { + ...existing.tags, + ...tags, + }, + }, + cb, + ) + } + + function use() { + try { + return ctx.use() + } catch (e) { + return { tags: {} } + } + } +} diff --git a/cloud/core/src/util/price.ts b/cloud/core/src/util/price.ts new file mode 100644 index 000000000..abdbca032 --- /dev/null +++ b/cloud/core/src/util/price.ts @@ -0,0 +1,3 @@ +export function centsToMicroCents(amount: number) { + return Math.round(amount * 1000000) +} |
