Skip to content

Commit

Permalink
Supports custom formatters in browsers (#947)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericanderson authored Nov 19, 2024
1 parent 11088aa commit 14624f7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-colts-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@osdk/client": patch
---

Supports custom formatters in browser
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,65 @@ const objectPrototypeCache = createClientCache(
},
);

if (process.env.NODE_ENV !== "production") {
const installed = Symbol();
const gw: any = typeof window === "undefined" ? global : window;
if (!(installed in gw)) {
gw[installed] = true;

gw.devtoolsFormatters ??= [];
gw.devtoolsFormatters.push({
header: function(object: any) {
const raw = object[RawObject];
if (raw == null) return null;

return [
"div",
{},

`Osdk.Instance<${raw.$apiName}> { $primaryKey:`,
["object", { object: raw.$primaryKey }],
`, $title:`,
["object", { object: raw.$title }],
`, ... }`,
];
},
hasBody: function(object: any) {
return object[RawObject] != null;
},
body: function(object: any) {
const raw = object[RawObject];
if (raw == null) return null;

return [
"ol",
{
style: `
list-style-type: none;
padding-left: 0;
margin-top: 0;
margin-left: 18px
`,
},
["li", {}, "$raw:", ["object", { object: raw }]],
// ...Object.keys(raw).map(key => {
// return [
// "li",
// {},
// [
// "span",
// { style: "color: #888888" },
// `${key}: `,
// ],
// ["object", { object: raw[key] }], // ["span", {}, `${key}: `]],
// ];
// }),
];
},
});
}
}

/** @internal */
export function createOsdkObject<
Q extends FetchedObjectTypeDefinition,
Expand Down
3 changes: 3 additions & 0 deletions packages/e2e.sandbox.todoapp/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ function App() {
const { todos, isLoading, toggleComplete, error, isValidating, createTodo } =
useTodos();

if (todos) {
console.log(todos[0]);
}
return (
<main className="flex min-h-screen flex-col items-center p-24 ">
<h1 className="mb-6 text-xl">Todos</h1>
Expand Down

0 comments on commit 14624f7

Please sign in to comment.