Skip to content

Commit d3e2c08

Browse files
authored
feat: migrate to http and ky-universal
2 parents e388384 + 664e074 commit d3e2c08

37 files changed

Lines changed: 9194 additions & 1906 deletions

.github/ISSUE_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!--
22
IMPORTANT: Please use the following link to create a new issue:
33
4-
https://cmty.app/nuxt/issues/new?repo=axios-module
4+
https://cmty.app/nuxt/issues/new?repo=http-module
55
66
If your issue was not created using the app above, it will be closed immediately.
77
-->

README.md

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 📦 HTTP Module
1+
# HTTP Module
22

33
[![npm version][npm-version-src]][npm-version-href]
44
[![npm downloads][npm-downloads-src]][npm-downloads-href]
@@ -7,25 +7,7 @@
77
[![Dependencies][david-dm-src]][david-dm-href]
88
[![Standard JS][standard-js-src]][standard-js-href]
99

10-
TODO
11-
12-
## ✅ Features
13-
14-
✓ Automatically set base URL for client & server side
15-
16-
✓ Exposes `setToken` function to `$axios` so we can easily and globally set authentication tokens
17-
18-
✓ Automatically enables `withCredentials` when requesting to base URL
19-
20-
✓ Proxy request headers in SSR (Useful for auth)
21-
22-
✓ Integrated with Nuxt.js Progressbar while making requests (TODO)
23-
24-
✓ Integrated with [Proxy Module](https://github.com/nuxt-community/proxy-module)
25-
26-
✓ Auto retry requests
27-
28-
📖 [**Read Documentation**](https://http.nuxtjs.org) (TODO)
10+
📖 [**Read Documentation**](https://http.nuxtjs.org)
2911

3012
## Development
3113

babel.config.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

commitlint.config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

docs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vuepress/dist

docs/.vuepress/config.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module.exports = {
2+
title: 'HTTP Module',
3+
description: 'Universal HTTP Module for Nuxt',
4+
themeConfig: {
5+
repo: 'nuxt/http',
6+
docsDir: 'docs',
7+
editLinks: true,
8+
displayAllHeaders: true,
9+
sidebar: [
10+
{
11+
collapsable: false,
12+
children: [
13+
'/',
14+
'setup',
15+
'usage',
16+
'options',
17+
'advanced',
18+
'migration'
19+
]
20+
}
21+
],
22+
nav: [
23+
{
24+
text: 'Release Notes',
25+
link: 'https://github.com/nuxt/http/blob/dev/CHANGELOG.md'
26+
}
27+
]
28+
}
29+
}

docs/README.md

Lines changed: 0 additions & 30 deletions
This file was deleted.

docs/SUMMARY.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

docs/advanced.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Advanced
2+
3+
## Hooks
4+
5+
Sometimes we want to globally intercept HTTP request and responses.
6+
for example display a toast on error or log them or dynamically modify requests.
7+
8+
HTTP module provides helpers to register hooks for request lifecycle:
9+
10+
- `onRequest(config)`
11+
- `onResponse(response)`
12+
- `onError(err)` (`err.response` may be available on response errors)
13+
14+
These functions don't have to return anything by default.
15+
16+
### Register Hooks
17+
18+
For registering hooks, you have to create a nuxt plugin:
19+
20+
**nuxt.config.js**
21+
22+
```js
23+
{
24+
modules: [
25+
'@nuxt/http',
26+
],
27+
28+
plugins: [
29+
'~/plugins/http'
30+
]
31+
}
32+
```
33+
34+
**plugins/http.js**
35+
36+
```js
37+
export default function ({ $http }) {
38+
$http.onRequest(config => {
39+
console.log('Making request to ' + config.url)
40+
})
41+
42+
$http.onError(error => {
43+
if(error.response.status === 500) {
44+
alert('Request Error!')
45+
}
46+
})
47+
}
48+
```
49+
50+
## Header Helpers
51+
52+
### `setHeader(name, value)`
53+
54+
Globally set a header to all subsequent requests.
55+
56+
> NOTE: This method should not be called inside hooks as it is global
57+
58+
Parameters:
59+
60+
* **name**: Name of the header
61+
* **value**: Value of the header
62+
63+
```js
64+
// Add header `Authorization: 123` to all requests
65+
this.$http.setHeader('Authorization', '123')
66+
67+
// Override `Authorization` header with new value
68+
this.$http.setHeader('Authorization', '456')
69+
70+
// Add header `Content-Type: application/x-www-form-urlencoded`
71+
this.$http.setHeader('Content-Type', 'application/x-www-form-urlencoded')
72+
73+
// Remove default Content-Type header
74+
this.$http.setHeader('Content-Type', false)
75+
```
76+
77+
### `setToken(token, type)`
78+
79+
Globally set `Authorization` header to all subsequent requests.
80+
81+
Parameters:
82+
83+
* **token**: Authorization token
84+
* **type**: Authorization token prefix, usually `Bearer`. Defaults to nothing
85+
86+
```js
87+
// Adds header: `Authorization: 123` to all requests
88+
this.$http.setToken('123')
89+
90+
// Overrides `Authorization` header with new value
91+
this.$http.setToken('456')
92+
93+
// Adds header: `Authorization: Bearer 123` to all requests
94+
this.$http.setToken('123', 'Bearer')
95+
96+
// Adds header: `Authorization: Bearer 123` to only post and delete requests
97+
this.$http.setToken('123', 'Bearer', ['post', 'delete'])
98+
99+
// Removes default Authorization header
100+
this.$http.setToken(false)
101+
```

docs/extend.md

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)