@@ -117,3 +117,93 @@ export async function getDownloadCountForSpacePerVersion(spaceId: string): Promi
117117 return ( await response . json ( ) ) . versions as Record < string , number > ;
118118}
119119
120+ export async function createSpace ( slug : string , title : string , summary : string , description : string , categories : string [ ] , iconURL : string ) : Promise < Space > {
121+ const userStore = useUserStore ( ) ;
122+ if ( ! userStore . isAuthenticated ) {
123+ throw new Error ( "User is not logged in" ) ;
124+ }
125+
126+ const response = await fetch (
127+ `/api/v1/spaces` ,
128+ {
129+ method : "POST" ,
130+ headers : {
131+ "Content-Type" : "application/json" ,
132+ "Accept" : "application/json" ,
133+ "Authorization" : `Bearer ${ userStore . token } ` ,
134+ } ,
135+ body : JSON . stringify ( {
136+ slug : slug ,
137+ title : title ,
138+ summary : summary ,
139+ description : description ,
140+ categories : categories ,
141+ icon_url : iconURL ,
142+ } ) ,
143+ } ,
144+ ) ;
145+
146+ if ( ! response . ok ) {
147+ throw new Error ( "Failed to create space: " + await response . text ( ) ) ;
148+ }
149+
150+ const space = await response . json ( ) ;
151+ space . created_at = new Date ( space . created_at ) ;
152+
153+ return space as Space ;
154+ }
155+
156+ export async function updateSpace ( spaceID : string , slug : string , title : string , summary : string , description : string , categories : string [ ] , iconURL : string ) : Promise < void > {
157+ const userStore = useUserStore ( ) ;
158+ if ( ! userStore . isAuthenticated ) {
159+ throw new Error ( "User is not logged in" ) ;
160+ }
161+
162+ const response = await fetch (
163+ `/api/v1/spaces/${ spaceID } ` ,
164+ {
165+ method : "PUT" ,
166+ headers : {
167+ "Content-Type" : "application/json" ,
168+ "Authorization" : `Bearer ${ userStore . token } ` ,
169+ } ,
170+ body : JSON . stringify ( {
171+ slug : slug ,
172+ title : title ,
173+ summary : summary ,
174+ description : description ,
175+ categories : categories ,
176+ icon_url : iconURL ,
177+ } ) ,
178+ } ,
179+ ) ;
180+
181+ if ( ! response . ok ) {
182+ throw new Error ( "Failed to update space: " + await response . text ( ) ) ;
183+ }
184+ }
185+
186+ export async function changeSpaceStatus ( spaceID : string , toStatus : string ) : Promise < void > {
187+ const userStore = useUserStore ( ) ;
188+ if ( ! userStore . isAuthenticated ) {
189+ throw new Error ( "User is not logged in" ) ;
190+ }
191+
192+ const response = await fetch (
193+ `/api/v1/spaces/${ spaceID } /status` ,
194+ {
195+ method : "POST" ,
196+ headers : {
197+ "Content-Type" : "application/json" ,
198+ "Authorization" : `Bearer ${ userStore . token } ` ,
199+ } ,
200+ body : JSON . stringify ( {
201+ to : toStatus
202+ } ) ,
203+ } ,
204+ ) ;
205+
206+ if ( ! response . ok ) {
207+ throw new Error ( "Failed to change space status: " + await response . text ( ) ) ;
208+ }
209+ }
0 commit comments