Skip to content

Commit e46d63e

Browse files
feat(model): add support to configuration at query builder (#142)
Add a new method `config` to `Model.js`. It can be used to override the http instance (e.g. axios) configuration at query builder. It can be used to change the method of update request to `PATCH`, to add additional data to the request, to add headers, and so on.
1 parent faf3d1e commit e46d63e

File tree

7 files changed

+248
-121
lines changed

7 files changed

+248
-121
lines changed

docs/content/en/api/crud-operations.md

+31
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,37 @@ Save or update a model in the database, then return the instance.
6565
</code-block>
6666
</code-group>
6767

68+
## `patch`
69+
- Returns: `Model | { data: Model }`
70+
71+
Make a `PATCH` request to update a model in the database, then return the instance.
72+
73+
<code-group>
74+
<code-block Label="Query" active>
75+
76+
```js
77+
const model = await Model.find(1)
78+
79+
model.foo = 'bar'
80+
81+
model.patch()
82+
```
83+
84+
</code-block>
85+
<code-block Label="Request">
86+
87+
```http request
88+
PATCH /resource/1
89+
```
90+
91+
</code-block>
92+
</code-group>
93+
94+
Alias for:
95+
```js
96+
model.config({ method: 'PATCH' }).save()
97+
```
98+
6899

69100
## `delete`
70101

docs/content/en/api/query-builder-methods.md

+16
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,22 @@ Build custom endpoints.
180180
</code-block>
181181
</code-group>
182182

183+
## `config`
184+
<alert type="success">Available in version >= v1.8.0</alert>
185+
186+
- Arguments: `(config)`
187+
- Returns: `self`
188+
189+
Configuration of HTTP Instance.
190+
191+
```js
192+
await Model.config({
193+
method: 'PATCH',
194+
header: { /* ... */ },
195+
data: { foo: 'bar' }
196+
}).save()
197+
```
198+
183199
## `get`
184200
- Returns: `Collection | { data: Collection }`
185201

docs/content/en/building-the-query.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,21 @@ We can build a resource to get the latest `Posts` that belongs to a **User**:
614614
</code-block>
615615
</code-group>
616616

617+
## Configuring the Request
618+
619+
<alert type="success">Available in version >= v1.8.0</alert>
620+
621+
See the [API reference](/api/query-builder-methods#config)
622+
623+
The `config` method can be used to configure the current request at query builder. We can pass any config available
624+
from the HTTP Instance. If we are using [Axios](https://github.com/axios/axios),
625+
we should pass an [AxiosRequestConfig](https://github.com/axios/axios#request-config).
626+
627+
We can add headers, change the method, anything we want:
628+
629+
```js
630+
await Post.config({ headers: { /*...*/ } }).get()
631+
```
617632

618633
## Needless Parent Request
619634

@@ -640,8 +655,10 @@ We can get a list of **Posts** that belongs to an **User**:
640655
</code-block>
641656
</code-group>
642657

643-
And the same thing using for the example above, if we want to define a dynamic resource,
644-
we can create a new **User** instance with the ID:
658+
And the same thing can be done if we want to define a
659+
[dynamic resource](/building-the-query#defining-a-dynamic-resource).
660+
661+
We can create a new **User** instance with the ID:
645662

646663
<code-group>
647664
<code-block label="Query" active>

docs/content/en/performing-operations.md

+32
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,38 @@ Then we can update our newly created **Post**:
9191
</code-block>
9292
</code-group>
9393

94+
And if we want to use `PATCH`, we can easily do that using [patch](/api/crud-operations#patch).
95+
96+
<code-group>
97+
<code-block Label="Query" active>
98+
99+
```js
100+
const post = await Post.find(1)
101+
102+
post.text = 'An updated text for our Post!'
103+
104+
await post.patch()
105+
```
106+
107+
</code-block>
108+
<code-block Label="Find Request">
109+
110+
```http request
111+
GET /posts/1
112+
```
113+
114+
</code-block>
115+
<code-block Label="Save Request">
116+
117+
```http request
118+
PATCH /posts/1
119+
```
120+
121+
</code-block>
122+
</code-group>
123+
124+
<alert type="info">You can safely use `PATCH` with `save()`. The `POST` method will not be overridden, only `PUT`.</alert>
125+
94126
### Deleting a Model
95127

96128
See the [API reference](/api/crud-operations#delete).

docs/static/sw.js

+13-87
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,17 @@
1-
const options = {"workboxURL":"https://cdn.jsdelivr.net/npm/[email protected]/workbox/workbox-sw.js","importScripts":[],"config":{"debug":false},"clientsClaim":true,"skipWaiting":true,"cleanupOutdatedCaches":true,"offlineAnalytics":false,"preCaching":["/vue-api-query/?standalone=true"],"runtimeCaching":[{"urlPattern":"/vue-api-query/_nuxt/","handler":"CacheFirst","method":"GET","strategyPlugins":[]},{"urlPattern":"/vue-api-query/","handler":"NetworkFirst","method":"GET","strategyPlugins":[]}],"offlinePage":null,"pagesURLPattern":"/vue-api-query/","offlineStrategy":"NetworkFirst"}
1+
// THIS FILE SHOULD NOT BE VERSION CONTROLLED
22

3-
importScripts(...[options.workboxURL, ...options.importScripts])
3+
// https://github.com/NekR/self-destroying-sw
44

5-
initWorkbox(workbox, options)
6-
workboxExtensions(workbox, options)
7-
precacheAssets(workbox, options)
8-
cachingExtensions(workbox, options)
9-
runtimeCaching(workbox, options)
10-
offlinePage(workbox, options)
11-
routingExtensions(workbox, options)
5+
self.addEventListener('install', function (e) {
6+
self.skipWaiting()
7+
})
128

13-
function getProp(obj, prop) {
14-
return prop.split('.').reduce((p, c) => p[c], obj)
15-
}
16-
17-
function initWorkbox(workbox, options) {
18-
if (options.config) {
19-
// Set workbox config
20-
workbox.setConfig(options.config)
21-
}
22-
23-
if (options.cacheNames) {
24-
// Set workbox cache names
25-
workbox.core.setCacheNameDetails(options.cacheNames)
26-
}
27-
28-
if (options.clientsClaim) {
29-
// Start controlling any existing clients as soon as it activates
30-
workbox.core.clientsClaim()
31-
}
32-
33-
if (options.skipWaiting) {
34-
workbox.core.skipWaiting()
35-
}
36-
37-
if (options.cleanupOutdatedCaches) {
38-
workbox.precaching.cleanupOutdatedCaches()
39-
}
40-
41-
if (options.offlineAnalytics) {
42-
// Enable offline Google Analytics tracking
43-
workbox.googleAnalytics.initialize()
44-
}
45-
}
46-
47-
function precacheAssets(workbox, options) {
48-
if (options.preCaching.length) {
49-
workbox.precaching.precacheAndRoute(options.preCaching, options.cacheOptions)
50-
}
51-
}
52-
53-
function runtimeCaching(workbox, options) {
54-
for (const entry of options.runtimeCaching) {
55-
const urlPattern = new RegExp(entry.urlPattern)
56-
const method = entry.method || 'GET'
57-
58-
const plugins = (entry.strategyPlugins || [])
59-
.map(p => new (getProp(workbox, p.use))(...p.config))
60-
61-
const strategyOptions = { ...entry.strategyOptions, plugins }
62-
63-
const strategy = new workbox.strategies[entry.handler](strategyOptions)
64-
65-
workbox.routing.registerRoute(urlPattern, strategy, method)
66-
}
67-
}
68-
69-
function offlinePage(workbox, options) {
70-
if (options.offlinePage) {
71-
// Register router handler for offlinePage
72-
workbox.routing.registerRoute(new RegExp(options.pagesURLPattern), ({ request, event }) => {
73-
const strategy = new workbox.strategies[options.offlineStrategy]
74-
return strategy
75-
.handle({ request, event })
76-
.catch(() => caches.match(options.offlinePage))
9+
self.addEventListener('activate', function (e) {
10+
self.registration.unregister()
11+
.then(function () {
12+
return self.clients.matchAll()
7713
})
78-
}
79-
}
80-
81-
function workboxExtensions(workbox, options) {
82-
83-
}
84-
85-
function cachingExtensions(workbox, options) {
86-
87-
}
88-
89-
function routingExtensions(workbox, options) {
90-
91-
}
14+
.then(function (clients) {
15+
clients.forEach(client => client.navigate(client.url))
16+
})
17+
})

0 commit comments

Comments
 (0)