1.1.11 • Published 3 years ago

odinplugs v1.1.11

Weekly downloads
35
License
ISC
Repository
github
Last release
3 years ago

odinplugs

standard-readme compliant TODO: Put more badges here.

the module is utils.contain axios and localstorage. Other updates will follow 中文说明

Table of Contents

Install

npm i odinplugs -g
or
npm i odinplugs --save-dev

Usage

LocalStorageHelper 示例:

import { LocalStorageHelper } from 'odinplugs/dist/LocalStorage/LocalStorageHelper';

从 localstorage 里取值,如果过期会自动移除

LocalStorageHelper.get(key)

向 localstorage 里赋值,expired 为过期时间

LocalStorageHelper.set(key,value,expired)

移除 localstorage 的键值对

LocalStorageHelper.remove(key)

axios 示例

export class MyAxios {
    // 请求拦截器标识
    static requestInterceptorsNumber: number | null = null;
    // 响应拦截器标识
    static responseInterceptorsNumber: number | null = null;
    static GetInstance(configModel: AxiosConfigModel): AxiosHelper {
        const axiosHelper = new AxiosHelper(configModel);
        axiosHelper.setInterceptors({
            interceptorsRequest: config => {
                return config;
            },
            interceptorResponse: data => {
                if (data.status === 200 && data.statusText === 'OK') {
                    return Promise.resolve(data.data);
                } else return Promise.resolve(data);
            },
            interceptorsResponseError: error => {
                return Promise.reject(error);
            },
        });
        this.requestInterceptorsNumber = axiosHelper.requestInterceptorsNumber;
        this.responseInterceptorsNumber = axiosHelper.responseInterceptorsNumber;
        return axiosHelper;
    }
}
export const axiosHelper = MyAxios.GetInstance(
    new AxiosConfigModel({
        url: '',
        baseURL: configs.baseUrl,
        defaultContentType: enumContentType.textHtml,
    })
);

//移除请求拦截器和响应拦截器
axiosHelper.removeInterceptors({
    requestInterceptors: true,
    responseInterceptors: true,
});

create api config file

import { axiosHelper } from '../../infrastructure/utils/myAxios/MyAxios';

export const DemoApi = {
    GetTodo() {
        return axiosHelper.instance.get('/todo');
    },
};

use api

DemoApi.GetTodo()
            .then(data => {
                console.log(data);
                OdinAuthen.authed = true;
                OdinAuthen.authPath = enumPages.Home;
                OdinHistory.Push(OdinAuthen);
            })
            .catch(error => {
                console.log(error);
            });

Maintainers

@https://github.com/odinsam/ts-node-odinplugs.git

Contributing

PRs accepted.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT © 2020 odinsam

1.1.11

3 years ago

1.1.10

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.24

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago