1 | /**
|
2 | * @file Defines the CreateOneEndpoint class.
|
3 | *
|
4 | * @author Kevin Sanders <kevin@c2cschools.com>
|
5 | * @since 5.0.0
|
6 | * @license See LICENSE.md for details about licensing.
|
7 | * @copyright 2017 C2C Schools, LLC
|
8 | */
|
9 |
|
10 | ;
|
11 |
|
12 | const BaseEndpoint = require( "./BaseEndpoint" );
|
13 | const ERRORS = require( "../errors" );
|
14 |
|
15 | /**
|
16 | * The parent class for all CREATE ('POST') endpoints that create one record.
|
17 | *
|
18 | * @memberOf Endpoint
|
19 | * @extends Endpoint.BaseEndpoint
|
20 | */
|
21 | class CreateOneEndpoint extends BaseEndpoint {
|
22 |
|
23 | /**
|
24 | * @param {Object} cfg - Basic endpoint settings.
|
25 | * @param {Object} [overrides] - A configuration object that allows certain,
|
26 | * default, behaviors to be overridden. This parameter is primarily
|
27 | * used by testing interfaces.
|
28 | */
|
29 | constructor( cfg, overrides ) {
|
30 |
|
31 | // Apply Overrides
|
32 | cfg = Object.assign( {}, cfg, overrides );
|
33 |
|
34 | // Set Endpoint Type
|
35 | cfg.endpointType = "createOne";
|
36 |
|
37 | // Define the default response class
|
38 | cfg.defaultSuccessResponse = "CreateOneResponse";
|
39 |
|
40 | // Call Parent
|
41 | super( cfg );
|
42 | }
|
43 |
|
44 | /**
|
45 | * Get a schema representing the request body within a valid request, in
|
46 | * JSON Schema object format.
|
47 | *
|
48 | * @public
|
49 | * @throws {Errors.MissingRequestBodySchemaError} If the schema is requested
|
50 | * but is not defined.
|
51 | * @returns {Promise<Object>} Parameter schema.
|
52 | */
|
53 | getRequestBodySchema() {
|
54 |
|
55 | const me = this;
|
56 |
|
57 | // Dependencies
|
58 | const BB = me.$dep( "bluebird" );
|
59 |
|
60 | if ( me.hasConfigValue( "requestBodySchema" ) ) {
|
61 |
|
62 | return BB.resolve(
|
63 | me.getConfigValue( "requestBodySchema" )
|
64 | );
|
65 | }
|
66 |
|
67 | return me._loadRequestBodySchema()
|
68 | .then( function ( requestBodySchema ) {
|
69 |
|
70 | me.setConfigValue( "requestBodySchema", requestBodySchema );
|
71 |
|
72 | return requestBodySchema;
|
73 | } );
|
74 | }
|
75 |
|
76 | /**
|
77 | * Loads the request body schema (from a file) using the
|
78 | * `requestBodySchemaPath`.
|
79 | *
|
80 | * @private
|
81 | * @throws {Errors.MissingRequestBodySchemaError} If the schema could not be
|
82 | * loaded (for any reason).
|
83 | * @returns {Promise<Object>} The loaded request body schema.
|
84 | */
|
85 | _loadRequestBodySchema() {
|
86 |
|
87 | const me = this;
|
88 |
|
89 | // Dependencies
|
90 | const BB = me.$dep( "bluebird" );
|
91 |
|
92 | return BB.try( function () {
|
93 |
|
94 | let projectPath = me.projectPath;
|
95 |
|
96 | /** @type Util.SchemaGenerator */
|
97 | let SchemaGenerator = require( "../util/SchemaGenerator" );
|
98 |
|
99 | let schemaGenerator = new SchemaGenerator( {
|
100 | serviceRootPath: projectPath,
|
101 | } );
|
102 |
|
103 | return schemaGenerator.buildSchema( me.requestBodySchemaPath );
|
104 |
|
105 | } ).catch( function ( err ) {
|
106 |
|
107 | throw new ERRORS.MissingRequestBodySchemaError(
|
108 | err,
|
109 | "Failed to load the request body schema."
|
110 | );
|
111 | } );
|
112 | }
|
113 | }
|
114 |
|
115 | module.exports = CreateOneEndpoint;
|