1.1.18 • Published 8 months ago

@suey/packages v1.1.18

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months 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

9 months ago

1.1.8

10 months ago

1.1.7

10 months ago

1.1.6

10 months ago

1.1.5

10 months ago

1.1.4

11 months ago

1.1.3

11 months ago

1.1.2

11 months ago

1.1.12

8 months ago

1.1.11

9 months ago

1.1.10

9 months ago

1.1.16

8 months ago

1.1.15

8 months ago

1.1.14

8 months ago

1.1.13

8 months ago

1.1.18

8 months ago

1.1.17

8 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.0.26

11 months ago

1.0.27

11 months ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago