@stevenleep/smart-service
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

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);

API

Examples

Contributing

Readme

Keywords

none

Package Sidebar

Install

npm i @stevenleep/smart-service

Weekly Downloads

0

Version

1.2.1

License

ISC

Unpacked Size

15.1 kB

Total Files

9

Last publish

Collaborators

  • stevenleep