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 π
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.
The "play" button mocks up, "clipboard" copies the mock created and the "X" button is for cleaning the view
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 ;
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. :).
Check here > Under construction π§
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
A: No :)
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'
}
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 '\\' :)"; } |
- 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
}; // β