bin/middleware.js
function findMatchingExample(req, code, type) {
    const swagger = req.swagger.root;
    const responses = req.swagger.rel.responses;

    // if no responses then exit
    const responseKeys = responses && typeof responses === 'object' ? Object.keys(responses) : [];
    if (responseKeys.length === 0) return { code: 501, type: undefined };

    // get first code if not provided
    if (arguments.length < 1) code = responseKeys[0];

    // validate that responses exist
    const responseSchema = responses[code];
    if (!responseSchema) return { code: 501, type: undefined };

    const examples = responses[code].examples;
    const accept = type ?
        type :
        req.headers.hasOwnProperty('accept')
            ? req.headers.accept
            : Array.isArray(swagger.produces) && swagger.produces.length > 0
                ? swagger.produces[0]
                : examples && Object.keys(examples)[0];

    // check if there are examples
    const keys = examples ? Object.keys(examples) : [];
    const typesLength = keys.length;
    if (typesLength === 0) return { code: 501, type: undefined };

    // if anything is accepted then return first example
    if (accept === '*') return { code: code, type: keys[0] };

    // determine what types and subtypes are supported by examples
    const types = keys.map(key => {
        const ar = key.split('/');
        return { type: ar[0] || '*', subtype: ar[1] || '*' };
    });

    // find all the types that the client accepts
    const accepts = accept.split(/,\s*/);
    const length = accepts.length;
    for (let i = 0; i < length; i++) {

        // remove variable from type and separate type and subtype
        const item = accepts[i].split(';')[0];
        const parts = item.split('/');
        const a = {
            type: parts[0] || '*',
            subtype: parts[1] || '*'
        };

        // look for a match between types and accepts
        for (let j = 0; j < typesLength; j++) {
            const t = types[j];
            if ((t.type === '*' || a.type === '*' || a.type === t.type) &&
                (t.subtype === '*' || a.subtype === '*' || a.subtype === t.subtype)) return { code: code, type: keys[j] };
        }
    }

    return { code: 406, type: undefined };
}
function findMatchingExample(req, code, type) {
    const swagger = req.swagger.root;
    const responses = req.swagger.rel.responses;

    // if no responses then exit
    const responseKeys = responses && typeof responses === 'object' ? Object.keys(responses) : [];
    if (responseKeys.length === 0) return { code: 501, type: undefined };

    // get first code if not provided
    if (arguments.length < 1) code = responseKeys[0];

    // validate that responses exist
    const responseSchema = responses[code];
    if (!responseSchema) return { code: 501, type: undefined };

    const examples = responses[code].examples;
    const accept = type ?
        type :
        req.headers.hasOwnProperty('accept')
            ? req.headers.accept
            : Array.isArray(swagger.produces) && swagger.produces.length > 0
                ? swagger.produces[0]
                : examples && Object.keys(examples)[0];

    // check if there are examples
    const keys = examples ? Object.keys(examples) : [];
    const typesLength = keys.length;
    if (typesLength === 0) return { code: 501, type: undefined };

    // if anything is accepted then return first example
    if (accept === '*') return { code: code, type: keys[0] };

    // determine what types and subtypes are supported by examples
    const types = keys.map(key => {
        const ar = key.split('/');
        return { type: ar[0] || '*', subtype: ar[1] || '*' };
    });

    // find all the types that the client accepts
    const accepts = accept.split(/,\s*/);
    const length = accepts.length;
    for (let i = 0; i < length; i++) {

        // remove variable from type and separate type and subtype
        const item = accepts[i].split(';')[0];
        const parts = item.split('/');
        const a = {
            type: parts[0] || '*',
            subtype: parts[1] || '*'
        };

        // look for a match between types and accepts
        for (let j = 0; j < typesLength; j++) {
            const t = types[j];
            if ((t.type === '*' || a.type === '*' || a.type === t.type) &&
                (t.subtype === '*' || a.subtype === '*' || a.subtype === t.subtype)) return { code: code, type: keys[j] };
        }
    }

    return { code: 406, type: undefined };
}
function findMatchingExample ( req , code , type ) { const swagger = req . swagger . root ; const responses = req . swagger . rel . responses ; // if no responses then exit const responseKeys = responses && typeof responses === 'object' ? Object . keys ( responses ) : [ ] ; if ( responseKeys . length === 0 ) return { code : 501 , type : undefined } ; // get first code if not provided if ( arguments . length < 1 ) code = responseKeys [ 0 ] ; // validate that responses exist const responseSchema = responses [ code ] ; if ( ! responseSchema ) return { code : 501 , type : undefined } ; const examples = responses [ code ] . examples ; const accept = type ? type : req . headers . hasOwnProperty ( 'accept' ) ? req . headers . accept : Array . isArray ( swagger . produces ) && swagger . produces . length > 0 ? swagger . produces [ 0 ] : examples && Object . keys ( examples ) [ 0 ] ; // check if there are examples const keys = examples ? Object . keys ( examples ) : [ ] ; const typesLength = keys . length ; if ( typesLength === 0 ) return { code : 501 , type : undefined } ; // if anything is accepted then return first example if ( accept === '*' ) return { code : code , type : keys [ 0 ] } ; // determine what types and subtypes are supported by examples const types = keys . map ( key => { const ar = key . split ( '/' ) ; return { type : ar [ 0 ] || '*' , subtype : ar [ 1 ] || '*' } ; } ) ; // find all the types that the client accepts const accepts = accept . split ( / ,\s* / ) ; const length = accepts . length ; for ( let i = 0 ; i < length ; i ++ ) { // remove variable from type and separate type and subtype const item = accepts [ i ] . split ( ';' ) [ 0 ] ; const parts = item . split ( '/' ) ; const a = { type : parts [ 0 ] || '*' , subtype : parts [ 1 ] || '*' } ; // look for a match between types and accepts for ( let j = 0 ; j < typesLength ; j ++ ) { const t = types [ j ] ; if ( ( t . type === '*' || a . type === '*' || a . type === t . type ) && ( t . subtype === '*' || a . subtype === '*' || a . subtype === t . subtype ) ) return { code : code , type : keys [ j ] } ; } } return { code : 406 , type : undefined } ; }
Look through swagger examples and find a match based on the accept content type
@param {object} req
@param {string|number} [code]
@param {string} [type]
@returns {{ code: number, type: string|undefined }}
Look through swagger examples and find a match based on the accept content type