@finema/core 1.1.5 → 1.2.2
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/README.md +87 -11
- package/dist/module.cjs +5 -0
- package/dist/module.d.mts +15 -0
- package/dist/module.d.ts +15 -0
- package/dist/module.json +8 -0
- package/dist/module.mjs +28 -0
- package/dist/runtime/composables/useLibs.d.ts +24 -0
- package/dist/runtime/composables/useLibs.mjs +4 -0
- package/dist/runtime/composables/useUtils.d.ts +10 -0
- package/dist/runtime/composables/useUtils.mjs +12 -0
- package/dist/{lib → runtime/lib}/Requester.d.ts +1 -1
- package/dist/runtime/lib/Requester.mjs +37 -0
- package/dist/{lib → runtime/lib}/api/apiListHelper.d.ts +2 -2
- package/dist/runtime/lib/api/apiListHelper.mjs +44 -0
- package/dist/{lib → runtime/lib}/api/apiObjectHelper.d.ts +2 -2
- package/dist/runtime/lib/api/apiObjectHelper.mjs +46 -0
- package/dist/{lib → runtime/lib}/api/apiPageHelper.d.ts +2 -2
- package/dist/runtime/lib/api/apiPageHelper.mjs +265 -0
- package/dist/runtime/lib/api/config.mjs +4 -0
- package/dist/{lib → runtime/lib}/api/loaderList.d.ts +1 -1
- package/dist/runtime/lib/api/loaderList.mjs +49 -0
- package/dist/{lib → runtime/lib}/api/loaderObject.d.ts +1 -1
- package/dist/runtime/lib/api/loaderObject.mjs +50 -0
- package/dist/{lib → runtime/lib}/api/loaderPage.d.ts +1 -1
- package/dist/runtime/lib/api/loaderPage.mjs +219 -0
- package/dist/{lib → runtime/lib}/api/loaderTypes.d.ts +3 -3
- package/dist/runtime/lib/api/loaderTypes.mjs +0 -0
- package/dist/{lib → runtime/lib}/index.d.ts +0 -2
- package/dist/runtime/lib/index.mjs +8 -0
- package/dist/runtime/plugin.d.ts +2 -0
- package/dist/runtime/plugin.mjs +4 -0
- package/dist/runtime/types/common.d.ts +13 -0
- package/dist/runtime/types/lib.d.ts +102 -0
- package/dist/runtime/utils/FileHelper.mjs +29 -0
- package/dist/{utils → runtime/utils}/ObjectHelper.d.ts +3 -3
- package/dist/runtime/utils/ObjectHelper.mjs +108 -0
- package/dist/{utils → runtime/utils}/ParamHelper.d.ts +2 -1
- package/dist/runtime/utils/ParamHelper.mjs +39 -0
- package/dist/runtime/utils/StringHelper.mjs +41 -0
- package/dist/runtime/utils/TimeHelper.mjs +83 -0
- package/dist/runtime/utils/lodash.mjs +38 -0
- package/dist/types.d.mts +15 -0
- package/dist/types.d.ts +15 -0
- package/package.json +64 -31
- package/dist/composables/index.d.ts +0 -1
- package/dist/composables/useCookie.d.ts +0 -5
- package/dist/composables/useWatch.d.ts +0 -3
- package/dist/core.js +0 -10031
- package/dist/core.umd.cjs +0 -49
- package/dist/index.d.ts +0 -3
- package/dist/lib/api/types.d.ts +0 -89
- package/dist/lib/api/utils.d.ts +0 -9
- package/dist/utils/ArrayHelper.d.ts +0 -7
- package/dist/utils/ArrayHelper.spec.d.ts +0 -1
- package/dist/utils/CookieHelper.d.ts +0 -9
- package/dist/utils/ObjectHelper.spec.d.ts +0 -1
- package/dist/utils/ParamHelper.spec.d.ts +0 -1
- package/dist/utils/StringHelper.spec.d.ts +0 -1
- package/dist/utils/TimeHelper.spec.d.ts +0 -1
- package/dist/utils/faker.d.ts +0 -2
- package/dist/utils/index.d.ts +0 -9
- package/dist/utils/types.d.ts +0 -8
- /package/dist/{lib → runtime/lib}/api/config.d.ts +0 -0
- /package/dist/{utils → runtime/utils}/FileHelper.d.ts +0 -0
- /package/dist/{utils → runtime/utils}/StringHelper.d.ts +0 -0
- /package/dist/{utils → runtime/utils}/TimeHelper.d.ts +0 -0
- /package/dist/{utils → runtime/utils}/lodash.d.ts +0 -0
package/README.md
CHANGED
|
@@ -1,18 +1,94 @@
|
|
|
1
|
-
|
|
1
|
+
<!--
|
|
2
|
+
Get your module up and running quickly.
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
Find and replace all on all files (CMD+SHIFT+F):
|
|
5
|
+
- Name: My Module
|
|
6
|
+
- Package name: my-module
|
|
7
|
+
- Description: My new Nuxt module
|
|
8
|
+
-->
|
|
4
9
|
|
|
5
|
-
|
|
10
|
+
# @finema/core Nuxt Module
|
|
6
11
|
|
|
7
|
-
|
|
12
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
13
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
14
|
+
[![License][license-src]][license-href]
|
|
15
|
+
[![Nuxt][nuxt-src]][nuxt-href]
|
|
8
16
|
|
|
9
|
-
|
|
17
|
+
@finema/core for doing amazing things.
|
|
10
18
|
|
|
11
|
-
|
|
19
|
+
- [✨ Release Notes](/CHANGELOG.md)
|
|
20
|
+
<!-- - [🏀 Online playground](https://stackblitz.com/github/your-org/my-module?file=playground%2Fapp.vue) -->
|
|
21
|
+
<!-- - [📖 Documentation](https://example.com) -->
|
|
12
22
|
|
|
13
|
-
|
|
23
|
+
## Features
|
|
14
24
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
25
|
+
<!-- Highlight some of the features your module provide here -->
|
|
26
|
+
- ⛰ Foo
|
|
27
|
+
- 🚠 Bar
|
|
28
|
+
- 🌲 Baz
|
|
29
|
+
|
|
30
|
+
## Quick Setup
|
|
31
|
+
|
|
32
|
+
1. Add `@finema/core` dependency to your project
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
# Using pnpm
|
|
36
|
+
pnpm add -D @finema/core
|
|
37
|
+
|
|
38
|
+
# Using yarn
|
|
39
|
+
yarn add --dev @finema/core
|
|
40
|
+
|
|
41
|
+
# Using npm
|
|
42
|
+
npm install --save-dev @finema/core
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
1. Add `@finema/core` to the `modules` section of `nuxt.config.ts`
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
export default defineNuxtConfig({
|
|
49
|
+
modules: [
|
|
50
|
+
'@finema/core'
|
|
51
|
+
]
|
|
52
|
+
})
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
That's it! You can now use @finema/core in your Nuxt app ✨
|
|
56
|
+
|
|
57
|
+
## Development
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
# Install dependencies
|
|
61
|
+
npm install
|
|
62
|
+
|
|
63
|
+
# Generate type stubs
|
|
64
|
+
npm run dev:prepare
|
|
65
|
+
|
|
66
|
+
# Develop with the playground
|
|
67
|
+
npm run dev
|
|
68
|
+
|
|
69
|
+
# Build the playground
|
|
70
|
+
npm run dev:build
|
|
71
|
+
|
|
72
|
+
# Run ESLint
|
|
73
|
+
npm run lint
|
|
74
|
+
|
|
75
|
+
# Run Vitest
|
|
76
|
+
npm run test
|
|
77
|
+
npm run test:watch
|
|
78
|
+
|
|
79
|
+
# Release new version
|
|
80
|
+
npm run release
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
<!-- Badges -->
|
|
84
|
+
[npm-version-src]: https://img.shields.io/npm/v/my-module/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
|
|
85
|
+
[npm-version-href]: https://npmjs.com/package/my-module
|
|
86
|
+
|
|
87
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/my-module.svg?style=flat&colorA=18181B&colorB=28CF8D
|
|
88
|
+
[npm-downloads-href]: https://npmjs.com/package/my-module
|
|
89
|
+
|
|
90
|
+
[license-src]: https://img.shields.io/npm/l/my-module.svg?style=flat&colorA=18181B&colorB=28CF8D
|
|
91
|
+
[license-href]: https://npmjs.com/package/my-module
|
|
92
|
+
|
|
93
|
+
[nuxt-src]: https://img.shields.io/badge/Nuxt-18181B?logo=nuxt.js
|
|
94
|
+
[nuxt-href]: https://nuxt.com
|
package/dist/module.cjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
|
|
3
|
+
interface ModuleOptions {
|
|
4
|
+
/**
|
|
5
|
+
* @default ''
|
|
6
|
+
*/
|
|
7
|
+
prefix?: string;
|
|
8
|
+
/**
|
|
9
|
+
* @default false
|
|
10
|
+
*/
|
|
11
|
+
global?: boolean;
|
|
12
|
+
}
|
|
13
|
+
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
|
|
14
|
+
|
|
15
|
+
export { type ModuleOptions, _default as default };
|
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
|
|
3
|
+
interface ModuleOptions {
|
|
4
|
+
/**
|
|
5
|
+
* @default ''
|
|
6
|
+
*/
|
|
7
|
+
prefix?: string;
|
|
8
|
+
/**
|
|
9
|
+
* @default false
|
|
10
|
+
*/
|
|
11
|
+
global?: boolean;
|
|
12
|
+
}
|
|
13
|
+
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
|
|
14
|
+
|
|
15
|
+
export { type ModuleOptions, _default as default };
|
package/dist/module.json
ADDED
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { defineNuxtModule, createResolver, addPlugin, addImportsDir } from '@nuxt/kit';
|
|
2
|
+
|
|
3
|
+
const name = "@finema/core";
|
|
4
|
+
const version = "1.2.2";
|
|
5
|
+
|
|
6
|
+
const module = defineNuxtModule({
|
|
7
|
+
meta: {
|
|
8
|
+
name,
|
|
9
|
+
version,
|
|
10
|
+
configKey: "core",
|
|
11
|
+
compatibility: {
|
|
12
|
+
nuxt: "^3.4.0"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
// Default configuration options of the Nuxt module
|
|
16
|
+
defaults: {},
|
|
17
|
+
setup(options, nuxt) {
|
|
18
|
+
const { resolve } = createResolver(import.meta.url);
|
|
19
|
+
const runtimeDir = resolve("./runtime");
|
|
20
|
+
nuxt.options.build.transpile.push(runtimeDir);
|
|
21
|
+
addPlugin({
|
|
22
|
+
src: resolve(runtimeDir, "plugin")
|
|
23
|
+
});
|
|
24
|
+
addImportsDir(resolve(runtimeDir, "composables"));
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export { module as default };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as CoreLibs from '../lib';
|
|
2
|
+
export declare const useLibs: () => {
|
|
3
|
+
Requester: typeof CoreLibs.Requester;
|
|
4
|
+
NewRequester: CoreLibs.IRequester;
|
|
5
|
+
apiListHelper: <T, O>(state: () => import("../types/lib").IAPIListState<T>, onUpdateStatus: (status: import("../types/lib").IStatus) => void, onUpdateOptions: (options: import("../types/lib").IAPIOptions) => void, onUpdateItems: (data: T[]) => void, opts: CoreLibs.IListLoaderOptions<T, O> & CoreLibs.IListRunLoaderOptions<T, O>) => Promise<void>;
|
|
6
|
+
apiObjectHelper: <T_1, B, O_1>(state: () => import("../types/lib").IAPIObjectState<T_1>, onUpdateStatus: (status: import("../types/lib").IStatus) => void, onUpdateOptions: (options: import("../types/lib").IAPIOptions) => void, onUpdateData: (data: any) => void, data: B | undefined, opts: CoreLibs.IObjectLoaderOptions<T_1, B, O_1> & CoreLibs.IObjectRunLoaderOptions<T_1, O_1>) => Promise<void>;
|
|
7
|
+
apiAddHelper: <T_2>(state: () => import("../types/lib").IAPIAddState<T_2>, onUpdateStatus: (status: import("../types/lib").IStatus) => void, onUpdateOptions: (options: import("../types/lib").IAPIOptions) => void, onUpdateData: (data: any) => void, onUpdateItems: (data: any[]) => void, data: any, opts: CoreLibs.IPageLoaderOptions<any> & CoreLibs.IPageFetchLoaderOptions<Record<string, any>>) => Promise<void>;
|
|
8
|
+
apiDeleteHelper: <T_3>(state: () => import("../types/lib").IAPIDeleteState<T_3>, onUpdateStatus: (status: import("../types/lib").IStatus) => void, onUpdateOptions: (options: import("../types/lib").IAPIOptions) => void, _onUpdateData: (data: T_3) => void, onUpdateItems: (data: T_3[]) => void, id: string | number, opts: CoreLibs.IPageLoaderOptions<any> & CoreLibs.IPageDeleteLoaderOptions<Record<string, any>>) => Promise<void>;
|
|
9
|
+
apiFetchHelper: <T_4>(state: () => import("../types/lib").IAPIFetchState<T_4>, onUpdateStatus: (status: import("../types/lib").IStatus) => void, onUpdateOptions: (options: import("../types/lib").IPageOptions) => void, onUpdateItems: (items: T_4[]) => void, page: number, query: string, opts: CoreLibs.IPageLoaderOptions<T_4> & CoreLibs.IPageFetchLoaderOptions<any>) => Promise<void>;
|
|
10
|
+
apiFindHelper: <T_5>(state: () => import("../types/lib").IAPIFindState<T_5>, onUpdateStatus: (status: import("../types/lib").IStatus) => void, onUpdateOptions: (options: import("../types/lib").IAPIOptions) => void, onUpdateData: (data: any) => void, id: string | number, opts: CoreLibs.IPageLoaderOptions<T_5> & CoreLibs.IPageFindLoaderOptions<Record<string, any>>) => Promise<void>;
|
|
11
|
+
updateHelper: <T_6>(state: () => import("../types/lib").IAPIUpdateState<T_6>, onUpdateStatus: (status: import("../types/lib").IStatus) => void, onUpdateOptions: (options: import("../types/lib").IAPIOptions) => void, onUpdateData: (data: T_6) => void, _onUpdateItems: (data: T_6[]) => void, onUpdateOldData: (data: T_6) => void, id: string | number, data: any, opts: CoreLibs.IPageLoaderOptions<any> & CoreLibs.IPageUpdateLoaderOptions<Record<string, any>>) => Promise<void>;
|
|
12
|
+
useListLoader: <T_7 = any, O_2 = Record<string, any>>(loaderOptions: CoreLibs.IListLoaderOptions<T_7, O_2>) => CoreLibs.IUseListLoader<T_7, O_2>;
|
|
13
|
+
useObjectLoader: <T_8 = any, B_1 = any, O_3 = Record<string, any>>(loaderOptions: CoreLibs.IObjectLoaderOptions<T_8, B_1, O_3>) => CoreLibs.IUseObjectLoader<T_8, B_1, O_3>;
|
|
14
|
+
initPageOptions: () => {
|
|
15
|
+
currentPageCount: number;
|
|
16
|
+
currentPage: number;
|
|
17
|
+
totalPage: number;
|
|
18
|
+
totalCount: number;
|
|
19
|
+
limit: number;
|
|
20
|
+
search: string;
|
|
21
|
+
primary: string;
|
|
22
|
+
};
|
|
23
|
+
usePageLoader: <T_9 = any>(loaderOptions: CoreLibs.IPageLoaderOptions<T_9>) => CoreLibs.IUsePageLoader<T_9>;
|
|
24
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { StringHelper } from '../utils/StringHelper';
|
|
2
|
+
import { TimeHelper } from '../utils/TimeHelper';
|
|
3
|
+
import { ParamHelper } from '../utils/ParamHelper';
|
|
4
|
+
import { FileHelper } from '../utils/FileHelper';
|
|
5
|
+
export declare const useUtils: () => {
|
|
6
|
+
StringHelper: typeof StringHelper;
|
|
7
|
+
TimeHelper: typeof TimeHelper;
|
|
8
|
+
ParamHelper: typeof ParamHelper;
|
|
9
|
+
FileHelper: typeof FileHelper;
|
|
10
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { StringHelper } from "../utils/StringHelper.mjs";
|
|
2
|
+
import { TimeHelper } from "../utils/TimeHelper.mjs";
|
|
3
|
+
import { ParamHelper } from "../utils/ParamHelper.mjs";
|
|
4
|
+
import { FileHelper } from "../utils/FileHelper.mjs";
|
|
5
|
+
export const useUtils = () => {
|
|
6
|
+
return {
|
|
7
|
+
StringHelper,
|
|
8
|
+
TimeHelper,
|
|
9
|
+
ParamHelper,
|
|
10
|
+
FileHelper
|
|
11
|
+
};
|
|
12
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AxiosInstance, AxiosRequestConfig, CancelTokenSource, CancelTokenStatic, Method } from 'axios';
|
|
1
|
+
import { type AxiosInstance, type AxiosRequestConfig, type CancelTokenSource, type CancelTokenStatic, type Method } from 'axios';
|
|
2
2
|
export interface IResponse<T = any> {
|
|
3
3
|
data: T;
|
|
4
4
|
status: number;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
export class Requester {
|
|
3
|
+
cancelToken;
|
|
4
|
+
source;
|
|
5
|
+
isCancel;
|
|
6
|
+
service;
|
|
7
|
+
options;
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
this.options = options;
|
|
10
|
+
this.service = axios.create(options);
|
|
11
|
+
this.cancelToken = axios.CancelToken;
|
|
12
|
+
this.source = this.cancelToken.source();
|
|
13
|
+
this.isCancel = axios.isCancel;
|
|
14
|
+
}
|
|
15
|
+
async get(path, options = {}) {
|
|
16
|
+
return this.service.get(path, options);
|
|
17
|
+
}
|
|
18
|
+
async put(path, payload, options = {}) {
|
|
19
|
+
return this.service.put(path, payload, options);
|
|
20
|
+
}
|
|
21
|
+
async delete(path, options = {}) {
|
|
22
|
+
return this.service.delete(path, options);
|
|
23
|
+
}
|
|
24
|
+
async post(path, payload, options = {}) {
|
|
25
|
+
return this.service.post(path, payload, options);
|
|
26
|
+
}
|
|
27
|
+
async create(method, path, payload, options = {}) {
|
|
28
|
+
return this.service({
|
|
29
|
+
method,
|
|
30
|
+
url: path,
|
|
31
|
+
data: payload,
|
|
32
|
+
...this.options,
|
|
33
|
+
...options
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export const NewRequester = new Requester({});
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { IAPIListState, IAPIOptions, IStatus } from '
|
|
2
|
-
import { IListLoaderOptions, IListRunLoaderOptions } from './loaderTypes';
|
|
1
|
+
import { type IAPIListState, type IAPIOptions, type IStatus } from '../../types/lib';
|
|
2
|
+
import { type IListLoaderOptions, type IListRunLoaderOptions } from './loaderTypes';
|
|
3
3
|
export declare const apiListHelper: <T, O>(state: () => IAPIListState<T>, onUpdateStatus: (status: IStatus) => void, onUpdateOptions: (options: IAPIOptions) => void, onUpdateItems: (data: T[]) => void, opts: IListLoaderOptions<T, O> & IListRunLoaderOptions<T, O>) => Promise<void>;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { NewRequester } from "../Requester.mjs";
|
|
2
|
+
import { ObjectHelper } from "../../utils/ObjectHelper.mjs";
|
|
3
|
+
import { ParamHelper } from "../../utils/ParamHelper.mjs";
|
|
4
|
+
export const apiListHelper = async (state, onUpdateStatus, onUpdateOptions, onUpdateItems, opts) => {
|
|
5
|
+
const timestamp = state().options._timestamp;
|
|
6
|
+
if (opts.expire && timestamp) {
|
|
7
|
+
if (timestamp + opts.expire > Date.now()) {
|
|
8
|
+
onUpdateStatus(ObjectHelper.toCompleteStatus(state().status));
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
onUpdateStatus(ObjectHelper.toLoadingStatus(state().status));
|
|
13
|
+
onUpdateOptions({});
|
|
14
|
+
const reqOptions = opts.getRequestOptions?.(opts) ?? {};
|
|
15
|
+
reqOptions.params = ParamHelper.getParams(opts, reqOptions);
|
|
16
|
+
try {
|
|
17
|
+
if (opts.isMock) {
|
|
18
|
+
const res = {
|
|
19
|
+
data: opts.mockItems
|
|
20
|
+
};
|
|
21
|
+
onUpdateItems(res.data);
|
|
22
|
+
onUpdateStatus(ObjectHelper.toSuccessStatus(state().status));
|
|
23
|
+
} else {
|
|
24
|
+
const { data: items, status } = await NewRequester.get(
|
|
25
|
+
opts.getURL ? opts.getURL(opts) : opts.url,
|
|
26
|
+
reqOptions
|
|
27
|
+
);
|
|
28
|
+
onUpdateItems(items);
|
|
29
|
+
onUpdateStatus(ObjectHelper.toSuccessStatus(state().status));
|
|
30
|
+
onUpdateOptions({
|
|
31
|
+
_timestamp: Date.now(),
|
|
32
|
+
_status: status,
|
|
33
|
+
request: reqOptions
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
} catch (e) {
|
|
37
|
+
onUpdateStatus(ObjectHelper.toErrorStatus(state().status, e));
|
|
38
|
+
onUpdateOptions({
|
|
39
|
+
_status: e.response?.status,
|
|
40
|
+
request: reqOptions
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
onUpdateStatus(ObjectHelper.toCompleteStatus(state().status));
|
|
44
|
+
};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { IAPIObjectState, IAPIOptions, IStatus } from '
|
|
2
|
-
import { IObjectLoaderOptions, IObjectRunLoaderOptions } from './loaderTypes';
|
|
1
|
+
import { type IAPIObjectState, type IAPIOptions, type IStatus } from '../../types/lib';
|
|
2
|
+
import { type IObjectLoaderOptions, type IObjectRunLoaderOptions } from './loaderTypes';
|
|
3
3
|
export declare const apiObjectHelper: <T, B, O>(state: () => IAPIObjectState<T>, onUpdateStatus: (status: IStatus) => void, onUpdateOptions: (options: IAPIOptions) => void, onUpdateData: (data: any) => void, data: B | undefined, opts: IObjectLoaderOptions<T, B, O> & IObjectRunLoaderOptions<T, O>) => Promise<void>;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { NewRequester } from "../Requester.mjs";
|
|
2
|
+
import { ObjectHelper } from "../../utils/ObjectHelper.mjs";
|
|
3
|
+
import { ParamHelper } from "../../utils/ParamHelper.mjs";
|
|
4
|
+
export const apiObjectHelper = async (state, onUpdateStatus, onUpdateOptions, onUpdateData, data, opts) => {
|
|
5
|
+
const timestamp = state().options._timestamp;
|
|
6
|
+
if (opts.expire && timestamp) {
|
|
7
|
+
if (timestamp + opts.expire > Date.now()) {
|
|
8
|
+
onUpdateStatus(ObjectHelper.toCompleteStatus(state().status));
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
onUpdateStatus(ObjectHelper.toLoadingStatus(state().status));
|
|
13
|
+
onUpdateOptions({});
|
|
14
|
+
const reqOptions = opts.getRequestOptions?.(data, opts) ?? {};
|
|
15
|
+
reqOptions.params = ParamHelper.getParams(opts, reqOptions);
|
|
16
|
+
try {
|
|
17
|
+
if (opts.isMock) {
|
|
18
|
+
const res = {
|
|
19
|
+
data: opts.mockItem
|
|
20
|
+
};
|
|
21
|
+
onUpdateData(res.data);
|
|
22
|
+
onUpdateStatus(ObjectHelper.toSuccessStatus(state().status));
|
|
23
|
+
} else {
|
|
24
|
+
const { data: item, status } = await NewRequester.create(
|
|
25
|
+
opts.method,
|
|
26
|
+
opts.getURL ? opts.getURL(data, opts) : opts.url,
|
|
27
|
+
data,
|
|
28
|
+
reqOptions
|
|
29
|
+
);
|
|
30
|
+
onUpdateData(item);
|
|
31
|
+
onUpdateStatus(ObjectHelper.toSuccessStatus(state().status));
|
|
32
|
+
onUpdateOptions({
|
|
33
|
+
request: reqOptions,
|
|
34
|
+
_timestamp: Date.now(),
|
|
35
|
+
_status: status
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
} catch (e) {
|
|
39
|
+
onUpdateStatus(ObjectHelper.toErrorStatus(state().status, e));
|
|
40
|
+
onUpdateOptions({
|
|
41
|
+
request: reqOptions,
|
|
42
|
+
_status: e.response?.status
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
onUpdateStatus(ObjectHelper.toCompleteStatus(state().status));
|
|
46
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { IAPIAddState, IAPIDeleteState, IAPIFetchState, IAPIFindState, IAPIOptions, IAPIUpdateState, IPageOptions, IStatus } from '
|
|
2
|
-
import { IPageDeleteLoaderOptions, IPageFetchLoaderOptions, IPageFindLoaderOptions, IPageLoaderOptions, IPageUpdateLoaderOptions } from './loaderTypes';
|
|
1
|
+
import { type IAPIAddState, type IAPIDeleteState, type IAPIFetchState, type IAPIFindState, type IAPIOptions, type IAPIUpdateState, type IPageOptions, type IStatus } from '../../types/lib';
|
|
2
|
+
import { type IPageDeleteLoaderOptions, type IPageFetchLoaderOptions, type IPageFindLoaderOptions, type IPageLoaderOptions, type IPageUpdateLoaderOptions } from './loaderTypes';
|
|
3
3
|
export declare const apiAddHelper: <T>(state: () => IAPIAddState<T>, onUpdateStatus: (status: IStatus) => void, onUpdateOptions: (options: IAPIOptions) => void, onUpdateData: (data: any) => void, onUpdateItems: (data: any[]) => void, data: any, opts: IPageLoaderOptions<any> & IPageFetchLoaderOptions) => Promise<void>;
|
|
4
4
|
export declare const apiDeleteHelper: <T>(state: () => IAPIDeleteState<T>, onUpdateStatus: (status: IStatus) => void, onUpdateOptions: (options: IAPIOptions) => void, _onUpdateData: (data: T) => void, onUpdateItems: (data: T[]) => void, id: string | number, opts: IPageLoaderOptions<any> & IPageDeleteLoaderOptions) => Promise<void>;
|
|
5
5
|
export declare const apiFetchHelper: <T>(state: () => IAPIFetchState<T>, onUpdateStatus: (status: IStatus) => void, onUpdateOptions: (options: IPageOptions) => void, onUpdateItems: (items: T[]) => void, page: number, query: string, opts: IPageLoaderOptions<T> & IPageFetchLoaderOptions<any>) => Promise<void>;
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { NewRequester } from "../Requester.mjs";
|
|
2
|
+
import { ObjectHelper } from "../../utils/ObjectHelper.mjs";
|
|
3
|
+
import { _findIndex, _get, _shuffle } from "../../utils/lodash.mjs";
|
|
4
|
+
import { ParamHelper } from "../../utils/ParamHelper.mjs";
|
|
5
|
+
import { CONFIG } from "./config.mjs";
|
|
6
|
+
export const apiAddHelper = async (state, onUpdateStatus, onUpdateOptions, onUpdateData, onUpdateItems, data, opts) => {
|
|
7
|
+
onUpdateStatus(ObjectHelper.toLoadingStatus(state().status));
|
|
8
|
+
onUpdateOptions({});
|
|
9
|
+
const reqOptions = {
|
|
10
|
+
...opts.getBaseRequestOptions?.() || {},
|
|
11
|
+
...opts.add?.getRequestOptions?.(data, opts) ?? {}
|
|
12
|
+
};
|
|
13
|
+
reqOptions.params = ParamHelper.getParams(opts, reqOptions);
|
|
14
|
+
try {
|
|
15
|
+
let item = null;
|
|
16
|
+
if (opts.isMock) {
|
|
17
|
+
const res = {
|
|
18
|
+
data: opts.mockItems?.[0]
|
|
19
|
+
};
|
|
20
|
+
onUpdateData(res.data);
|
|
21
|
+
onUpdateStatus(ObjectHelper.toSuccessStatus(state().status));
|
|
22
|
+
} else {
|
|
23
|
+
const { data: itemm, status } = await NewRequester.post(
|
|
24
|
+
opts.add?.getURL ? opts.add.getURL(data, opts) : opts.baseURL,
|
|
25
|
+
data,
|
|
26
|
+
reqOptions
|
|
27
|
+
);
|
|
28
|
+
onUpdateData(itemm);
|
|
29
|
+
item = itemm;
|
|
30
|
+
onUpdateStatus(ObjectHelper.toSuccessStatus(state().status));
|
|
31
|
+
onUpdateOptions({
|
|
32
|
+
_timestamp: Date.now(),
|
|
33
|
+
_status: status,
|
|
34
|
+
request: reqOptions
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (state().items) {
|
|
38
|
+
onUpdateItems([item, ...state().items]);
|
|
39
|
+
}
|
|
40
|
+
} catch (e) {
|
|
41
|
+
onUpdateStatus(ObjectHelper.toErrorStatus(state().status, e));
|
|
42
|
+
onUpdateOptions({
|
|
43
|
+
_status: e.response?.status,
|
|
44
|
+
request: reqOptions
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
onUpdateStatus(ObjectHelper.toCompleteStatus(state().status));
|
|
48
|
+
};
|
|
49
|
+
export const apiDeleteHelper = async (state, onUpdateStatus, onUpdateOptions, _onUpdateData, onUpdateItems, id, opts) => {
|
|
50
|
+
const primaryKey = opts.primary ?? "id";
|
|
51
|
+
const getPrimaryKey = (item) => _get(item, primaryKey, "");
|
|
52
|
+
onUpdateStatus(ObjectHelper.toLoadingStatus(state().status));
|
|
53
|
+
onUpdateOptions({});
|
|
54
|
+
const reqOptions = {
|
|
55
|
+
...opts.getBaseRequestOptions?.() || {},
|
|
56
|
+
...opts.delete?.getRequestOptions?.(id, opts) ?? {}
|
|
57
|
+
};
|
|
58
|
+
reqOptions.params = ParamHelper.getParams(opts, reqOptions);
|
|
59
|
+
try {
|
|
60
|
+
if (opts.isMock) {
|
|
61
|
+
onUpdateStatus(ObjectHelper.toSuccessStatus(state().status));
|
|
62
|
+
} else {
|
|
63
|
+
const { status } = await NewRequester.delete(
|
|
64
|
+
opts.delete?.getURL ? opts.delete.getURL(id, opts) : `${opts.baseURL}/${id}`,
|
|
65
|
+
reqOptions
|
|
66
|
+
);
|
|
67
|
+
onUpdateStatus(ObjectHelper.toSuccessStatus(state().status));
|
|
68
|
+
onUpdateOptions({
|
|
69
|
+
_timestamp: Date.now(),
|
|
70
|
+
_status: status,
|
|
71
|
+
request: reqOptions
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
if (state().items) {
|
|
75
|
+
onUpdateItems(
|
|
76
|
+
state().items.filter((item) => {
|
|
77
|
+
return getPrimaryKey(item) !== id;
|
|
78
|
+
})
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
} catch (e) {
|
|
82
|
+
onUpdateStatus(ObjectHelper.toErrorStatus(state().status, e));
|
|
83
|
+
onUpdateOptions({
|
|
84
|
+
_status: e.response?.status,
|
|
85
|
+
request: reqOptions
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
onUpdateStatus(ObjectHelper.toCompleteStatus(state().status));
|
|
89
|
+
};
|
|
90
|
+
export const apiFetchHelper = async (state, onUpdateStatus, onUpdateOptions, onUpdateItems, page, query, opts) => {
|
|
91
|
+
const timestamp = state().options._timestamp;
|
|
92
|
+
if (opts.expire && timestamp) {
|
|
93
|
+
if (timestamp + opts.expire > Date.now()) {
|
|
94
|
+
onUpdateStatus(ObjectHelper.toCompleteStatus(state().status));
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
onUpdateStatus(ObjectHelper.toLoadingStatus(state().status));
|
|
99
|
+
onUpdateOptions({
|
|
100
|
+
...state().options,
|
|
101
|
+
...opts,
|
|
102
|
+
currentPage: page,
|
|
103
|
+
search: query,
|
|
104
|
+
primary: opts.primary ?? "id",
|
|
105
|
+
_status: void 0
|
|
106
|
+
});
|
|
107
|
+
const getOptions = (data) => ({
|
|
108
|
+
...state().options,
|
|
109
|
+
currentPageCount: state().items?.length || 0,
|
|
110
|
+
currentPage: page,
|
|
111
|
+
totalCount: data.total,
|
|
112
|
+
limit: data.limit,
|
|
113
|
+
search: query,
|
|
114
|
+
totalPage: Math.ceil(data.total / data.limit)
|
|
115
|
+
});
|
|
116
|
+
const reqOptions = {
|
|
117
|
+
...opts.getBaseRequestOptions?.() || {},
|
|
118
|
+
...opts.fetch?.getRequestOptions?.(page, query, opts) ?? {}
|
|
119
|
+
};
|
|
120
|
+
const limit = reqOptions.params?.limit || CONFIG.LIMIT_PER_PAGE;
|
|
121
|
+
reqOptions.params = {
|
|
122
|
+
limit,
|
|
123
|
+
page,
|
|
124
|
+
q: (query || "").trim()
|
|
125
|
+
};
|
|
126
|
+
reqOptions.params = ParamHelper.getParams(opts, reqOptions);
|
|
127
|
+
try {
|
|
128
|
+
if (opts.isMock) {
|
|
129
|
+
await new Promise((resolve) => {
|
|
130
|
+
setTimeout(resolve, 500);
|
|
131
|
+
});
|
|
132
|
+
const res = {
|
|
133
|
+
data: {
|
|
134
|
+
items: opts.mockItems || [],
|
|
135
|
+
totalCount: (opts.mockItems || []).length
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
onUpdateItems(_shuffle(res.data.items));
|
|
139
|
+
onUpdateOptions(getOptions(res.data));
|
|
140
|
+
onUpdateStatus(ObjectHelper.toSuccessStatus(state().status));
|
|
141
|
+
} else {
|
|
142
|
+
const {
|
|
143
|
+
data: { items: itemss, ...moreOptions },
|
|
144
|
+
status
|
|
145
|
+
} = await NewRequester.get(
|
|
146
|
+
opts.fetch?.getURL ? opts.fetch.getURL(page, query, opts) : opts.baseURL,
|
|
147
|
+
reqOptions
|
|
148
|
+
);
|
|
149
|
+
onUpdateItems(itemss);
|
|
150
|
+
onUpdateOptions({
|
|
151
|
+
...getOptions(moreOptions),
|
|
152
|
+
_timestamp: Date.now(),
|
|
153
|
+
_status: status,
|
|
154
|
+
request: reqOptions
|
|
155
|
+
});
|
|
156
|
+
onUpdateStatus(ObjectHelper.toSuccessStatus(state().status));
|
|
157
|
+
}
|
|
158
|
+
} catch (e) {
|
|
159
|
+
onUpdateStatus(ObjectHelper.toErrorStatus(state().status, e));
|
|
160
|
+
onUpdateOptions({
|
|
161
|
+
...state().options,
|
|
162
|
+
_status: e.response?.status,
|
|
163
|
+
request: reqOptions
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
onUpdateStatus(ObjectHelper.toCompleteStatus(state().status));
|
|
167
|
+
};
|
|
168
|
+
export const apiFindHelper = async (state, onUpdateStatus, onUpdateOptions, onUpdateData, id, opts) => {
|
|
169
|
+
const timestamp = state().options._timestamp;
|
|
170
|
+
if (opts.expire && timestamp) {
|
|
171
|
+
if (timestamp + opts.expire > Date.now()) {
|
|
172
|
+
onUpdateStatus(ObjectHelper.toCompleteStatus(state().status));
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
onUpdateStatus(ObjectHelper.toLoadingStatus(state().status));
|
|
177
|
+
onUpdateOptions({});
|
|
178
|
+
const reqOptions = {
|
|
179
|
+
...opts.getBaseRequestOptions?.() || {},
|
|
180
|
+
...opts.find?.getRequestOptions?.(id, opts) ?? {}
|
|
181
|
+
};
|
|
182
|
+
reqOptions.params = ParamHelper.getParams(opts, reqOptions);
|
|
183
|
+
try {
|
|
184
|
+
if (opts.isMock) {
|
|
185
|
+
const res = {
|
|
186
|
+
data: opts.mockItems?.[0]
|
|
187
|
+
};
|
|
188
|
+
onUpdateData(res.data);
|
|
189
|
+
onUpdateStatus(ObjectHelper.toSuccessStatus(state().status));
|
|
190
|
+
} else {
|
|
191
|
+
const { data, status } = await NewRequester.get(
|
|
192
|
+
opts.find?.getURL ? opts.find.getURL(id, opts) : `${opts.baseURL}/${id}`,
|
|
193
|
+
reqOptions
|
|
194
|
+
);
|
|
195
|
+
onUpdateData(data);
|
|
196
|
+
onUpdateStatus(ObjectHelper.toSuccessStatus(state().status));
|
|
197
|
+
onUpdateOptions({
|
|
198
|
+
_timestamp: Date.now(),
|
|
199
|
+
_status: status,
|
|
200
|
+
request: reqOptions
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
} catch (e) {
|
|
204
|
+
onUpdateStatus(ObjectHelper.toErrorStatus(state().status, e));
|
|
205
|
+
onUpdateOptions({
|
|
206
|
+
_status: e.response?.status,
|
|
207
|
+
request: reqOptions
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
onUpdateStatus(ObjectHelper.toCompleteStatus(state().status));
|
|
211
|
+
};
|
|
212
|
+
export const updateHelper = async (state, onUpdateStatus, onUpdateOptions, onUpdateData, _onUpdateItems, onUpdateOldData, id, data, opts) => {
|
|
213
|
+
onUpdateStatus(ObjectHelper.toLoadingStatus(state().status));
|
|
214
|
+
onUpdateOptions({});
|
|
215
|
+
const reqOptions = {
|
|
216
|
+
...opts.getBaseRequestOptions?.() || {},
|
|
217
|
+
...opts.update?.getRequestOptions?.(id, data, opts) ?? {}
|
|
218
|
+
};
|
|
219
|
+
reqOptions.params = ParamHelper.getParams(opts, reqOptions);
|
|
220
|
+
try {
|
|
221
|
+
let finalItem;
|
|
222
|
+
if (opts.isMock) {
|
|
223
|
+
const res = {
|
|
224
|
+
data: opts.mockItems?.[0]
|
|
225
|
+
};
|
|
226
|
+
onUpdateData(res.data);
|
|
227
|
+
finalItem = res.data;
|
|
228
|
+
onUpdateStatus(ObjectHelper.toSuccessStatus(state().status));
|
|
229
|
+
} else {
|
|
230
|
+
const { data: item, status } = await NewRequester.put(
|
|
231
|
+
opts.update?.getURL ? opts.update.getURL(id, data, opts) : `${opts.baseURL}/${id}`,
|
|
232
|
+
data,
|
|
233
|
+
reqOptions
|
|
234
|
+
);
|
|
235
|
+
onUpdateData(item);
|
|
236
|
+
finalItem = item;
|
|
237
|
+
onUpdateStatus(ObjectHelper.toSuccessStatus(state().status));
|
|
238
|
+
onUpdateOptions({
|
|
239
|
+
_timestamp: Date.now(),
|
|
240
|
+
_status: status,
|
|
241
|
+
request: reqOptions
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
if (state().oldData) {
|
|
245
|
+
onUpdateOldData(finalItem);
|
|
246
|
+
}
|
|
247
|
+
if (state().items) {
|
|
248
|
+
const index = _findIndex(state().items, (item) => item.id === id);
|
|
249
|
+
if (index !== -1) {
|
|
250
|
+
const itemsTemp = state().items;
|
|
251
|
+
if (itemsTemp?.[index]) {
|
|
252
|
+
itemsTemp[index] = finalItem;
|
|
253
|
+
state().items = itemsTemp;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
} catch (e) {
|
|
258
|
+
onUpdateStatus(ObjectHelper.toErrorStatus(state().status, e));
|
|
259
|
+
onUpdateOptions({
|
|
260
|
+
_status: e.response?.status,
|
|
261
|
+
request: reqOptions
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
onUpdateStatus(ObjectHelper.toCompleteStatus(state().status));
|
|
265
|
+
};
|