New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

anymock-openapi

Package Overview
Dependencies
Maintainers
3
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

anymock-openapi

  • 0.2.1
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-81.25%
Maintainers
3
Weekly downloads
 
Created
Source

anymock-openapi

Anymock

可跨端的 Anymock Openapi SDK。可用于获取 Mock 数据以及其他众多 openapi。

—— 一款好用的 Anymock 平台 辅助工具

Install

tnpm install anymock-openapi --save

Concept

通过调用者使用不同环境下的的请求发生器,该 openapi sdk 可以灵活适配浏览器或 Node.js 场景。

请求发生器在本 SDK 中被抽象成“管道”,英文名 pipe。

Use

Browser

import AnymockOpenapi from 'anymock-openapi';
import { EClients, ETypes } from 'anymock-include';

const fetch = window.fetch;
const config = {
  fromClient: EClients.chromeExtensionAnymock,
  projectToken: 'YOUR_ANYMOCK_PROJECT_TOKEN',
};

/**
 * Pipe for browser.
 * @params {IRequestOptions} options
 * @return Promise<any>
 */
const pipe = (options) => {
  // interface IRequestOptions {
  //   url: string;
  //   headers: { [key: string]: any };
  //   method: string;
  //   data: { [key: string]: any }
  // }

  const url = options.url;
  const body = JSON.stringify(options.data);

  delete options.url;
  delete options.data;

  return fetch(url, { ...options, body })
    .then(res => res.json());
};

const api = new AnymockOpenapi(config, pipe);

// 1. fetch mock data
const result = await api.mock.query({
  type: ETypes.RPC,
  matching: 'com.aaa.w34',
});

// 2. fetch project details
const { data: projectDetails } = await api.project.get();

// projectDetails interface
// {
//   id: string;
//   name: string;
//   cname: string;
//   // ...
// }

Node.js

const fetch = require('node-fetch');
const AnymockOpenapi = require('anymock-openapi');
const { EClients, ETypes } =  require('anymock-include');

const config = {
  fromClient: EClients.nodejsAnymock,
  projectToken: 'YOUR_ANYMOCK_PROJECT_TOKEN',
};

/**
 * Pipe for Node.js.
 * @params {IRequestOptions} options
 * @return Promise<T>
 */
const pipe = (options) => {
  // interface IRequestOptions {
  //   url: string;
  //   headers: { [key: string]: any };
  //   method: string;
  //   data: { [key: string]: any }
  // }

  const url = options.url;
  const body = JSON.stringify(options.data);

  delete options.url;
  delete options.data;

  return fetch(url, { ...options,body })
    .then(res => res.json());
};

const api = new AnymockOpenapi(config, pipe);

const result = await api.mock.query({
  type: ETypes.RPC,
  matching: 'com.aaa.w34',
});

Develop

UT

单测项目:http://anymock.local.alipay.net:7002/project/2000008/workspace

yarn test packages/openapi/__tests__/basic.spec.ts

FAQs

Package last updated on 24 Feb 2020

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc