diff --git a/src/handlers/request_handler_helper.ts b/src/handlers/request_handler_helper.ts index 9e6baad..d5c8bf1 100644 --- a/src/handlers/request_handler_helper.ts +++ b/src/handlers/request_handler_helper.ts @@ -2,14 +2,12 @@ import { HTTP_STATUS_CODE, MIME_TYPE, HTTP_METHOD, HTTP_RESULT_TYPE } from "../e import { CONTENT_TYPE, SET_COOKIE, FORT_GLOBAL } from "../constants"; import * as Negotiator from "negotiator"; import { IComponentProp, IException, IFileResultInfo, IHttpResult } from "../interfaces"; -import { textResult, getResultBasedOnMiMe } from "../helpers"; +import { textResult, getResultBasedOnMiMe, getAvailableMimeTypes } from "../helpers"; import { HttpFormatResult } from "../types"; import { parse } from "path"; import { FileHandler } from "./file_handler"; -const jsonMimeType = [MIME_TYPE.Json, MIME_TYPE.Xml]; -const textMimeType = [MIME_TYPE.Text, MIME_TYPE.Html, MIME_TYPE.Js, -MIME_TYPE.Css, MIME_TYPE.Rtf, MIME_TYPE.Csv]; +const mimeTypeMap = new Map(); export class RequestHandlerHelper { protected componentProps: IComponentProp; @@ -25,9 +23,9 @@ export class RequestHandlerHelper { } protected getContentTypeFromNegotiation(type: MIME_TYPE) { - const negotiator = new Negotiator(this.request); - const availableTypes: MIME_TYPE[] = this.getAvailableTypes_(type) || [type]; - return negotiator.mediaType(availableTypes) as MIME_TYPE; + return this.getContentTypeFromNegotiationHavingMultipleTypes( + getAvailableMimeTypes(type) || [type] + ); } protected getContentTypeFromNegotiationHavingMultipleTypes(types: MIME_TYPE[]) { @@ -35,21 +33,6 @@ export class RequestHandlerHelper { return negotiator.mediaType(types) as MIME_TYPE; } - private getAvailableTypes_(type: MIME_TYPE) { - switch (type) { - case MIME_TYPE.Json: - case MIME_TYPE.Xml: - return jsonMimeType; - case MIME_TYPE.Html: - case MIME_TYPE.Css: - case MIME_TYPE.Csv: - case MIME_TYPE.Js: - case MIME_TYPE.Rtf: - case MIME_TYPE.Text: - return textMimeType; - } - } - protected onBadRequest(error) { return new FORT_GLOBAL.errorHandler().onBadRequest(error).then(data => { return this.onResultFromError_(data); diff --git a/src/helpers/get_available_mime_type.ts b/src/helpers/get_available_mime_type.ts new file mode 100644 index 0000000..825d201 --- /dev/null +++ b/src/helpers/get_available_mime_type.ts @@ -0,0 +1,20 @@ +import { MIME_TYPE } from "../enums"; + +const jsonMimeType = [MIME_TYPE.Json, MIME_TYPE.Xml]; +const textMimeType = [MIME_TYPE.Text, MIME_TYPE.Html, MIME_TYPE.Js, +MIME_TYPE.Css, MIME_TYPE.Rtf, MIME_TYPE.Csv]; + +export const getAvailableMimeTypes = (type: MIME_TYPE) => { + switch (type) { + case MIME_TYPE.Json: + case MIME_TYPE.Xml: + return jsonMimeType; + case MIME_TYPE.Html: + case MIME_TYPE.Css: + case MIME_TYPE.Csv: + case MIME_TYPE.Js: + case MIME_TYPE.Rtf: + case MIME_TYPE.Text: + return textMimeType; + } +} \ No newline at end of file diff --git a/src/helpers/index.ts b/src/helpers/index.ts index 32c1b8d..b267cd8 100644 --- a/src/helpers/index.ts +++ b/src/helpers/index.ts @@ -26,4 +26,5 @@ export * from './validate_param_guard'; export * from './validate_body_guard'; export * from './validate_query_shield'; export * from './multi_format_result'; -export * from './test'; \ No newline at end of file +export * from './test'; +export * from './get_available_mime_type'; \ No newline at end of file