1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 153x 80x 122x 122x 76x 6x 76x 453x 182x 182x 182x 182x 182x 1x 1x 1x 182x 189x 189x 160x 3x 3x 157x 157x 29x 189x 189x 189x 5x 5x 5x 5x 5x 2x 2x 2x 1x 1x | /** * @module Repository * @preferred * @description This module stores the Repository (entry-point to sense NET API) related classes, interfaces and functions. */ /** */ import { Observable, Subject } from '@reactivex/rxjs'; import { VersionInfo } from './'; import { RequestMethodType, BaseHttpProvider } from '../HttpProviders'; import { SnConfigModel } from '../Config/snconfigmodel'; import { ODataRequestOptions } from '../ODataApi/ODataRequestOptions'; import { IAuthenticationService } from '../Authentication/'; import { ICustomActionOptions } from '../ODataApi/CustomAction'; import { IODataParams, ODataParams } from '../ODataApi/ODataParams'; import { ContentType } from '../ContentTypes'; import { Content, IContentOptions } from '../Content'; import { ODataApi } from '../ODataApi/ODataApi'; import { ODataHelper, Authentication, ContentTypes } from '../SN'; import { ODataCollectionResponse } from '../ODataApi/ODataCollectionResponse'; import { ContentSerializer } from '../ContentSerializer'; /** * */ export class BaseRepository<TProviderType extends BaseHttpProvider = BaseHttpProvider, TAuthenticationServiceType extends IAuthenticationService = IAuthenticationService> { private odataApi: ODataApi<TProviderType, Content>; private onContentCreatedSubject = new Subject<Content>(); private onContentCreateFailedSubject = new Subject<{ content: Content, error: any }>(); private onContentModifiedSubject = new Subject<{ content: Content, originalFields: IContentOptions, change: IContentOptions }>(); private onContentModificationFailedSubject = new Subject<{ content: Content, change: IContentOptions, error: any }>(); private onContentLoadedSubject = new Subject<Content>(); private onContentDeletedSubject = new Subject<{ contentData: IContentOptions, permanently: boolean }>(); private onContentDeleteFailedSubject = new Subject<{ content: Content, permanently: boolean, error: any }>(); private onCustomActionExecutedSubject = new Subject<{ActionOptions: ICustomActionOptions, ODataParams?: IODataParams, Result: Object}>(); private onCustomActionFailedSubject = new Subject<{actionOptions: ICustomActionOptions, ODataParams?: IODataParams, ResultType: { new(...args: any[]): Object }, Error: Error}>(); private onContentMovedSubject = new Subject<{fromPath: string, toPath: string, content: Content}>() private onContentMoveFailedSubject = new Subject<{fromPath: string, toPath: string, content: Content, err: Error}>() /** * Triggered after a succesful Content creation */ public OnContentCreated = this.onContentCreatedSubject.asObservable(); /** * Triggered after Content creation has been failed */ public OnContentCreateFailed = this.onContentCreateFailedSubject.asObservable(); /** * Triggered after modifying a Content */ public OnContentModified = this.onContentModifiedSubject.asObservable(); /** * Triggered when failed to modify a Content */ public OnContentModificationFailed = this.onContentModificationFailedSubject.asObservable(); /** * Triggered when a Content is loaded from the Repository */ public OnContentLoaded = this.onContentLoadedSubject.asObservable(); /** * Triggered after deleting a Content */ public OnContentDeleted = this.onContentDeletedSubject.asObservable(); /** * Triggered after deleting a content has been failed */ public OnContentDeleteFailed = this.onContentDeleteFailedSubject.asObservable(); /** * Triggered after moving a content to another location */ public OnContentMoved = this.onContentMovedSubject.asObservable(); /** * Triggered after moving a content has been failed */ public OnContentMoveFailed = this.onContentMoveFailedSubject.asObservable(); /** * Triggered after a custom OData Action has been executed */ public OnCustomActionExecuted = this.onCustomActionExecutedSubject.asObservable(); /** * Triggered after a custom OData Action has been failed */ public OnCustomActionFailed = this.onCustomActionFailedSubject.asObservable(); /** * Will be true if the Repository's host differs from the current host */ public get IsCrossDomain() { return this.Config.RepositoryUrl !== SnConfigModel.DEFAULT_BASE_URL; } /** * Returns the Repository's base OData Url (e.g.: https://demo.sensenet.com/odata.svc) */ public get ODataBaseUrl() { return ODataHelper.joinPaths(this.Config.RepositoryUrl, this.Config.ODataToken); } /** * Public endpoint for making Ajax calls to the Repository * @param {string} path The Path for the call * @param {RequestMethodType} method The method type * @param {{ new (...args): T }} returnsType The expected return type * @param {any} body The post body (optional) * @returns {Observable<T>} An observable, which will be updated with the response. */ public Ajax<T>(path: string, method: RequestMethodType, returnsType?: { new(...args: any[]): T }, body?: any): Observable<T> { this.Authentication.CheckForUpdate(); return this.Authentication.State.skipWhile(state => state === Authentication.LoginState.Pending) .first() .flatMap(state => { if (!returnsType) { returnsType = Object as { new(...args: any[]): any }; } return this.httpProviderRef.Ajax<T>(returnsType, { url: ODataHelper.joinPaths(this.ODataBaseUrl, path), method: method, body: body, crossDomain: this.IsCrossDomain, withCredentials: this.IsCrossDomain, responseType: 'json', }); }); } /** * Reference to the Http Provider used by the current repository */ public readonly httpProviderRef: TProviderType; /** * Reference to the OData API used by the current repository */ public get Content(): ODataApi<TProviderType, any>{ console.warn('The property repository.Content is deprecated and will be removed in the near future. Use repositoy.GetODataApi() instead.') return this.odataApi; }; public GetODataApi(): ODataApi<TProviderType, Content>{ return this.odataApi; } /** * Reference to the Authentication Service used by the current repository */ public readonly Authentication: TAuthenticationServiceType; /** * Reference to the configuration used by the current repository */ public readonly Config: SnConfigModel; /** * @param config The Repository's configuration entry * @param httpProviderType The type of the Http Provider, should extend HttpProviders.BaseHttpProvider * @param authentication The type of the Authentication Service to be used. */ constructor(config: Partial<SnConfigModel>, private readonly httpProviderType: { new(): TProviderType }, authentication: { new(...args: any[]): TAuthenticationServiceType }) { this.httpProviderRef = new httpProviderType(); this.Config = new SnConfigModel(config); //warning: Authentication constructor parameterization is not type-safe this.Authentication = new authentication(this.httpProviderRef, this.Config.RepositoryUrl, this.Config.JwtTokenKeyTemplate, this.Config.JwtTokenPersist); this.odataApi = new ODataApi(this.httpProviderType, this); } /** * Gets the complete version information about the core product and the installed applications. This function is accessible only for administrators by default. You can learn more about the * subject in the SnAdmin article. You can read detailed description of the function result. * @returns {Observable} Returns an RxJS observable that you can subscribe of in your code. * ``` * let getVersionInfo = GetVersionInfo(); * getVersionInfo.subscribe({ * next: response => { * console.log('success'); * }, * error: error => console.error('something wrong occurred: ' + error.responseJSON.error.message.value), * complete: () => console.log('done'), * }); * ``` */ public GetVersionInfo() { return this.odataApi.CreateCustomAction({ name: 'GetVersionInfo', path: '/Root', isAction: false }, {}, VersionInfo); } /** * Returns the list of all ContentTypes in the system. * @returns {Observable<ODataCollectionResponse<ContentTypes.ContentType>>} Returns an RxJS observable that you can subscribe of in your code. * ``` * let getAllContentTypes = GetAllContentTypes(); * getAllContentTypes.subscribe({ * next: response => { * console.log('success'); * }, * error: error => console.error('something wrong occurred: ' + error.responseJSON.error.message.value), * complete: () => console.log('done'), * }); * ``` */ public GetAllContentTypes(): Observable<ContentType[]> { return this.odataApi.CreateCustomAction<ODataCollectionResponse<ContentType>>({ name: 'GetAllContentTypes', path: '/Root', isAction: false }, undefined, ODataCollectionResponse) .map(resp => { return resp.d.results.map(c => this.HandleLoadedContent(c, ContentType)); }); } private _loadedContentReferenceCache: Content[] = []; /** * Creates a Content instance that is loaded from the Repository. This method should be used only to instantiate content from payload received from the backend. * @param type {string} The Content will be a copy of the given type. * @param options {SenseNet.IContentOptions} Optional list of fields and values. * @returns {SenseNet.Content} * ```ts * var content = SenseNet.Content.HandleLoadedContent('Folder', { DisplayName: 'My folder' }); // content is an instance of the Folder with the DisplayName 'My folder' * ``` */ public HandleLoadedContent<T extends Content, O extends T['options']>(opt: O, contentType?: { new(...args: any[]): T }): T { let instance: T; const realContentType = (contentType || (opt.Type && (ContentTypes as any)[opt.Type]) || Content) as {new(...args: any[]): T}; if (opt.Id){ if (this._loadedContentReferenceCache[opt.Id]){ instance = this._loadedContentReferenceCache[opt.Id] as T; instance['UpdateLastSavedFields'](opt); } else { instance = Content.Create(realContentType, opt, this); this._loadedContentReferenceCache[opt.Id] = instance; } } else { instance = Content.Create(realContentType, opt, this); } instance['_isSaved'] = true; this.onContentLoadedSubject.next(instance); return instance; } /** * Requests a Content by the given id. * @param idOrPath {number|string} Id of the requested Content. * @param version {string} A string containing the version of the requested Content. * @param options {Object} JSON object with the possible ODATA parameters like select, expand, etc. * @returns {Observable<T>} Returns an RxJS observable that you can subscribe of in your code. * ```ts * var content = SenseNet.Content.Load(1234, 'A.1', { expand: 'Avatar' }); * content * .map(response => response.d) * .subscribe({ * next: response => { * //do something with the response * }, * error: error => console.error('something wrong occurred: ' + error.responseJSON.error.message.value), * complete: () => console.log('done'), * }) * ``` */ public Load<TContentType extends Content>( idOrPath: string | number, options?: IODataParams, version?: string, returns?: { new(...args: any[]): TContentType }): Observable<TContentType> { let contentURL = typeof idOrPath === 'string' ? ODataHelper.getContentURLbyPath(idOrPath) : ODataHelper.getContentUrlbyId(idOrPath); let params = new ODataParams(options || {}); let odataRequestOptions = new ODataRequestOptions({ path: contentURL, params: params }) const returnType = returns || Content as { new(...args: any[]): any }; return this.odataApi.Get(odataRequestOptions, returnType) .share() .map(r => { return this.HandleLoadedContent(r.d, returnType); }); } /** * Parses a Content instance from a stringified SerializedContent<T> instance * @param stringifiedContent The stringified SerializedContent<T> * @throws Error if the Content belongs to another Repository (based it's Origin) * @returns The loaded Content */ public ParseContent<T extends Content = Content>(stringifiedContent: string): T { const serializedContent = ContentSerializer.Parse<T>(stringifiedContent); if (serializedContent.Origin.indexOf(this.ODataBaseUrl) !== 0){ throw new Error('Content belongs to a different Repository.'); } return this.HandleLoadedContent(serializedContent.Data) } } |