- 
                Notifications
    You must be signed in to change notification settings 
- Fork 17
Print deprecation warning from Kuzzle #614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            MathieuVeber
  merged 12 commits into
  7-dev
from
#593-Print-deprecation-warning-from-Kuzzle
  
      
      
   
  Mar 16, 2021 
      
    
  
     Merged
                    Changes from 8 commits
      Commits
    
    
            Show all changes
          
          
            12 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      47baf03
              
                Print deprecation warning from Kuzzle
              
              
                MathieuVeber 6fd4464
              
                Lint
              
              
                MathieuVeber 5f35a97
              
                Tests
              
              
                MathieuVeber 2efc412
              
                Lint
              
              
                MathieuVeber fc818d8
              
                Doc
              
              
                MathieuVeber 78e512c
              
                Update src/controllers/Base.ts
              
              
                Aschen 476ff49
              
                Disable in production
              
              
                MathieuVeber ba5ddb8
              
                Adding production mode awareness
              
              
                MathieuVeber 4ed9f73
              
                .then necessary
              
              
                MathieuVeber 8feafac
              
                Lint...
              
              
                MathieuVeber 7050010
              
                Update src/types/ResponsePayload.ts
              
              
                MathieuVeber 8b869af
              
                Deleting multiple NODE_ENV calls
              
              
                MathieuVeber File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { ResponsePayload } from 'src/types/ResponsePayload'; | ||
|  | ||
| export class Deprecation { | ||
|  | ||
| private _deprecationWarning: boolean; | ||
|  | ||
| constructor (deprecationWarning: boolean) { | ||
| this._deprecationWarning = deprecationWarning; | ||
| } | ||
|  | ||
| /** | ||
| * Warn the developer that he is using a deprecated action (disabled if NODE_ENV=production) | ||
| * | ||
| * @param response Result of a query to the API | ||
| * | ||
| * @returns Same as response param, just like a middleware | ||
| */ | ||
| logDeprecation (response: ResponsePayload) { | ||
| if ( process.env.NODE_ENV !== 'production' | ||
|         
                  Aschen marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| && this._deprecationWarning | ||
| && response.deprecations | ||
| && response.deprecations.length | ||
| ) { | ||
| for (const deprecation of response.deprecations) { | ||
| // eslint-disable-next-line no-console | ||
| console.warn(deprecation.message); | ||
| } | ||
| } | ||
| return response; | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| const should = require('should'); | ||
| const sinon = require('sinon'); | ||
| const { Deprecation } = require('../../src/utils/Deprecation'); | ||
|  | ||
| describe('Deprecation', () => { | ||
| let deprecationHandler, response; | ||
| const sandbox = sinon.createSandbox(); | ||
|  | ||
| beforeEach(()=>{ | ||
| sandbox.stub(console, 'warn'); | ||
| deprecationHandler = new Deprecation(true); | ||
| process.env.NODE_ENV = 'development'; | ||
|  | ||
| response = { | ||
| action: 'test', | ||
| collection: 'collection', | ||
| controller: 'controller', | ||
| deprecations: [ | ||
| { | ||
| message: 'Use this route instead: http://kuzzle:7512/test/succeed', | ||
| version: '6.6.6' | ||
| } | ||
| ], | ||
| error: null, | ||
| index: null, | ||
| node: 'nodeJS', | ||
| requestId: 'idididididid', | ||
| result: true, | ||
| status: 200, | ||
| volatile: null | ||
| }; | ||
| }); | ||
|  | ||
| afterEach(()=>{ | ||
| sandbox.restore(); | ||
| }); | ||
|  | ||
| it('should warn the developer that he is using a deprecated action', () => { | ||
| deprecationHandler.logDeprecation(response); | ||
|  | ||
| should(console.warn) | ||
| .be.calledOnce() | ||
| .be.calledWith(response.deprecations[0].message); | ||
| }); | ||
|  | ||
| it('should return the same response as it has received', () => { | ||
| should(deprecationHandler.logDeprecation(response)).match(response); | ||
| }); | ||
|  | ||
| it('should handle multiple deprecations', () => { | ||
| response.deprecations.push(response); | ||
|  | ||
| deprecationHandler.logDeprecation(response); | ||
|  | ||
| should(console.warn).be.calledTwice(); | ||
| }); | ||
|  | ||
| it('should not warn the developer if he refused to', () => { | ||
| deprecationHandler = new Deprecation(false); | ||
|  | ||
| deprecationHandler.logDeprecation(response); | ||
|  | ||
| should(console.warn).not.have.been.called(); | ||
| }); | ||
|  | ||
| it('should not warn the developer if there is no deprecation', () => { | ||
| response.deprecations = []; | ||
|  | ||
| deprecationHandler.logDeprecation(response); | ||
|  | ||
| should(console.warn).not.have.been.called(); | ||
| }); | ||
|  | ||
| it('should not warn the developer in production', () => { | ||
| process.env.NODE_ENV = 'production'; | ||
|  | ||
| deprecationHandler.logDeprecation(response); | ||
|  | ||
| should(console.warn).not.have.been.called(); | ||
| }); | ||
| }); | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.