Skip to content
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

Convert Request, Response, CodeResponseType, TokenResponseType to ES6 classes #225

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 41 additions & 51 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,57 @@
const InvalidArgumentError = require('./errors/invalid-argument-error');
const typeis = require('type-is');

/**
* Constructor.
*/
class Request {
constructor({ headers, method, query, body, ...otherOptions } = {}) {
if (!headers) {
throw new InvalidArgumentError('Missing parameter: `headers`');
}

function Request(options) {
options = options || {};
if (!method) {
throw new InvalidArgumentError('Missing parameter: `method`');
}

if (!options.headers) {
throw new InvalidArgumentError('Missing parameter: `headers`');
}
if (!query) {
throw new InvalidArgumentError('Missing parameter: `query`');
}

if (!options.method) {
throw new InvalidArgumentError('Missing parameter: `method`');
this.body = body || {};
this.headers = {};
this.method = method;
this.query = query;

// Store the headers in lower case.
Object.entries(headers).forEach(([header, value]) => {
this.headers[header.toLowerCase()] = value;
});

// Store additional properties of the request 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;
});
}

if (!options.query) {
throw new InvalidArgumentError('Missing parameter: `query`');
/**
* Get a request header.
* @param {String} field
*/
get(field) {
return this.headers[field.toLowerCase()];
}

this.body = options.body || {};
this.headers = {};
this.method = options.method;
this.query = options.query;

// 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];
/**
* Check if the content-type matches any of the given mime types.
* @param {...String|Array} types
*/
is(...types) {
if (types.length === 1 && Array.isArray(types[0])) {
types = types[0];
jorenvandeweyer marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Store additional properties of the request object passed in
for (const property in options) {
if (Object.prototype.hasOwnProperty.call(options, property) && !this[property]) {
this[property] = options[property];
}
return typeis(this, types) || false;
}
}

/**
* Get a request header.
*/

Request.prototype.get = function(field) {
return this.headers[field.toLowerCase()];
};

/**
* Check if the content-type matches any of the given mime type.
*/

Request.prototype.is = function(types) {
if (!Array.isArray(types)) {
types = [].slice.call(arguments);
}

return typeis(this, types) || false;
};

/**
* Export constructor.
*/

module.exports = Request;
42 changes: 16 additions & 26 deletions lib/response-types/code-response-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,27 @@
const InvalidArgumentError = require('../errors/invalid-argument-error');
const url = require('url');

/**
* Constructor.
*/
class CodeResponseType {
constructor(code) {
if (!code) {
throw new InvalidArgumentError('Missing parameter: `code`');
}

function CodeResponseType(code) {
if (!code) {
throw new InvalidArgumentError('Missing parameter: `code`');
this.code = code;
}

this.code = code;
}
buildRedirectUri(redirectUri) {
if (!redirectUri) {
throw new InvalidArgumentError('Missing parameter: `redirectUri`');
}

/**
* Build redirect uri.
*/

CodeResponseType.prototype.buildRedirectUri = function(redirectUri) {
if (!redirectUri) {
throw new InvalidArgumentError('Missing parameter: `redirectUri`');
}
const uri = url.parse(redirectUri, true);

const uri = url.parse(redirectUri, true);
uri.query.code = this.code;
uri.search = null;

uri.query.code = this.code;
uri.search = null;

return uri;
};

/**
* Export constructor.
*/
return uri;
}
}

module.exports = CodeResponseType;
14 changes: 4 additions & 10 deletions lib/response-types/token-response-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@

const ServerError = require('../errors/server-error');

/**
* Constructor.
*/

function TokenResponseType() {
throw new ServerError('Not implemented.');
class TokenResponseType {
constructor() {
throw new ServerError('Not implemented.');
}
}

/**
* Export constructor.
*/

module.exports = TokenResponseType;
83 changes: 35 additions & 48 deletions lib/response.js
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 (const property in options) {
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;
Loading