Blog
`fn` utility in Opencode console package.

`fn` utility in Opencode console package.

In this article, we review fn utility in Opencode console package. We will look at:

  1. What is fn function?

  2. fn usage

I study patterns used in an open source project found on Github Trending. For this week, I reviewed some parts of opencode codebase and wrote this article.

What is fn function?

You will find the following fn definition in sst/opencode/packages/console/core/src/util/fn.ts

import { z } from "zod"

export function fn<T extends z.ZodType, Result>(
  schema: T, 
  cb: (input: z.infer<T>
) => Result) {
  const result = (input: z.infer<T>) => {
    const parsed = schema.parse(input)
    return cb(parsed)
  }
  result.force = (input: z.infer<T>) => cb(input)
  result.schema = schema
  return result
}

Seems like a pretty generic function that parses the input and this parsed result is used a paramter in callback function cb .

fn usage

In sst/opencode/packages/console/core/src/account.ts, you will find some references to this fn function.

create function

export const create = fn(
    z.object({
      email: z.string().email(),
      id: z.string().optional(),
    }),
    async (input) =>
      Database.transaction(async (tx) => {
        const id = input.id ?? Identifier.create("account")
        await tx.insert(AccountTable).values({
          id,
          email: input.email,
        })
        return id
      }),
  )

fromID function

export const fromID = fn(z.string(), async (id) =>
    Database.transaction(async (tx) => {
      return tx
        .select()
        .from(AccountTable)
        .where(eq(AccountTable.id, id))
        .execute()
        .then((rows) => rows[0])
    }),
  )

In both these functions, you can understand that first parameter is the schema and the second parameter is the callback function. 

input passed as a parameter to the callback is parsed based on the first parameter, schema.

Below is the fn function declaration:

export function fn<T extends z.ZodType, Result>(
  schema: T, 
  cb: (input: z.infer<T>
)

About me:

Hey, my name is Ramu Narasinga. I study codebase architecture in large open-source projects.

Email: ramu.narasinga@gmail.com

Want to learn from open-source? Solve challenges inspired by open-source projects.

References:

  1. https://github.com/sst/opencode/blob/dev/packages/console/core/src/util/fn.ts

  2. https://github.com/sst/opencode/blob/dev/packages/console/core/src/account.ts