Build forms from types in a Raycast extension

Let a Raycast command save an item of any type, with a form built from the type's fields, and see what Marfa says when the key can't write a type.

  • You'll build a form that changes with the type you choose, built from what Marfa says about the type.
  • You need the extension from Build a Raycast extension, its key, and the Quickstart terminal.
  • You'll have a command that saves an item of any type its key can write, and shows the server's refusal for one it can't.

A type lists the fields its items hold, and each field says what kind of value it takes. Read the type, and the extension can draw a form for it with no code that knows the type by name. A type you add later gets its form with no change to the extension.

Step 1 of 5: Read the types

Read the types

In the extension's folder, add Raycast's utilities, which the form uses to load data:

npm install @raycast/utils

In src/marfa.ts, import the client's schema types alongside createClient, and add two calls beside createItem:

src/marfa.ts
import { createClient, type components } from "@withmarfa/client";

export type TypeDefinition = components["schemas"]["TypeDefinition"];

export async function listTypes() {
  const { data, error, response } = await client().GET("/types");
  if (error) throw refused(response, error);
  return data.data;
}

export async function getType(id: string) {
  const { data, error, response } = await client().GET("/types/{id}", {
    params: { path: { id } },
  });
  if (error) throw refused(response, error);
  return data;
}

List types answers every type the instance holds. Get a type answers one with every field it holds, including the ones it inherits from its parent type, so a form built from it is complete.

Next steps

Add a type of your own and open the command again. Its form appears with no change to the extension. To save an item of it, give the extension a key that names the type, as in the last step.

On this page