mirror of
https://github.com/nextcloud/server.git
synced 2026-06-09 08:44:07 -04:00
refactor(dav): migrate services to Typescript
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
This commit is contained in:
parent
502ab0e90b
commit
f82269d0d1
6 changed files with 62 additions and 60 deletions
|
|
@ -1,31 +0,0 @@
|
|||
/**
|
||||
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
import { getCurrentUser, getRequestToken, onRequestTokenUpdate } from '@nextcloud/auth'
|
||||
import { generateRemoteUrl } from '@nextcloud/router'
|
||||
import memoize from 'lodash/fp/memoize.js'
|
||||
import { createClient } from 'webdav'
|
||||
|
||||
export const getClient = memoize((service) => {
|
||||
// init webdav client
|
||||
const remote = generateRemoteUrl(`dav/${service}/${getCurrentUser().uid}`)
|
||||
const client = createClient(remote)
|
||||
|
||||
// set CSRF token header
|
||||
const setHeaders = (token) => {
|
||||
client.setHeaders({
|
||||
// Add this so the server knows it is an request from the browser
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
// Inject user auth
|
||||
requesttoken: token ?? '',
|
||||
})
|
||||
}
|
||||
|
||||
// refresh headers when request token changes
|
||||
onRequestTokenUpdate(setHeaders)
|
||||
setHeaders(getRequestToken())
|
||||
|
||||
return client
|
||||
})
|
||||
39
apps/dav/src/dav/client.ts
Normal file
39
apps/dav/src/dav/client.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
import type { WebDAVClient } from 'webdav'
|
||||
|
||||
import { getCurrentUser, getRequestToken, onRequestTokenUpdate } from '@nextcloud/auth'
|
||||
import { generateRemoteUrl } from '@nextcloud/router'
|
||||
import { createClient } from 'webdav'
|
||||
|
||||
let client: WebDAVClient | undefined = undefined
|
||||
|
||||
/**
|
||||
* Get the WebDAV client for the current user on the calendars endpoint.
|
||||
*/
|
||||
export function getClient(): WebDAVClient {
|
||||
if (!client) {
|
||||
// init webdav client
|
||||
const remote = generateRemoteUrl(`dav/calendars/${getCurrentUser()!.uid}`)
|
||||
client = createClient(remote)
|
||||
|
||||
// set CSRF token header
|
||||
const setHeaders = (token) => {
|
||||
client!.setHeaders({
|
||||
// Add this so the server knows it is an request from the browser
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
// Inject user auth
|
||||
requesttoken: token ?? '',
|
||||
})
|
||||
}
|
||||
|
||||
// refresh headers when request token changes
|
||||
onRequestTokenUpdate(setHeaders)
|
||||
setHeaders(getRequestToken())
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
|
@ -8,11 +8,11 @@ import {
|
|||
vavailabilityToSlots,
|
||||
} from '@nextcloud/calendar-availability-vue'
|
||||
import { parseXML } from 'webdav'
|
||||
import { getClient } from '../dav/client.js'
|
||||
import { getClient } from '../dav/client.ts'
|
||||
import { logger } from './logger.ts'
|
||||
|
||||
/**
|
||||
*
|
||||
* Get an object representing empty time slots for each day of the week.
|
||||
*/
|
||||
export function getEmptySlots() {
|
||||
return {
|
||||
|
|
@ -27,12 +27,10 @@ export function getEmptySlots() {
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Find the availability of the schedule inbox.
|
||||
*/
|
||||
export async function findScheduleInboxAvailability() {
|
||||
const client = getClient('calendars')
|
||||
|
||||
const response = await client.customRequest('inbox', {
|
||||
const response = await getClient().customRequest('inbox', {
|
||||
method: 'PROPFIND',
|
||||
data: `<?xml version="1.0"?>
|
||||
<x0:propfind xmlns:x0="DAV:">
|
||||
|
|
@ -57,8 +55,10 @@ export async function findScheduleInboxAvailability() {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param {any} slots -
|
||||
* @param {any} timezoneId -
|
||||
* Save the availability of the schedule inbox.
|
||||
*
|
||||
* @param slots - The availability slots to save.
|
||||
* @param timezoneId - The timezone identifier.
|
||||
*/
|
||||
export async function saveScheduleInboxAvailability(slots, timezoneId) {
|
||||
const all = [...Object.keys(slots).flatMap((dayId) => slots[dayId].map((slot) => ({
|
||||
|
|
@ -72,8 +72,7 @@ export async function saveScheduleInboxAvailability(slots, timezoneId) {
|
|||
vavailability,
|
||||
})
|
||||
|
||||
const client = getClient('calendars')
|
||||
await client.customRequest('inbox', {
|
||||
await getClient().customRequest('inbox', {
|
||||
method: 'PROPPATCH',
|
||||
data: `<?xml version="1.0"?>
|
||||
<x0:propertyupdate xmlns:x0="DAV:">
|
||||
|
|
@ -9,10 +9,9 @@ import { generateUrl } from '@nextcloud/router'
|
|||
/**
|
||||
* Configure the creation of example events on a user's first login.
|
||||
*
|
||||
* @param {boolean} enable Whether to enable or disable the feature.
|
||||
* @return {Promise<void>}
|
||||
* @param enable - Whether to enable or disable the feature.
|
||||
*/
|
||||
export async function setCreateExampleEvent(enable) {
|
||||
export async function setCreateExampleEvent(enable: boolean): Promise<void> {
|
||||
const url = generateUrl('/apps/dav/api/exampleEvent/enable')
|
||||
await axios.post(url, {
|
||||
enable,
|
||||
|
|
@ -22,10 +21,9 @@ export async function setCreateExampleEvent(enable) {
|
|||
/**
|
||||
* Upload a custom example event.
|
||||
*
|
||||
* @param {string} ics The ICS data of the event.
|
||||
* @return {Promise<void>}
|
||||
* @param ics - The ICS data of the event.
|
||||
*/
|
||||
export async function uploadExampleEvent(ics) {
|
||||
export async function uploadExampleEvent(ics: string): Promise<void> {
|
||||
const url = generateUrl('/apps/dav/api/exampleEvent/event')
|
||||
await axios.post(url, {
|
||||
ics,
|
||||
|
|
@ -34,10 +32,8 @@ export async function uploadExampleEvent(ics) {
|
|||
|
||||
/**
|
||||
* Delete a previously uploaded custom example event.
|
||||
*
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
export async function deleteExampleEvent() {
|
||||
export async function deleteExampleEvent(): Promise<void> {
|
||||
const url = generateUrl('/apps/dav/api/exampleEvent/event')
|
||||
await axios.delete(url)
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
/**
|
||||
/*!
|
||||
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
|
@ -9,8 +9,8 @@ import { generateOcsUrl } from '@nextcloud/router'
|
|||
/**
|
||||
* Enable user status automation based on availability
|
||||
*/
|
||||
export async function enableUserStatusAutomation() {
|
||||
return await axios.post(
|
||||
export async function enableUserStatusAutomation(): Promise<void> {
|
||||
await axios.post(
|
||||
generateOcsUrl('/apps/provisioning_api/api/v1/config/users/{appId}/{configKey}', {
|
||||
appId: 'dav',
|
||||
configKey: 'user_status_automation',
|
||||
|
|
@ -24,8 +24,8 @@ export async function enableUserStatusAutomation() {
|
|||
/**
|
||||
* Disable user status automation based on availability
|
||||
*/
|
||||
export async function disableUserStatusAutomation() {
|
||||
return await axios.delete(generateOcsUrl('/apps/provisioning_api/api/v1/config/users/{appId}/{configKey}', {
|
||||
export async function disableUserStatusAutomation(): Promise<void> {
|
||||
await axios.delete(generateOcsUrl('/apps/provisioning_api/api/v1/config/users/{appId}/{configKey}', {
|
||||
appId: 'dav',
|
||||
configKey: 'user_status_automation',
|
||||
}))
|
||||
|
|
@ -6,12 +6,11 @@
|
|||
/**
|
||||
* Format a date as 'YYYY-MM-DD'.
|
||||
*
|
||||
* @param {Date} date A date instance to format.
|
||||
* @return {string} 'YYYY-MM-DD'
|
||||
* @param date - A date instance to format.
|
||||
*/
|
||||
export function formatDateAsYMD(date) {
|
||||
export function formatDateAsYMD(date: Date): `${number}-${number}-${number}` {
|
||||
const year = date.getFullYear()
|
||||
const month = (date.getMonth() + 1).toString().padStart(2, '0')
|
||||
const day = date.getDate().toString().padStart(2, '0')
|
||||
const month = (date.getMonth() + 1).toString().padStart(2, '0') as `${number}`
|
||||
const day = date.getDate().toString().padStart(2, '0') as `${number}`
|
||||
return `${year}-${month}-${day}`
|
||||
}
|
||||
Loading…
Reference in a new issue