xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

TypeScript api response interface All In One

TypeScript api response interface All In One

interface apiObj {
    msg: string;
    status: number;
}
interface apiRes {
    data: apiObj;
    // data?: T;
}

interface apiErr {
    data: apiObj;
    // data?: T;
}

    // Service
    const createData = (params = {}) => {
        setIsModalLoading(true);
        createMonitor(params).then(res => {
            const {
                msg,
                status,
            } = res?.data ?? {};
            if(status === 200) {
                message.success(`${msg}`);
                onResetData();
                setIsModalVisible(false);
            }
        }).catch(err => {
            const {
                status,
                data: {
                    message: msg,
                },
            } = err;
            if(status === 417) {
                message.error(`${msg}`);
            }
        }).finally(() => {
            setIsModalLoading(false);
        });
    };






refs

https://www.freecodecamp.org/news/make-typescript-easy-using-basic-ts-generics/



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2021-11-12 15:14  xgqfrms  阅读(42)  评论(3编辑  收藏  举报