-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate to http and ky-universal
- Loading branch information
Showing
37 changed files
with
9,194 additions
and
1,906 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<!-- | ||
IMPORTANT: Please use the following link to create a new issue: | ||
https://cmty.app/nuxt/issues/new?repo=axios-module | ||
https://cmty.app/nuxt/issues/new?repo=http-module | ||
If your issue was not created using the app above, it will be closed immediately. | ||
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.vuepress/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module.exports = { | ||
title: 'HTTP Module', | ||
description: 'Universal HTTP Module for Nuxt', | ||
themeConfig: { | ||
repo: 'nuxt/http', | ||
docsDir: 'docs', | ||
editLinks: true, | ||
displayAllHeaders: true, | ||
sidebar: [ | ||
{ | ||
collapsable: false, | ||
children: [ | ||
'/', | ||
'setup', | ||
'usage', | ||
'options', | ||
'advanced', | ||
'migration' | ||
] | ||
} | ||
], | ||
nav: [ | ||
{ | ||
text: 'Release Notes', | ||
link: 'https://github.com/nuxt/http/blob/dev/CHANGELOG.md' | ||
} | ||
] | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Advanced | ||
|
||
## Hooks | ||
|
||
Sometimes we want to globally intercept HTTP request and responses. | ||
for example display a toast on error or log them or dynamically modify requests. | ||
|
||
HTTP module provides helpers to register hooks for request lifecycle: | ||
|
||
- `onRequest(config)` | ||
- `onResponse(response)` | ||
- `onError(err)` (`err.response` may be available on response errors) | ||
|
||
These functions don't have to return anything by default. | ||
|
||
### Register Hooks | ||
|
||
For registering hooks, you have to create a nuxt plugin: | ||
|
||
**nuxt.config.js** | ||
|
||
```js | ||
{ | ||
modules: [ | ||
'@nuxt/http', | ||
], | ||
|
||
plugins: [ | ||
'~/plugins/http' | ||
] | ||
} | ||
``` | ||
|
||
**plugins/http.js** | ||
|
||
```js | ||
export default function ({ $http }) { | ||
$http.onRequest(config => { | ||
console.log('Making request to ' + config.url) | ||
}) | ||
|
||
$http.onError(error => { | ||
if(error.response.status === 500) { | ||
alert('Request Error!') | ||
} | ||
}) | ||
} | ||
``` | ||
|
||
## Header Helpers | ||
|
||
### `setHeader(name, value)` | ||
|
||
Globally set a header to all subsequent requests. | ||
|
||
> NOTE: This method should not be called inside hooks as it is global | ||
Parameters: | ||
|
||
* **name**: Name of the header | ||
* **value**: Value of the header | ||
|
||
```js | ||
// Add header `Authorization: 123` to all requests | ||
this.$http.setHeader('Authorization', '123') | ||
|
||
// Override `Authorization` header with new value | ||
this.$http.setHeader('Authorization', '456') | ||
|
||
// Add header `Content-Type: application/x-www-form-urlencoded` | ||
this.$http.setHeader('Content-Type', 'application/x-www-form-urlencoded') | ||
|
||
// Remove default Content-Type header | ||
this.$http.setHeader('Content-Type', false) | ||
``` | ||
|
||
### `setToken(token, type)` | ||
|
||
Globally set `Authorization` header to all subsequent requests. | ||
|
||
Parameters: | ||
|
||
* **token**: Authorization token | ||
* **type**: Authorization token prefix, usually `Bearer`. Defaults to nothing | ||
|
||
```js | ||
// Adds header: `Authorization: 123` to all requests | ||
this.$http.setToken('123') | ||
|
||
// Overrides `Authorization` header with new value | ||
this.$http.setToken('456') | ||
|
||
// Adds header: `Authorization: Bearer 123` to all requests | ||
this.$http.setToken('123', 'Bearer') | ||
|
||
// Adds header: `Authorization: Bearer 123` to only post and delete requests | ||
this.$http.setToken('123', 'Bearer', ['post', 'delete']) | ||
|
||
// Removes default Authorization header | ||
this.$http.setToken(false) | ||
``` |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.