Skip to content

Commit

Permalink
feat: $http.getBaseURL (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
sundowndev authored Feb 2, 2021
1 parent a6f2195 commit 332bf34
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion docs/content/en/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Helpers available on `$http` instance.

- arguments: `(baseURL)`

Globally set a header to all subsequent requests
Globally set a base URL to all subsequent requests

```js
// Set baseURL (both client and server)
Expand All @@ -32,6 +32,15 @@ if (process.server) {
}
```

## `getBaseURL`

Get the value of the base URL

```js
// Get baseURL value
this.$http.getBaseURL()
```

## `setHeader`

- arguments: `(name, value)`
Expand Down
4 changes: 4 additions & 0 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class HTTP {
this._ky = ky
}

getBaseURL () {
return this._defaults.prefixUrl
}

setBaseURL (baseURL) {
this._defaults.prefixUrl = baseURL
}
Expand Down
2 changes: 2 additions & 0 deletions test/fixture/pages/instance.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<div>
<div>prefixUrl:{{ defaults.prefixUrl }}</div>
<div>baseURL:{{ baseURL }}</div>
<div v-for="(value, key) in defaults.headers" :key="key">
{{ key }}:{{ value }}
</div>
Expand All @@ -11,6 +12,7 @@
export default {
asyncData ({ app: { $api } }) {
return {
baseURL: $api.getBaseURL(),
defaults: $api._defaults
}
}
Expand Down
1 change: 1 addition & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('module', () => {
const html = await fetch(url('/instance')).then(r => r.text())

expect(html).toContain('prefixUrl:https://jsonplaceholder.typicode.com/')
expect(html).toContain('baseURL:https://jsonplaceholder.typicode.com/')
expect(html).toContain('testing:oui')
})
})
6 changes: 6 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ interface NuxtHTTPInstance {
$delete<T = unknown>(url: Request | URL | string, options?: Options): Promise<T>;


/**
* Get the baseURL value.
* @returns string - the base URL value
*/
getBaseURL(): string

/**
* Set the baseURL for all subsequent requests.
* @param baseURL - the base URL (e.g. `https://myapi.com/`)
Expand Down

0 comments on commit 332bf34

Please sign in to comment.