Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@rjgf/http-client

Package Overview
Dependencies
Maintainers
7
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rjgf/http-client - npm Package Compare versions

Comparing version 0.1.11 to 0.1.13

65

dist/index.d.ts

@@ -1,5 +0,3 @@

import { AxiosRequestConfig } from 'axios';
export interface Dispatchable {
dispatch(event: string): void | Promise<void>;
}
import { AxiosRequestConfig, AxiosResponse } from 'axios';
import EventEmitter from 'eventemitter3';
export interface ApiResult<T = unknown> {

@@ -16,4 +14,37 @@ msg: string;

}
export declare class HTTPClient {
private store;
export interface HTTPEventTypes {
error: (err: HTTPError) => void;
'not-login': (err: HTTPError) => void;
}
export interface IHTTPClient extends EventEmitter<HTTPEventTypes> {
request(config: AxiosRequestConfig): Promise<ApiResult>;
get<T = unknown>(url: string): Promise<ApiResult<T>>;
get<T = unknown>(url: string, params?: {
[key: string]: unknown;
}): Promise<ApiResult<T>>;
get<T = unknown>(url: string, params?: {
[key: string]: unknown;
}, config?: AxiosRequestConfig): Promise<ApiResult<T>>;
post<T = unknown>(url: string): Promise<ApiResult<T>>;
post<T = unknown>(url: string, body?: unknown): Promise<ApiResult<T>>;
post<T = unknown>(url: string, body?: unknown, config?: AxiosRequestConfig): Promise<ApiResult<T>>;
put<T = unknown>(url: string): Promise<ApiResult<T>>;
put<T = unknown>(url: string, body?: unknown): Promise<ApiResult<T>>;
put<T = unknown>(url: string, body?: unknown, config?: AxiosRequestConfig): Promise<ApiResult<T>>;
delete<T = unknown>(url: string): Promise<ApiResult<T>>;
delete<T = unknown>(url: string, params?: {
[key: string]: unknown;
}): Promise<ApiResult<T>>;
delete<T = unknown>(url: string, params?: {
[key: string]: unknown;
}, config?: AxiosRequestConfig): Promise<ApiResult<T>>;
}
export declare class HTTPError extends Error {
private _code;
private _response;
constructor(message: string, _code: number, _response: AxiosResponse);
get code(): number;
get response(): AxiosResponse;
}
export declare class HTTPClient extends EventEmitter<HTTPEventTypes> implements IHTTPClient {
private client;

@@ -27,6 +58,6 @@ private static instances;

static setToken(token: string, prefix?: string): void;
constructor(baseURL: string, store: Dispatchable);
constructor(baseURL: string);
get defaults(): {
url?: string | undefined;
method?: "link" | "head" | "get" | "GET" | "delete" | "DELETE" | "HEAD" | "options" | "OPTIONS" | "post" | "POST" | "put" | "PUT" | "patch" | "PATCH" | "purge" | "PURGE" | "LINK" | "unlink" | "UNLINK" | undefined;
method?: "link" | "get" | "GET" | "delete" | "DELETE" | "head" | "HEAD" | "options" | "OPTIONS" | "post" | "POST" | "put" | "PUT" | "patch" | "PATCH" | "purge" | "PURGE" | "LINK" | "unlink" | "UNLINK" | undefined;
baseURL?: string | undefined;

@@ -44,3 +75,3 @@ transformRequest?: import("axios").AxiosTransformer | import("axios").AxiosTransformer[] | undefined;

auth?: import("axios").AxiosBasicCredentials | undefined;
responseType?: "text" | "document" | "arraybuffer" | "blob" | "json" | "stream" | undefined;
responseType?: "arraybuffer" | "blob" | "document" | "json" | "text" | "stream" | undefined;
xsrfCookieName?: string | undefined;

@@ -67,11 +98,11 @@ xsrfHeaderName?: string | undefined;

*/
request(conf: AxiosRequestConfig): Promise<ApiResult>;
get(url: string, params?: {
request<T = unknown>(conf: AxiosRequestConfig): Promise<ApiResult<T>>;
get<T = unknown>(url: string, params?: {
[key: string]: unknown;
}, config?: AxiosRequestConfig): Promise<ApiResult<unknown> | undefined>;
post(url: string, body?: unknown, config?: AxiosRequestConfig): Promise<ApiResult<unknown> | undefined>;
put(url: string, body?: unknown, config?: AxiosRequestConfig): Promise<ApiResult<unknown> | undefined>;
delete(url: string, params?: {
}, config?: AxiosRequestConfig): Promise<ApiResult<T>>;
post<T = unknown>(url: string, body?: unknown, config?: AxiosRequestConfig): Promise<ApiResult<T>>;
put<T = unknown>(url: string, body?: unknown, config?: AxiosRequestConfig): Promise<ApiResult<T>>;
delete<T = unknown>(url: string, params?: {
[key: string]: unknown;
}, config?: AxiosRequestConfig): Promise<ApiResult<unknown> | undefined>;
}, config?: AxiosRequestConfig): Promise<ApiResult<T>>;
/**

@@ -82,3 +113,3 @@ * 检查登录是否过期

*/
private isLoginFail;
static isLoginFail(response: AxiosResponse): boolean;
}
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -6,8 +15,23 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

Object.defineProperty(exports, "__esModule", { value: true });
exports.HTTPClient = void 0;
exports.HTTPClient = exports.HTTPError = void 0;
const axios_1 = __importDefault(require("axios"));
const eventemitter3_1 = __importDefault(require("eventemitter3"));
class HTTPError extends Error {
constructor(message, _code, _response) {
super(message);
this._code = _code;
this._response = _response;
}
get code() {
return this._code;
}
get response() {
return this._response;
}
}
exports.HTTPError = HTTPError;
// eslint-disable-next-line import/no-unused-modules
class HTTPClient {
constructor(baseURL, store) {
this.store = store;
class HTTPClient extends eventemitter3_1.default {
constructor(baseURL) {
super();
this.client = axios_1.default.create();

@@ -30,3 +54,3 @@ this.client.defaults.baseURL = baseURL;

get defaults() {
return { ...this.client.defaults };
return Object.assign({}, this.client.defaults);
}

@@ -39,129 +63,112 @@ /**

*/
async request(conf) {
request(conf) {
var _a;
try {
const res = await this.client.request(conf);
if (conf.responseType === 'blob') {
return res.data;
return __awaiter(this, void 0, void 0, function* () {
try {
const res = yield this.client.request(conf);
if (conf.responseType === 'blob') {
return res.data;
}
// 全局处理如果 code 为 0 就是处理成功,反之则抛出异常(后端配置)
if (res.data.code === 0) {
return res.data;
}
throw new HTTPError(res.data.msg, res.data.code, res);
}
// 全局处理如果 code 为 0 就是处理成功,反之则抛出异常(后端配置)
if (res.data.code === 0) {
return res.data;
catch (err) {
const { response } = err;
if (err.message === 'Network Error') {
err.message = '网络错误,请稍后重试';
}
const httpError = err instanceof HTTPError ? err : new HTTPError(((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.title) || err.message, 0, err);
// 未登录的情况下就跳转登录
if (response && HTTPClient.isLoginFail(response)) {
this.emit('not-login', httpError);
// this.store.dispatch('user/logout')
// getRouter().replace('/login')
// window.location.href = location.origin + '/login'
}
this.emit('error', httpError);
throw httpError;
}
throw new Error(res.data.msg);
}
catch (err) {
const { response } = err;
// eslint-disable-next-line no-console
console.error(err);
// 未登录的情况下就跳转登录
if (response && this.isLoginFail(response)) {
this.store.dispatch('user/logout');
// getRouter().replace('/login')
window.location.href = location.origin + '/login';
});
}
get(url, params, config) {
return __awaiter(this, arguments, void 0, function* () {
if (arguments.length === 3) {
return this.request(Object.assign({ method: 'GET', url,
params }, config));
}
if (err.message === 'Network Error') {
err.message = '网络错误,请稍后重试';
else if (arguments.length === 2) {
return this.request(Object.assign({ method: 'GET', url, params: params }, params));
}
throw new Error(((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.title) || err.message);
}
else if (arguments.length === 1) {
return this.request({
method: 'GET',
url,
});
}
else {
return Promise.reject(new Error(`请求 '${url}' 参数不争气`));
}
});
}
async get(url, params, config) {
if (arguments.length === 3) {
return this.request({
method: 'GET',
url,
params,
...config,
});
}
else if (arguments.length === 2) {
return this.request({
method: 'GET',
url,
params: params,
...params,
});
}
else if (arguments.length === 1) {
return this.request({
method: 'GET',
url,
});
}
post(url, body, config) {
return __awaiter(this, arguments, void 0, function* () {
if (arguments.length === 3) {
return this.request(Object.assign({ method: 'POST', url, data: body }, config));
}
else if (arguments.length === 2) {
return this.request(Object.assign({ method: 'POST', url, data: body }, (Object.prototype.toString.call(body) === '[object Object]' ? body : {})));
}
else if (arguments.length === 1) {
return this.request({
method: 'POST',
url,
});
}
else {
return Promise.reject(new Error(`请求 '${url}' 参数不争气`));
}
});
}
async post(url, body, config) {
if (arguments.length === 3) {
return this.request({
method: 'POST',
url,
data: body,
...config,
});
}
else if (arguments.length === 2) {
return this.request({
method: 'POST',
url,
data: body,
// @ts-expect-error object
...(Object.prototype.toString.call(body) === '[object Object]' ? body : {}),
});
}
else if (arguments.length === 1) {
return this.request({
method: 'POST',
url,
});
}
put(url, body, config) {
return __awaiter(this, arguments, void 0, function* () {
if (arguments.length === 3) {
return this.request(Object.assign({ method: 'PUT', url, data: body }, config));
}
else if (arguments.length === 2) {
return this.request(Object.assign({ method: 'PUT', url, data: body }, (Object.prototype.toString.call(body) === '[object Object]' ? body : {})));
}
else if (arguments.length === 1) {
return this.request({
method: 'PUT',
url,
});
}
else {
return Promise.reject(new Error(`请求 '${url}' 参数不争气`));
}
});
}
async put(url, body, config) {
if (arguments.length === 3) {
return this.request({
method: 'PUT',
url,
data: body,
...config,
});
}
else if (arguments.length === 2) {
return this.request({
method: 'PUT',
url,
data: body,
// @ts-expect-error object
...(Object.prototype.toString.call(body) === '[object Object]' ? body : {}),
});
}
else if (arguments.length === 1) {
return this.request({
method: 'PUT',
url,
});
}
delete(url, params = {}, config = {}) {
return __awaiter(this, arguments, void 0, function* () {
if (arguments.length === 3) {
return this.request(Object.assign({ method: 'DELETE', url,
params }, config));
}
else if (arguments.length === 2) {
return this.request(Object.assign({ method: 'DELETE', url, params: params }, params));
}
else if (arguments.length === 1) {
return this.request({
method: 'DELETE',
url,
});
}
else {
return Promise.reject(new Error(`请求 '${url}' 参数不争气`));
}
});
}
async delete(url, params = {}, config = {}) {
if (arguments.length === 3) {
return this.request({
method: 'DELETE',
url,
params,
...config,
});
}
else if (arguments.length === 2) {
return this.request({
method: 'DELETE',
url,
params: params,
...params,
});
}
else if (arguments.length === 1) {
return this.request({
method: 'DELETE',
url,
});
}
}
/**

@@ -172,3 +179,3 @@ * 检查登录是否过期

*/
isLoginFail(response) {
static isLoginFail(response) {
return response.status === 401;

@@ -175,0 +182,0 @@ }

{
"author": "rjgh",
"dependencies": {
"axios": "^0.21.1"
"axios": "^0.21.1",
"eventemitter3": "^4.0.7"
},

@@ -18,3 +19,3 @@ "description": "HTTP 请求的客户端",

],
"gitHead": "d31009fde5b99484604402eea0b66095e8d64119",
"gitHead": "2f8eaf49af616f4986be743e2c425081d704d35d",
"license": "Apache-2.0",

@@ -33,3 +34,3 @@ "main": "./dist/index.js",

},
"version": "0.1.11"
"version": "0.1.13"
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc