DEV Community

Rafael Mejia
Rafael Mejia

Posted on

How to Typescript to JSON with Butlermock

Butlermock

It's a tool for building mocks from typescript's types/interfaces into object with actual data provided by Fakerjs

It's open source :)

Web usage

Just go to https://butlermock.deno.dev/ and in the left panel paste your interfaces or types.

Wait until the monaco editor shows up and paste your interfaces to later clicking the play button and the mock shows up almost immediately.

Image description

The "play" button mocks up, "clipboard" copies the mock created and the "X" button is for cleaning the view

API documentation

Easy peasy


fetch(new URL("/api/mock", 'https://butlermock.deno.dev').href, {
    method: "POST",
    headers: {
      "content-type": "application/json",
    },
    body: JSON.stringify({
      value: `interface Test {
        Hello: "World!";
        }`,
      valueForAny: "null", // any parseable json value, values: "null" | "\"Whatever the text you want\"" | "{}" | "0" | "1" | ... | "9"
      mustReturn: "", // empty or the top-level name of the interfaces that you provided
      quantity: 1, // if is greater than 1, the api response will be an array of the interfaces provided
    }),
  })
  .then(res => res.json()).then(console.log); // should log:
  //{
  // "Test": {
  //    "Hello": "World!"
  //  }
  //}

Enter fullscreen mode Exit fullscreen mode

Always remember to format your interfaces or types correctly, if you forget to add ";" it may result in bad parsing

new interface2mock(`interface Test {
  Hello: "World!" // -> No closing semicolon ( ; )
  supa: string;
}`).buildMock()

/*
{
  "Test": {
    "Hello": null
  }
}
*/
Enter fullscreen mode Exit fullscreen mode

Recommedations when using the API

If you are going to consume the API I suggest that you use fetch() instead of your own HTTPS calling implementation, to avoid sending credentials which I don't store, but security reasons try to use it in the way I suggest, just for privacy reasons. REPEATING, I DO NOT STORE ANY DATA. :).

Limitations

This section is for known limitations that Butlermocks has, but in the future might be able to recognize and mock:

type casting description
Array Array<string \ boolean \

Status

  • [x] Interfaces
interface NonMockeableInterface {} // ❌ empty interface

interface MockeableInterface {
  somethingToMock: any;
} // ✅ Mockeable

export interface AnotherMockeableInterface {
  anotherThingToMock: any;
} // ✅ Mockeable, 'export' is ignored
Enter fullscreen mode Exit fullscreen mode
  • [x] Interfaces with nested interfaces/types
type mockeableType = {
  name: string;
} // ✅ Mockeable

interface MockeableInterface {
  somethingToMock: any;
  nestedInterface: AnotherMockeableInterface;
  extraNestedInterface: AnotherMockeableInterface[];
} // ✅ Mockeable

export interface AnotherMockeableInterface {
  anotherThingToMock: any;
} // ✅ Mockeable, 'export' is ignored
Enter fullscreen mode Exit fullscreen mode
  • [ ] Interfaces that extends from other interfaces/types
// Not mockeable yet
interface That { ... }

interface This extends That {} // ❌
Enter fullscreen mode Exit fullscreen mode
  • [ ] Generic Interfaces
// Not mockeable yet
interface This<T> {
  here: T;
} // ❌
Enter fullscreen mode Exit fullscreen mode
  • [ ] Generic Interfaces that extends from other interfaces/types
// Not mockeable yet
interface Evnt<T> {
  name: T;
}

interface IPropertiesToAdd<T> extends Evnt<T> {
  on(): void;
  off(): void;
} // ❌
Enter fullscreen mode Exit fullscreen mode
  • [x] Primitive Type
// Not mockeable yet
type justString = string; // ✅ Mockeable
Enter fullscreen mode Exit fullscreen mode
  • [x] Type object
type justString = {
  yes: 'you can';
}; // ✅ Mockeable
Enter fullscreen mode Exit fullscreen mode
  • [x] Type with nested interfaces/types

type mockeableType = {
  nestedObject: MockeableInterface;
} // ✅ Mockeable

interface MockeableInterface {
  somethingToMock: any;
} // ✅ Mockeable
Enter fullscreen mode Exit fullscreen mode
  • [ ] Generic Type
// Not mockeable yet
type IPropertiesToAdd<T extends {}> = T & {
    on(): void
    off(): void
}; // ❌
Enter fullscreen mode Exit fullscreen mode

Top comments (0)