1.1.18 • Published 2 years ago

@suey/packages v1.1.18

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

设置 npm 源

npm config set registry https://registry.npmjs.org

安装

npm install @suey/packages --save

.. code::

|-- fnUtils

| |-- index.ts

| |-- encryption.ts 加密解密相关函数

| |-- judgeType.ts 类型判断相关函数

| -- random.ts

|-- request

| |-- index.ts

| `-- request.ts 关于axios的二次封装函数

|-- storage

| |-- index.ts

|-- web

| |-- index.ts

|-- hooks

| |-- index.ts

import { isType, isBoolean, isNull, isUnDef } from '@suey/packages';
import { loGet } from '@suey/packages/storage';
// 如果你导入一个子模块,并且TS出现无法识别类型,请启动tsconfigjson的 resolvePackageJsonExports 选项

const isObject = isType('Object');

isObject(1); // false
isObject({}); // true

isBoolean(true); // true

isNull(undefined); // false
isNull(null); // true

// 引入加密函数, 还有 md5, aesEncryptAlgorithm, aesDecryptAlgorithm, aesEncrypt, aesDecrypt
// rsaEncryptAlgorithm, rsaDecryptAlgorithm
import { aesDecrypt } from '@suey/packages';
import { createApiRequest, REQ_METHODS } from '@suey/packages';

// 可以像如下方式创建请求
const { request, apiGet, apiPost, createApi } = createApiRequest('http://localhost:3000', {
  onFulfilled: config => { // axios 拦截器, 请求可以放行的拦截, 可以在下面做一些全局配置
    if (!config.headers) config.headers = {};

    let token = 'Bearer token......';

    if (config.hConfig?.needAuth) {

      if (config.hConfig?.encryption) token = aesDecrypt(token);

      config.headers['authorization'] = token;
    }

    if (config.hConfig?.needTimestamp) config.headers['_t'] = new Date().getTime();

  }
}, {
  onFulfilled: response => { // 和后端配合做状态解码
    if (response.status !== 200) {
      return Promise.reject(response);
    }

    return Promise.resolve(response);

  },
  onRejected: response => { // 失败时候的响应
    return Promise.reject(response);
  }
});

// 向目标发起一个 GET 请求
request({
  url: '/api',
  method: REQ_METHODS.GET,
  hConfig: {
    needAuth: true,
    encryption: true
  }
});

// 向目标发起一个 GET 请求
apiGet('/api', {
  needAuth: true,
  encryption: true,
  needTimestamp: true
});

// 向目标发起一个 POST 请求
apiPost('/api', {
  needAuth: true,
  encryption: true,
  needTimestamp: true
});

// 如果 GET 和 POST 还不足满足你的请求类型, 你可以像下面这样
const apiPut = createApi(REQ_METHODS.PUT);
apiPut('/api/put');
1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

1.1.18

2 years ago

1.1.17

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.26

2 years ago

1.0.27

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago