Skip to content

Commit 27ee611

Browse files
authored
Merge pull request #11 from mindedge/dev
feat(model): add file return
2 parents 2e0013a + 77b4381 commit 27ee611

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,21 @@ await Model.$find(1)
363363

364364
<alert type="info">These `$`-prefixed convenience methods always return the requested content.
365365
They handle and unwrap responses within "data".</alert>
366+
367+
## `file`
368+
- Returns: `Binary`
369+
370+
Execute the query with $http.responseType as `blob` and returns a binary
371+
372+
```js
373+
// get the blob
374+
const data = await Model.file()
375+
376+
// force file download
377+
const url = window.URL.createObjectURL(new Blob([data]));
378+
const link = document.createElement('a');
379+
link.href = url;
380+
link.setAttribute('download', 'model.xlsx'); //or any other extension
381+
document.body.appendChild(link);
382+
link.click();
383+
```

index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,14 @@ export class Model extends StaticModel {
733733
*/
734734
get(): QueryPromise<this[]>
735735

736+
/**
737+
* Execute the query and get all results.
738+
*
739+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#file|API Reference}
740+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#retrieving-a-list-of-records|Building the Query}
741+
*/
742+
file(): QueryPromise<BlobPart[]>
743+
736744
/**
737745
* Execute the query and get all results.
738746
*

src/Model.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,28 @@ export default class Model extends StaticModel {
469469
})
470470
}
471471

472+
file() {
473+
let base = this._fromResource || `${this.baseURL()}/${this.resource()}`
474+
base = this._customResource
475+
? `${this.baseURL()}/${this._customResource}`
476+
: base
477+
478+
let url = `${base}${this._builder.query()}`
479+
480+
return this.request(
481+
this._reqConfig({
482+
url,
483+
method: 'GET',
484+
responseType: 'blob',
485+
headers: {
486+
accept: 'application/octet-stream'
487+
}
488+
})
489+
).then((response) => {
490+
return response.data
491+
})
492+
}
493+
472494
$get() {
473495
return this.get().then((response) => response.data || response)
474496
}

src/StaticModel.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ export default class StaticModel {
146146
return self.get()
147147
}
148148

149+
static file() {
150+
let self = this.instance()
151+
152+
self.file()
153+
}
154+
149155
static all() {
150156
let self = this.instance()
151157

0 commit comments

Comments
 (0)