Simplify the writing of Javascript front-end Service layer code by using json configuration..
- [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.
You can install it using pnpm or yarn:
pnpm add @stevenleep/smart-service
# or
yarn add @stevenleep/smart-service
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);
});
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);