@stevenleep/smart-service
v1.2.1
Published
Simplify the writing of Javascript front-end Service layer code by using json configuration.
Downloads
1
Readme
Smart service
Simplify the writing of Javascript front-end Service layer code by using json configuration..
Features
- [x] Automatically generate API request functions through simple JSON configuration.
- [x] Completely use your own request library to initiate interface requests (does not rely on any request library internally).
- [x] Supports Proxy and Loose modes, available in IE.
- [x] TypeScript Support.
Installation
You can install it using pnpm or yarn:
pnpm add @stevenleep/smart-service
# or
yarn add @stevenleep/smart-service
Usage
import { ProxyService } from "@stevenleep/smart-service";
// Create an axios instance
import axios from "axios";
const axiosInstance = axios.create({
baseURL: "https://jsonplaceholder.typicode.com",
});
// connect axiosInstance to ProxyService
const { createServices } = new ProxyService(axiosInstance);
const postServices = createServices({ getPosts: "/posts" });
// Call the getPosts function to get the data from the server side
postServices.getPosts().then((res) => {
console.log(res);
});
Use in IE
If you need to use the createServices
API in an IE environment, you can use LooseService
initialization.
Compared with ProxyService mode, LooseService mode uses
Reflect.defineProperty
to create request functions.
import { LooseService } from "@stevenleep/smart-service";
const axiosInstance = ...;
const { createServices } = new LooseService(axiosInstance);