@axios-use/vue
Advanced tools
Comparing version 0.2.0-alpha.0 to 0.2.0-alpha.1
@@ -7,5 +7,7 @@ import type { InjectionKey } from "vue"; | ||
instance?: AxiosInstance; | ||
/** custom `data` value. @default response['data'] */ | ||
getResponseItem?: (res?: any) => unknown; | ||
}; | ||
export declare const AXIOS_USE_VUE_PROVIDE_KEY: InjectionKey<RequestConfigType>; | ||
export declare const setUseRequestConfig: (app: App, options?: RequestConfigType) => void; | ||
export declare const getUseRequestConfig: () => RequestConfigType & Required<Pick<RequestConfigType, "instance">>; | ||
export declare const getUseRequestConfig: () => RequestConfigType & Required<Pick<RequestConfigType, "instance" | "getResponseItem">>; |
@@ -26,2 +26,3 @@ import { getCurrentInstance, inject } from "vue"; | ||
}; | ||
var defaultGetResponseData = function (res) { return res === null || res === void 0 ? void 0 : res.data; }; | ||
export var getUseRequestConfig = function () { | ||
@@ -32,6 +33,4 @@ var _isInside = Boolean(getCurrentInstance()); | ||
} | ||
var _a = (_isInside | ||
? inject(AXIOS_USE_VUE_PROVIDE_KEY, {}) | ||
: {}).instance, instance = _a === void 0 ? axios : _a; | ||
return { instance: instance }; | ||
var _a = _isInside ? inject(AXIOS_USE_VUE_PROVIDE_KEY, {}) : {}, _b = _a.instance, instance = _b === void 0 ? axios : _b, _c = _a.getResponseItem, getResponseItem = _c === void 0 ? defaultGetResponseData : _c; | ||
return { instance: instance, getResponseItem: getResponseItem }; | ||
}; |
import type { AxiosRequestConfig, AxiosResponse, AxiosError, Canceler } from "axios"; | ||
export type _ResponseDataItemType<T> = T extends AxiosResponse<infer D1> ? D1 : T extends { | ||
data: infer D2; | ||
} | { | ||
data?: infer D2; | ||
} ? D2 : undefined; | ||
export interface Resource<T, D = any, W = AxiosResponse> extends AxiosRequestConfig<D> { | ||
_payload?: W extends AxiosResponse ? AxiosResponse<T> : T; | ||
export interface Resource<T = AxiosResponse, D = any, K1 extends keyof T = never, K2 extends keyof T[K1] = never, K3 extends keyof T[K1][K2] = never> extends AxiosRequestConfig<D> { | ||
_payload?: T; | ||
_payload_item?: [K3] extends [never] ? [K2] extends [never] ? [K1] extends [never] ? T extends AxiosResponse<infer DD> | { | ||
data?: infer DD; | ||
} ? DD : undefined : T[K1] : T[K1][K2] : T[K1][K2][K3]; | ||
} | ||
export type Request<T = any, D = any, W = any> = (...args: any[]) => Resource<T, D, W>; | ||
export type Payload<T extends Request, Check = false> = Check extends true ? _ResponseDataItemType<ReturnType<T>["_payload"]> : ReturnType<T>["_payload"]; | ||
export type Request<T = any, D = any, K1 extends keyof T = any, K2 extends keyof T[K1] = any, K3 extends keyof T[K1][K2] = any> = (...args: any[]) => Resource<T, D, K1, K2, K3>; | ||
export type Payload<T extends Request, Check = false> = Check extends true ? ReturnType<T>["_payload_item"] : T extends Request<AxiosResponse> ? NonNullable<ReturnType<T>["_payload"]> : ReturnType<T>["_payload"]; | ||
export type BodyData<T extends Request> = ReturnType<T>["data"]; | ||
@@ -16,3 +14,3 @@ export interface RequestFactory<T extends Request> { | ||
cancel: Canceler; | ||
ready: () => Promise<readonly [Payload<T, true>, NonNullable<Payload<T>>]>; | ||
ready: () => Promise<readonly [Payload<T, true>, Payload<T>]>; | ||
}; | ||
@@ -38,3 +36,3 @@ } | ||
*/ | ||
onCompleted?: (data: Payload<T, true>, response: NonNullable<Payload<T>>) => void; | ||
onCompleted?: (data: Payload<T, true>, response: Payload<T>) => void; | ||
/** | ||
@@ -49,3 +47,7 @@ * A callback function that's called when the request encounters one or more errors. | ||
*/ | ||
export declare function request<T, D = any, W = AxiosResponse>(config: AxiosRequestConfig<D>): Resource<T, D, W>; | ||
export declare function _request<T, D = any, K1 extends keyof T = never, K2 extends keyof T[K1] = never, K3 extends keyof T[K1][K2] = never>(config: AxiosRequestConfig<D>): Resource<T, D, K1, K2, K3>; | ||
/** | ||
* For TypeScript type deduction | ||
*/ | ||
export declare const request: <T, D = any>(config: AxiosRequestConfig<D>) => Resource<AxiosResponse<T, D>, D, never, never, never>; | ||
export declare function createRequestError<T = any, D = any, E = AxiosError<T, D> | AxiosResponse<T, D>>(error: E): RequestError<T, D, E>; |
@@ -5,5 +5,11 @@ import axios from "axios"; | ||
*/ | ||
export function request(config) { | ||
export function _request(config) { | ||
return config; | ||
} | ||
/** | ||
* For TypeScript type deduction | ||
*/ | ||
export var request = function (config) { | ||
return _request(config); | ||
}; | ||
export function createRequestError(error) { | ||
@@ -10,0 +16,0 @@ var _a, _b, _c, _d, _e, _f, _g; |
@@ -6,2 +6,4 @@ import type { ComputedRef } from "vue"; | ||
instance?: AxiosInstance; | ||
/** custom returns the value of `data`(index 0). @default (r) => r?.data */ | ||
getResponseItem?: (res?: any) => unknown; | ||
}; | ||
@@ -8,0 +10,0 @@ export type UseRequestResult<T extends Request> = [ |
@@ -60,4 +60,7 @@ var __assign = (this && this.__assign) || function () { | ||
removeCancelToken(_source.token); | ||
onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(res === null || res === void 0 ? void 0 : res.data, res); | ||
return [res === null || res === void 0 ? void 0 : res.data, res]; | ||
var _data = ((options === null || options === void 0 ? void 0 : options.getResponseItem) | ||
? options.getResponseItem(res) | ||
: requestConfig.getResponseItem(res)); | ||
onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(_data, res); | ||
return [_data, res]; | ||
}) | ||
@@ -64,0 +67,0 @@ .catch(function (err) { |
@@ -23,3 +23,3 @@ import type { ComputedRef } from "vue"; | ||
]; | ||
export type UseResourceOptions<T extends Request> = Pick<RequestConfigType, "instance"> & RequestCallbackFn<T> & { | ||
export type UseResourceOptions<T extends Request> = Pick<RequestConfigType, "instance" | "getResponseItem"> & RequestCallbackFn<T> & { | ||
/** Conditional Fetching */ | ||
@@ -26,0 +26,0 @@ filter?: (...args: Parameters<T>) => boolean; |
@@ -76,2 +76,3 @@ var __assign = (this && this.__assign) || function () { | ||
instance: options === null || options === void 0 ? void 0 : options.instance, | ||
getResponseItem: options === null || options === void 0 ? void 0 : options.getResponseItem, | ||
}), createRequest = _a[0], clear = _a[1].clear; | ||
@@ -78,0 +79,0 @@ var _b = useReducer(getNextState, __assign({ isLoading: getDefaultStateLoading(requestParams ? unrefs(requestParams) : requestParams, options === null || options === void 0 ? void 0 : options.filter) }, options === null || options === void 0 ? void 0 : options.defaultState)), state = _b[0], dispatch = _b[1]; |
@@ -7,5 +7,7 @@ import type { InjectionKey } from "vue"; | ||
instance?: AxiosInstance; | ||
/** custom `data` value. @default response['data'] */ | ||
getResponseItem?: (res?: any) => unknown; | ||
}; | ||
export declare const AXIOS_USE_VUE_PROVIDE_KEY: InjectionKey<RequestConfigType>; | ||
export declare const setUseRequestConfig: (app: App, options?: RequestConfigType) => void; | ||
export declare const getUseRequestConfig: () => RequestConfigType & Required<Pick<RequestConfigType, "instance">>; | ||
export declare const getUseRequestConfig: () => RequestConfigType & Required<Pick<RequestConfigType, "instance" | "getResponseItem">>; |
@@ -33,2 +33,3 @@ "use strict"; | ||
exports.setUseRequestConfig = setUseRequestConfig; | ||
var defaultGetResponseData = function (res) { return res === null || res === void 0 ? void 0 : res.data; }; | ||
var getUseRequestConfig = function () { | ||
@@ -39,7 +40,5 @@ var _isInside = Boolean((0, vue_1.getCurrentInstance)()); | ||
} | ||
var _a = (_isInside | ||
? (0, vue_1.inject)(exports.AXIOS_USE_VUE_PROVIDE_KEY, {}) | ||
: {}).instance, instance = _a === void 0 ? axios_1.default : _a; | ||
return { instance: instance }; | ||
var _a = _isInside ? (0, vue_1.inject)(exports.AXIOS_USE_VUE_PROVIDE_KEY, {}) : {}, _b = _a.instance, instance = _b === void 0 ? axios_1.default : _b, _c = _a.getResponseItem, getResponseItem = _c === void 0 ? defaultGetResponseData : _c; | ||
return { instance: instance, getResponseItem: getResponseItem }; | ||
}; | ||
exports.getUseRequestConfig = getUseRequestConfig; |
import type { AxiosRequestConfig, AxiosResponse, AxiosError, Canceler } from "axios"; | ||
export type _ResponseDataItemType<T> = T extends AxiosResponse<infer D1> ? D1 : T extends { | ||
data: infer D2; | ||
} | { | ||
data?: infer D2; | ||
} ? D2 : undefined; | ||
export interface Resource<T, D = any, W = AxiosResponse> extends AxiosRequestConfig<D> { | ||
_payload?: W extends AxiosResponse ? AxiosResponse<T> : T; | ||
export interface Resource<T = AxiosResponse, D = any, K1 extends keyof T = never, K2 extends keyof T[K1] = never, K3 extends keyof T[K1][K2] = never> extends AxiosRequestConfig<D> { | ||
_payload?: T; | ||
_payload_item?: [K3] extends [never] ? [K2] extends [never] ? [K1] extends [never] ? T extends AxiosResponse<infer DD> | { | ||
data?: infer DD; | ||
} ? DD : undefined : T[K1] : T[K1][K2] : T[K1][K2][K3]; | ||
} | ||
export type Request<T = any, D = any, W = any> = (...args: any[]) => Resource<T, D, W>; | ||
export type Payload<T extends Request, Check = false> = Check extends true ? _ResponseDataItemType<ReturnType<T>["_payload"]> : ReturnType<T>["_payload"]; | ||
export type Request<T = any, D = any, K1 extends keyof T = any, K2 extends keyof T[K1] = any, K3 extends keyof T[K1][K2] = any> = (...args: any[]) => Resource<T, D, K1, K2, K3>; | ||
export type Payload<T extends Request, Check = false> = Check extends true ? ReturnType<T>["_payload_item"] : T extends Request<AxiosResponse> ? NonNullable<ReturnType<T>["_payload"]> : ReturnType<T>["_payload"]; | ||
export type BodyData<T extends Request> = ReturnType<T>["data"]; | ||
@@ -16,3 +14,3 @@ export interface RequestFactory<T extends Request> { | ||
cancel: Canceler; | ||
ready: () => Promise<readonly [Payload<T, true>, NonNullable<Payload<T>>]>; | ||
ready: () => Promise<readonly [Payload<T, true>, Payload<T>]>; | ||
}; | ||
@@ -38,3 +36,3 @@ } | ||
*/ | ||
onCompleted?: (data: Payload<T, true>, response: NonNullable<Payload<T>>) => void; | ||
onCompleted?: (data: Payload<T, true>, response: Payload<T>) => void; | ||
/** | ||
@@ -49,3 +47,7 @@ * A callback function that's called when the request encounters one or more errors. | ||
*/ | ||
export declare function request<T, D = any, W = AxiosResponse>(config: AxiosRequestConfig<D>): Resource<T, D, W>; | ||
export declare function _request<T, D = any, K1 extends keyof T = never, K2 extends keyof T[K1] = never, K3 extends keyof T[K1][K2] = never>(config: AxiosRequestConfig<D>): Resource<T, D, K1, K2, K3>; | ||
/** | ||
* For TypeScript type deduction | ||
*/ | ||
export declare const request: <T, D = any>(config: AxiosRequestConfig<D>) => Resource<AxiosResponse<T, D>, D, never, never, never>; | ||
export declare function createRequestError<T = any, D = any, E = AxiosError<T, D> | AxiosResponse<T, D>>(error: E): RequestError<T, D, E>; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createRequestError = exports.request = void 0; | ||
exports.createRequestError = exports.request = exports._request = void 0; | ||
var axios_1 = __importDefault(require("axios")); | ||
@@ -12,5 +12,12 @@ /** | ||
*/ | ||
function request(config) { | ||
function _request(config) { | ||
return config; | ||
} | ||
exports._request = _request; | ||
/** | ||
* For TypeScript type deduction | ||
*/ | ||
var request = function (config) { | ||
return _request(config); | ||
}; | ||
exports.request = request; | ||
@@ -17,0 +24,0 @@ function createRequestError(error) { |
@@ -6,2 +6,4 @@ import type { ComputedRef } from "vue"; | ||
instance?: AxiosInstance; | ||
/** custom returns the value of `data`(index 0). @default (r) => r?.data */ | ||
getResponseItem?: (res?: any) => unknown; | ||
}; | ||
@@ -8,0 +10,0 @@ export type UseRequestResult<T extends Request> = [ |
@@ -66,4 +66,7 @@ "use strict"; | ||
removeCancelToken(_source.token); | ||
onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(res === null || res === void 0 ? void 0 : res.data, res); | ||
return [res === null || res === void 0 ? void 0 : res.data, res]; | ||
var _data = ((options === null || options === void 0 ? void 0 : options.getResponseItem) | ||
? options.getResponseItem(res) | ||
: requestConfig.getResponseItem(res)); | ||
onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(_data, res); | ||
return [_data, res]; | ||
}) | ||
@@ -70,0 +73,0 @@ .catch(function (err) { |
@@ -23,3 +23,3 @@ import type { ComputedRef } from "vue"; | ||
]; | ||
export type UseResourceOptions<T extends Request> = Pick<RequestConfigType, "instance"> & RequestCallbackFn<T> & { | ||
export type UseResourceOptions<T extends Request> = Pick<RequestConfigType, "instance" | "getResponseItem"> & RequestCallbackFn<T> & { | ||
/** Conditional Fetching */ | ||
@@ -26,0 +26,0 @@ filter?: (...args: Parameters<T>) => boolean; |
@@ -79,2 +79,3 @@ "use strict"; | ||
instance: options === null || options === void 0 ? void 0 : options.instance, | ||
getResponseItem: options === null || options === void 0 ? void 0 : options.getResponseItem, | ||
}), createRequest = _a[0], clear = _a[1].clear; | ||
@@ -81,0 +82,0 @@ var _b = (0, utils_1.useReducer)(getNextState, __assign({ isLoading: getDefaultStateLoading(requestParams ? (0, utils_1.unrefs)(requestParams) : requestParams, options === null || options === void 0 ? void 0 : options.filter) }, options === null || options === void 0 ? void 0 : options.defaultState)), state = _b[0], dispatch = _b[1]; |
{ | ||
"name": "@axios-use/vue", | ||
"version": "0.2.0-alpha.0", | ||
"version": "0.2.0-alpha.1", | ||
"description": "A Vue composition utilities for Axios.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -263,2 +263,4 @@ <br> | ||
```ts | ||
import { request } from "@axios-use/vue"; | ||
const api = { | ||
@@ -289,2 +291,21 @@ getUsers: () => { | ||
custom response type. (if you change the response's return value. like `axios.interceptors.response`) | ||
```ts | ||
import { request, _request } from "@axios-use/vue"; | ||
const [reqState] = useResource(() => request<DataType>({ url: `/users` })); | ||
// AxiosResponse<DataType> | ||
unref(reqState).response; | ||
// DataType | ||
unref(reqState).data; | ||
// custom response type | ||
const [reqState] = useResource(() => _request<MyWrapper<DataType>>({ url: `/users` })); | ||
// MyWrapper<DataType> | ||
unref(reqState).response; | ||
// MyWrapper<DataType>["data"]. maybe `undefined` type | ||
unref(reqState).data; | ||
``` | ||
#### createRequestError | ||
@@ -291,0 +312,0 @@ |
59194
1013
326