Server/project/src/utils/HttpResponseUtil.ts
Refringe 50c7a26a58
ESLint Pass
This is the first pass of ESLint on the codebase.

ESLint formatting is less strict when it comes to line-length and line-breaks then dprint/biome, so if you see formatting that you don't like... fix it! It shouldn't require a configuration change.

- This should merge clean into master (when the time comes).
- This will not merge clean into `3.9.0-DEV`, but the conflicts aren't that bad.
2024-05-07 23:57:08 -04:00

97 lines
2.9 KiB
TypeScript

import { inject, injectable } from "tsyringe";
import { IGetBodyResponseData } from "@spt-aki/models/eft/httpResponse/IGetBodyResponseData";
import { INullResponseData } from "@spt-aki/models/eft/httpResponse/INullResponseData";
import { IItemEventRouterResponse } from "@spt-aki/models/eft/itemEvent/IItemEventRouterResponse";
import { BackendErrorCodes } from "@spt-aki/models/enums/BackendErrorCodes";
import { LocalisationService } from "@spt-aki/services/LocalisationService";
import { JsonUtil } from "@spt-aki/utils/JsonUtil";
@injectable()
export class HttpResponseUtil
{
constructor(
@inject("JsonUtil") protected jsonUtil: JsonUtil,
@inject("LocalisationService") protected localisationService: LocalisationService,
)
{}
protected clearString(s: string): any
{
return s.replace(/[\b]/g, "").replace(/[\f]/g, "").replace(/[\n]/g, "")
.replace(/[\r]/g, "")
.replace(
/[\t]/g,
"",
);
}
/**
* Return passed in data as JSON string
* @param data
* @returns
*/
public noBody(data: any): any
{
return this.clearString(this.jsonUtil.serialize(data));
}
/**
* Game client needs server responses in a particular format
* @param data
* @param err
* @param errmsg
* @returns
*/
public getBody<T>(data: T, err = 0, errmsg = null, sanitize = true): IGetBodyResponseData<T>
{
return sanitize
? this.clearString(this.getUnclearedBody(data, err, errmsg))
: (this.getUnclearedBody(data, err, errmsg) as any);
}
public getUnclearedBody(data: any, err = 0, errmsg = null): string
{
return this.jsonUtil.serialize({ err: err, errmsg: errmsg, data: data });
}
public emptyResponse(): IGetBodyResponseData<string>
{
return this.getBody("", 0, "");
}
public nullResponse(): INullResponseData
{
return this.clearString(this.getUnclearedBody(null, 0, null));
}
public emptyArrayResponse(): IGetBodyResponseData<any[]>
{
return this.getBody([]);
}
/**
* 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
*/
public appendErrorToOutput(
output: IItemEventRouterResponse,
message = this.localisationService.getText("http-unknown_error"),
errorCode = BackendErrorCodes.NONE,
): IItemEventRouterResponse
{
if (output.warnings?.length > 0)
{
output.warnings.push({ index: output.warnings?.length - 1, errmsg: message, code: errorCode.toString() });
}
else
{
output.warnings = [{ index: 0, errmsg: message, code: errorCode.toString() }];
}
return output;
}
}