- 
 
- 
  Notifications
 You must be signed in to change notification settings 
- Fork 54
Convert Request, Response, CodeResponseType, TokenResponseType to ES6 classes #225
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
 
 
 jankapunkt
 merged 4 commits into
 node-oauth:development 
from
menewman:fix-convert-request-response-classes-to-es6 
 
 
 
  
 Aug 26, 2023 
 
 
 
  Merged
 Changes from all commits
 Commits
 
 
 Show all changes
 
 
 4 commits
 
 
 Select commit
 Hold shift + click to select a range
 
 0d142f0
 
 Convert Request, Response, CodeResponseType, TokenResponseType to ES6...
 
 
 menewman a23d682
 
 Use types.flat() to handle 'is' arguments
 
 
 menewman 4c7927a
 
 Merge branch 'development' into fix-convert-request-response-classes-...
 
 
 menewman 8ea6699
 
 Push unit tests that verify that prototype methods can't be overwritten
 
 
 menewman 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
 
 
 
 
 
 
 
 
 83 changes: 35 additions & 48 deletions
 
 
 
 lib/response.js
 
 
 
 
  
 
 
 
 
 
 
 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 | 
|---|---|---|
| @@ -1,58 +1,45 @@ | ||
| 'use strict'; | ||
|  | ||
| /** | ||
| * Constructor. | ||
| */ | ||
|  | ||
| function Response(options) { | ||
| options = options || {}; | ||
| class Response { | ||
| constructor({ headers = {}, body = {}, ...otherOptions } = {}) { | ||
| this.status = 200; | ||
| this.body = body; | ||
| this.headers = {}; | ||
|  | ||
| // Store the headers in lower case. | ||
| Object.entries(headers).forEach(([header, value]) => { | ||
| this.headers[header.toLowerCase()] = value; | ||
| }); | ||
|  | ||
| // Store additional properties of the response object passed in | ||
| Object.entries(otherOptions) | ||
| .filter(([property]) => !this[property]) | ||
|  
 jankapunkt marked this conversation as resolved.
 Show resolved
 Hide resolved | ||
| .forEach(([property, value]) => { | ||
| this[property] = value; | ||
| }); | ||
| } | ||
|  | ||
| this.body = options.body || {}; | ||
| this.headers = {}; | ||
| this.status = 200; | ||
| /** | ||
| * Get a response header. | ||
| */ | ||
| get(field) { | ||
| return this.headers[field.toLowerCase()]; | ||
| } | ||
|  | ||
| // Store the headers in lower case. | ||
| for (const field in options.headers) { | ||
| if (Object.prototype.hasOwnProperty.call(options.headers, field)) { | ||
| this.headers[field.toLowerCase()] = options.headers[field]; | ||
| } | ||
| /** | ||
| * Redirect response. | ||
| */ | ||
| redirect(url) { | ||
| this.set('Location', url); | ||
| this.status = 302; | ||
| } | ||
|  | ||
| // Store additional properties of the response object passed in | ||
| for(constpropertyinoptions){ | ||
| if(Object.prototype.hasOwnProperty.call(options,property)&&!this[property]){ | ||
| this[property]=options[property]; | ||
| } | ||
| /** | ||
| * Set a response header. | ||
| */ | ||
| set(field,value){ | ||
| this.headers[field.toLowerCase()]=value; | ||
| } | ||
| } | ||
|  | ||
| /** | ||
| * Get a response header. | ||
| */ | ||
|  | ||
| Response.prototype.get = function(field) { | ||
| return this.headers[field.toLowerCase()]; | ||
| }; | ||
|  | ||
| /** | ||
| * Redirect response. | ||
| */ | ||
|  | ||
| Response.prototype.redirect = function(url) { | ||
| this.set('Location', url); | ||
| this.status = 302; | ||
| }; | ||
|  | ||
| /** | ||
| * Set a response header. | ||
| */ | ||
|  | ||
| Response.prototype.set = function(field, value) { | ||
| this.headers[field.toLowerCase()] = value; | ||
| }; | ||
|  | ||
| /** | ||
| * Export constructor. | ||
| */ | ||
|  | ||
| module.exports = 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
 
 
 
 
 
 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.