@potentii/rest-envelopes

    1.0.2 • Public • Published

    REST Envelopes

    Enveloping of REST messages


    Usage

    Example in an expressjs application:

    import { RequestEnvelope, ResponseEnvelope, ApiError, ApiErrorDetail } from '@potentii/rest-envelopes';
    
    app.post('/api/users', (req, res, next) => {
       const body = RequestEnvelope.from(req.body);
    	
       // ...
       
       if(!body.data.fullname) 
          return res
             .status(400)
             .json(ResponseEnvelope.withError(
                ApiError.create('VALIDATION_ERR', 'Registration have failed', ApiErrorDetail.create('USER_ID_INVALID', 'The full name of the user must be set', 'fullname', body.data.fullname))
             ))
             .end();
        
       // Response body:
       // {
       //    error: {
       //       code: 'VALIDATION_ERR',
       //       message: 'Registration have failed',
       //       errors: [
       //          {
       //             code: 'USER_ID_INVALID', 
       //             message: 'The full name of the user must be set', 
       //             field: 'fullname', 
       //             value: null
       //          }
       //       ]
       //    }
       // }
    	
       // ...
       
       let createdUser = {
          user_id: 1,
          fullname: 'John Doe'
       };
       
       res
          .status(200)
          .json(ResponseEnvelope.withData(createdUser)) // data can be an object or a list
          .end();
    
       // Response body:
       // {
       //    data: {
       //       user_id: 1,
       //       fullname: 'John Doe'
       //    }
       //    
       // }
       
    	
    });

    License

    MIT

    Install

    npm i @potentii/rest-envelopes

    DownloadsWeekly Downloads

    7

    Version

    1.0.2

    License

    MIT

    Unpacked Size

    6.22 kB

    Total Files

    8

    Last publish

    Collaborators

    • potentii