@@ -15,6 +15,7 @@ import {
15
15
OFSGetPropertiesParams ,
16
16
} from "./model" ;
17
17
18
+ export * from "./model" ;
18
19
export class OFS {
19
20
private _credentials ! : OFSCredentials ;
20
21
private _hash ! : string ;
@@ -57,7 +58,8 @@ export class OFS {
57
58
58
59
private _get (
59
60
partialURL : string ,
60
- params : any = undefined
61
+ params : any = undefined ,
62
+ extraHeaders : Headers = new Headers ( )
61
63
) : Promise < OFSResponse > {
62
64
var theURL = new URL ( partialURL , this . _baseURL ) ;
63
65
if ( params != undefined ) {
@@ -66,6 +68,10 @@ export class OFS {
66
68
}
67
69
var myHeaders = new Headers ( ) ;
68
70
myHeaders . append ( "Authorization" , this . authorization ) ;
71
+ extraHeaders . forEach ( ( value , key ) => {
72
+ console . log ( key , value ) ;
73
+ myHeaders . append ( key , value ) ;
74
+ } ) ;
69
75
var requestOptions = {
70
76
method : "GET" ,
71
77
headers : myHeaders ,
@@ -74,12 +80,24 @@ export class OFS {
74
80
. then ( async function ( response ) {
75
81
// Your code for handling the data you get from the API
76
82
if ( response . status < 400 ) {
77
- var data = await response . json ( ) ;
83
+ var data ;
84
+ if (
85
+ response . headers . get ( "Content-Type" ) ?. includes ( "json" )
86
+ ) {
87
+ data = await response . json ( ) ;
88
+ } else if (
89
+ response . headers . get ( "Content-Type" ) ?. includes ( "text" )
90
+ ) {
91
+ data = await response . text ( ) ;
92
+ } else {
93
+ data = await response . blob ( ) ;
94
+ }
78
95
return new OFSResponse (
79
96
theURL ,
80
97
response . status ,
81
98
undefined ,
82
- data
99
+ data ,
100
+ response . headers . get ( "Content-Type" ) || undefined
83
101
) ;
84
102
} else {
85
103
return new OFSResponse (
@@ -134,27 +152,51 @@ export class OFS {
134
152
return fetchPromise ;
135
153
}
136
154
137
- private _put ( partialURL : string , requestData : any ) : Promise < OFSResponse > {
155
+ private _put (
156
+ partialURL : string ,
157
+ requestData : any ,
158
+ contentType : string = "application/json" ,
159
+ fileName ?: string
160
+ ) : Promise < OFSResponse > {
138
161
var theURL = new URL ( partialURL , this . _baseURL ) ;
139
162
var myHeaders = new Headers ( ) ;
140
163
myHeaders . append ( "Authorization" , this . authorization ) ;
141
- myHeaders . append ( "Content-Type" , "application/json" ) ;
164
+ myHeaders . append ( "Content-Type" , contentType ) ;
165
+ if ( contentType == "application/json" ) {
166
+ requestData = JSON . stringify ( requestData ) ;
167
+ }
168
+ if ( fileName ) {
169
+ myHeaders . append (
170
+ "Content-Disposition" ,
171
+ `attachment; filename=${ fileName } `
172
+ ) ;
173
+ }
142
174
var requestOptions : RequestInit = {
143
175
method : "PUT" ,
144
176
headers : myHeaders ,
145
- body : JSON . stringify ( requestData ) ,
177
+ body : requestData ,
146
178
} ;
147
179
const fetchPromise = fetch ( theURL , requestOptions )
148
180
. then ( async function ( response ) {
149
181
// Your code for handling the data you get from the API
150
182
if ( response . status < 400 ) {
151
- var data = await response . json ( ) ;
152
- return new OFSResponse (
153
- theURL ,
154
- response . status ,
155
- undefined ,
156
- data
157
- ) ;
183
+ if ( response . status == 204 ) {
184
+ //No data here
185
+ return new OFSResponse (
186
+ theURL ,
187
+ response . status ,
188
+ undefined ,
189
+ undefined
190
+ ) ;
191
+ } else {
192
+ var data = await response . json ( ) ;
193
+ return new OFSResponse (
194
+ theURL ,
195
+ response . status ,
196
+ undefined ,
197
+ data
198
+ ) ;
199
+ }
158
200
} else {
159
201
return new OFSResponse (
160
202
theURL ,
@@ -319,6 +361,69 @@ export class OFS {
319
361
return this . _patch ( partialURL , data ) ;
320
362
}
321
363
364
+ async getActivityFilePropertyContent (
365
+ aid : number ,
366
+ propertyLabel : string ,
367
+ nediaType : string = "*/*"
368
+ ) : Promise < OFSResponse > {
369
+ var myHeaders = new Headers ( ) ;
370
+ myHeaders . append ( "Accept" , nediaType ) ;
371
+ const partialURL = `/rest/ofscCore/v1/activities/${ aid } /${ propertyLabel } ` ;
372
+ return this . _get ( partialURL , undefined , myHeaders ) ;
373
+ }
374
+
375
+ async getActivityFilePropertyMetadata (
376
+ aid : number ,
377
+ propertyLabel : string
378
+ ) : Promise < OFSResponse > {
379
+ var myHeaders = new Headers ( ) ;
380
+ myHeaders . append ( "Accept" , "*/*" ) ;
381
+ const partialURL = `/rest/ofscCore/v1/activities/${ aid } /${ propertyLabel } ` ;
382
+ return this . _get ( partialURL , undefined , myHeaders ) ;
383
+ }
384
+
385
+ async getActivityFileProperty (
386
+ aid : number ,
387
+ propertyLabel : string
388
+ ) : Promise < OFSResponse > {
389
+ var myHeaders = new Headers ( ) ;
390
+ const partialURL = `/rest/ofscCore/v1/activities/${ aid } /${ propertyLabel } ` ;
391
+ var metadata = await this . getActivityFilePropertyMetadata (
392
+ aid ,
393
+ propertyLabel
394
+ ) ;
395
+ if ( metadata . status < 400 ) {
396
+ var contentType = metadata . contentType ;
397
+ if ( contentType ) {
398
+ myHeaders . append ( "Accept" , contentType ) ;
399
+ }
400
+ var content = this . _get ( partialURL , undefined , myHeaders ) ;
401
+ return new OFSResponse (
402
+ metadata . url ,
403
+ metadata . status ,
404
+ metadata . description ,
405
+ {
406
+ ...metadata . data ,
407
+ content : ( await content ) . data ,
408
+ } ,
409
+ metadata . contentType
410
+ ) ;
411
+ } else {
412
+ return metadata ;
413
+ }
414
+ }
415
+
416
+ async setActivityFileProperty (
417
+ aid : number ,
418
+ propertyLabel : string ,
419
+ blob : Blob ,
420
+ fileName : string ,
421
+ contentType : string = "*/*"
422
+ ) : Promise < OFSResponse > {
423
+ const partialURL = `/rest/ofscCore/v1/activities/${ aid } /${ propertyLabel } ` ;
424
+ return this . _put ( partialURL , blob , contentType , fileName ) ;
425
+ }
426
+
322
427
// Core: User Management
323
428
async getUsers (
324
429
offset : number = 0 ,
0 commit comments