Skip to content

Commit

Permalink
fix: use-request plugin type
Browse files Browse the repository at this point in the history
fix: use-request plugin type
  • Loading branch information
inhiblab committed Apr 12, 2023
2 parents f01235e + 67eea9d commit b17e3fe
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 118 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ module.exports = {
'no-inner-declarations': 0,
'@typescript-eslint/indent': 0,
'no-constant-condition': 0,
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-empty-function': 0,
'@typescript-eslint/explicit-member-accessibility': [2, { accessibility: 'no-public' }],
'@typescript-eslint/no-non-null-assertion': 0,
'@typescript-eslint/explicit-function-return-type': [1, { allowExpressions: true }],
// '@typescript-eslint/explicit-function-return-type': [2, { allowExpressions: true }],
'@typescript-eslint/no-use-before-define': [2, { functions: false }],
'@typescript-eslint/no-namespace': 0,
'@typescript-eslint/ban-ts-ignore': 0,
Expand Down
10 changes: 9 additions & 1 deletion packages/hooks/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ export default defineConfig({
href: svg,
},
],
[
'meta',
{
name: 'viewport',
content: 'width=device-width,height=device-height, maximum-scale=1.0,minimum-scale=1.0',
},
],
],
description: 'High-quality & Reliable 馃Р Vue3 Hooks library',
base,
Expand All @@ -53,6 +60,7 @@ export default defineConfig({
apiKey: '268129e44d6b58950b4626bf86e8bd1e',
indexName: 'vue-hooks-plus-search',
},
// @ts-ignore
localeLinks: {
text: '',
items: [
Expand Down Expand Up @@ -132,7 +140,7 @@ export default defineConfig({
},
{
icon: 'discord',
link: 'https://discord.gg/RU6ZPjf8',
link: 'https://discord.gg/z5Ve5r9Kwp',
},
{
icon: 'twitter',
Expand Down
112 changes: 56 additions & 56 deletions packages/hooks/src/useAsyncOrder/demo/demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,66 @@
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { useAsyncOrder } from 'vue-hooks-plus'
import { ref } from 'vue'
import { useAsyncOrder } from 'vue-hooks-plus'
const error = ref<number>(0)
const list = ref<string[]>([])
const error = ref<number>(0)
const list = ref<string[]>([])
function getUsername(): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('No.1')
}, 3000)
})
}
function getUsername(): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('No.1')
}, 3000)
})
}
function getUsername2(): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('No.2')
}, 2000)
})
}
function getUsername2(): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('No.2')
}, 2000)
})
}
function getUsername3(): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('No.3')
}, 2000)
})
}
function getUsername3(): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('No.3')
}, 2000)
})
}
useAsyncOrder({
task: [
resolve => {
getUsername().then(res => {
resolve?.(res)
})
},
resolve => {
getUsername2().then(res => {
resolve?.(res)
})
},
(_, reject) => {
getUsername2().then(() => {
reject?.({ err: 'error' })
})
},
(_, reject) => {
getUsername3().then(() => {
reject?.({ error: 'error' })
})
},
],
option: {
onError: err => {
error.value += 1
},
onSuccess: res => {
list.value.push(res as string)
},
useAsyncOrder({
task: [
resolve => {
getUsername().then(res => {
resolve?.(res)
})
},
})
resolve => {
getUsername2().then(res => {
resolve?.(res)
})
},
(_, reject) => {
getUsername2().then(() => {
reject?.({ err: 'error' })
})
},
(_, reject) => {
getUsername3().then(() => {
reject?.({ error: 'error' })
})
},
],
option: {
onError: err => {
error.value += 1
},
onSuccess: res => {
list.value.push(res as string)
},
},
})
</script>
18 changes: 9 additions & 9 deletions packages/hooks/src/useRequest/docs/basic/demo/demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
</template>

<script lang="ts" setup>
import { useRequest } from 'vue-hooks-plus'
import { useRequest } from 'vue-hooks-plus'
function getUsername(params: { desc: string }): Promise<string> {
return new Promise(resolve => {
setTimeout(() => {
resolve(`vue-hooks-plus ${params.desc}`)
}, 1000)
})
}
function getUsername(params: { desc: string }): Promise<string> {
return new Promise(resolve => {
setTimeout(() => {
resolve(`vue-hooks-plus ${params.desc}`)
}, 1000)
})
}
const { data, loading } = useRequest(() => getUsername({ desc: 'good' }))
const { data, loading } = useRequest(() => getUsername({ desc: 'good' }))
</script>
86 changes: 44 additions & 42 deletions packages/hooks/src/useRequest/docs/pluginDoc/demo/demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,56 @@
</template>

<script lang="ts" setup>
import { useRequest } from 'vue-hooks-plus'
import { useRequest } from 'vue-hooks-plus'

import { UseRequestPlugin } from '../../../types'
import { UseRequestPlugin } from '../../../types'

interface FormatterDataType {
name: string
age: number
}
interface FormatterDataType {
name: string
age: number
}

interface Formatter {
(data?: FormatterDataType): { name: string; age: number }
}
interface CustomPluginFieldType {
formatter: (data?: FormatterDataType) => { name: string; age: number }
}

const useFormatterPlugin: UseRequestPlugin<
FormatterDataType,
[],
{
formatter: Formatter
}
> = (fetchInstance, { formatter }) => {
return {
onSuccess: () => {
fetchInstance.setFetchState(formatter?.(fetchInstance.state.data), 'data')
},
}
const useFormatterPlugin: UseRequestPlugin<
FormatterDataType,
[],
{
formatter: CustomPluginFieldType['formatter']
}

function getUsername(): Promise<{ name: string; age: number }> {
return new Promise(resolve => {
setTimeout(() => {
resolve({
name: 'vue-hooks-plus',
age: 18,
})
}, 1000)
})
> = (fetchInstance, { formatter }) => {
return {
onSuccess: () => {
fetchInstance.setFetchState(formatter?.(fetchInstance.state.data), 'data')
},
}
}

function getUsername(): Promise<{ name: string; age: number }> {
return new Promise(resolve => {
setTimeout(() => {
resolve({
name: 'vue-hooks-plus',
age: 18,
})
}, 1000)
})
}



const { data, loading } = useRequest(
() => getUsername(),
{
formatter: (params?: FormatterDataType) => {
return {
name: `${params?.name} - plugins update`,
age: 20,
}
},
const { data, loading } = useRequest(
() => getUsername(),
{
formatter: (params?: FormatterDataType) => {
return {
name: `${params?.name} - plugins update`,
age: 20
}
},
[useFormatterPlugin],
)
},
[useFormatterPlugin],
)
</script>
4 changes: 2 additions & 2 deletions packages/hooks/src/useRequest/useRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ function useRequest<
PluginsOptions extends (infer P)[]
? P extends UseRequestPlugin<TData, TParams, infer R>
? R
: any
: any
: never
: never
>,
plugins?: PluginsOptions,
) {
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"types": ["node", "vitest", "vitest/globals"],
"paths": {
"@/*": ["src/*"],
"vue-hooks-plus": ["src/index.ts"],
"vue-hooks-plus": ["./src/index.ts"],
"test-utils/*": ["test-utils/*"]
}
},
Expand Down
4 changes: 2 additions & 2 deletions packages/use-request/src/useRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ function useRequest<
PluginsOptions extends (infer P)[]
? P extends UseRequestPlugin<TData, TParams, infer R>
? R
: any
: any
: never
: never
>,
plugins?: PluginsOptions,
) {
Expand Down
20 changes: 16 additions & 4 deletions packages/use-request/src/useRequestImplement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import { ref, reactive, toRefs, onUnmounted, inject, UnwrapRef, watchEffect, computed, isRef, onMounted, unref } from 'vue'
import {
ref,
reactive,
toRefs,
onUnmounted,
inject,
UnwrapRef,
watchEffect,
computed,
isRef,
onMounted,
unref,
} from 'vue'

import Fetch from './Fetch'
import { USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY } from './config'
Expand Down Expand Up @@ -28,7 +40,7 @@ function useRequestImplement<TData, TParams extends any[]>(
service: UseRequestService<TData, TParams>,
options: UseRequestOptions<TData, TParams, any> = {},
plugins: UseRequestPlugin<TData, TParams>[] = [],
) {
): useRequestResult<TData, TParams> {
// global option
const USEREQUEST_GLOBAL_OPTIONS = inject<Record<string, any>>(
USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY,
Expand Down Expand Up @@ -57,7 +69,7 @@ function useRequestImplement<TData, TParams extends any[]>(
error: undefined,
})

const setState = (currentState: unknown, field?: keyof typeof state) => {
const setState = (currentState: unknown, field?: keyof typeof state): void => {
if (field) {
// if (isUseRequestFetchStateKey<UnwrapRef<TData>, UnwrapRef<TParams>>(field, currentState)) {
// state[field] = currentState as any
Expand Down Expand Up @@ -89,7 +101,7 @@ function useRequestImplement<TData, TParams extends any[]>(
return p(fetchInstance, fetchOptions)
})

const readyComputed = computed(() => isRef(ready) ? ready.value : ready)
const readyComputed = computed(() => (isRef(ready) ? ready.value : ready))

// const isMount = ref(false)

Expand Down

0 comments on commit b17e3fe

Please sign in to comment.