UNPKG

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