@pinia-orm/axios-edge 1.7.1-28257628.bbb8bab

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022-present Gregor Becker
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,96 @@
1
+ [![Pinia ORM banner](./.github/assets/banner.png)](https://github.com/storm-tail/pinia-orm)
2
+
3
+ [![npm version][npm-version-src]][npm-version-href]
4
+ [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
+ [![Github Actions CI][github-actions-ci-src]][github-actions-ci-href]
6
+ [![Coverage][code-coverage-src]][code-coverage-href]
7
+ [![License][license-src]][license-href]
8
+
9
+ # Welcome to pinia-orm
10
+
11
+ > Intuitive, type safe and flexible ORM for Pinia based on [Vuex ORM Next](https://github.com/vuex-orm/vuex-orm-next)
12
+
13
+ - [✨  Release Notes](https://pinia-orm.codedredd.de/changelog)
14
+ - [📖  Documentation](https://pinia-orm.codedredd.de)
15
+ - [👾  Playground](https://pinia-orm-play.codedredd.de)
16
+
17
+ ## Migration from vuex-orm
18
+
19
+ You want to migrate from vuex to pinia and with it vuex-orm to pinia-orm but you don't know yet?
20
+ Well maybe this table will help you to decide. This comparison is just about facts and current state.
21
+
22
+ | Features | pinia-orm@v1.4.0 | @vuex-orm/core@0.36.4 | @vuex-orm/core@1.0.0-draft.16 |
23
+ |------------------------------------------------------------------------|------------------------------------------------------------| ----------------------------------------------------------------- | ------------------------------------------------------------------------- |
24
+ | Bundle Size (Min + GZIP) | [9.9 KB](https://bundlephobia.com/package/pinia-orm@1.4.0) | [16.7 KB](https://bundlephobia.com/package/@vuex-orm/core@0.36.4) | [12.6 KB](https://bundlephobia.com/package/@vuex-orm/core@1.0.0-draft.16) |
25
+ | Relations (hasMany, belongsTo, morphOne, hasManyBy, hasOne, morphTo) | ✅ | ✅ | ✅ |
26
+ | Relations (morphMany, belongsToMany, hasManyThrough) | ✅ | ✅ | ❌ |
27
+ | Relations (morphToMany, morphedByMany) | ❌ | ✅ | ❌ |
28
+ | Mutators | ✅ | ✅ | ❌ |
29
+ | Casts | ✅ | ❌ | ❌ |
30
+ | Decorators | ✅ | ❌ | ✅ |
31
+ | Single Table Inheritance | ✅ | ✅ | ❌ |
32
+ | Lifecycle Hooks | ✅ | ✅ | ❌ |
33
+ | Aggregates | ✅ | ✅ | ❌ |
34
+ | Query (orHas, doesntHave, orDoesntHave, whereHas, orWhereHas, groupBy) | ✅ | ❌ | ❌ |
35
+ | Collection Helpers | ✅ | (✅) can use pinia-orm helpers too | (✅) can use pinia-orm helpers too |
36
+ | Hidden Fields | ✅ | ❌ | ❌ |
37
+ | Metadata field | ✅ | ❌ | ❌ |
38
+ | Caching of queries with gc | ✅ | (✅) with plugin | ❌ |
39
+
40
+ If you decide to migrate then there are some breaking changes. A guide how to migrate will be written.
41
+ Small overview:
42
+
43
+ - Fields are by default `null`
44
+ - Renamed some functions aligning more with laravel naming
45
+ - Code is based on `vuex-orm-next` and not on `vuex-orm` !
46
+
47
+ ## Help me keep working on this project 💚
48
+
49
+ - [Become a Sponsor on GitHub](https://github.com/sponsors/codedredd)
50
+ - [One-time donation via PayPal](https://paypal.me/dredd1984)
51
+
52
+ <p align="center">
53
+ <a href="https://pinia-orm.codedredd.de/sponsorkit/sponsors.png">
54
+ <img src='https://pinia-orm.codedredd.de/sponsorkit/sponsors.svg'/>
55
+ </a>
56
+ </p>
57
+
58
+ ---
59
+
60
+ ## 💻 Development
61
+
62
+ - Clone this repository
63
+ - Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` (use `npm i -g corepack` for Node.js < 16.10)
64
+ - Install dependencies using `pnpm install`
65
+ - Build normalizr package: `pnpm build`
66
+ - Run interactive tests using `cd packages/pinia-orm && pnpm test:ui`
67
+
68
+ ## Credits
69
+
70
+ - [Kia King Ishii](https://github.com/kiaking)
71
+ - [Cuebit](https://github.com/cuebit)
72
+ - [Posva](https://github.com/posva)
73
+
74
+ ## Related projects
75
+
76
+ - [Vuex ORM](https://github.com/vuex-orm/vuex-orm)
77
+ - [Vuex ORM Next](https://github.com/vuex-orm/vuex-orm-next)
78
+
79
+ ## License
80
+
81
+ Made with ❤️
82
+
83
+ Published under [MIT License](./LICENCE).
84
+
85
+ <!-- Badges -->
86
+
87
+ [npm-version-src]: https://img.shields.io/npm/v/pinia-orm/latest.svg
88
+ [code-coverage-src]: https://img.shields.io/codecov/c/github/CodeDredd/pinia-orm?logo=Codecov&logoColor=white&token=BYLAJJOOLS
89
+ [code-coverage-href]: https://app.codecov.io/gh/CodeDredd/pinia-orm
90
+ [npm-version-href]: https://npmjs.com/package/pinia-orm
91
+ [npm-downloads-src]: https://img.shields.io/npm/dm/pinia-orm.svg
92
+ [npm-downloads-href]: https://npmjs.com/package/pinia-orm
93
+ [github-actions-ci-src]: https://github.com/codedredd/pinia-orm/actions/workflows/ci.yml/badge.svg
94
+ [github-actions-ci-href]: https://github.com/codedredd/pinia-orm/actions?query=workflow%3Aci
95
+ [license-src]: https://img.shields.io/npm/l/pinia-orm.svg
96
+ [license-href]: https://npmjs.com/package/pinia-orm
package/dist/index.cjs ADDED
@@ -0,0 +1,292 @@
1
+ 'use strict';
2
+
3
+ const piniaOrm = require('pinia-orm');
4
+
5
+ var __defProp$2 = Object.defineProperty;
6
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __publicField$2 = (obj, key, value) => {
8
+ __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
9
+ return value;
10
+ };
11
+ class Response {
12
+ /**
13
+ * Create a new response instance.
14
+ */
15
+ constructor(repository, config, response) {
16
+ /**
17
+ * The repository that called the request.
18
+ */
19
+ __publicField$2(this, "repository");
20
+ /**
21
+ * The request configuration.
22
+ */
23
+ __publicField$2(this, "config");
24
+ /**
25
+ * The axios response instance.
26
+ */
27
+ __publicField$2(this, "response");
28
+ /**
29
+ * Entities created by Pinia ORM.
30
+ */
31
+ __publicField$2(this, "entities", null);
32
+ /**
33
+ * Whether if response data is saved to the store or not.
34
+ */
35
+ __publicField$2(this, "isSaved", false);
36
+ this.repository = repository;
37
+ this.config = config;
38
+ this.response = response;
39
+ }
40
+ /**
41
+ * Save response data to the store.
42
+ */
43
+ async save() {
44
+ const data = this.getDataFromResponse();
45
+ if (!this.validateData(data)) {
46
+ console.warn(
47
+ "[Pinia ORM Axios] The response data could not be saved to the store because it is not an object or an array. You might want to use `dataTransformer` option to handle non-array/object response before saving it to the store."
48
+ );
49
+ return;
50
+ }
51
+ let method = this.config.persistBy || "save";
52
+ if (!this.validatePersistAction(method)) {
53
+ console.warn(
54
+ '[Pinia ORM Axios] The "persistBy" option configured is not a recognized value. Response data will be persisted by the default `save` method.'
55
+ );
56
+ method = "save";
57
+ }
58
+ const result = await this.repository[method](data);
59
+ this.entities = Array.isArray(result) ? result : [result];
60
+ this.isSaved = true;
61
+ }
62
+ /**
63
+ * Delete the entity record where the `delete` option is configured.
64
+ */
65
+ async delete() {
66
+ if (this.config.delete === void 0) {
67
+ throw new Error(
68
+ "[Pinia ORM Axios] Could not delete records because the `delete` option is not set."
69
+ );
70
+ }
71
+ await this.repository.query().destroy(this.config.delete);
72
+ }
73
+ /**
74
+ * Get the response data from the axios response object. If a `dataTransformer`
75
+ * option is configured, it will be applied to the response object. If the
76
+ * `dataKey` option is configured, it will return the data from the given
77
+ * property within the response body.
78
+ */
79
+ getDataFromResponse() {
80
+ if (this.config.dataTransformer) {
81
+ return this.config.dataTransformer(this.response);
82
+ }
83
+ if (this.config.dataKey) {
84
+ return this.response.data[this.config.dataKey];
85
+ }
86
+ return this.response.data;
87
+ }
88
+ /**
89
+ * Get persist options if any set in config.
90
+ */
91
+ // protected getPersistOptions (): PersistOptions | undefined {
92
+ // const persistOptions = this.config.persistOptions
93
+ //
94
+ // if (!persistOptions || typeof persistOptions !== 'object') {
95
+ // return
96
+ // }
97
+ //
98
+ // return Object.keys(persistOptions)
99
+ // .filter(this.validatePersistAction) // Filter to avoid polluting the payload.
100
+ // .reduce((carry, key) => {
101
+ // carry[key] = persistOptions[key]
102
+ // return carry
103
+ // }, {} as PersistOptions)
104
+ // }
105
+ /**
106
+ * Validate the given data to ensure the Pinia ORM persist methods accept it.
107
+ */
108
+ validateData(data) {
109
+ return data !== null && typeof data === "object";
110
+ }
111
+ /**
112
+ * Validate the given string as to ensure it correlates with the available
113
+ * Pinia ORM persist methods.
114
+ */
115
+ validatePersistAction(action) {
116
+ return ["save", "insert"].includes(action);
117
+ }
118
+ }
119
+
120
+ var __defProp$1 = Object.defineProperty;
121
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
122
+ var __publicField$1 = (obj, key, value) => {
123
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
124
+ return value;
125
+ };
126
+ class Request {
127
+ /**
128
+ * Create a new api instance.
129
+ */
130
+ constructor(repository) {
131
+ /**
132
+ * The repository class.
133
+ */
134
+ __publicField$1(this, "repository");
135
+ /**
136
+ * The default config.
137
+ */
138
+ __publicField$1(this, "config", {
139
+ save: true
140
+ });
141
+ this.repository = repository;
142
+ this.registerActions();
143
+ }
144
+ /**
145
+ * Get the axios client.
146
+ */
147
+ get axios() {
148
+ if (!this.repository.axios) {
149
+ throw new Error(
150
+ "[Pinia ORM Axios] The axios instance is not registered. Please register the axios instance to the repository."
151
+ );
152
+ }
153
+ return this.repository.axios;
154
+ }
155
+ /**
156
+ * Register actions from the repository config.
157
+ */
158
+ registerActions() {
159
+ const actions = { ...this.repository.config.axiosApi?.actions, ...this.repository.getModel().$config()?.axiosApi?.actions };
160
+ if (!actions) {
161
+ return;
162
+ }
163
+ for (const name in actions) {
164
+ const action = actions[name];
165
+ typeof action === "function" ? this.registerFunctionAction(name, action) : this.registerObjectAction(name, action);
166
+ }
167
+ }
168
+ /**
169
+ * Register the given object action.
170
+ */
171
+ registerObjectAction(name, action) {
172
+ this[name] = (config) => {
173
+ return this.request({ ...action, ...config });
174
+ };
175
+ }
176
+ /**
177
+ * Register the given function action.
178
+ */
179
+ registerFunctionAction(name, action) {
180
+ this[name] = action.bind(this);
181
+ }
182
+ /**
183
+ * Perform a get request.
184
+ */
185
+ get(url, config = {}) {
186
+ return this.request({ method: "get", url, ...config });
187
+ }
188
+ /**
189
+ * Perform a post request.
190
+ */
191
+ post(url, data = {}, config = {}) {
192
+ return this.request({ method: "post", url, data, ...config });
193
+ }
194
+ /**
195
+ * Perform a put request.
196
+ */
197
+ put(url, data = {}, config = {}) {
198
+ return this.request({ method: "put", url, data, ...config });
199
+ }
200
+ /**
201
+ * Perform a patch request.
202
+ */
203
+ patch(url, data = {}, config = {}) {
204
+ return this.request({ method: "patch", url, data, ...config });
205
+ }
206
+ /**
207
+ * Perform a delete request.
208
+ */
209
+ delete(url, config = {}) {
210
+ return this.request({ method: "delete", url, ...config });
211
+ }
212
+ /**
213
+ * Perform an api request.
214
+ */
215
+ async request(config) {
216
+ const requestConfig = this.createConfig(config);
217
+ const axiosResponse = await this.axios.request(requestConfig);
218
+ return this.createResponse(axiosResponse, requestConfig);
219
+ }
220
+ /**
221
+ * Create a new config by merging the global config, the repository config,
222
+ * and the given config.
223
+ */
224
+ createConfig(config) {
225
+ return {
226
+ ...this.config,
227
+ ...this.repository.globalApiConfig,
228
+ ...this.repository.apiConfig,
229
+ ...config
230
+ };
231
+ }
232
+ /**
233
+ * Create a new response instance by applying a few initialization processes.
234
+ * For example, it saves response data if `save` option id set to `true`.
235
+ */
236
+ async createResponse(axiosResponse, config) {
237
+ const response = new Response(this.repository, config, axiosResponse);
238
+ if (config.delete !== void 0) {
239
+ await response.delete();
240
+ return response;
241
+ }
242
+ config.save && await response.save();
243
+ return response;
244
+ }
245
+ }
246
+
247
+ var __defProp = Object.defineProperty;
248
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
249
+ var __publicField = (obj, key, value) => {
250
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
251
+ return value;
252
+ };
253
+ class AxiosRepository extends piniaOrm.Repository {
254
+ constructor() {
255
+ super(...arguments);
256
+ __publicField(this, "axios", piniaOrm.config?.axiosApi?.axios || null);
257
+ __publicField(this, "globalApiConfig", piniaOrm.config?.axiosApi || {});
258
+ __publicField(this, "apiConfig", {});
259
+ }
260
+ api() {
261
+ return useAxiosApi(this);
262
+ }
263
+ setAxios(axios) {
264
+ this.axios = axios;
265
+ return this;
266
+ }
267
+ }
268
+
269
+ function useAxiosApi(repository) {
270
+ return new Request(repository);
271
+ }
272
+
273
+ function useAxiosRepo(model) {
274
+ AxiosRepository.useModel = model;
275
+ return piniaOrm.useRepo(AxiosRepository);
276
+ }
277
+
278
+ function createPiniaOrmAxios(axiosConfig) {
279
+ return piniaOrm.definePiniaOrmPlugin((context) => {
280
+ context.config.axiosApi = axiosConfig;
281
+ return context;
282
+ });
283
+ }
284
+ const piniaOrmPluginAxios = createPiniaOrmAxios();
285
+
286
+ exports.AxiosRepository = AxiosRepository;
287
+ exports.Request = Request;
288
+ exports.Response = Response;
289
+ exports.createPiniaOrmAxios = createPiniaOrmAxios;
290
+ exports.piniaOrmPluginAxios = piniaOrmPluginAxios;
291
+ exports.useAxiosApi = useAxiosApi;
292
+ exports.useAxiosRepo = useAxiosRepo;
@@ -0,0 +1,164 @@
1
+ import { AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
2
+ import { Element, Model, Repository, Collection, PiniaOrmPlugin } from 'pinia-orm';
3
+
4
+ type PersistMethods = 'save' | 'insert';
5
+ type PersistOptions = {
6
+ [P in PersistMethods]?: string[];
7
+ };
8
+ interface Config extends AxiosRequestConfig {
9
+ dataKey?: string;
10
+ url?: string;
11
+ method?: string;
12
+ data?: any;
13
+ dataTransformer?: (response: AxiosResponse) => Element | Element[];
14
+ save?: boolean;
15
+ persistBy?: PersistMethods;
16
+ persistOptions?: PersistOptions;
17
+ delete?: string | number | ((model: Model) => boolean);
18
+ actions?: {
19
+ [name: string]: any;
20
+ };
21
+ }
22
+ interface GlobalConfig extends Config {
23
+ axios?: AxiosInstance;
24
+ }
25
+
26
+ declare class AxiosRepository<M extends Model = Model> extends Repository<M> {
27
+ axios: AxiosInstance;
28
+ globalApiConfig: any;
29
+ apiConfig: Config;
30
+ api(): Request;
31
+ setAxios(axios: AxiosInstance): this;
32
+ }
33
+
34
+ declare class Response {
35
+ /**
36
+ * The repository that called the request.
37
+ */
38
+ repository: AxiosRepository;
39
+ /**
40
+ * The request configuration.
41
+ */
42
+ config: Config;
43
+ /**
44
+ * The axios response instance.
45
+ */
46
+ response: AxiosResponse;
47
+ /**
48
+ * Entities created by Pinia ORM.
49
+ */
50
+ entities: Collection | null;
51
+ /**
52
+ * Whether if response data is saved to the store or not.
53
+ */
54
+ isSaved: boolean;
55
+ /**
56
+ * Create a new response instance.
57
+ */
58
+ constructor(repository: AxiosRepository, config: Config, response: AxiosResponse);
59
+ /**
60
+ * Save response data to the store.
61
+ */
62
+ save(): Promise<void>;
63
+ /**
64
+ * Delete the entity record where the `delete` option is configured.
65
+ */
66
+ delete(): Promise<void>;
67
+ /**
68
+ * Get the response data from the axios response object. If a `dataTransformer`
69
+ * option is configured, it will be applied to the response object. If the
70
+ * `dataKey` option is configured, it will return the data from the given
71
+ * property within the response body.
72
+ */
73
+ getDataFromResponse(): Element | Element[];
74
+ /**
75
+ * Get persist options if any set in config.
76
+ */
77
+ /**
78
+ * Validate the given data to ensure the Pinia ORM persist methods accept it.
79
+ */
80
+ protected validateData(data: any): data is Element | Element[];
81
+ /**
82
+ * Validate the given string as to ensure it correlates with the available
83
+ * Pinia ORM persist methods.
84
+ */
85
+ protected validatePersistAction(action: string): action is PersistMethods;
86
+ }
87
+
88
+ declare class Request {
89
+ /**
90
+ * The repository class.
91
+ */
92
+ repository: AxiosRepository;
93
+ /**
94
+ * The default config.
95
+ */
96
+ config: Config;
97
+ /**
98
+ * Create a new api instance.
99
+ */
100
+ constructor(repository: AxiosRepository);
101
+ /**
102
+ * Index key for the user defined actions.
103
+ */
104
+ [action: string]: any;
105
+ /**
106
+ * Get the axios client.
107
+ */
108
+ get axios(): AxiosInstance;
109
+ /**
110
+ * Register actions from the repository config.
111
+ */
112
+ private registerActions;
113
+ /**
114
+ * Register the given object action.
115
+ */
116
+ private registerObjectAction;
117
+ /**
118
+ * Register the given function action.
119
+ */
120
+ private registerFunctionAction;
121
+ /**
122
+ * Perform a get request.
123
+ */
124
+ get(url: string, config?: Config): Promise<Response>;
125
+ /**
126
+ * Perform a post request.
127
+ */
128
+ post(url: string, data?: any, config?: Config): Promise<Response>;
129
+ /**
130
+ * Perform a put request.
131
+ */
132
+ put(url: string, data?: any, config?: Config): Promise<Response>;
133
+ /**
134
+ * Perform a patch request.
135
+ */
136
+ patch(url: string, data?: any, config?: Config): Promise<Response>;
137
+ /**
138
+ * Perform a delete request.
139
+ */
140
+ delete(url: string, config?: Config): Promise<Response>;
141
+ /**
142
+ * Perform an api request.
143
+ */
144
+ request(config: Config): Promise<Response>;
145
+ /**
146
+ * Create a new config by merging the global config, the repository config,
147
+ * and the given config.
148
+ */
149
+ private createConfig;
150
+ /**
151
+ * Create a new response instance by applying a few initialization processes.
152
+ * For example, it saves response data if `save` option id set to `true`.
153
+ */
154
+ private createResponse;
155
+ }
156
+
157
+ declare function useAxiosApi(repository: AxiosRepository): Request;
158
+
159
+ declare function useAxiosRepo<M extends Model>(model: M): AxiosRepository<Model>;
160
+
161
+ declare function createPiniaOrmAxios(axiosConfig?: GlobalConfig): PiniaOrmPlugin;
162
+ declare const piniaOrmPluginAxios: PiniaOrmPlugin;
163
+
164
+ export { AxiosRepository, Request, Response, createPiniaOrmAxios, piniaOrmPluginAxios, useAxiosApi, useAxiosRepo };
@@ -0,0 +1,164 @@
1
+ import { AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
2
+ import { Element, Model, Repository, Collection, PiniaOrmPlugin } from 'pinia-orm';
3
+
4
+ type PersistMethods = 'save' | 'insert';
5
+ type PersistOptions = {
6
+ [P in PersistMethods]?: string[];
7
+ };
8
+ interface Config extends AxiosRequestConfig {
9
+ dataKey?: string;
10
+ url?: string;
11
+ method?: string;
12
+ data?: any;
13
+ dataTransformer?: (response: AxiosResponse) => Element | Element[];
14
+ save?: boolean;
15
+ persistBy?: PersistMethods;
16
+ persistOptions?: PersistOptions;
17
+ delete?: string | number | ((model: Model) => boolean);
18
+ actions?: {
19
+ [name: string]: any;
20
+ };
21
+ }
22
+ interface GlobalConfig extends Config {
23
+ axios?: AxiosInstance;
24
+ }
25
+
26
+ declare class AxiosRepository<M extends Model = Model> extends Repository<M> {
27
+ axios: AxiosInstance;
28
+ globalApiConfig: any;
29
+ apiConfig: Config;
30
+ api(): Request;
31
+ setAxios(axios: AxiosInstance): this;
32
+ }
33
+
34
+ declare class Response {
35
+ /**
36
+ * The repository that called the request.
37
+ */
38
+ repository: AxiosRepository;
39
+ /**
40
+ * The request configuration.
41
+ */
42
+ config: Config;
43
+ /**
44
+ * The axios response instance.
45
+ */
46
+ response: AxiosResponse;
47
+ /**
48
+ * Entities created by Pinia ORM.
49
+ */
50
+ entities: Collection | null;
51
+ /**
52
+ * Whether if response data is saved to the store or not.
53
+ */
54
+ isSaved: boolean;
55
+ /**
56
+ * Create a new response instance.
57
+ */
58
+ constructor(repository: AxiosRepository, config: Config, response: AxiosResponse);
59
+ /**
60
+ * Save response data to the store.
61
+ */
62
+ save(): Promise<void>;
63
+ /**
64
+ * Delete the entity record where the `delete` option is configured.
65
+ */
66
+ delete(): Promise<void>;
67
+ /**
68
+ * Get the response data from the axios response object. If a `dataTransformer`
69
+ * option is configured, it will be applied to the response object. If the
70
+ * `dataKey` option is configured, it will return the data from the given
71
+ * property within the response body.
72
+ */
73
+ getDataFromResponse(): Element | Element[];
74
+ /**
75
+ * Get persist options if any set in config.
76
+ */
77
+ /**
78
+ * Validate the given data to ensure the Pinia ORM persist methods accept it.
79
+ */
80
+ protected validateData(data: any): data is Element | Element[];
81
+ /**
82
+ * Validate the given string as to ensure it correlates with the available
83
+ * Pinia ORM persist methods.
84
+ */
85
+ protected validatePersistAction(action: string): action is PersistMethods;
86
+ }
87
+
88
+ declare class Request {
89
+ /**
90
+ * The repository class.
91
+ */
92
+ repository: AxiosRepository;
93
+ /**
94
+ * The default config.
95
+ */
96
+ config: Config;
97
+ /**
98
+ * Create a new api instance.
99
+ */
100
+ constructor(repository: AxiosRepository);
101
+ /**
102
+ * Index key for the user defined actions.
103
+ */
104
+ [action: string]: any;
105
+ /**
106
+ * Get the axios client.
107
+ */
108
+ get axios(): AxiosInstance;
109
+ /**
110
+ * Register actions from the repository config.
111
+ */
112
+ private registerActions;
113
+ /**
114
+ * Register the given object action.
115
+ */
116
+ private registerObjectAction;
117
+ /**
118
+ * Register the given function action.
119
+ */
120
+ private registerFunctionAction;
121
+ /**
122
+ * Perform a get request.
123
+ */
124
+ get(url: string, config?: Config): Promise<Response>;
125
+ /**
126
+ * Perform a post request.
127
+ */
128
+ post(url: string, data?: any, config?: Config): Promise<Response>;
129
+ /**
130
+ * Perform a put request.
131
+ */
132
+ put(url: string, data?: any, config?: Config): Promise<Response>;
133
+ /**
134
+ * Perform a patch request.
135
+ */
136
+ patch(url: string, data?: any, config?: Config): Promise<Response>;
137
+ /**
138
+ * Perform a delete request.
139
+ */
140
+ delete(url: string, config?: Config): Promise<Response>;
141
+ /**
142
+ * Perform an api request.
143
+ */
144
+ request(config: Config): Promise<Response>;
145
+ /**
146
+ * Create a new config by merging the global config, the repository config,
147
+ * and the given config.
148
+ */
149
+ private createConfig;
150
+ /**
151
+ * Create a new response instance by applying a few initialization processes.
152
+ * For example, it saves response data if `save` option id set to `true`.
153
+ */
154
+ private createResponse;
155
+ }
156
+
157
+ declare function useAxiosApi(repository: AxiosRepository): Request;
158
+
159
+ declare function useAxiosRepo<M extends Model>(model: M): AxiosRepository<Model>;
160
+
161
+ declare function createPiniaOrmAxios(axiosConfig?: GlobalConfig): PiniaOrmPlugin;
162
+ declare const piniaOrmPluginAxios: PiniaOrmPlugin;
163
+
164
+ export { AxiosRepository, Request, Response, createPiniaOrmAxios, piniaOrmPluginAxios, useAxiosApi, useAxiosRepo };
@@ -0,0 +1,164 @@
1
+ import { AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
2
+ import { Element, Model, Repository, Collection, PiniaOrmPlugin } from 'pinia-orm';
3
+
4
+ type PersistMethods = 'save' | 'insert';
5
+ type PersistOptions = {
6
+ [P in PersistMethods]?: string[];
7
+ };
8
+ interface Config extends AxiosRequestConfig {
9
+ dataKey?: string;
10
+ url?: string;
11
+ method?: string;
12
+ data?: any;
13
+ dataTransformer?: (response: AxiosResponse) => Element | Element[];
14
+ save?: boolean;
15
+ persistBy?: PersistMethods;
16
+ persistOptions?: PersistOptions;
17
+ delete?: string | number | ((model: Model) => boolean);
18
+ actions?: {
19
+ [name: string]: any;
20
+ };
21
+ }
22
+ interface GlobalConfig extends Config {
23
+ axios?: AxiosInstance;
24
+ }
25
+
26
+ declare class AxiosRepository<M extends Model = Model> extends Repository<M> {
27
+ axios: AxiosInstance;
28
+ globalApiConfig: any;
29
+ apiConfig: Config;
30
+ api(): Request;
31
+ setAxios(axios: AxiosInstance): this;
32
+ }
33
+
34
+ declare class Response {
35
+ /**
36
+ * The repository that called the request.
37
+ */
38
+ repository: AxiosRepository;
39
+ /**
40
+ * The request configuration.
41
+ */
42
+ config: Config;
43
+ /**
44
+ * The axios response instance.
45
+ */
46
+ response: AxiosResponse;
47
+ /**
48
+ * Entities created by Pinia ORM.
49
+ */
50
+ entities: Collection | null;
51
+ /**
52
+ * Whether if response data is saved to the store or not.
53
+ */
54
+ isSaved: boolean;
55
+ /**
56
+ * Create a new response instance.
57
+ */
58
+ constructor(repository: AxiosRepository, config: Config, response: AxiosResponse);
59
+ /**
60
+ * Save response data to the store.
61
+ */
62
+ save(): Promise<void>;
63
+ /**
64
+ * Delete the entity record where the `delete` option is configured.
65
+ */
66
+ delete(): Promise<void>;
67
+ /**
68
+ * Get the response data from the axios response object. If a `dataTransformer`
69
+ * option is configured, it will be applied to the response object. If the
70
+ * `dataKey` option is configured, it will return the data from the given
71
+ * property within the response body.
72
+ */
73
+ getDataFromResponse(): Element | Element[];
74
+ /**
75
+ * Get persist options if any set in config.
76
+ */
77
+ /**
78
+ * Validate the given data to ensure the Pinia ORM persist methods accept it.
79
+ */
80
+ protected validateData(data: any): data is Element | Element[];
81
+ /**
82
+ * Validate the given string as to ensure it correlates with the available
83
+ * Pinia ORM persist methods.
84
+ */
85
+ protected validatePersistAction(action: string): action is PersistMethods;
86
+ }
87
+
88
+ declare class Request {
89
+ /**
90
+ * The repository class.
91
+ */
92
+ repository: AxiosRepository;
93
+ /**
94
+ * The default config.
95
+ */
96
+ config: Config;
97
+ /**
98
+ * Create a new api instance.
99
+ */
100
+ constructor(repository: AxiosRepository);
101
+ /**
102
+ * Index key for the user defined actions.
103
+ */
104
+ [action: string]: any;
105
+ /**
106
+ * Get the axios client.
107
+ */
108
+ get axios(): AxiosInstance;
109
+ /**
110
+ * Register actions from the repository config.
111
+ */
112
+ private registerActions;
113
+ /**
114
+ * Register the given object action.
115
+ */
116
+ private registerObjectAction;
117
+ /**
118
+ * Register the given function action.
119
+ */
120
+ private registerFunctionAction;
121
+ /**
122
+ * Perform a get request.
123
+ */
124
+ get(url: string, config?: Config): Promise<Response>;
125
+ /**
126
+ * Perform a post request.
127
+ */
128
+ post(url: string, data?: any, config?: Config): Promise<Response>;
129
+ /**
130
+ * Perform a put request.
131
+ */
132
+ put(url: string, data?: any, config?: Config): Promise<Response>;
133
+ /**
134
+ * Perform a patch request.
135
+ */
136
+ patch(url: string, data?: any, config?: Config): Promise<Response>;
137
+ /**
138
+ * Perform a delete request.
139
+ */
140
+ delete(url: string, config?: Config): Promise<Response>;
141
+ /**
142
+ * Perform an api request.
143
+ */
144
+ request(config: Config): Promise<Response>;
145
+ /**
146
+ * Create a new config by merging the global config, the repository config,
147
+ * and the given config.
148
+ */
149
+ private createConfig;
150
+ /**
151
+ * Create a new response instance by applying a few initialization processes.
152
+ * For example, it saves response data if `save` option id set to `true`.
153
+ */
154
+ private createResponse;
155
+ }
156
+
157
+ declare function useAxiosApi(repository: AxiosRepository): Request;
158
+
159
+ declare function useAxiosRepo<M extends Model>(model: M): AxiosRepository<Model>;
160
+
161
+ declare function createPiniaOrmAxios(axiosConfig?: GlobalConfig): PiniaOrmPlugin;
162
+ declare const piniaOrmPluginAxios: PiniaOrmPlugin;
163
+
164
+ export { AxiosRepository, Request, Response, createPiniaOrmAxios, piniaOrmPluginAxios, useAxiosApi, useAxiosRepo };
package/dist/index.mjs ADDED
@@ -0,0 +1,284 @@
1
+ import { Repository, config, useRepo, definePiniaOrmPlugin } from 'pinia-orm';
2
+
3
+ var __defProp$2 = Object.defineProperty;
4
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
+ var __publicField$2 = (obj, key, value) => {
6
+ __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ return value;
8
+ };
9
+ class Response {
10
+ /**
11
+ * Create a new response instance.
12
+ */
13
+ constructor(repository, config, response) {
14
+ /**
15
+ * The repository that called the request.
16
+ */
17
+ __publicField$2(this, "repository");
18
+ /**
19
+ * The request configuration.
20
+ */
21
+ __publicField$2(this, "config");
22
+ /**
23
+ * The axios response instance.
24
+ */
25
+ __publicField$2(this, "response");
26
+ /**
27
+ * Entities created by Pinia ORM.
28
+ */
29
+ __publicField$2(this, "entities", null);
30
+ /**
31
+ * Whether if response data is saved to the store or not.
32
+ */
33
+ __publicField$2(this, "isSaved", false);
34
+ this.repository = repository;
35
+ this.config = config;
36
+ this.response = response;
37
+ }
38
+ /**
39
+ * Save response data to the store.
40
+ */
41
+ async save() {
42
+ const data = this.getDataFromResponse();
43
+ if (!this.validateData(data)) {
44
+ console.warn(
45
+ "[Pinia ORM Axios] The response data could not be saved to the store because it is not an object or an array. You might want to use `dataTransformer` option to handle non-array/object response before saving it to the store."
46
+ );
47
+ return;
48
+ }
49
+ let method = this.config.persistBy || "save";
50
+ if (!this.validatePersistAction(method)) {
51
+ console.warn(
52
+ '[Pinia ORM Axios] The "persistBy" option configured is not a recognized value. Response data will be persisted by the default `save` method.'
53
+ );
54
+ method = "save";
55
+ }
56
+ const result = await this.repository[method](data);
57
+ this.entities = Array.isArray(result) ? result : [result];
58
+ this.isSaved = true;
59
+ }
60
+ /**
61
+ * Delete the entity record where the `delete` option is configured.
62
+ */
63
+ async delete() {
64
+ if (this.config.delete === void 0) {
65
+ throw new Error(
66
+ "[Pinia ORM Axios] Could not delete records because the `delete` option is not set."
67
+ );
68
+ }
69
+ await this.repository.query().destroy(this.config.delete);
70
+ }
71
+ /**
72
+ * Get the response data from the axios response object. If a `dataTransformer`
73
+ * option is configured, it will be applied to the response object. If the
74
+ * `dataKey` option is configured, it will return the data from the given
75
+ * property within the response body.
76
+ */
77
+ getDataFromResponse() {
78
+ if (this.config.dataTransformer) {
79
+ return this.config.dataTransformer(this.response);
80
+ }
81
+ if (this.config.dataKey) {
82
+ return this.response.data[this.config.dataKey];
83
+ }
84
+ return this.response.data;
85
+ }
86
+ /**
87
+ * Get persist options if any set in config.
88
+ */
89
+ // protected getPersistOptions (): PersistOptions | undefined {
90
+ // const persistOptions = this.config.persistOptions
91
+ //
92
+ // if (!persistOptions || typeof persistOptions !== 'object') {
93
+ // return
94
+ // }
95
+ //
96
+ // return Object.keys(persistOptions)
97
+ // .filter(this.validatePersistAction) // Filter to avoid polluting the payload.
98
+ // .reduce((carry, key) => {
99
+ // carry[key] = persistOptions[key]
100
+ // return carry
101
+ // }, {} as PersistOptions)
102
+ // }
103
+ /**
104
+ * Validate the given data to ensure the Pinia ORM persist methods accept it.
105
+ */
106
+ validateData(data) {
107
+ return data !== null && typeof data === "object";
108
+ }
109
+ /**
110
+ * Validate the given string as to ensure it correlates with the available
111
+ * Pinia ORM persist methods.
112
+ */
113
+ validatePersistAction(action) {
114
+ return ["save", "insert"].includes(action);
115
+ }
116
+ }
117
+
118
+ var __defProp$1 = Object.defineProperty;
119
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
120
+ var __publicField$1 = (obj, key, value) => {
121
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
122
+ return value;
123
+ };
124
+ class Request {
125
+ /**
126
+ * Create a new api instance.
127
+ */
128
+ constructor(repository) {
129
+ /**
130
+ * The repository class.
131
+ */
132
+ __publicField$1(this, "repository");
133
+ /**
134
+ * The default config.
135
+ */
136
+ __publicField$1(this, "config", {
137
+ save: true
138
+ });
139
+ this.repository = repository;
140
+ this.registerActions();
141
+ }
142
+ /**
143
+ * Get the axios client.
144
+ */
145
+ get axios() {
146
+ if (!this.repository.axios) {
147
+ throw new Error(
148
+ "[Pinia ORM Axios] The axios instance is not registered. Please register the axios instance to the repository."
149
+ );
150
+ }
151
+ return this.repository.axios;
152
+ }
153
+ /**
154
+ * Register actions from the repository config.
155
+ */
156
+ registerActions() {
157
+ const actions = { ...this.repository.config.axiosApi?.actions, ...this.repository.getModel().$config()?.axiosApi?.actions };
158
+ if (!actions) {
159
+ return;
160
+ }
161
+ for (const name in actions) {
162
+ const action = actions[name];
163
+ typeof action === "function" ? this.registerFunctionAction(name, action) : this.registerObjectAction(name, action);
164
+ }
165
+ }
166
+ /**
167
+ * Register the given object action.
168
+ */
169
+ registerObjectAction(name, action) {
170
+ this[name] = (config) => {
171
+ return this.request({ ...action, ...config });
172
+ };
173
+ }
174
+ /**
175
+ * Register the given function action.
176
+ */
177
+ registerFunctionAction(name, action) {
178
+ this[name] = action.bind(this);
179
+ }
180
+ /**
181
+ * Perform a get request.
182
+ */
183
+ get(url, config = {}) {
184
+ return this.request({ method: "get", url, ...config });
185
+ }
186
+ /**
187
+ * Perform a post request.
188
+ */
189
+ post(url, data = {}, config = {}) {
190
+ return this.request({ method: "post", url, data, ...config });
191
+ }
192
+ /**
193
+ * Perform a put request.
194
+ */
195
+ put(url, data = {}, config = {}) {
196
+ return this.request({ method: "put", url, data, ...config });
197
+ }
198
+ /**
199
+ * Perform a patch request.
200
+ */
201
+ patch(url, data = {}, config = {}) {
202
+ return this.request({ method: "patch", url, data, ...config });
203
+ }
204
+ /**
205
+ * Perform a delete request.
206
+ */
207
+ delete(url, config = {}) {
208
+ return this.request({ method: "delete", url, ...config });
209
+ }
210
+ /**
211
+ * Perform an api request.
212
+ */
213
+ async request(config) {
214
+ const requestConfig = this.createConfig(config);
215
+ const axiosResponse = await this.axios.request(requestConfig);
216
+ return this.createResponse(axiosResponse, requestConfig);
217
+ }
218
+ /**
219
+ * Create a new config by merging the global config, the repository config,
220
+ * and the given config.
221
+ */
222
+ createConfig(config) {
223
+ return {
224
+ ...this.config,
225
+ ...this.repository.globalApiConfig,
226
+ ...this.repository.apiConfig,
227
+ ...config
228
+ };
229
+ }
230
+ /**
231
+ * Create a new response instance by applying a few initialization processes.
232
+ * For example, it saves response data if `save` option id set to `true`.
233
+ */
234
+ async createResponse(axiosResponse, config) {
235
+ const response = new Response(this.repository, config, axiosResponse);
236
+ if (config.delete !== void 0) {
237
+ await response.delete();
238
+ return response;
239
+ }
240
+ config.save && await response.save();
241
+ return response;
242
+ }
243
+ }
244
+
245
+ var __defProp = Object.defineProperty;
246
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
247
+ var __publicField = (obj, key, value) => {
248
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
249
+ return value;
250
+ };
251
+ class AxiosRepository extends Repository {
252
+ constructor() {
253
+ super(...arguments);
254
+ __publicField(this, "axios", config?.axiosApi?.axios || null);
255
+ __publicField(this, "globalApiConfig", config?.axiosApi || {});
256
+ __publicField(this, "apiConfig", {});
257
+ }
258
+ api() {
259
+ return useAxiosApi(this);
260
+ }
261
+ setAxios(axios) {
262
+ this.axios = axios;
263
+ return this;
264
+ }
265
+ }
266
+
267
+ function useAxiosApi(repository) {
268
+ return new Request(repository);
269
+ }
270
+
271
+ function useAxiosRepo(model) {
272
+ AxiosRepository.useModel = model;
273
+ return useRepo(AxiosRepository);
274
+ }
275
+
276
+ function createPiniaOrmAxios(axiosConfig) {
277
+ return definePiniaOrmPlugin((context) => {
278
+ context.config.axiosApi = axiosConfig;
279
+ return context;
280
+ });
281
+ }
282
+ const piniaOrmPluginAxios = createPiniaOrmAxios();
283
+
284
+ export { AxiosRepository, Request, Response, createPiniaOrmAxios, piniaOrmPluginAxios, useAxiosApi, useAxiosRepo };
package/package.json ADDED
@@ -0,0 +1,82 @@
1
+ {
2
+ "name": "@pinia-orm/axios-edge",
3
+ "version": "1.7.1-28257628.bbb8bab",
4
+ "description": "Axios plugin for pinia-orm",
5
+ "bugs": {
6
+ "url": "https://github.com/CodeDredd/pinia-orm/issues"
7
+ },
8
+ "homepage": "https://github.com/CodeDredd/pinia-orm",
9
+ "repository": {
10
+ "url": "https://github.com/CodeDredd/pinia-orm.git",
11
+ "type": "git"
12
+ },
13
+ "keywords": [
14
+ "axios",
15
+ "pinia-orm",
16
+ "api"
17
+ ],
18
+ "files": [
19
+ "dist/",
20
+ "index.d.ts",
21
+ "LICENSE",
22
+ "README.md"
23
+ ],
24
+ "type": "module",
25
+ "funding": "https://github.com/sponsors/codedredd",
26
+ "jsdelivr": "dist/index.mjs",
27
+ "unpkg": "dist/index.mjs",
28
+ "main": "./dist/index.cjs",
29
+ "module": "./dist/index.mjs",
30
+ "types": "./dist/index.d.ts",
31
+ "exports": {
32
+ ".": {
33
+ "require": "./dist/index.cjs",
34
+ "import": "./dist/index.mjs"
35
+ },
36
+ "./*": "./*"
37
+ },
38
+ "sideEffects": false,
39
+ "author": {
40
+ "name": "Gregor Becker",
41
+ "email": "gregor@codedredd.de"
42
+ },
43
+ "license": "MIT",
44
+ "peerDependencies": {
45
+ "pinia-orm": "npm:pinia-orm-edge@1.7.3-28257628.bbb8bab",
46
+ "axios": ">=1.5.0"
47
+ },
48
+ "devDependencies": {
49
+ "@nuxtjs/eslint-config-typescript": "^12.1.0",
50
+ "@size-limit/preset-small-lib": "^9.0.0",
51
+ "@vitest/coverage-v8": "^0.34.5",
52
+ "axios": "^1.5.0",
53
+ "axios-mock-adapter": "^1.22.0",
54
+ "eslint": "^8.49.0",
55
+ "pinia-orm": "npm:pinia-orm-edge@1.7.3-28257628.bbb8bab",
56
+ "pinia": "^2.1.6",
57
+ "size-limit": "^9.0.0",
58
+ "typescript": "^5.2.2",
59
+ "unbuild": "^2.0.0",
60
+ "vitest": "^0.34.5",
61
+ "vue-demi": "^0.14.6"
62
+ },
63
+ "size-limit": [
64
+ {
65
+ "path": "dist/index.mjs",
66
+ "limit": "2 kB"
67
+ }
68
+ ],
69
+ "_name": "@pinia-orm/axios",
70
+ "scripts": {
71
+ "build": "unbuild",
72
+ "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . -l @pinia-orm/axios -r 1",
73
+ "size": "size-limit",
74
+ "lint": "eslint . --ext .ts",
75
+ "lint:fix": "eslint . --fix --ext .ts",
76
+ "test:ui": "vue-demi-switch 3 && vitest --ui --api 9527",
77
+ "test:watch": "vue-demi-switch 3 && vitest --watch",
78
+ "test:2": "vue-demi-switch 2 vue2 && vitest --run",
79
+ "test:3": "vue-demi-switch 3 && vitest --run",
80
+ "test": "pnpm run test:3"
81
+ }
82
+ }