Skip to content

Tool for creating mocks objects with fake data using TypeScript types/interfaces as schema and Faker as fake data source

License

Notifications You must be signed in to change notification settings

Devrax/Butlermock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

96 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Butlermock

Made with Fresh

Buy Me A Coffee

Tool for building mocks from typescript's types/interfaces into object with actual data using Fakerjs

Documentation on develop, in the near future I will expose the endpoints for you to use it within your client application, so, stay on tune πŸ’

Table of contents

Web usage

Just go to https://butlermock.online/ and in the left panel paste your interfaces or types, just be aware of the current limitations ( I am working for supporting the others )

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

Butlermock's landing

button guide

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.online').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!"
  //  }
  //}

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

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. :).

Example as package/library πŸ‘·πŸ»β€β™‚οΈπŸš§

Check here > Under construction 🚧

Guide when providing an interface or type to be mocked up

const mock = new Interface2Mock(`interface Greeting {
    hello: string;
    cursed: {
        damn: string;
    }[];
  }`); // ❌ Butler mock cannot process direct objects, yet
const mock = new Interface2Mock(`interface Greeting {
    hello: string;
    cursed: CursedWord[];
  }

  type CursedWord = {
    damn: string;
  }`); // βœ… Butler Mock can format this way

FAQs

Q: Is it using an AI?

A: No :)

Q: How can I tell the API or website to give me a fixed value?

A: You just type explicitly the value you want:

interface ThisIsSparta {
  troops: 300;
  leader: "Leonidas";
  murderedBy: true;
}

interface mightBeSparta {
  troops: 200 | 594 | 2893 | 39;
  leader: "Trump" | "Me" | "You?";
  murderedBy: true; // fixed Booleans can not be multiples values, for that use explicitly 'boolean'
}

Q: Can I copy full code snippet and it will detect the interfaces?

A: Yes :) Can I copy full code snippet and it will detect the interfaces?

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 | number | any> This casting is not supported, use (string | boolean | number | any)[] instead
fixed string When fixed string includes ";" or ":" When providing a fixed string value with a semicolon in it, you must use escape for that semicolon inside the string for correct json representation; Example: interface Example { fixedString: "I am using a semicolon '\\;' so I used escaping '\\' :)"; }

Status

  • Interfaces
interface NonMockeableInterface {} // ❌ empty interface

interface MockeableInterface {
  somethingToMock: any;
} // βœ… Mockeable

export interface AnotherMockeableInterface {
  anotherThingToMock: any;
} // βœ… Mockeable, 'export' is ignored

  • 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

  • Interfaces that extends from other interfaces/types
interface That { ... }

interface This extends That {} // βœ… Mockeable

  • Generic Interfaces
// Not mockeable yet
interface This<T> {
  here: T;
} // ❌

  • 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;
} // ❌

  • Primitive Type
// Not mockeable yet
type justString = string; // βœ… Mockeable

  • Type object
type justString = {
  yes: 'you can';
}; // βœ… Mockeable

  • Type with nested interfaces/types
type mockeableType = {
  nestedObject: MockeableInterface;
} // βœ… Mockeable

interface MockeableInterface {
  somethingToMock: any;
} // βœ… Mockeable

  • Generic Type
// Not mockeable yet
type IPropertiesToAdd<T extends {}> = T & {
    on(): void
    off(): void
}; // ❌

About

Tool for creating mocks objects with fake data using TypeScript types/interfaces as schema and Faker as fake data source

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project