Skip to content

Commit

Permalink
create seperate method for getAvailableMimeTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Jan 21, 2024
1 parent 2b2ac15 commit c138fae
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
27 changes: 5 additions & 22 deletions src/handlers/request_handler_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>();

Check warning on line 10 in src/handlers/request_handler_helper.ts

View workflow job for this annotation

GitHub Actions / build

'mimeTypeMap' is assigned a value but never used

Check warning on line 10 in src/handlers/request_handler_helper.ts

View workflow job for this annotation

GitHub Actions / build

'mimeTypeMap' is assigned a value but never used

export class RequestHandlerHelper {
protected componentProps: IComponentProp;
Expand All @@ -25,31 +23,16 @@ 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[]) {
const negotiator = new Negotiator(this.request);
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);
Expand Down
20 changes: 20 additions & 0 deletions src/helpers/get_available_mime_type.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 2 additions & 1 deletion src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
export * from './test';
export * from './get_available_mime_type';

0 comments on commit c138fae

Please sign in to comment.