Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/content/en/api/query-builder-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,21 @@ await Model.$find(1)

<alert type="info">These `$`-prefixed convenience methods always return the requested content.
They handle and unwrap responses within "data".</alert>

## `file`
- Returns: `Binary`

Execute the query with $http.responseType as `blob` and returns a binary

```js
// get the blob
const data = await Model.file()

// force file download
const url = window.URL.createObjectURL(new Blob([data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'model.xlsx'); //or any other extension
document.body.appendChild(link);
link.click();
```
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,14 @@ export class Model extends StaticModel {
*/
get(): QueryPromise<this[]>

/**
* Execute the query and get all results.
*
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#file|API Reference}
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#retrieving-a-list-of-records|Building the Query}
*/
file(): QueryPromise<BlobPart[]>

/**
* Execute the query and get all results.
*
Expand Down
22 changes: 22 additions & 0 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,28 @@ export default class Model extends StaticModel {
})
}

file() {
let base = this._fromResource || `${this.baseURL()}/${this.resource()}`
base = this._customResource
? `${this.baseURL()}/${this._customResource}`
: base

let url = `${base}${this._builder.query()}`

return this.request(
this._reqConfig({
url,
method: 'GET',
responseType: 'blob',
headers: {
accept: 'application/octet-stream'
}
})
).then((response) => {
return response.data
})
}

$get() {
return this.get().then((response) => response.data || response)
}
Expand Down
6 changes: 6 additions & 0 deletions src/StaticModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ export default class StaticModel {
return self.get()
}

static file() {
let self = this.instance()

self.file()
}

static all() {
let self = this.instance()

Expand Down