npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kaka-api-service

v1.1.8

Published

When developing B-end management system projects, facing the table data operations and form add, delete, modify, and query functions of numerous pages, we can adopt the following strategies to design a unified data management tool class to improve the fle

Downloads

137

Readme

Kaka API Service 灵活和高效的前端数据管理工具

kaka-api-service 是一个用于管理和调用不同页面 API 接口的工具类库。它提供了一个统一的方式来处理数据的增删改查操作,并支持批量请求、缓存管理、错误处理等功能。

安装

使用 npm 安装:

npm install kaka-api-service

或使用 yarn 安装:

yarn add kaka-api-service

快速开始

1. 注册 API

首先,在项目中引入 kaka-api-service 并注册页面的 API。假设你有一个登录页面和一个用户信息页面:

import API_Service from 'kaka-api-service';
import request from './path/to/axios/request';

// 注册登录API
API_Service.registerApi('login', {
  fetch: (data) => request.post('/users/login', data),
});

// 你也可以使用 fetch 方法注册多个请求
API_Service.registerApi('login', {
  fetch: {
    default:(data) => request.post('/users/loginDefault', data),
    login:(data) => request.post('/users/login', data),
  },
});


// 注册用户信息API
API_Service.registerApi('userProfile', {
  fetch: (data) => request.get('/users/profile', { params: data }),
});

2. 发起单个请求

你可以通过 fetch 方法发起请求,获取某个页面的数据:

const loginClickHandle = async () => {
  try {
    const result = await API_Service.fetch('login', { userName: 'Zhao SiYuan', userPwd: '0415' });
    console.log('Login Result:', result);
  } catch (error) {
    console.error('Login failed:', error);
  }
};

如果使用 fetch 方法注册多个请求,可以按一下方法使用:

const loginClickHandle = async () => {
  try {
    // 增加第三个参数,值为注册请求时的key值,如果不填,默认为default
    const result = await API_Service.fetch('login', { userName: 'Zhao SiYuan', userPwd: '0415' },'login');
    console.log('Login Result:', result);
  } catch (error) {
    console.error('Login failed:', error);
  }
};

3. 批量请求数据

如果需要在一个页面内同时获取多个数据源的数据,可以使用 fetchMultiple 方法:

const fetchMultipleDataExample = async () => {
  const requests = [
    { pageName: 'login', params: { userName: 'Zhao SiYuan', userPwd: '0415' } ,'login'},
    { pageName: 'login', params: { userId: 123 } },
  ];

  try {
    const [loginData, profileData] = await API_Service.fetchMultiple(requests);
    console.log('Login Data:', loginData);
    console.log('Profile Data:', profileData);
  } catch (error) {
    console.error('Error fetching data:', error);
  }
};

4. 并发控制的批量请求

如果需要控制并发请求的数量,可以使用 fetchWithConcurrencyControl 方法:

const fetchWithLimitedConcurrency = async () => {
  const requests = [
    { pageName: 'login', params: { userName: 'Zhao SiYuan', userPwd: '0415' } },
    { pageName: 'userProfile', params: { userId: 123 } },
    // 其他请求...
  ];

  try {
    const results = await API_Service.fetchWithConcurrencyControl(requests, 2); // 并发限制为2
    console.log('Results:', results);
  } catch (error) {
    console.error('Error fetching data:', error);
  }
};

5. 添加缓存支持 (TODO)

如果你希望缓存某些页面的数据,可以在 fetch 中启用缓存,并设置缓存的失效时间(默认 60 秒):

const fetchWithCache = async () => {
  const result = await API_Service.fetch('userProfile', { userId: 123 }, true, 60000);
  console.log('Cached Profile Data:', result);
};

6. 其他操作

kaka-api-service 还提供了其他常见的 API 操作,如创建、更新、删除和导出数据:

// 创建数据
await API_Service.create('userProfile', { name: 'New User', age: 25 });

// 更新数据
await API_Service.update('userProfile', 123, { name: 'Updated User' });

// 删除数据
await API_Service.delete('userProfile', 123);

// 导出数据
await API_Service.export('userProfile', { filter: 'active' });

贡献

欢迎为 kaka-api-service 做出贡献,请通过提交 Pull Request 或者 Issue 与我们联系。

许可协议

此项目遵循 MIT 协议。