@@ -5,8 +5,15 @@ import isArray from 'lodash/isArray'
55import btoa from 'btoa'
66import url from 'url'
77import http , { mergeInQueryOrForm } from '../http'
8- import { getOperationRaw , idFromPathMethod , legacyIdFromPathMethod , isOAS3 } from '../helpers'
98import createError from '../specmap/lib/create-error'
9+ import SWAGGER2_PARAMETER_BUILDERS from './swagger2-parameter-builders'
10+ import OAS3_PARAMETER_BUILDERS from './oas3-parameter-builders'
11+ import {
12+ getOperationRaw ,
13+ idFromPathMethod ,
14+ legacyIdFromPathMethod ,
15+ isOAS3
16+ } from '../helpers'
1017
1118const arrayOrEmpty = ( ar ) => {
1219 return Array . isArray ( ar ) ? ar : [ ]
@@ -26,18 +33,6 @@ export const self = {
2633 buildRequest
2734}
2835
29- // These functions will update the request.
30- // They'll be given {req, value, paramter, spec, operation}.
31-
32-
33- export const PARAMETER_BUILDERS = {
34- body : bodyBuilder ,
35- header : headerBuilder ,
36- query : queryBuilder ,
37- path : pathBuilder ,
38- formData : formDataBuilder
39- }
40-
4136// Execute request, with the given operationId and parameters
4237// pathName/method or operationId is optional
4338export function execute ( {
@@ -77,7 +72,15 @@ export function buildRequest({
7772} ) {
7873 const specIsOAS3 = isOAS3 ( spec )
7974
80- parameterBuilders = parameterBuilders || PARAMETER_BUILDERS
75+ if ( ! parameterBuilders ) {
76+ // user did not provide custom parameter builders
77+ if ( specIsOAS3 ) {
78+ parameterBuilders = OAS3_PARAMETER_BUILDERS
79+ }
80+ else {
81+ parameterBuilders = SWAGGER2_PARAMETER_BUILDERS
82+ }
83+ }
8184
8285 // Base Template
8386 let req = {
@@ -246,68 +249,6 @@ export function buildRequest({
246249 return req
247250}
248251
249- // Add the body to the request
250- export function bodyBuilder ( { req, value, specIsOAS3} ) {
251- if ( specIsOAS3 ) {
252- return
253- }
254- req . body = value
255- }
256-
257- // Add a form data object.
258- export function formDataBuilder ( { req, value, parameter} ) {
259- // REVIEW: OAS3: check for any parameter changes that affect the builder
260- req . form = req . form || { }
261- if ( value || parameter . allowEmptyValue ) {
262- req . form [ parameter . name ] = {
263- value,
264- allowEmptyValue : parameter . allowEmptyValue ,
265- collectionFormat : parameter . collectionFormat ,
266- }
267- }
268- }
269-
270- // Add a header to the request
271- export function headerBuilder ( { req, parameter, value} ) {
272- // REVIEW: OAS3: check for any parameter changes that affect the builder
273- req . headers = req . headers || { }
274- if ( typeof value !== 'undefined' ) {
275- req . headers [ parameter . name ] = value
276- }
277- }
278-
279- // Replace path paramters, with values ( ie: the URL )
280- export function pathBuilder ( { req, value, parameter} ) {
281- // REVIEW: OAS3: check for any parameter changes that affect the builder
282- req . url = req . url . replace ( `{${ parameter . name } }` , encodeURIComponent ( value ) )
283- }
284-
285- // Add a query to the `query` object, which will later be stringified into the URL's search
286- export function queryBuilder ( { req, value, parameter} ) {
287- // REVIEW: OAS3: check for any parameter changes that affect the builder
288- req . query = req . query || { }
289-
290- if ( value === false && parameter . type === 'boolean' ) {
291- value = 'false'
292- }
293-
294- if ( value === 0 && [ 'number' , 'integer' ] . indexOf ( parameter . type ) > - 1 ) {
295- value = '0'
296- }
297-
298- if ( value ) {
299- req . query [ parameter . name ] = {
300- collectionFormat : parameter . collectionFormat ,
301- value
302- }
303- }
304- else if ( parameter . allowEmptyValue ) {
305- const paramName = parameter . name
306- req . query [ paramName ] = req . query [ paramName ] || { }
307- req . query [ paramName ] . allowEmptyValue = true
308- }
309- }
310-
311252const stripNonAlpha = str => ( str ? str . replace ( / \W / g, '' ) : null )
312253
313254export function baseUrl ( obj ) {
0 commit comments