@rainte/js 0.0.9 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierrc.json +8 -8
- package/README.md +8 -8
- package/dist/index.js +838 -834
- package/dist/index.umd.cjs +6 -6
- package/dist/types/src/fast/index.d.ts +1 -1
- package/dist/types/src/file/index.d.ts +2 -1
- package/dist/types/src/http/index.d.ts +1 -0
- package/index.html +13 -13
- package/lib/index.ts +5 -5
- package/package.json +25 -25
- package/src/date/index.ts +19 -19
- package/src/fast/index.ts +27 -27
- package/src/file/index.ts +25 -18
- package/src/http/axios.ts +66 -66
- package/src/http/index.ts +21 -16
- package/src/main.ts +20 -20
- package/src/storage/index.ts +29 -29
- package/src/vite-env.d.ts +1 -1
- package/tsconfig.json +26 -26
- package/vite.config.ts +12 -12
package/src/http/axios.ts
CHANGED
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
import Axios from 'axios'
|
|
2
|
-
import * as storage from '../storage'
|
|
3
|
-
|
|
4
|
-
export const ACCESS_TOKEN = 'access_token'
|
|
5
|
-
|
|
6
|
-
export const message = (data: any, text = '请求异常.') => {
|
|
7
|
-
return data.message || data.msg || text
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const axios = Axios.create({
|
|
11
|
-
baseURL: '',
|
|
12
|
-
timeout: 3000,
|
|
13
|
-
headers: { 'Content-Type': 'application/json' }
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
export const tokenRequestInterceptor = () => {
|
|
17
|
-
return axios.interceptors.request.use((config) => {
|
|
18
|
-
const token = storage.session.get(ACCESS_TOKEN)
|
|
19
|
-
token && (config.headers.Authorization = `Bearer ${token}`)
|
|
20
|
-
return config
|
|
21
|
-
})
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const responseInterceptor = (onRejected?: ((error: any) => any) | null) => {
|
|
25
|
-
return axios.interceptors.response.use(
|
|
26
|
-
(response) => {
|
|
27
|
-
const data = response.data
|
|
28
|
-
if (response.status === 200) {
|
|
29
|
-
return data.data
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const error = new Error(message(data))
|
|
33
|
-
return onRejected ? onRejected(error) : Promise.reject(error)
|
|
34
|
-
},
|
|
35
|
-
(error) => {
|
|
36
|
-
return onRejected ? onRejected(error) : Promise.reject(error)
|
|
37
|
-
}
|
|
38
|
-
)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
let isRefreshing = false
|
|
42
|
-
let requests: any[] = []
|
|
43
|
-
|
|
44
|
-
export const retry = (error: any, refreshURL: string, refreshRequest: () => Promise<any>) => {
|
|
45
|
-
const { response } = error
|
|
46
|
-
|
|
47
|
-
if (response.config.url == refreshURL) {
|
|
48
|
-
isRefreshing = false
|
|
49
|
-
requests = []
|
|
50
|
-
return Promise.reject(error)
|
|
51
|
-
} else {
|
|
52
|
-
if (!isRefreshing) {
|
|
53
|
-
isRefreshing = true
|
|
54
|
-
refreshRequest()
|
|
55
|
-
.then(() => requests.forEach((cb) => cb()))
|
|
56
|
-
.finally(() => {
|
|
57
|
-
isRefreshing = false
|
|
58
|
-
requests = []
|
|
59
|
-
})
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return new Promise((resolve) => {
|
|
63
|
-
requests.push(() => resolve(axios(response.config)))
|
|
64
|
-
})
|
|
65
|
-
}
|
|
66
|
-
}
|
|
1
|
+
import Axios from 'axios'
|
|
2
|
+
import * as storage from '../storage'
|
|
3
|
+
|
|
4
|
+
export const ACCESS_TOKEN = 'access_token'
|
|
5
|
+
|
|
6
|
+
export const message = (data: any, text = '请求异常.') => {
|
|
7
|
+
return data.message || data.msg || text
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const axios = Axios.create({
|
|
11
|
+
baseURL: '',
|
|
12
|
+
timeout: 3000,
|
|
13
|
+
headers: { 'Content-Type': 'application/json' }
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
export const tokenRequestInterceptor = () => {
|
|
17
|
+
return axios.interceptors.request.use((config) => {
|
|
18
|
+
const token = storage.session.get(ACCESS_TOKEN)
|
|
19
|
+
token && (config.headers.Authorization = `Bearer ${token}`)
|
|
20
|
+
return config
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const responseInterceptor = (onRejected?: ((error: any) => any) | null) => {
|
|
25
|
+
return axios.interceptors.response.use(
|
|
26
|
+
(response) => {
|
|
27
|
+
const data = response.data
|
|
28
|
+
if (response.status === 200) {
|
|
29
|
+
return data.data
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const error = new Error(message(data))
|
|
33
|
+
return onRejected ? onRejected(error) : Promise.reject(error)
|
|
34
|
+
},
|
|
35
|
+
(error) => {
|
|
36
|
+
return onRejected ? onRejected(error) : Promise.reject(error)
|
|
37
|
+
}
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let isRefreshing = false
|
|
42
|
+
let requests: any[] = []
|
|
43
|
+
|
|
44
|
+
export const retry = (error: any, refreshURL: string, refreshRequest: () => Promise<any>) => {
|
|
45
|
+
const { response } = error
|
|
46
|
+
|
|
47
|
+
if (response.config.url == refreshURL) {
|
|
48
|
+
isRefreshing = false
|
|
49
|
+
requests = []
|
|
50
|
+
return Promise.reject(error)
|
|
51
|
+
} else {
|
|
52
|
+
if (!isRefreshing) {
|
|
53
|
+
isRefreshing = true
|
|
54
|
+
refreshRequest()
|
|
55
|
+
.then(() => requests.forEach((cb) => cb()))
|
|
56
|
+
.finally(() => {
|
|
57
|
+
isRefreshing = false
|
|
58
|
+
requests = []
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return new Promise((resolve) => {
|
|
63
|
+
requests.push(() => resolve(axios(response.config)))
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
}
|
package/src/http/index.ts
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
import { axios } from './axios'
|
|
2
|
-
import { AxiosRequestConfig } from 'axios'
|
|
3
|
-
|
|
4
|
-
export * from './axios'
|
|
5
|
-
|
|
6
|
-
export const get = (url: string, params?: Record<string, any>) => {
|
|
7
|
-
return request({ url, method: 'GET', params })
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const post = (url: string, data?: Record<string, any>) => {
|
|
11
|
-
return request({ url, method: 'POST', data })
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
import { axios } from './axios'
|
|
2
|
+
import { AxiosRequestConfig } from 'axios'
|
|
3
|
+
|
|
4
|
+
export * from './axios'
|
|
5
|
+
|
|
6
|
+
export const get = (url: string, params?: Record<string, any>) => {
|
|
7
|
+
return request({ url, method: 'GET', params })
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const post = (url: string, data?: Record<string, any>) => {
|
|
11
|
+
return request({ url, method: 'POST', data })
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const download = (config: AxiosRequestConfig) => {
|
|
15
|
+
config.responseType = config.responseType || 'blob'
|
|
16
|
+
return request(config)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const request = (config: AxiosRequestConfig) => {
|
|
20
|
+
return axios.request(config)
|
|
21
|
+
}
|
package/src/main.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import * as date from './date'
|
|
2
|
-
import * as fast from './fast'
|
|
3
|
-
|
|
4
|
-
const make = (content: string, tag: keyof HTMLElementTagNameMap = 'li') => {
|
|
5
|
-
const ele = document.createElement(tag)
|
|
6
|
-
ele.textContent = content
|
|
7
|
-
return ele
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const list = [
|
|
11
|
-
{ label: 'label1', value: 'value1' },
|
|
12
|
-
{ label: 'label2', value: 'value2' },
|
|
13
|
-
{ label: 'label3', value: 'value3' }
|
|
14
|
-
]
|
|
15
|
-
|
|
16
|
-
document
|
|
17
|
-
.querySelector<HTMLDivElement>('#app')
|
|
18
|
-
?.appendChild(document.createElement('ul'))
|
|
19
|
-
?.appendChild(make(date.dayjs().format(date.FORMAT.DATETIME)))
|
|
20
|
-
?.appendChild(make(fast.find(list, 'value3', '无知')))
|
|
1
|
+
import * as date from './date'
|
|
2
|
+
import * as fast from './fast'
|
|
3
|
+
|
|
4
|
+
const make = (content: string, tag: keyof HTMLElementTagNameMap = 'li') => {
|
|
5
|
+
const ele = document.createElement(tag)
|
|
6
|
+
ele.textContent = content
|
|
7
|
+
return ele
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const list = [
|
|
11
|
+
{ label: 'label1', value: 'value1' },
|
|
12
|
+
{ label: 'label2', value: 'value2' },
|
|
13
|
+
{ label: 'label3', value: 'value3' }
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
document
|
|
17
|
+
.querySelector<HTMLDivElement>('#app')
|
|
18
|
+
?.appendChild(document.createElement('ul'))
|
|
19
|
+
?.appendChild(make(date.dayjs().format(date.FORMAT.DATETIME)))
|
|
20
|
+
?.appendChild(make(fast.find(list, 'value3', '无知')))
|
package/src/storage/index.ts
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import jsan from 'jsan'
|
|
2
|
-
|
|
3
|
-
const cache = (storage?: Storage) => {
|
|
4
|
-
return {
|
|
5
|
-
getRaw: (key: string, defaultValue: any = null) => {
|
|
6
|
-
return storage?.getItem(key) || defaultValue
|
|
7
|
-
},
|
|
8
|
-
setRaw: (key: string, value: string) => {
|
|
9
|
-
storage?.setItem(key, value)
|
|
10
|
-
},
|
|
11
|
-
get: (key: string, defaultValue: any = null) => {
|
|
12
|
-
const res = storage?.getItem(key)
|
|
13
|
-
return res ? jsan.parse(res) : defaultValue
|
|
14
|
-
},
|
|
15
|
-
set: (key: string, value: any) => {
|
|
16
|
-
const serialize = jsan.stringify(value, undefined, undefined, true)
|
|
17
|
-
storage?.setItem(key, serialize)
|
|
18
|
-
},
|
|
19
|
-
del: (key: string) => {
|
|
20
|
-
storage?.removeItem(key)
|
|
21
|
-
},
|
|
22
|
-
clear: () => {
|
|
23
|
-
storage?.clear()
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const session = cache(sessionStorage)
|
|
29
|
-
export const local = cache(localStorage)
|
|
1
|
+
import jsan from 'jsan'
|
|
2
|
+
|
|
3
|
+
const cache = (storage?: Storage) => {
|
|
4
|
+
return {
|
|
5
|
+
getRaw: (key: string, defaultValue: any = null) => {
|
|
6
|
+
return storage?.getItem(key) || defaultValue
|
|
7
|
+
},
|
|
8
|
+
setRaw: (key: string, value: string) => {
|
|
9
|
+
storage?.setItem(key, value)
|
|
10
|
+
},
|
|
11
|
+
get: (key: string, defaultValue: any = null) => {
|
|
12
|
+
const res = storage?.getItem(key)
|
|
13
|
+
return res ? jsan.parse(res) : defaultValue
|
|
14
|
+
},
|
|
15
|
+
set: (key: string, value: any) => {
|
|
16
|
+
const serialize = jsan.stringify(value, undefined, undefined, true)
|
|
17
|
+
storage?.setItem(key, serialize)
|
|
18
|
+
},
|
|
19
|
+
del: (key: string) => {
|
|
20
|
+
storage?.removeItem(key)
|
|
21
|
+
},
|
|
22
|
+
clear: () => {
|
|
23
|
+
storage?.clear()
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const session = cache(sessionStorage)
|
|
29
|
+
export const local = cache(localStorage)
|
package/src/vite-env.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
/// <reference types="vite/client" />
|
|
1
|
+
/// <reference types="vite/client" />
|
package/tsconfig.json
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"useDefineForClassFields": true,
|
|
5
|
-
"module": "ESNext",
|
|
6
|
-
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
|
7
|
-
"skipLibCheck": true,
|
|
8
|
-
"declaration": true, // 自动生成声明文件.
|
|
9
|
-
"declarationDir": "dist/types", // 声明文件输出的目录.
|
|
10
|
-
"emitDeclarationOnly": true, // 只输出声明文件,不转译ts文件.
|
|
11
|
-
|
|
12
|
-
/* Bundler mode */
|
|
13
|
-
"moduleResolution": "bundler",
|
|
14
|
-
"allowImportingTsExtensions": true,
|
|
15
|
-
"isolatedModules": true,
|
|
16
|
-
"moduleDetection": "force",
|
|
17
|
-
// "noEmit": true,
|
|
18
|
-
|
|
19
|
-
/* Linting */
|
|
20
|
-
"strict": true,
|
|
21
|
-
"noUnusedLocals": true,
|
|
22
|
-
"noUnusedParameters": true,
|
|
23
|
-
"noFallthroughCasesInSwitch": true
|
|
24
|
-
},
|
|
25
|
-
"include": ["src", "lib", "vite.config.ts"]
|
|
26
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"useDefineForClassFields": true,
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
|
7
|
+
"skipLibCheck": true,
|
|
8
|
+
"declaration": true, // 自动生成声明文件.
|
|
9
|
+
"declarationDir": "dist/types", // 声明文件输出的目录.
|
|
10
|
+
"emitDeclarationOnly": true, // 只输出声明文件,不转译ts文件.
|
|
11
|
+
|
|
12
|
+
/* Bundler mode */
|
|
13
|
+
"moduleResolution": "bundler",
|
|
14
|
+
"allowImportingTsExtensions": true,
|
|
15
|
+
"isolatedModules": true,
|
|
16
|
+
"moduleDetection": "force",
|
|
17
|
+
// "noEmit": true,
|
|
18
|
+
|
|
19
|
+
/* Linting */
|
|
20
|
+
"strict": true,
|
|
21
|
+
"noUnusedLocals": true,
|
|
22
|
+
"noUnusedParameters": true,
|
|
23
|
+
"noFallthroughCasesInSwitch": true
|
|
24
|
+
},
|
|
25
|
+
"include": ["src", "lib", "vite.config.ts"]
|
|
26
|
+
}
|
package/vite.config.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { defineConfig } from 'vite'
|
|
2
|
-
import { resolve } from 'path'
|
|
3
|
-
|
|
4
|
-
export default defineConfig({
|
|
5
|
-
build: {
|
|
6
|
-
lib: {
|
|
7
|
-
entry: resolve(__dirname, 'lib/index.ts'),
|
|
8
|
-
name: '@rainte/js',
|
|
9
|
-
fileName: 'index'
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
})
|
|
1
|
+
import { defineConfig } from 'vite'
|
|
2
|
+
import { resolve } from 'path'
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
build: {
|
|
6
|
+
lib: {
|
|
7
|
+
entry: resolve(__dirname, 'lib/index.ts'),
|
|
8
|
+
name: '@rainte/js',
|
|
9
|
+
fileName: 'index'
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
})
|