Skip to content

Commit

Permalink
Request class is missing status code property #3218
Browse files Browse the repository at this point in the history
  • Loading branch information
alansemenov committed Aug 2, 2023
1 parent 43eb1f2 commit 0f846b5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
36 changes: 26 additions & 10 deletions src/main/resources/assets/admin/common/js/rest/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Path} from './Path';
import {RequestError} from './RequestError';
import {HttpMethod} from './HttpMethod';
import {Response} from './Response';
import {StatusCode} from './StatusCode';

export abstract class Request {

Expand All @@ -17,6 +18,8 @@ export abstract class Request {

protected request: XMLHttpRequest = new XMLHttpRequest();

private status: number;

constructor(method: HttpMethod) {
this.method = method;
}
Expand All @@ -43,11 +46,17 @@ export abstract class Request {
if (this.request.readyState === 4) {
let errorJson = null;

if (this.request.status === 204) {
if (this.isRedirect()) {
this.status = StatusCode.REDIRECT;
} else {
this.status = this.request.status;
}

if (this.request.status === StatusCode.NO_CONTENT) {
deferred.resolve(null);
} else if (this.request.status >= 200 && this.request.status < 300) {
} else if (this.request.status >= StatusCode.OK && this.request.status < StatusCode.MULTIPLE_OPTIONS) {
deferred.resolve(this.request.response);
} else if (this.request.status === 403) {
} else if (this.request.status === StatusCode.FORBIDDEN) {
deferred.reject(new AccessDeniedException('Access denied'));
} else {
try {
Expand All @@ -64,16 +73,11 @@ export abstract class Request {
return deferred;
}

private sendRequest(): Q.Promise<Response> {
const deferred = this.bindRequestEventsHandlers();

this.request.send(this.createRequestData());

return deferred.promise;
getStatus(): number {
return this.status;
}

send(): Q.Promise<Response> {

this.prepareRequest();
return this.sendRequest();
}
Expand All @@ -88,4 +92,16 @@ export abstract class Request {
}

protected abstract createRequestURI(): string;

private sendRequest(): Q.Promise<Response> {
const deferred = this.bindRequestEventsHandlers();

this.request.send(this.createRequestData());

return deferred.promise;
}

private isRedirect(): boolean {
return !this.request.responseURL.endsWith(this.createRequestURI());
}
}
33 changes: 21 additions & 12 deletions src/main/resources/assets/admin/common/js/rest/ResourceRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {GetRequest} from './GetRequest';
import {PostRequest} from './PostRequest';
import {Response} from './Response';
import {HeadRequest} from './HeadRequest';
import {StatusCode} from './StatusCode';

export abstract class ResourceRequest<PARSED_TYPE>
implements HttpRequest<PARSED_TYPE> {
Expand All @@ -25,20 +26,14 @@ export abstract class ResourceRequest<PARSED_TYPE>

protected isJsonResponse: boolean = true;

protected status: number;

private pathElements: string[] = [];

constructor() {
this.restPath = Path.create().fromString(this.getPostfixUri()).build();
}

protected getPostfixUri() {
return UriHelper.getRestUri('');
}

protected addRequestPathElements(...items: string[]) {
this.pathElements.push(...items);
}

setMethod(value: string | HttpMethod) {
if (typeof value === 'string') {
this.method = HttpMethod[value.toUpperCase()];
Expand All @@ -59,6 +54,22 @@ export abstract class ResourceRequest<PARSED_TYPE>
return {};
}

protected getPostfixUri() {
return UriHelper.getRestUri('');
}

protected addRequestPathElements(...items: string[]) {
this.pathElements.push(...items);
}

protected isRedirect(): boolean {
return this.status === StatusCode.REDIRECT;
}

protected parseResponse(response: Response): PARSED_TYPE {
return response.getResult();
}

setTimeout(timeoutMillis: number) {
this.timeoutMillis = timeoutMillis;
}
Expand Down Expand Up @@ -88,6 +99,8 @@ export abstract class ResourceRequest<PARSED_TYPE>
.setTimeout(!this.heavyOperation ? this.timeoutMillis : 0);

return request.send().then((rawResponse: any) => {
this.status = request.getStatus();

return this.isJsonResponse ? new JsonResponse(rawResponse) : new Response(rawResponse);
});
}
Expand Down Expand Up @@ -115,8 +128,4 @@ export abstract class ResourceRequest<PARSED_TYPE>
return this.parseResponse(response);
});
}

protected parseResponse(response: Response): PARSED_TYPE {
return response.getResult();
}
}
8 changes: 8 additions & 0 deletions src/main/resources/assets/admin/common/js/rest/StatusCode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
export class StatusCode {

public static OK: number = 200;
public static NO_CONTENT: number = 204;

public static MULTIPLE_OPTIONS: number = 300;
public static REDIRECT: number = 307;

public static FORBIDDEN: number = 403;
public static NOT_FOUND: number = 404;
public static I_AM_A_TEAPOT: number = 418;

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {UploadFailedEvent} from './UploadFailedEvent';
import {KeyBindings} from '../KeyBindings';
import {KeyBinding} from '../KeyBinding';
import {KeyHelper} from '../KeyHelper';
import {StatusCode} from '../../rest/StatusCode';

export interface FineUploaderFile {
name: string;
Expand Down Expand Up @@ -857,7 +858,7 @@ export class UploaderEl<MODEL extends Equitable>
}

private fileCompleteCallback(id: number, _name: string, response: any, xhrOrXdr: XMLHttpRequest) {
if (xhrOrXdr && xhrOrXdr.status === 200) {
if (xhrOrXdr && xhrOrXdr.status === StatusCode.OK) {
try {
const uploadItem = this.findUploadItemById(id);
if (uploadItem) {
Expand All @@ -877,7 +878,7 @@ export class UploaderEl<MODEL extends Equitable>
}

private errorCallback(id: number, name: string, _errorReason: any, xhrOrXdr: XMLHttpRequest) {
if (xhrOrXdr && xhrOrXdr.status !== 200) {
if (xhrOrXdr && xhrOrXdr.status !== StatusCode.OK) {
try {
const responseObj = JSON.parse(xhrOrXdr.response);
const error = new RequestError(responseObj.status, responseObj.message);
Expand Down

0 comments on commit 0f846b5

Please sign in to comment.