Skip to content

Commit

Permalink
[cb-#99] set request.response_content_type
Browse files Browse the repository at this point in the history
  • Loading branch information
crookse committed Dec 6, 2019
1 parent 1b165b3 commit a57dc13
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 49 deletions.
51 changes: 2 additions & 49 deletions src/http/response.ts
Expand Up @@ -63,7 +63,7 @@ export default class Response {
constructor(request) {
this.request = request;
this.headers = new Headers();
this.headers.set("Content-Type", this.getHeaderContentType());
this.headers.set("Content-Type", request.response_content_type);
}

// FILE MARKER: METHODS - PUBLIC /////////////////////////////////////////////
Expand All @@ -75,6 +75,7 @@ export default class Response {
* @return any
*/
public generateResponse(): any {
console.log("Wtf");
switch (this.headers.get("Content-Type")) {
case "application/json":
this.body_generated = this.generateJsonResponse();
Expand Down Expand Up @@ -239,52 +240,4 @@ export default class Response {
}

// FILE MARKER: METHODS - PROTECTED //////////////////////////////////////////

/**
* @description
* Get this response's `Content-Type` header. There are three ways to set
* the response's `Content-Type` header from a request: (1) the request's
* headers by setting `Response-Content-Type: "type"`, (2) the request's
* URL query params by setting `?response_content_type=type`, and the
* request's body by setting `{response_content_type: "type"}`.
*
* Setting the content type using the request's body takes precedence over
* all other settings.
*
* Setting the content type using the request's URL query params takes
* precedence over the header setting and the default setting.
*
* Setting the content type using the request's header setting takes
* precedence over the default setting.
*
* If no content type is specified by the request's body, URL query
* params, or header, then the default content type will be used. The
* default content type is the content type defined in the
* `Drash.Http.Server` object's `response_output` config.
*
* @return string
*/
protected getHeaderContentType(): string {
let contentType = this.request.headers.get("Response-Content-Type-Default");

// Check the request's headers to see if `response-content-type:
// {content-type}` has been specified
contentType = this.request.headers.get("Response-Content-Type")
? this.request.headers.get("Response-Content-Type")
: contentType;

// Check the request's URL query params to see if
// ?response_content_type={content-type} has been specified
contentType = this.request.url_query_params.response_content_type
? this.request.url_query_params.response_content_type
: contentType;

// Check the request's body to see if
// {response_content_type: {content-type}} has been specified
contentType = this.request.body_parsed.response_content_type
? this.request.body_parsed.response_content_type
: contentType;

return contentType;
}
}
48 changes: 48 additions & 0 deletions src/services/http_service.ts
Expand Up @@ -128,6 +128,7 @@ export default class HttpService {
request.getQueryParam = function(httpVar: string): any {
return request.url_query_params[httpVar];
};
request.response_content_type = this.getResponseContentType(request);

return request;
}
Expand Down Expand Up @@ -252,6 +253,53 @@ export default class HttpService {
return mimeType;
}

/**
* @description
* Get the request's requested content type.
*
* There are three ways to get this value: (1) the request's headers by
* setting `Response-Content-Type: "type"`, (2) the request's URL query
* params by setting `?response_content_type=type`, and the request's body
* by setting `{response_content_type: "type"}`.
*
* The request's body takes precedence over all other settings.
*
* The request's URL query params takes precedence over the header setting
* and the default setting.
*
* The request's header setting takes precedence over the default setting.
*
* If no content type is specified by the request's body, URL query
* params, or header, then the default content type will be used. The
* default content type is the content type defined in the
* `Drash.Http.Server` object's `response_output` config.
*
* @return string
*/
public getResponseContentType(request): string {
let contentType = request.headers.get("Response-Content-Type-Default");

// Check the request's headers to see if `response-content-type:
// {content-type}` has been specified
contentType = request.headers.get("Response-Content-Type")
? request.headers.get("Response-Content-Type")
: contentType;

// Check the request's URL query params to see if
// ?response_content_type={content-type} has been specified
contentType = request.url_query_params.response_content_type
? request.url_query_params.response_content_type
: contentType;

// Check the request's body to see if
// {response_content_type: {content-type}} has been specified
contentType = request.body_parsed.response_content_type
? request.body_parsed.response_content_type
: contentType;

return contentType;
}

/**
* @description
* Parse a URL query string in it's raw form.
Expand Down

0 comments on commit a57dc13

Please sign in to comment.