zustand-fetching
Advanced tools
Comparing version 2.1.5 to 2.2.0
@@ -12,4 +12,4 @@ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; | ||
}, { | ||
fulfilled: (result, params) => { | ||
console.log("everything ok", result.type, params); | ||
fulfilled: ({ result, payload }) => { | ||
console.log("everything ok", result.type, payload); | ||
}, | ||
@@ -16,0 +16,0 @@ }); |
@@ -16,11 +16,15 @@ import { StoreApi } from "zustand"; | ||
} | ||
export interface IRequestCallback<Payload, Result> { | ||
previousResult: Result; | ||
result: Result; | ||
payload: Payload; | ||
requestId: string; | ||
error?: string; | ||
} | ||
export interface ILeitenRequestOptions<Payload, Result> { | ||
fulfilled?: (result: Result, params: Payload, requestId?: string) => void; | ||
rejected?: (params: Payload, error: any) => void; | ||
abort?: (params: Payload) => void; | ||
resolved?: (params: Payload) => void; | ||
action?: (params: Payload, extraParams?: { | ||
status?: ILoadingStatus; | ||
requestId?: string; | ||
}) => void; | ||
fulfilled?: (options: Omit<IRequestCallback<Payload, Result>, "error">) => void; | ||
rejected?: (options: Omit<IRequestCallback<Payload, Result>, "result">) => void; | ||
abort?: (options: Omit<IRequestCallback<Payload, Result>, "error" | "result">) => void; | ||
resolved?: (options: Omit<IRequestCallback<Payload, Result>, "result" | "error">) => void; | ||
action?: (options: Omit<IRequestCallback<Payload, Result>, "error" | "result">) => void; | ||
initialStatus?: ILoadingStatus; | ||
@@ -27,0 +31,0 @@ optimisticUpdate?: (params: Payload) => Result; |
@@ -44,19 +44,23 @@ import { produce } from "immer"; | ||
}; | ||
let prevContent = getContent(); | ||
let previousResult = getContent(); | ||
const reactions = { | ||
actionReaction: (params, extraParams) => { | ||
var _a, _b; | ||
actionReaction: (payload, status, requestId) => { | ||
var _a; | ||
setState({ | ||
status: (_a = extraParams === null || extraParams === void 0 ? void 0 : extraParams.status) !== null && _a !== void 0 ? _a : "loading", | ||
payload: params, | ||
status: status !== null && status !== void 0 ? status : "loading", | ||
payload, | ||
error: undefined, | ||
requestId: extraParams === null || extraParams === void 0 ? void 0 : extraParams.requestId, | ||
requestId: requestId, | ||
}); | ||
(_b = options === null || options === void 0 ? void 0 : options.action) === null || _b === void 0 ? void 0 : _b.call(options, params); | ||
prevContent = getContent(); | ||
(_a = options === null || options === void 0 ? void 0 : options.action) === null || _a === void 0 ? void 0 : _a.call(options, { | ||
previousResult, | ||
requestId: requestId || "", | ||
payload, | ||
}); | ||
previousResult = getContent(); | ||
if (options === null || options === void 0 ? void 0 : options.optimisticUpdate) { | ||
setContent(options.optimisticUpdate(params)); | ||
setContent(options.optimisticUpdate(payload)); | ||
} | ||
}, | ||
fulfilledReaction: (content, params, requestId) => { | ||
fulfilledReaction: (result, payload, requestId) => { | ||
var _a; | ||
@@ -67,30 +71,35 @@ const state = getState(); | ||
setState({ ...state, status: "loaded" }); | ||
if (content !== undefined && | ||
(!(options === null || options === void 0 ? void 0 : options.optimisticUpdate) || !isEqual(prevContent, content))) { | ||
setContent(content); | ||
if (result !== undefined && | ||
(!(options === null || options === void 0 ? void 0 : options.optimisticUpdate) || !isEqual(previousResult, result))) { | ||
setContent(result); | ||
} | ||
// }); | ||
(_a = options === null || options === void 0 ? void 0 : options.fulfilled) === null || _a === void 0 ? void 0 : _a.call(options, content, params); | ||
(_a = options === null || options === void 0 ? void 0 : options.fulfilled) === null || _a === void 0 ? void 0 : _a.call(options, { previousResult, requestId, payload, result }); | ||
} | ||
}, | ||
rejectedReaction: (params, error) => { | ||
rejectedReaction: (payload, error, requestId) => { | ||
var _a; | ||
const state = getState(); | ||
setState({ ...state, status: "error", error }); | ||
(_a = options === null || options === void 0 ? void 0 : options.rejected) === null || _a === void 0 ? void 0 : _a.call(options, params, error); | ||
(_a = options === null || options === void 0 ? void 0 : options.rejected) === null || _a === void 0 ? void 0 : _a.call(options, { | ||
previousResult, | ||
requestId: requestId || "", | ||
payload, | ||
error, | ||
}); | ||
if (options === null || options === void 0 ? void 0 : options.optimisticUpdate) { | ||
setContent(prevContent); | ||
setContent(previousResult); | ||
} | ||
}, | ||
abortReaction: (params) => { | ||
abortReaction: (payload, requestId) => { | ||
var _a; | ||
setState(initialState); | ||
(_a = options === null || options === void 0 ? void 0 : options.abort) === null || _a === void 0 ? void 0 : _a.call(options, params); | ||
(_a = options === null || options === void 0 ? void 0 : options.abort) === null || _a === void 0 ? void 0 : _a.call(options, { previousResult, requestId, payload }); | ||
if (options === null || options === void 0 ? void 0 : options.optimisticUpdate) { | ||
setContent(prevContent); | ||
setContent(previousResult); | ||
} | ||
}, | ||
resolvedReaction: (params) => { | ||
resolvedReaction: (payload, requestId) => { | ||
var _a; | ||
(_a = options === null || options === void 0 ? void 0 : options.resolved) === null || _a === void 0 ? void 0 : _a.call(options, params); | ||
(_a = options === null || options === void 0 ? void 0 : options.resolved) === null || _a === void 0 ? void 0 : _a.call(options, { previousResult, requestId, payload }); | ||
}, | ||
@@ -97,0 +106,0 @@ }; |
@@ -18,10 +18,7 @@ import { StoreApi } from "zustand"; | ||
interface IReaction<Payload, Result> { | ||
fulfilledReaction?: (result: Result, params: Payload, requestId?: string) => void; | ||
rejectedReaction?: (params: Payload, error?: any) => void; | ||
abortReaction?: (params: Payload) => void; | ||
resolvedReaction?: (params: Payload) => void; | ||
actionReaction?: (params: Payload, extraParams?: { | ||
status?: ILoadingStatus; | ||
requestId?: string; | ||
}) => void; | ||
fulfilledReaction?: (result: Result, params: Payload, requestId: string) => void; | ||
rejectedReaction?: (params: Payload, error: string, requestId?: string) => void; | ||
abortReaction?: (params: Payload, requestId: string) => void; | ||
resolvedReaction?: (params: Payload, requestId: string) => void; | ||
actionReaction?: (params: Payload, status?: ILoadingStatus, requestId?: string) => void; | ||
} | ||
@@ -39,3 +36,3 @@ export type IExtraReaction<Payload, Result> = { | ||
export declare function createAsyncActions<Payload, Result>(payloadCreator: (params: Payload, extraArgument?: IExtraArgument) => Promise<Result>, extra?: IReaction<Payload, Result>): { | ||
action: (params: Payload, extraParams?: { | ||
action: (params: Payload, options?: { | ||
status?: ILoadingStatus; | ||
@@ -42,0 +39,0 @@ requestId?: string; |
@@ -6,4 +6,4 @@ import { nanoid } from "nanoid"; | ||
const reactions = { | ||
actionReaction: (params, extraParams) => { | ||
var _a, _b, _c; | ||
actionReaction: (params, status, requestId) => { | ||
var _a, _b; | ||
const state = get(); | ||
@@ -15,7 +15,7 @@ set({ | ||
: state.content, | ||
status: (_b = extraParams === null || extraParams === void 0 ? void 0 : extraParams.status) !== null && _b !== void 0 ? _b : "loading", | ||
status: status !== null && status !== void 0 ? status : "loading", | ||
payload: params, | ||
requestId: extraParams === null || extraParams === void 0 ? void 0 : extraParams.requestId, | ||
requestId: requestId, | ||
}); | ||
(_c = extra === null || extra === void 0 ? void 0 : extra.actionReaction) === null || _c === void 0 ? void 0 : _c.call(extra, params); | ||
(_b = extra === null || extra === void 0 ? void 0 : extra.actionReaction) === null || _b === void 0 ? void 0 : _b.call(extra, params, status, state.requestId); | ||
}, | ||
@@ -33,6 +33,6 @@ fulfilledReaction: (content, params, requestId) => { | ||
}); | ||
(_b = extra === null || extra === void 0 ? void 0 : extra.fulfilledReaction) === null || _b === void 0 ? void 0 : _b.call(extra, content, params); | ||
(_b = extra === null || extra === void 0 ? void 0 : extra.fulfilledReaction) === null || _b === void 0 ? void 0 : _b.call(extra, content, params, requestId); | ||
} | ||
}, | ||
rejectedReaction: (params, error) => { | ||
rejectedReaction: (params, error, requestId) => { | ||
var _a, _b, _c; | ||
@@ -48,5 +48,5 @@ const state = get(); | ||
}); | ||
(_c = extra === null || extra === void 0 ? void 0 : extra.rejectedReaction) === null || _c === void 0 ? void 0 : _c.call(extra, params, error); | ||
(_c = extra === null || extra === void 0 ? void 0 : extra.rejectedReaction) === null || _c === void 0 ? void 0 : _c.call(extra, params, error, requestId); | ||
}, | ||
abortReaction: (params) => { | ||
abortReaction: (params, requestId) => { | ||
var _a, _b; | ||
@@ -62,7 +62,7 @@ const state = get(); | ||
}); | ||
(_b = extra === null || extra === void 0 ? void 0 : extra.abortReaction) === null || _b === void 0 ? void 0 : _b.call(extra, params); | ||
(_b = extra === null || extra === void 0 ? void 0 : extra.abortReaction) === null || _b === void 0 ? void 0 : _b.call(extra, params, requestId); | ||
}, | ||
resolvedReaction: (params) => { | ||
resolvedReaction: (params, requestId) => { | ||
var _a; | ||
(_a = extra === null || extra === void 0 ? void 0 : extra.resolvedReaction) === null || _a === void 0 ? void 0 : _a.call(extra, params); | ||
(_a = extra === null || extra === void 0 ? void 0 : extra.resolvedReaction) === null || _a === void 0 ? void 0 : _a.call(extra, params, requestId); | ||
}, | ||
@@ -96,6 +96,6 @@ }; | ||
}; | ||
const action = (params, extraParams) => { | ||
const action = (params, options) => { | ||
var _a; | ||
const requestId = (extraParams === null || extraParams === void 0 ? void 0 : extraParams.requestId) || nanoid(); | ||
(_a = extra === null || extra === void 0 ? void 0 : extra.actionReaction) === null || _a === void 0 ? void 0 : _a.call(extra, params, { status: extraParams === null || extraParams === void 0 ? void 0 : extraParams.status, requestId }); | ||
const requestId = (options === null || options === void 0 ? void 0 : options.requestId) || nanoid(); | ||
(_a = extra === null || extra === void 0 ? void 0 : extra.actionReaction) === null || _a === void 0 ? void 0 : _a.call(extra, params, options === null || options === void 0 ? void 0 : options.status, requestId); | ||
payloadCreator(params, { signal }) | ||
@@ -109,6 +109,6 @@ .then((result) => { | ||
if (error.message === "The user aborted a request.") { | ||
(_a = extra === null || extra === void 0 ? void 0 : extra.abortReaction) === null || _a === void 0 ? void 0 : _a.call(extra, params); | ||
(_a = extra === null || extra === void 0 ? void 0 : extra.abortReaction) === null || _a === void 0 ? void 0 : _a.call(extra, params, requestId); | ||
} | ||
else { | ||
(_b = extra === null || extra === void 0 ? void 0 : extra.rejectedReaction) === null || _b === void 0 ? void 0 : _b.call(extra, params, error); | ||
(_b = extra === null || extra === void 0 ? void 0 : extra.rejectedReaction) === null || _b === void 0 ? void 0 : _b.call(extra, params, error, requestId); | ||
} | ||
@@ -119,3 +119,3 @@ }) | ||
if (extra === null || extra === void 0 ? void 0 : extra.resolvedReaction) { | ||
(_a = extra === null || extra === void 0 ? void 0 : extra.resolvedReaction) === null || _a === void 0 ? void 0 : _a.call(extra, params); | ||
(_a = extra === null || extra === void 0 ? void 0 : extra.resolvedReaction) === null || _a === void 0 ? void 0 : _a.call(extra, params, requestId); | ||
} | ||
@@ -122,0 +122,0 @@ }); |
{ | ||
"name": "zustand-fetching", | ||
"version": "2.1.5", | ||
"version": "2.2.0", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "Zustand fetching helpers", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
89398
1810