This commit is contained in:
2022-07-18 02:50:52 +00:00
parent befd344ab0
commit 06181b34d6
8569 changed files with 818704 additions and 352705 deletions
+31 -31
View File
@@ -1,35 +1,32 @@
# 📦 Axios Module
[![@nuxtjs/axios](https://axios.nuxtjs.org/preview.png)](https://axios.nuxtjs.org)
# @nuxtjs/axios
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![Circle CI][circle-ci-src]][circle-ci-href]
[![Github Actions CI][github-actions-ci-src]][github-actions-ci-href]
[![Codecov][codecov-src]][codecov-href]
[![Dependencies][david-dm-src]][david-dm-href]
[![Standard JS][standard-js-src]][standard-js-href]
[![License][license-src]][license-href]
> Secure and Easy <a href="https://github.com/mzabriskie/axios">Axios</a> integration with Nuxt.js
> Secure and easy [Axios](https://github.com/axios/axios) integration for [Nuxt](https://nuxtjs.org).
## ✅ Features
- [✨ &nbsp;Release Notes](https://axios.nuxtjs.org/releases)
- [📖 &nbsp;Documentation](https://axios.nuxtjs.org)
✓ Automatically set base URL for client & server side
## Features
✓ Exposes `setToken` function to `$axios` so we can easily and globally set authentication tokens
- 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
- Fetch Style requests
- Integrated with Nuxt progress bar
- Integrated with Proxy Module
- Auto retry requests with axios-retry
✓ Automatically enables `withCredentials` when requesting to base URL
[📖 &nbsp;Read more](https://axios.nuxtjs.org)
✓ Proxy request headers in SSR (Useful for auth)
✓ Fetch Style requests
✓ Integrated with Nuxt.js Progressbar while making requests
✓ Integrated with [Proxy Module](https://github.com/nuxt-community/proxy-module)
✓ Auto retry requests with [axios-retry](https://github.com/softonic/axios-retry)
📖 [**Read Documentation**](https://axios.nuxtjs.org)
## Development
## Contributing
1. Clone this repository
2. Install dependencies using `yarn install` or `npm install`
@@ -42,15 +39,18 @@
Copyright (c) Nuxt Community
<!-- Badges -->
[npm-version-src]: https://img.shields.io/npm/dt/@nuxtjs/axios.svg?style=flat-square
<!-- Badges -->
[npm-version-src]: https://flat.badgen.net/npm/v/@nuxtjs/axios
[npm-version-href]: https://npmjs.com/package/@nuxtjs/axios
[npm-downloads-src]: https://img.shields.io/npm/v/@nuxtjs/axios/latest.svg?style=flat-square
[npm-downloads-src]: https://flat.badgen.net/npm/dm/@nuxtjs/axios
[npm-downloads-href]: https://npmjs.com/package/@nuxtjs/axios
[circle-ci-src]: https://img.shields.io/circleci/project/github/nuxt-community/axios-module.svg?style=flat-square
[circle-ci-href]: https://circleci.com/gh/nuxt-community/axios-module
[codecov-src]: https://img.shields.io/codecov/c/github/nuxt-community/axios-module.svg?style=flat-square
[github-actions-ci-src]: https://github.com/nuxt-community/axios-module/workflows/ci/badge.svg
[github-actions-ci-href]: https://github.com/nuxt-community/axios-module/actions?query=workflow%3Aci
[codecov-src]: https://flat.badgen.net/codecov/c/github/nuxt-community/axios-module
[codecov-href]: https://codecov.io/gh/nuxt-community/axios-module
[david-dm-src]: https://david-dm.org/nuxt-community/axios-module/status.svg?style=flat-square
[david-dm-href]: https://david-dm.org/nuxt-community/axios-module
[standard-js-src]: https://img.shields.io/badge/code_style-standard-brightgreen.svg?style=flat-square
[standard-js-href]: https://standardjs.com
[license-src]: https://img.shields.io/npm/l/@nuxtjs/axios.svg
[license-href]: https://npmjs.com/package/@nuxtjs/axios
+27 -1
View File
@@ -37,6 +37,11 @@ function axiosModule (_moduleOptions) {
defaultHost = 'localhost'
}
// Transpile defu (IE11)
if (nuxt.options.build.transpile /* nuxt 1 */) {
nuxt.options.build.transpile.push(({ isClient }) => isClient && 'defu')
}
// Default prefix
const prefix = process.env.API_PREFIX || moduleOptions.prefix || '/'
@@ -56,6 +61,16 @@ function axiosModule (_moduleOptions) {
patch: {}
}
// Support baseUrl alternative
if (moduleOptions.baseUrl) {
moduleOptions.baseURL = moduleOptions.baseUrl
delete moduleOptions.baseUrl
}
if (moduleOptions.browserBaseUrl) {
moduleOptions.browserBaseURL = moduleOptions.browserBaseUrl
delete moduleOptions.browserBaseUrl
}
// Apply defaults
const options = defu(moduleOptions, {
baseURL: `http://${defaultHost}:${defaultPort}${prefix}`,
@@ -64,7 +79,18 @@ function axiosModule (_moduleOptions) {
debug: false,
progress: true,
proxyHeaders: true,
proxyHeadersIgnore: ['accept', 'host', 'cf-ray', 'cf-connecting-ip', 'content-length', 'content-md5', 'content-type'],
proxyHeadersIgnore: [
'accept',
'cf-connecting-ip',
'cf-ray',
'content-length',
'content-md5',
'content-type',
'host',
'x-forwarded-host',
'x-forwarded-port',
'x-forwarded-proto'
],
proxy: false,
retry: false,
https,
+19 -10
View File
@@ -8,10 +8,10 @@ const axiosExtra = {
this.defaults.baseURL = baseURL
},
setHeader (name, value, scopes = 'common') {
for (let scope of Array.isArray(scopes) ? scopes : [ scopes ]) {
for (const scope of Array.isArray(scopes) ? scopes : [ scopes ]) {
if (!value) {
delete this.defaults.headers[scope][name];
return
continue
}
this.defaults.headers[scope][name] = value
}
@@ -42,12 +42,12 @@ const axiosExtra = {
}
// Request helpers ($get, $post, ...)
for (let method of ['request', 'delete', 'get', 'head', 'options', 'post', 'put', 'patch']) {
for (const method of ['request', 'delete', 'get', 'head', 'options', 'post', 'put', 'patch']) {
axiosExtra['$' + method] = function () { return this[method].apply(this, arguments).then(res => res && res.data) }
}
const extendAxiosInstance = axios => {
for (let key in axiosExtra) {
for (const key in axiosExtra) {
axios[key] = axiosExtra[key].bind(axios)
}
}
@@ -61,11 +61,16 @@ const createAxiosInstance = axiosOptions => {
// Extend axios proto
extendAxiosInstance(axios)
// Intercept to apply default headers
axios.onRequest((config) => {
config.headers = { ...axios.defaults.headers.common, ...config.headers }
})
// Setup interceptors
<% if (options.debug) { %>setupDebugInterceptor(axios) <% } %>
<% if (options.credentials) { %>setupCredentialsInterceptor(axios)<% } %>
<% if (options.credentials) { %>setupCredentialsInterceptor(axios) <% } %>
<% if (options.progress) { %>setupProgress(axios) <% } %>
<% if (options.retry) { %>axiosRetry(axios, <%= serialize(options.retry) %>)<% } %>
<% if (options.retry) { %>axiosRetry(axios, <%= serialize(options.retry) %>) <% } %>
return axios
}
@@ -161,6 +166,10 @@ const setupProgress = (axios) => {
currentRequests--
if (Axios.isCancel(error)) {
if (currentRequests <= 0) {
currentRequests = 0
$loading().finish()
}
return
}
@@ -169,7 +178,7 @@ const setupProgress = (axios) => {
})
const onProgress = e => {
if (!currentRequests) {
if (!currentRequests || !e.total) {
return
}
const progress = ((e.loaded * 100) / (e.total * currentRequests))
@@ -185,8 +194,8 @@ export default (ctx, inject) => {
const runtimeConfig = ctx.$config && ctx.$config.axios || {}
// baseURL
const baseURL = process.browser
? (runtimeConfig.browserBaseURL || runtimeConfig.baseURL || '<%= options.browserBaseURL || '' %>')
: (runtimeConfig.baseURL || process.env._AXIOS_BASE_URL_ || '<%= options.baseURL || '' %>')
? (runtimeConfig.browserBaseURL || runtimeConfig.browserBaseUrl || runtimeConfig.baseURL || runtimeConfig.baseUrl || '<%= options.browserBaseURL || '' %>')
: (runtimeConfig.baseURL || runtimeConfig.baseUrl || process.env._AXIOS_BASE_URL_ || '<%= options.baseURL || '' %>')
// Create fresh objects for all default header scopes
// Axios creates only one which is shared across SSR requests!
@@ -202,7 +211,7 @@ export default (ctx, inject) => {
// Proxy SSR request headers headers
if (process.server && ctx.req && ctx.req.headers) {
const reqHeaders = { ...ctx.req.headers }
for (let h of <%= serialize(options.proxyHeadersIgnore) %>) {
for (const h of <%= serialize(options.proxyHeadersIgnore) %>) {
delete reqHeaders[h]
}
axiosOptions.headers.common = { ...reqHeaders, ...axiosOptions.headers.common }
+18 -31
View File
@@ -1,33 +1,33 @@
{
"_args": [
[
"@nuxtjs/axios@5.12.0",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
"@nuxtjs/axios@5.13.6",
"/home/node/nuxt"
]
],
"_from": "@nuxtjs/axios@5.12.0",
"_id": "@nuxtjs/axios@5.12.0",
"_from": "@nuxtjs/axios@5.13.6",
"_id": "@nuxtjs/axios@5.13.6",
"_inBundle": false,
"_integrity": "sha512-VQI9Nnf12jWknldrgCNGzCQxnWO3/CvMwrkWKNUr3WtGYuOKryUOd1XXxDbaJmopfX4SGjKvDL1G6qTkWLiPew==",
"_integrity": "sha512-XS+pOE0xsDODs1zAIbo95A0LKlilvJi8YW0NoXYuq3/jjxGgWDxizZ6Yx0AIIjZOoGsXJOPc0/BcnSEUQ2mFBA==",
"_location": "/@nuxtjs/axios",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "@nuxtjs/axios@5.12.0",
"raw": "@nuxtjs/axios@5.13.6",
"name": "@nuxtjs/axios",
"escapedName": "@nuxtjs%2faxios",
"scope": "@nuxtjs",
"rawSpec": "5.12.0",
"rawSpec": "5.13.6",
"saveSpec": null,
"fetchSpec": "5.12.0"
"fetchSpec": "5.13.6"
},
"_requiredBy": [
"/"
],
"_resolved": "https://registry.npmjs.org/@nuxtjs/axios/-/axios-5.12.0.tgz",
"_spec": "5.12.0",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"_resolved": "https://registry.npmjs.org/@nuxtjs/axios/-/axios-5.13.6.tgz",
"_spec": "5.13.6",
"_where": "/home/node/nuxt",
"bugs": {
"url": "https://github.com/nuxt-community/axios-module/issues"
},
@@ -38,31 +38,21 @@
}
],
"dependencies": {
"@nuxtjs/proxy": "^2.0.0",
"axios": "^0.19.2",
"axios-retry": "^3.1.8",
"consola": "^2.14.0",
"defu": "^2.0.4"
"@nuxtjs/proxy": "^2.1.0",
"axios": "^0.21.1",
"axios-retry": "^3.1.9",
"consola": "^2.15.3",
"defu": "^5.0.0"
},
"description": "Secure and easy axios integration with Nuxt.js",
"description": "Secure and easy Axios integration with Nuxt.js",
"devDependencies": {
"@babel/core": "latest",
"@babel/preset-env": "latest",
"@commitlint/cli": "latest",
"@commitlint/config-conventional": "latest",
"@nuxtjs/eslint-config": "latest",
"babel-eslint": "latest",
"babel-jest": "latest",
"codecov": "latest",
"eslint": "latest",
"eslint-config-standard": "latest",
"eslint-plugin-import": "latest",
"eslint-plugin-jest": "latest",
"eslint-plugin-node": "latest",
"eslint-plugin-promise": "latest",
"eslint-plugin-standard": "latest",
"eslint-plugin-vue": "latest",
"husky": "latest",
"jest": "latest",
"nuxt-edge": "latest",
"standard-version": "latest"
@@ -75,9 +65,6 @@
"license": "MIT",
"main": "lib/module.js",
"name": "@nuxtjs/axios",
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nuxt-community/axios-module.git"
@@ -89,5 +76,5 @@
"test": "yarn lint && jest"
},
"types": "types/index.d.ts",
"version": "5.12.0"
"version": "5.13.6"
}
+14 -6
View File
@@ -17,11 +17,13 @@ interface NuxtAxiosInstance extends AxiosStatic {
setHeader(name: string, value?: string | false, scopes?: string | string[]): void
setToken(token: string | false, type?: string, scopes?: string | string[]): void
onRequest(callback: (config: AxiosRequestConfig) => void): void
onResponse<T = any>(callback: (response: AxiosResponse<T>) => void): void
onError(callback: (error: AxiosError) => void): void
onRequestError(callback: (error: AxiosError) => void): void
onResponseError(callback: (error: AxiosError) => void): void
onRequest(callback: (config: AxiosRequestConfig) => void | AxiosRequestConfig | Promise<AxiosRequestConfig>): void
onResponse<T = any>(callback: (response: AxiosResponse<T>) => void | AxiosResponse<T> | Promise<AxiosResponse<T>> ): void
onError(callback: (error: AxiosError) => any): void
onRequestError(callback: (error: AxiosError) => any): void
onResponseError(callback: (error: AxiosError) => any): void
create(options?: AxiosRequestConfig): NuxtAxiosInstance
}
interface AxiosOptions {
@@ -29,7 +31,7 @@ interface AxiosOptions {
browserBaseURL?: string,
credentials?: boolean,
debug?: boolean,
host?: string,
host?: string,
prefix?: string,
progress?: boolean,
proxyHeaders?: boolean,
@@ -49,6 +51,12 @@ interface AxiosOptions {
},
}
declare module 'axios' {
interface AxiosRequestConfig {
progress?: boolean;
}
}
declare module '@nuxt/vue-app' {
interface Context {
$axios: NuxtAxiosInstance