Skip to content

Commit

Permalink
feat: migrate to http and ky-universal
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Apr 9, 2019
2 parents e388384 + 664e074 commit d3e2c08
Show file tree
Hide file tree
Showing 37 changed files with 9,194 additions and 1,906 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE.md
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.
-->
22 changes: 2 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 📦 HTTP Module
# HTTP Module

[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
Expand All @@ -7,25 +7,7 @@
[![Dependencies][david-dm-src]][david-dm-href]
[![Standard JS][standard-js-src]][standard-js-href]

TODO

## ✅ Features

✓ Automatically set base URL for client & server side

✓ Exposes `setToken` function to `$axios` so we can easily and globally set authentication tokens

✓ Automatically enables `withCredentials` when requesting to base URL

✓ Proxy request headers in SSR (Useful for auth)

✓ Integrated with Nuxt.js Progressbar while making requests (TODO)

✓ Integrated with [Proxy Module](https://github.com/nuxt-community/proxy-module)

✓ Auto retry requests

📖 [**Read Documentation**](https://http.nuxtjs.org) (TODO)
📖 [**Read Documentation**](https://http.nuxtjs.org)

## Development

Expand Down
11 changes: 0 additions & 11 deletions babel.config.js

This file was deleted.

5 changes: 0 additions & 5 deletions commitlint.config.js

This file was deleted.

1 change: 1 addition & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vuepress/dist
29 changes: 29 additions & 0 deletions docs/.vuepress/config.js
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'
}
]
}
}
30 changes: 0 additions & 30 deletions docs/README.md

This file was deleted.

9 changes: 0 additions & 9 deletions docs/SUMMARY.md

This file was deleted.

101 changes: 101 additions & 0 deletions docs/advanced.md
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)
```
34 changes: 0 additions & 34 deletions docs/extend.md

This file was deleted.

96 changes: 0 additions & 96 deletions docs/helpers.md

This file was deleted.

Loading

0 comments on commit d3e2c08

Please sign in to comment.