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.1.7
  • npm
  • Socket score

Version published
Maintainers
3
Created
Source

anymock-openapi

Anymock

可跨端的 Anymock Openapi SDK。可用于 Mock、获取项目详情等。

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',
});

FAQs

Package last updated on 28 Dec 2019

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