From c7557cf5b6b36259d83b6a47229989ebb8ee7780 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Tue, 5 Apr 2022 12:20:18 -0500 Subject: [PATCH] Allow overriding content-type of plaintext response factory --- package.json | 2 +- src/http/response/StringResponseFactory.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a08e46b..8d1ff62 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@extollo/lib", - "version": "0.9.28", + "version": "0.9.29", "description": "The framework library that lifts up your code.", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/http/response/StringResponseFactory.ts b/src/http/response/StringResponseFactory.ts index 84dd61c..2f103bb 100644 --- a/src/http/response/StringResponseFactory.ts +++ b/src/http/response/StringResponseFactory.ts @@ -13,6 +13,8 @@ export function plaintext(value: string): StringResponseFactory { * Response factory that renders a given string as the response in plaintext. */ export class StringResponseFactory extends ResponseFactory { + protected targetContentType = 'text/plain' + constructor( /** The string to write as the body. */ public readonly value: string, @@ -22,8 +24,14 @@ export class StringResponseFactory extends ResponseFactory { public async write(request: Request): Promise { request = await super.write(request) - request.response.setHeader('Content-Type', 'text/plain') + request.response.setHeader('Content-Type', this.targetContentType) request.response.body = this.value return request } + + /** Override the content type of the string. */ + public contentType(type: string): this { + this.targetContentType = type + return this + } }