Socket
Book a DemoInstallSign in
Socket

faker-ts-mock

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

faker-ts-mock

根据ts类型生成mock数据

0.0.5
latest
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

faker-ts-mock

根据ts类型生成mock数据

Usage

import { tsMock, tsMockService } from 'faker-ts-mock';

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

yarn global add faker-ts-mock

e.g.

echo "interface IFoo { title: string; }" > foo.ts
faker-ts foo.ts IFoo # Mock data

Mock Server with Koa

import Koa from 'koa';
import Router from 'koa-router';
import * as ts from 'typescript';
import { tsMockService } from 'faker-ts-mock';

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;
}

FAQs

Package last updated on 04 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.