There are two types of plugins. The ones that help building certain type systems with Manta Style, and the ones that help mocking data.
Provides Manta Style examples for returns. Currently supports string
and number
.
This plugin is included by default.
Manta Style reads all @example
strings and returns one of them at random.
/**
* @example tanhauhau
* @example cryrivers
* @example jennieji
*/
userName: string; // returns 'tanhauhau', 'cryrivers', or 'jennieji' at random
Manta Style returns a random value between all @example
numbers rounded to @precision
unit. If no @precision
is provided, Manta Style will use 0, returning an integer. If Manta Style sees only one @example
number, it will return that number.
/**
* @example 5
* @example 10
* @precision 2
*/
score: number; // 6.27
Generate mock data using faker.js
$ npm install --save-dev @manta-style/mock-faker
For strings, our Mock Faker users Faker.js's Faker.fake()
.
Mock Faker is transparent to Faker.js's. You may use any available Faker.js's API.
You can check out Faker.js's Faker.fake()
API here
For numbers, mock-faker
calls faker.method.path()
.
Here's a few examples.
/**
* @faker {{internet.userName}}
*/
userName: string;
/**
* @faker {{address.city}}, {{address.state}}, {{address.country}}
*/
address: string;
/**
* @faker date.past
*/
lastLoggedIn: number;
This string
only mock plugin generates random quote of the day from FavQs
npm install --save-dev @manta-style/mock-qotd
/**
* @qotd
*/
wiseWords: string; // My comedy is for children from three to 93. You do need a slightly childish sense of humour and if you haven't got that, it's very sad.
Iterates and loops through all of the strings provided by the annotations.
$ npm install --save-dev @manta-style/mock-iterate
/**
*
* @iterate 'Happy'
* @iterate 'Birthday'
* @iterate 'To'
* @iterate 'You'
*
*/
message: string;
- Every call will generate the next message
- First call will return
'Happy'
, then'Birthday'
, then'To'
, then'You'
, then back to'Happy'
again.
Generates a random number between given range.
This plugin is included by default.
/**
* @range {min} {max}
*/
value: number;
- For numbers only
- Generate random number between
{min}
and{max}
To build your own Manta Style plugin, you will (likely) need peer dependency of @manta-style/core
. This provides you with the getAnnotationsByKey
util that you will use.
Your plugin should export default
an object of the following type:
interface MockPlugin {
name: string;
mock: {
StringType?: (annotations: Annotation[]) => MockResult<string>;
NumberType?: (annotations: Annotation[]) => MockResult<number>;
BooleanType?: (annotations: Annotation[]) => MockResult<boolean>;
TypeLiteral?: (annotations: Annotation[]) => MockResult<AnyObject>;
};
}
Here name
is by which you wish Manta Style to extract when you annotate your type system with @name
in the comment prior to that definition.
And mock
is where you supply your main mocker functions.
You may support one or more of the four types.
Your mock data source may be async
. However, as the mock function is synchronous, all asychronous request should be wrapped with a Fetcher
.
Here is an example of a mock function with asynchronous data source:
import { annotationUtils, MockPlugin, Fetcher } from '@manta-style/core';
const qotdPlugin: MockPlugin = {
name: 'qotd',
mock: {
StringType(annotations) {
const jsdocRange = annotationUtils.getAnnotationsByKey(
'qotd',
annotations,
);
if (jsdocRange.length === 0) {
return null;
}
try {
return new Fetcher(
fetch('https://favqs.com/api/qotd')
.then((response) => response.json())
.then((json) => json.quote.body),
);
} catch (e) {
return null;
}
},
},
};
Manta Style supports TypeScript via builder-typescript
and FlowType via builder-flowtype
. More language support is coming soon.
You can check the language support for TypeScript Syntax and Flow Syntax