@@ -6,11 +6,17 @@ import btoa from 'btoa'
66import url from 'url'
77import http , { mergeInQueryOrForm } from './http'
88import { getOperationRaw , idFromPathMethod , legacyIdFromPathMethod } from './helpers'
9+ import createError from './specmap/lib/create-error'
910
1011const arrayOrEmpty = ( ar ) => {
1112 return Array . isArray ( ar ) ? ar : [ ]
1213}
1314
15+ const OperationNotFoundError = createError ( 'OperationNotFoundError' , function ( message , extra , oriError ) {
16+ this . originalError = oriError
17+ Object . assign ( this , extra || { } )
18+ } )
19+
1420// For stubbing in tests
1521export const self = {
1622 buildRequest
@@ -87,20 +93,26 @@ export function buildRequest({
8793 return req
8894 }
8995
90- const { operation = { } , method, pathName} = getOperationRaw ( spec , operationId )
96+ const operationRaw = getOperationRaw ( spec , operationId )
97+ if ( ! operationRaw ) {
98+ throw new OperationNotFoundError ( `Operation ${ operationId } not found` )
99+ }
100+
101+ const { operation = { } , method, pathName} = operationRaw
91102
92103 req . url += pathName // Have not yet replaced the path parameters
93104 req . method = ( `${ method } ` ) . toUpperCase ( )
94105
95106 parameters = parameters || { }
107+ const path = spec . paths [ pathName ] || { }
96108
97109 if ( responseContentType ) {
98110 req . headers . accept = responseContentType
99111 }
100112
101113 // Add values to request
102114 arrayOrEmpty ( operation . parameters ) // operation parameters
103- . concat ( arrayOrEmpty ( spec . paths [ pathName ] . parameters ) ) // path parameters
115+ . concat ( arrayOrEmpty ( path . parameters ) ) // path parameters
104116 . forEach ( ( parameter ) => {
105117 const builder = parameterBuilders [ parameter . in ]
106118 let value
@@ -219,7 +231,7 @@ export function baseUrl({spec, scheme, contextUrl = ''}) {
219231// Add security values, to operations - that declare their need on them
220232export function applySecurities ( { request, securities = { } , operation = { } , spec} ) {
221233 const result = assign ( { } , request )
222- const { authorized = { } , specSecurity = { } } = securities
234+ const { authorized = { } , specSecurity = [ ] } = securities
223235 const security = operation . security || specSecurity
224236 const isAuthorized = authorized && ! ! Object . keys ( authorized ) . length
225237 const securityDef = spec . securityDefinitions
0 commit comments