Skip to content

nextcloud-libraries/nextcloud-axios

Folders and files

NameName
Last commit message
Last commit date
Jul 8, 2024
Jun 17, 2024
Oct 19, 2024
Jun 17, 2024
Jun 27, 2023
Jun 17, 2024
Jun 17, 2024
Jun 17, 2024
Sep 21, 2018
Jun 17, 2024
Jul 8, 2024
Dec 7, 2024
Dec 7, 2024
Apr 20, 2023
Jun 17, 2024
Jun 17, 2024

Repository files navigation

@nextcloud/axios

REUSE status Build Status npm code coverage

Simple, typed wrapper of an Axios instance for Nextcloud that automatically sends authentication headers. Cancellation is supported as well.

Installation

npm install @nextcloud/axios --save
yarn add @nextcloud/axios

Usage

import axios from '@nextcloud/axios'

axios.get('nextcloud.com')

See https://github.com/axios/axios for details.

Defining baseURL

You are able to define baseURL to simplify the usage of axios across your app.

import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'

const baseURL = generateUrl('/apps/your_app_id/api')

axios.defaults.baseURL = baseURL

Retry handling

This package can optionally retry requests if they fail due to Nextcloud's maintenance mode. To activate this feature, pass retryIfMaintenanceMode: true into the request options. This mechanism will only catch relatively short server maintenance downtime in the range of seconds to a minute. Any longer downtime still has to be handled by the application, show feedback to the user, reload the page etc.

import axios from '@nextcloud/axios'

const pizzas = await axios.get('/apps/pizza/api/pizzas', {
    retryIfMaintenanceMode: true,
})
const myPizza = await axios.post('/apps/pizza/api/pizzas', { toppings: ['pineapple'] }, {
    retryIfMaintenanceMode: true,
})

Expired sessions handling

This package can optionally trigger a page reload whenever a request fails due to an expired user session. This interrupts application logic and should be the last resort. If possible, handle the expired session higher up in the application.

import axios from '@nextcloud/axios'

const response = await axios.get('/apps/foo/api/bar', {
    reloadExpiredSession: true,
})

References