22 * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
33 * SPDX-License-Identifier: AGPL-3.0-or-later
44 */
5- import axios from '@nextcloud/axios'
6- import { generateUrl } from '@nextcloud/router'
7-
8- export class ConnectionClosedError extends Error {
9- constructor (
10- message = 'Close has already been called on the connection' ,
11- ...rest
12- ) {
13- super ( message , ...rest )
14- }
15- }
165
176export class SessionConnection {
187 #content
@@ -67,25 +56,6 @@ export class SessionConnection {
6756 return this . #hasOwner
6857 }
6958
70- get #defaultParams( ) {
71- return {
72- documentId : this . #document. id ,
73- sessionId : this . #session. id ,
74- sessionToken : this . #session. token ,
75- token : this . connection . shareToken ,
76- }
77- }
78-
79- // TODO: maybe return a new connection here so connections have immutable state
80- update ( guestName ) {
81- return this . #post( this . #url( `session/${ this . #document. id } /session` ) , {
82- ...this . #defaultParams,
83- guestName,
84- } ) . then ( ( { data } ) => {
85- this . #session = data
86- } )
87- }
88-
8959 close ( ) {
9060 this . closed = true
9161 }
@@ -94,29 +64,4 @@ export class SessionConnection {
9464 setBaseVersionEtag ( baseVersionEtag ) {
9565 this . #document. baseVersionEtag = baseVersionEtag
9666 }
97-
98- #post( ...args ) {
99- if ( this . closed ) {
100- return Promise . reject ( new ConnectionClosedError ( ) )
101- }
102- return axios . post ( ...args )
103- }
104-
105- #url( endpoint ) {
106- const isPublic = ! ! this . #defaultParams. token
107- return _endpointUrl ( endpoint , isPublic )
108- }
109- }
110-
111- /**
112- *
113- * @param {string } endpoint - endpoint of the url inside apps/text
114- * @param {boolean } isPublic - public url or not
115- */
116- function _endpointUrl ( endpoint , isPublic = false ) {
117- const _baseUrl = generateUrl ( '/apps/text' )
118- if ( isPublic ) {
119- return `${ _baseUrl } /public/${ endpoint } `
120- }
121- return `${ _baseUrl } /${ endpoint } `
12267}
0 commit comments