diff --git a/docs/content/en/api/query-builder-methods.md b/docs/content/en/api/query-builder-methods.md index 263f927..9e21c2a 100644 --- a/docs/content/en/api/query-builder-methods.md +++ b/docs/content/en/api/query-builder-methods.md @@ -363,3 +363,21 @@ await Model.$find(1) These `$`-prefixed convenience methods always return the requested content. They handle and unwrap responses within "data". + +## `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(); +``` \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index e924e70..7d59bdd 100644 --- a/index.d.ts +++ b/index.d.ts @@ -733,6 +733,14 @@ export class Model extends StaticModel { */ get(): QueryPromise + /** + * 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 + /** * Execute the query and get all results. * diff --git a/src/Model.js b/src/Model.js index f1eda56..302525f 100644 --- a/src/Model.js +++ b/src/Model.js @@ -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) } diff --git a/src/StaticModel.js b/src/StaticModel.js index da2caec..d7357d8 100644 --- a/src/StaticModel.js +++ b/src/StaticModel.js @@ -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()