2024-05-21 17:59:04 +00:00
|
|
|
import { IGetBodyResponseData } from "@spt/models/eft/httpResponse/IGetBodyResponseData";
|
|
|
|
import { INullResponseData } from "@spt/models/eft/httpResponse/INullResponseData";
|
|
|
|
import { IItemEventRouterResponse } from "@spt/models/eft/itemEvent/IItemEventRouterResponse";
|
|
|
|
import { BackendErrorCodes } from "@spt/models/enums/BackendErrorCodes";
|
|
|
|
import { LocalisationService } from "@spt/services/LocalisationService";
|
|
|
|
import { JsonUtil } from "@spt/utils/JsonUtil";
|
2024-07-23 11:12:53 -04:00
|
|
|
import { inject, injectable } from "tsyringe";
|
2023-03-03 15:23:46 +00:00
|
|
|
|
|
|
|
@injectable()
|
2024-07-23 11:12:53 -04:00
|
|
|
export class HttpResponseUtil {
|
2023-03-03 15:23:46 +00:00
|
|
|
constructor(
|
|
|
|
@inject("JsonUtil") protected jsonUtil: JsonUtil,
|
2023-11-16 21:42:06 +00:00
|
|
|
@inject("LocalisationService") protected localisationService: LocalisationService,
|
2024-07-23 11:12:53 -04:00
|
|
|
) {}
|
2023-03-03 15:23:46 +00:00
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
protected clearString(s: string): any {
|
2024-05-17 15:32:41 -04:00
|
|
|
return s
|
|
|
|
.replace(/[\b]/g, "")
|
|
|
|
.replace(/[\f]/g, "")
|
|
|
|
.replace(/[\n]/g, "")
|
2024-05-07 23:57:08 -04:00
|
|
|
.replace(/[\r]/g, "")
|
2024-05-17 15:32:41 -04:00
|
|
|
.replace(/[\t]/g, "");
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 14:29:09 +01:00
|
|
|
/**
|
|
|
|
* Return passed in data as JSON string
|
2023-11-16 21:42:06 +00:00
|
|
|
* @param data
|
|
|
|
* @returns
|
2023-08-10 14:29:09 +01:00
|
|
|
*/
|
2024-07-23 11:12:53 -04:00
|
|
|
public noBody(data: any): any {
|
2023-03-03 15:23:46 +00:00
|
|
|
return this.clearString(this.jsonUtil.serialize(data));
|
|
|
|
}
|
|
|
|
|
2023-11-26 21:11:03 +00:00
|
|
|
/**
|
|
|
|
* Game client needs server responses in a particular format
|
2023-12-24 19:54:27 +00:00
|
|
|
* @param data
|
|
|
|
* @param err
|
|
|
|
* @param errmsg
|
|
|
|
* @returns
|
2023-11-26 21:11:03 +00:00
|
|
|
*/
|
2024-07-23 11:12:53 -04:00
|
|
|
public getBody<T>(data: T, err = 0, errmsg?: string, sanitize = true): IGetBodyResponseData<T> {
|
2023-12-24 19:54:27 +00:00
|
|
|
return sanitize
|
|
|
|
? this.clearString(this.getUnclearedBody(data, err, errmsg))
|
|
|
|
: (this.getUnclearedBody(data, err, errmsg) as any);
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
public getUnclearedBody(data: any, err = 0, errmsg?: string): string {
|
2023-11-16 21:42:06 +00:00
|
|
|
return this.jsonUtil.serialize({ err: err, errmsg: errmsg, data: data });
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
public emptyResponse(): IGetBodyResponseData<string> {
|
2023-03-03 15:23:46 +00:00
|
|
|
return this.getBody("", 0, "");
|
|
|
|
}
|
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
public nullResponse(): INullResponseData {
|
2024-05-27 20:06:07 +00:00
|
|
|
return this.clearString(this.getUnclearedBody(undefined, 0, undefined));
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
public emptyArrayResponse(): IGetBodyResponseData<any[]> {
|
2023-03-03 15:23:46 +00:00
|
|
|
return this.getBody([]);
|
|
|
|
}
|
|
|
|
|
2024-01-16 12:04:55 +00:00
|
|
|
/**
|
|
|
|
* Add an error into the 'warnings' array of the client response message
|
|
|
|
* @param output IItemEventRouterResponse
|
|
|
|
* @param message Error message
|
|
|
|
* @param errorCode Error code
|
|
|
|
* @returns IItemEventRouterResponse
|
|
|
|
*/
|
2023-11-16 21:42:06 +00:00
|
|
|
public appendErrorToOutput(
|
|
|
|
output: IItemEventRouterResponse,
|
|
|
|
message = this.localisationService.getText("http-unknown_error"),
|
|
|
|
errorCode = BackendErrorCodes.NONE,
|
2024-07-23 11:12:53 -04:00
|
|
|
): IItemEventRouterResponse {
|
|
|
|
if (output.warnings?.length > 0) {
|
2024-02-02 13:54:07 -05:00
|
|
|
output.warnings.push({ index: output.warnings?.length - 1, errmsg: message, code: errorCode.toString() });
|
2024-07-23 11:12:53 -04:00
|
|
|
} else {
|
2024-01-16 12:04:55 +00:00
|
|
|
output.warnings = [{ index: 0, errmsg: message, code: errorCode.toString() }];
|
|
|
|
}
|
2023-03-03 15:23:46 +00:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
}
|