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

v3.0.1 refactor #29

Merged
merged 16 commits into from
Dec 16, 2021
Merged
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
Prev Previous commit
Next Next commit
refactor(Request): consolidate request parameters into a general requ…
…est function

- fix(createRequestInstance): remove double quotes around `prefixUrl`

- feat(Request): add helper functions

- refactor(Tasks): use request helper function to obtain the instance token

- chore(eslintrc): turn off `no-restricted-syntax`

- chore(ignores): add ignore file for `eslint` and update structure of `prettierignore`
  • Loading branch information
ComfortablyCoding committed Dec 15, 2021

Verified

This commit was signed with the committer’s verified signature.
Dunedan Daniel Roschka
commit 0f2c3bd9e2983800a02426fb9ac6242311772a07
45 changes: 45 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# ESLint
# git
.git
.gitattributes
.gitignore

# vscode
.vscode

# docs
docs/

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Dependency directories
node_modules/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -8,13 +8,13 @@
},
"extends": ["airbnb-base", "prettier"],
"plugins": ["prettier"],
"ignorePatterns": ["node_modules/**/*.js"],
"parserOptions": {
"ecmaVersion": 11
},
"rules": {
"prettier/prettier": "error",
"no-underscore-dangle": 0,
"no-param-reassign": "off"
"no-param-reassign": "off",
"no-restricted-syntax": "off"
}
}
48 changes: 41 additions & 7 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
# Prettier
node_modules
# git
.git
.vscode
.eslintrc.json
.prettierrc.json
.gitattributes
.gitignore
npm-debug.log
test/
docs/

# vscode
.vscode

# docs
docs/

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Dependency directories
node_modules/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test
2 changes: 1 addition & 1 deletion src/routes/Tasks.js
Original file line number Diff line number Diff line change
@@ -90,7 +90,7 @@ class Tasks {

// setting headers
const headers = form.getHeaders();
headers.authorization = this._request._token;
headers.authorization = this._request.getToken();

return this._request.post({
endpoint: `${this.route}/${taskId}/attachment`,
115 changes: 74 additions & 41 deletions src/structures/Request.js
Original file line number Diff line number Diff line change
@@ -9,12 +9,16 @@ class Request {
* @param {import('got/dist/source').ExtendOptions} requestOptions Options for the created request instance.
*/
constructor(token, requestOptions) {
/**
* The access token
*/
this._token = token;
// create service instance
/**
* The request instance
* @private
*/
this._request = createRequestInstance(token, requestOptions);
this._service = createRequestInstance(token, requestOptions);
}

/**
@@ -25,11 +29,7 @@ class Request {
* @param {Object} options.params The parameters to add to the endpoint
*/
async get({ endpoint, params }) {
const options = {};
if (params) {
options.searchParams = buildSearchParams(params);
}
return this._request.get(endpoint, options);
return this._request({ endpoint, method: 'GET', params });
}

/**
@@ -41,14 +41,53 @@ class Request {
* @param {Object} options.data The data to send in the body of the request
* @param {Object} options.headers The headers to send along with the request
*/
async post({ endpoint, params, data = {}, headers }) {
const options = {};
async post({ endpoint, params, data, headers }) {
return this._request({ endpoint, method: 'POST', params, data, headers });
}

/**
* Makes an HTTP PUT request
*
* @param {Object} options Options to pass to the api call
* @param {String} options.endpoint The endpoint to make a request to
* @param {Object} options.params The query parameters to add to the request
* @param {Object} options.data The data to send in the body of the request
*/
async put({ endpoint, params, data }) {
return this._request({ endpoint, method: 'PUT', params, data });
}

/**
* Makes an HTTP DELETE request
*
* @param {Object} options Options to pass to the api call
* @param {String} options.endpoint The endpoint to make a request to
* @param {Object} options.params The query parameters to add to the request
*/
async delete({ endpoint, params }) {
return this._request({ endpoint, method: 'DELETE', params });
}

/**
* Makes an HTTP request
*
* @param {Object} options Options to pass to the api call
* @param {String} options.endpoint The endpoint to make a request to
* @param {String} options.method The request method to use in the request
* @param {Object} options.params The query parameters to add to the request
* @param {Object} options.data The data to send in the body of the request
* @param {Object} options.headers The headers to send along with the request
*/
async _request({ endpoint, method = 'GET', params, data = {}, headers }) {
const options = {
method,
};

if (params) {
options.searchParams = buildSearchParams(params);
}

let contentType = this._request.defaults.options.headers['content-type'];
let contentType = this.getHeader('content-type');

if (headers) {
options.headers = headers;
@@ -57,49 +96,43 @@ class Request {
}
}

// json data must be sent via json property, all others are sent via body
const dataType = contentType === 'application/json' ? 'json' : 'body';
options[dataType] = data;
if (method !== 'GET') {
// json data must be sent via json property, all others are sent via body
const dataType = contentType === 'application/json' ? 'json' : 'body';
options[dataType] = data;
}

return this._request.post(endpoint, options);
return this._service(endpoint, options);
}

/**
* Makes an HTTP PUT request
*
* @param {Object} options Options to pass to the api call
* @param {String} options.endpoint The endpoint to make a request to
* @param {Object} options.params The query parameters to add to the request
* @param {Object} options.data The data to send in the body of the request
* Helper to obtain the instance headers
*/
async put({ endpoint, params, data = {} }) {
const options = {};

if (params) {
options.searchParams = buildSearchParams(params);
}
getHeaders() {
const options = this.getOptions();
return options.headers;
}

// json data must be sent via json property, all others are sent via body
const contentType = this._request.defaults.options.headers['content-type'];
const dataType = contentType === 'application/json' ? 'json' : 'body';
options[dataType] = data;
/**
* Helper to obtain a specific header
*/
getHeader(name) {
const options = this.getOptions();
return options.headers[name];
}

return this._request.put(endpoint, options);
/**
* Helper to obtain the access token
*/
getToken() {
return this._token;
}

/**
* Makes an HTTP DELETE request
*
* @param {Object} options Options to pass to the api call
* @param {String} options.endpoint The endpoint to make a request to
* @param {Object} options.params The query parameters to add to the request
* Helper to obtain the options
*/
async delete({ endpoint, params }) {
const options = {};
if (params) {
options.searchParams = buildSearchParams(params);
}
return this._request.delete(endpoint, options);
getOptions() {
return this._service.defaults.options;
}
}

2 changes: 1 addition & 1 deletion src/utils/createRequestInstance.js
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ const createRequestInstance = (token, requestOptions = {}) => {
'Content-Type': 'application/json',
},
responseType: 'json',
prefixUrl: "'https://api.clickup.com/api/v2'",
prefixUrl: 'https://api.clickup.com/api/v2',
};
// apply defaults where necessary
const requestConfig = merge(requestOptions, requestDefaultOptions);