How to use middy - 10 common examples

To help you get started, we’ve selected a few middy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github josephluck / internote / services / notes / update.ts View on Github external
}
  const note = await updateNoteById(noteId, userId, event.body);
  return callback(null, success(note));
};

export const validator = validateRequestBody({
  noteId: [],
  userId: [],
  content: [required], // TODO: validate slate schema
  title: [required, isString],
  tags: [required, isArray(v => typeof v === "string")],
  dateUpdated: [],
  overwrite: []
});

export const handler = middy(update)
  .use(jsonBodyParser())
  .use(validator)
  .use(encodeResponse())
  .use(jsonErrorHandler())
  .use(cors());
github josephluck / internote / services / notes / update.ts View on Github external
export const validator = validateRequestBody({
  noteId: [],
  userId: [],
  content: [required], // TODO: validate slate schema
  title: [required, isString],
  tags: [required, isArray(v => typeof v === "string")],
  dateUpdated: [],
  overwrite: []
});

export const handler = middy(update)
  .use(jsonBodyParser())
  .use(validator)
  .use(encodeResponse())
  .use(jsonErrorHandler())
  .use(cors());
github netlify / cli / src / functions-templates / js / using-middleware / using-middleware.js View on Github external
},
    required: ['body']
  }
}

/* Export inputSchema & outputSchema for automatic documentation */
exports.schema = schema

exports.handler = middy(businessLogic)
  .use(httpHeaderNormalizer())
  // parses the request body when it's a JSON and converts it to an object
  .use(jsonBodyParser())
  // validates the input
  .use(validator({ inputSchema: schema.input }))
  // handles common http errors and returns proper responses
  .use(httpErrorHandler())
github DavidWells / netlify-functions-workshop / lessons-code-complete / core-concepts / 7-using-middleware / functions / using-middleware.js View on Github external
return ({
    before: (handler, next) => {
      // might read options from `config`
    },
    after: (handler, next) => {
      // might read options from `config`
    },
    onError: (handler, next) => {
      // might read options from `config`
    }
  })
}

// Export the handler
exports.handler = middy(businessLogic)
  .use(jsonBodyParser())
  .use(myMiddleware())
github netlify / cli / src / functions-templates / js / using-middleware / using-middleware.js View on Github external
result: { type: 'string' },
          message: { type: 'string' }
        }
      }
    },
    required: ['body']
  }
}

/* Export inputSchema & outputSchema for automatic documentation */
exports.schema = schema

exports.handler = middy(businessLogic)
  .use(httpHeaderNormalizer())
  // parses the request body when it's a JSON and converts it to an object
  .use(jsonBodyParser())
  // validates the input
  .use(validator({ inputSchema: schema.input }))
  // handles common http errors and returns proper responses
  .use(httpErrorHandler())
github mcnamee / serverless-jwt-auth / app / Handlers / Users.js View on Github external
return userByEmail(queryValues[':em']) // Check if the new email already exists
    .then((foundUser) => {
      if (foundUser && foundUser.email) {
        // New email exists, and doesn't belong to the current user
        if (foundUser.email === queryValues[':em'] && foundUser.id !== id) {
          throw new Error('That email belongs to another user');
        }
      }
    })
    .then(() => DB.update(params).promise()) // Update the data to the DB
    .then(user => cb(null, {message: 'Success - user updated', data: user }));
}

module.exports.update = middy(update)
  .use(jsonBodyParser())
  .use(validatorMiddleware({ inputSchema: requestSchema.update }))
  .use(apiResponseMiddleware());
github netlify / cli / src / functions-templates / js / using-middleware / using-middleware.js View on Github external
required: ['result', 'message'],
        properties: {
          result: { type: 'string' },
          message: { type: 'string' }
        }
      }
    },
    required: ['body']
  }
}

/* Export inputSchema & outputSchema for automatic documentation */
exports.schema = schema

exports.handler = middy(businessLogic)
  .use(httpHeaderNormalizer())
  // parses the request body when it's a JSON and converts it to an object
  .use(jsonBodyParser())
  // validates the input
  .use(validator({ inputSchema: schema.input }))
  // handles common http errors and returns proper responses
  .use(httpErrorHandler())
github theburningmonk / manning-aws-lambda-in-motion / functions / get-index.js View on Github external
cloudwatch.incrCount('RestaurantsReturned', restaurants.length);

  const response = {
    statusCode: 200,
    body: html,
    headers: {
      'content-type': 'text/html; charset=UTF-8'
    }
  };

  callback(null, response);
});

module.exports.handler = wrapper(handler)
  .use(ssm({
    cache: true,
    cacheExpiryInMillis: 3 * 60 * 1000, // 3 mins
    setToContext: true,
    names: {
      restaurants_api: `/bigmouth/${STAGE}/restaurants_api`,
      orders_api: `/bigmouth/${STAGE}/orders_api`
    }
  }))
  .use(secretsManager({
    cache: true,
    cacheExpiryInMillis: 3 * 60 * 1000, // 3 mins
    secrets: {
      cognito: `/bigmouth/${STAGE}/cognito`
    }
  }));
github netlify / cli / src / functions-templates / js / using-middleware / using-middleware.js View on Github external
}
      }
    },
    required: ['body']
  }
}

/* Export inputSchema & outputSchema for automatic documentation */
exports.schema = schema

exports.handler = middy(businessLogic)
  .use(httpHeaderNormalizer())
  // parses the request body when it's a JSON and converts it to an object
  .use(jsonBodyParser())
  // validates the input
  .use(validator({ inputSchema: schema.input }))
  // handles common http errors and returns proper responses
  .use(httpErrorHandler())
github theburningmonk / manning-aws-lambda-in-motion / functions / get-index.js View on Github external
};

  callback(null, response);
});

module.exports.handler = wrapper(handler)
  .use(ssm({
    cache: true,
    cacheExpiryInMillis: 3 * 60 * 1000, // 3 mins
    setToContext: true,
    names: {
      restaurants_api: `/bigmouth/${STAGE}/restaurants_api`,
      orders_api: `/bigmouth/${STAGE}/orders_api`
    }
  }))
  .use(secretsManager({
    cache: true,
    cacheExpiryInMillis: 3 * 60 * 1000, // 3 mins
    secrets: {
      cognito: `/bigmouth/${STAGE}/cognito`
    }
  }));