https://github.com/cytle/faker-ts
Mock data from TypeScript
https://github.com/cytle/faker-ts
Last synced: about 1 month ago
JSON representation
Mock data from TypeScript
- Host: GitHub
- URL: https://github.com/cytle/faker-ts
- Owner: cytle
- Created: 2019-06-30T06:42:58.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-04-12T23:15:13.000Z (about 3 years ago)
- Last Synced: 2024-04-10T16:04:52.273Z (about 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 206 KB
- Stars: 32
- Watchers: 1
- Forks: 7
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# faker-ts
Mock data from TypeScript
## Usage
``` ts
import { tsMock, tsMockService } from 'faker-ts';const files = ['foo.ts'];
// foo.ts
// export interface IFoo { a: string }const mocker = tsMock(files);
mocker.generateMock('IFoo'); // { "a": "commodo voluptate pariatur" }// or listening file change
const mocker = tsMockService(files); // see more Mock Server with Koa
```### CLI
``` shell
yarn global add faker-ts
```e.g.
``` shell
echo "interface IFoo { title: string; }" > foo.ts
faker-ts foo.ts IFoo # Mock data
```### Mock Server with Koa
``` ts
import Koa from 'koa';
import Router from 'koa-router';
import * as ts from 'typescript';
import { tsMockService } from 'faker-ts';export function createServer(files: string[], jsonCompilerOptions?: ts.CompilerOptions, basePath?: string) {
const app = new Koa();
const router = new Router();const mocker = tsMockService(files, jsonCompilerOptions, basePath);
app.use(async (ctx, next) => {
try {
await next();
} catch (error) {
ctx.body = { msg: error.message };
}
});router.get('/mocks/:symbol', async (ctx) => {
ctx.body = mocker.generateMock(ctx.params.symbol);
});router.get('/schemas/:symbol', async (ctx) => {
ctx.body = mocker.generateSchema(ctx.params.symbol);
});router.get('/schemas', async (ctx) => {
ctx.body = mocker.generateSchema();
});router.get('/symbols', async (ctx) => {
ctx.body = mocker.generator.getMainFileSymbols(mocker.program);
});app
.use(router.routes())
.use(router.allowedMethods());return app;
}
```