import { HEADER_NAMES, HTTP_METHODS } from "../constants"; import { SDKOptions, Token } from "../Sitefinity"; import { utils } from "../utils"; import { ExecutableRequest } from "./ExecutableRequest"; import { RequestOptionsBaseExtended } from "./RequestBase"; export class GetRequest extends ExecutableRequest { constructor(requestOptions: RequestOptionsBaseExtended, token: Token, sdkOptions: SDKOptions) { super(requestOptions, token, sdkOptions); } getMethod(): string { return HTTP_METHODS.get; } } export class GetByIdRequest extends ExecutableRequest { constructor(requestOptions: RequestOptionsBaseExtended, token: Token, sdkOptions: SDKOptions) { super(requestOptions, token, sdkOptions); } getMethod(): string { return HTTP_METHODS.get; } } export class DeleteRequest extends ExecutableRequest { constructor(requestOptions: RequestOptionsBaseExtended, token: Token, sdkOptions: SDKOptions) { super(requestOptions, token, sdkOptions); } getMethod(): string { return HTTP_METHODS.delete; } } export class UpdateRequest extends ExecutableRequest { private data: object; private saveTemp: boolean; constructor(requestOptions: PutRequestOptionsExtended, token: Token, sdkOptions: SDKOptions) { super(requestOptions, token, sdkOptions); this.data = requestOptions.data; this.saveTemp = requestOptions.saveTemp; } getMethod(): string { return HTTP_METHODS.patch; } getBody(): object { const body = utils.convertDateFieldToISOString(this.data); return body; } getHeaders(): { [key: string]: string } { const headers = super.getHeaders(); if (this.saveTemp) { headers[HEADER_NAMES.operation] = "SaveTemp"; } return headers; } } export class CreateRequest extends ExecutableRequest { protected data: object; constructor(requestOptions: PostRequestOptionsExtended, token: Token, sdkOptions: SDKOptions) { super(requestOptions, token, sdkOptions); this.data = requestOptions.data; } getMethod(): string { return HTTP_METHODS.post; } getBody(): object { if (!(this.data instanceof File)) { return utils.convertDateFieldToISOString(this.data); } return this.data; } } export class GetRelatedRequest extends GetByIdRequest { private property: string; constructor(requestOptions: RelatedRequestOptionsExtended, token: Token, sdkOptions: SDKOptions) { super(requestOptions, token, sdkOptions); this.property = requestOptions.property; } buildUrl(): string { const url = this.urlOptions.baseUrl + this.urlOptions.entitySet + "(" + this.key + ")/" + this.property; return this.addQueryParameters(url); } } export class GetRelatedByIdRequest extends GetByIdRequest { private property: string; private relatedId: string; constructor(requestOptions: RelatedByIdRequestOptionsExtended, token: Token, sdkOptions: SDKOptions) { super(requestOptions, token, sdkOptions); this.property = requestOptions.property; this.relatedId = requestOptions.relatedId; } buildUrl(): string { const url = this.urlOptions.baseUrl + this.urlOptions.entitySet + "(" + this.key + ")/" + this.property + "(" + this.relatedId + ")"; return this.addQueryParameters(url); } } export class DeleteRelatedRequest extends DeleteRequest { private property: string; constructor(requestOptions: RelatedRequestOptionsExtended, token: Token, sdkOptions: SDKOptions) { super(requestOptions, token, sdkOptions); this.property = requestOptions.property; } buildUrl(): string { const url = this.urlOptions.baseUrl + this.urlOptions.entitySet + "(" + this.key + ")/" + this.property + "/$ref"; return this.addQueryParameters(url); } } export class DeleteRelatedByIdRequest extends DeleteRequest { private property: string; private relatedId: string; constructor(requestOptions: RelatedByIdRequestOptionsExtended, token: Token, sdkOptions: SDKOptions) { super(requestOptions, token, sdkOptions); this.property = requestOptions.property; this.relatedId = requestOptions.relatedId; } buildUrl(): string { const url = this.urlOptions.baseUrl + this.urlOptions.entitySet + "(" + this.key + ")/" + this.property + "(" + this.relatedId + ")/$ref"; return this.addQueryParameters(url); } } export class CreateRelatedRequest extends CreateRequest { private property: string; constructor(requestOptions: RelatedRequestOptionsExtended, token: Token, sdkOptions: SDKOptions) { super(requestOptions, token, sdkOptions); this.property = requestOptions.property; this.key = requestOptions.key; } buildUrl(): string { const url = this.urlOptions.baseUrl + this.urlOptions.entitySet + "(" + this.key + ")/" + this.property + "/$ref"; return this.addQueryParameters(url); } } export class UploadRequest extends CreateRequest { private fileName: string; private uploadProperties: { [key: string]: string }; private contentType: string; constructor(requestOptions: UploadRequestOptionsExtended, token: Token, sdkOptions: SDKOptions) { super(requestOptions, token, sdkOptions); this.fileName = requestOptions.fileName; this.uploadProperties = requestOptions.uploadProperties; this.contentType = requestOptions.contentType; } getHeaders(): { [key: string]: string } { const headers = super.getHeaders(); headers[HEADER_NAMES.contentType] = this.contentType || "application/octet-stream"; headers[HEADER_NAMES.fileName] = this.fileName; headers[HEADER_NAMES.properties] = JSON.stringify(this.uploadProperties); headers[HEADER_NAMES.contentEncoding] = "identity"; return headers; } getBody(): object { return this.data; } getMethod(): string { if (this.key) { return HTTP_METHODS.patch; } return HTTP_METHODS.post; } } export class UploadChunkRequest extends CreateRequest { contentType: string; chunkOrdinal: number; chunkSize: number; chunkedFileUploadSessionId: string; numberOfChunks: number; positionInQueue: number; blobStorageProviderName: string; fileName: string; constructor(requestOptions: UploadChunkRequestOptionsExtended, token: Token, sdkOptions: SDKOptions) { super(requestOptions, token, sdkOptions); this.contentType = requestOptions.fileMimeType; this.chunkOrdinal = requestOptions.chunkOrdinal; this.chunkedFileUploadSessionId = requestOptions.chunkedFileUploadSessionId; this.chunkSize = requestOptions.chunkSize; this.numberOfChunks = requestOptions.numberOfChunks; this.positionInQueue = requestOptions.positionOfChunkInQueue; this.blobStorageProviderName = requestOptions.blobStorageProviderName; this.data = requestOptions.chunkBlob; this.fileName = requestOptions.fileName; } getHeaders(): { [key: string]: string } { const headers = super.getHeaders(); headers[HEADER_NAMES.contentEncoding] = "identity"; headers[HEADER_NAMES.contentType] = this.contentType || "application/octet-stream"; headers[HEADER_NAMES.chunkOrdinal] = this.chunkOrdinal.toString(); headers[HEADER_NAMES.chunkedFileUploadSessionId] = this.chunkedFileUploadSessionId; headers[HEADER_NAMES.chunkSize] = this.chunkSize.toString(); headers[HEADER_NAMES.blobStorageProviderName] = this.blobStorageProviderName.toString(); headers[HEADER_NAMES.fileName] = this.fileName; return headers; } getBody(): object { return this.data; } getMethod(): string { return HTTP_METHODS.post; } protected override executeProgressEvent(): void { if (this.http.upload) { this.http.upload.onprogress = () => { const payload = { lengthComputable: true, loaded: this.positionInQueue, total: this.numberOfChunks }; if (this.progressCb && utils.isFunction(this.progressCb)) { this.progressCb(payload); } }; } } } export class CommitChunksRequest extends CreateRequest { constructor(requestOptions: CommitChunksRequestOptionsExtended, token: Token, sdkOptions: SDKOptions) { super(requestOptions, token, sdkOptions); this.action = "Default.CommitChunks"; this.data = { // ! this must be the param name that the operation provider on the server accepts commitChunksDto: { chunkedFileUploadSessionId: requestOptions.chunkedFileUploadSessionId, contentType: requestOptions.contentType, fileTotalSize: requestOptions.fileTotalSize, numberOfChunks: requestOptions.numberOfChunks, fileName: requestOptions.fileName, blobStorageProviderName: requestOptions.blobStorageProviderName, itemId: requestOptions.itemId || null, // ! must be named properties properties: requestOptions.uploadProperties } }; } getMethod(): string { return HTTP_METHODS.post; } } export interface PostRequestOptionsExtended extends RequestOptionsBaseExtended { data?: object; } export interface PutRequestOptionsExtended extends PostRequestOptionsExtended { saveTemp: boolean; } export interface RelatedRequestOptionsExtended extends PostRequestOptionsExtended { property: string; } export interface RelatedByIdRequestOptionsExtended extends RelatedRequestOptionsExtended { relatedId: string; } export interface UploadRequestOptionsExtended extends PostRequestOptionsExtended { fileName: string; contentType: string; uploadProperties: { [key: string]: string }; } export interface UploadChunkRequestOptionsExtended extends PostRequestOptionsExtended { fileMimeType: string; fileName: string; chunkedFileUploadSessionId: string; chunkSize: number; chunkOrdinal: number; numberOfChunks: number; chunkBlob: Blob; blobStorageProviderName: string; positionOfChunkInQueue?: number; } export interface CommitChunksRequestOptionsExtended extends PostRequestOptionsExtended { chunkedFileUploadSessionId: string blobStorageProviderName: string; contentType: string; fileTotalSize: number; numberOfChunks: number; fileName: string; uploadProperties: { [key: string]: string }; itemId?: string; }