Skip to content

Simplify the writing of Javascript front-end Service layer code by using json configuration..

Notifications You must be signed in to change notification settings

stevenleep/smart-service

Repository files navigation

Smart service

Simplify the writing of Javascript front-end Service layer code by using json configuration..

Demo.mp4

Features

  • Automatically generate API request functions through simple JSON configuration.
  • Completely use your own request library to initiate interface requests (does not rely on any request library internally).
  • Supports Proxy and Loose modes, available in IE.
  • 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);

API

Examples

Contributing

About

Simplify the writing of Javascript front-end Service layer code by using json configuration..

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published