UNPKG

1.15 kBJavaScriptView Raw
1/**
2 * @file Defines the CreateOneResponse class.
3 *
4 * @author Luke Chavers <luke@c2cschools.com>
5 * @author Kevin Sanders <kevin@c2cschools.com>
6 * @since 5.0.0
7 * @license See LICENSE.md for details about licensing.
8 * @copyright 2017 C2C Schools, LLC
9 */
10
11"use strict";
12
13const SuccessResponse = require( "./SuccessResponse" );
14
15/**
16 * This response class represents an API response for a 'CreateOne' endpoint.
17 *
18 * @memberOf Response
19 * @extends Response.SuccessResponse
20 */
21module.exports = class CreateOneResponse extends SuccessResponse {
22
23 /**
24 * @inheritDoc
25 *
26 */
27 _initialize( cfg ) {
28
29 cfg.isError = false;
30 cfg.statusCode = 201; // Created
31
32 // Call parent
33 super._initialize( cfg );
34 }
35
36 /**
37 * Creates a JSON-API compatible scaffold for the response body [object].
38 *
39 * This method extends the `BaseResponse` scaffold by adding the data.
40 *
41 * @private
42 * @returns {Object} Response body.
43 */
44 _createResponseBody() {
45
46 const me = this;
47
48 let sBody = super._createResponseBody();
49
50 if ( me.data ) {
51
52 sBody.data = me.data._serializeToJsonApiObject();
53
54 } else {
55
56 sBody.data = {};
57 }
58
59 return sBody;
60 }
61};