Skip to content

✅️ Implement family of RestfulApiResponseFlusher #382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions lib/server/restfulapi/flushers/BaseRestfulApiResponseFlusher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import ConcreteMemberNotFoundRestfulApiError from '../errors/concretes/ConcreteMemberNotFoundRestfulApiError.js'

/**
* Base class for flushing RESTful API responses.
*/
export default class BaseRestfulApiResponseFlusher {
/**
* Constructor.
*
* @param {BaseRestfulApiResponseFlusherParams} params - Parameters of this constructor.
*/
constructor ({
expressResponse,
renderResponse,
}) {
this.expressResponse = expressResponse
this.renderResponse = renderResponse
}

/**
* Factory method.
*
* @template {X extends typeof BaseRestfulApiResponseFlusher ? X : never} T, X
* @param {BaseRestfulApiResponseFlusherParams} params - Parameters of this factory method.
* @returns {InstanceType<T>} - Instance of this constructor.
* @this {T}
*/
static create ({
expressResponse,
renderResponse,
}) {
return /** @type {InstanceType<T>} */ (
new this({
expressResponse,
renderResponse,
})
)
}

/**
* get: Headers of the response.
*
* @returns {Record<string, string>} - Headers of the response.
* @throws {Error} - If the headers are not defined.
*/
static get headers () {
return {
'Content-Type': this.contentType,
}
}

/**
* get: Content type of the response.
*
* @abstract
* @returns {string} - Content type of the response.
* @throws {Error} - If the content type is not defined.
*/
static get contentType () {
throw ConcreteMemberNotFoundRestfulApiError.create({
value: {
memberName: 'BaseRestfulApiResponseFlusher.get:contentType',
},
})
}

/**
* get: Flusher constructor.
*
* @returns {RestfulApiType.ResponseFlusherCtor} - Flusher constructor.
*/
get Ctor () {
return /** @type {typeof BaseRestfulApiResponseFlusher} */ (this.constructor)
}

/**
* Flush the response.
*
* @abstract
* @throws {Error} - If the response cannot be flushed.
*/
flushResponse () {
this.flushStatus()

this.flushHeaders()

this.flushResponseBody()
}

/**
* Flush the response status.
*/
flushStatus () {
this.expressResponse.status(
this.renderResponse.status
)
}

/**
* Flush the response headers.
*
* @return {void}
*/
flushHeaders () {
const headers = this.collectHeaders()

this.expressResponse.set(headers)
}

/**
* Collect headers.
*
* @returns {Record<string, string>} - Headers of the response.
*/
collectHeaders () {
return {
...this.Ctor.headers,
...this.renderResponse.headers,
}
}

/**
* Flush the response body.
*
* @abstract
* @throws {Error} - If the response body cannot be flushed.
*/
flushResponseBody () {
throw ConcreteMemberNotFoundRestfulApiError.create({
value: {
memberName: 'BaseRestfulApiResponseFlusher#flushResponseBody()',
},
})
}
}

/**
* @typedef {{
* expressResponse: ExpressType.Response
* renderResponse: RestfulApiType.RenderResponse
* }} BaseRestfulApiResponseFlusherParams
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import BaseRestfulApiResponseFlusher from '../BaseRestfulApiResponseFlusher.js'

/**
* CSV RESTful API response flusher.
*/
export default class CsvRestfulApiResponseFlusher extends BaseRestfulApiResponseFlusher {
/** @override */
static get contentType () {
return 'text/csv'
}

/** @override */
flushResponseBody () {
this.expressResponse.send(
this.renderResponse.content
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import BaseRestfulApiResponseFlusher from '../BaseRestfulApiResponseFlusher.js'

/**
* HTML RESTful API response flusher.
*/
export default class HtmlRestfulApiResponseFlusher extends BaseRestfulApiResponseFlusher {
/** @override */
static get contentType () {
return 'text/html'
}

/** @override */
flushResponseBody () {
this.expressResponse.send(
this.renderResponse.content
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import BaseRestfulApiResponseFlusher from '../BaseRestfulApiResponseFlusher.js'

/**
* JSON RESTful API response flusher.
*/
export default class JsonRestfulApiResponseFlusher extends BaseRestfulApiResponseFlusher {
/** @override */
static get contentType () {
return 'application/json'
}

/** @override */
flushResponseBody () {
const json = this.generateJsonBody()

this.expressResponse.json(json)
}

/**
* Generate JSON body.
*
* @returns {{
* content: * | null
* error: RestfulApiType.Error | null
* }} - An value hash of JSON body.
*/
generateJsonBody () {
return {
content: this.renderResponse.content,
error: this.renderResponse.error,
}
}
}
Loading