feat: Adding the ability to add sudo to specific requests

This commit is contained in:
Justin Dalrymple 2018-10-18 11:45:43 -04:00
parent 9f59610c0f
commit 780244f35f
2 changed files with 43 additions and 16 deletions

View File

@ -34,6 +34,9 @@ class BaseModel {
// Set sudo
if (sudo) this.headers['Sudo'] = sudo;
// Freeze properties
Object.freeze(this);
}
}

View File

@ -3,17 +3,23 @@ import LinkParser from 'parse-link-header';
import URLJoin from 'url-join';
import Request from 'got';
interface GetPaginatedOptions {
interface DefaultRequestOptions {
body?: object;
query?: object;
sudo?: string | number;
}
interface PaginatedRequestOptions extends DefaultRequestOptions {
showPagination?: boolean;
maxPages?: number;
page?: number;
}
function defaultRequest(service, endpoint, { body, query }: { body?: Object, query?: Object }) {
function defaultRequest(service, endpoint, { body, query, sudo }: DefaultRequestOptions = {}) {
return [
URLJoin(service.url, endpoint),
{
headers: service.headers,
headers: { ...service.headers, sudo },
query: query && Humps.decamelizeKeys(query),
body: body && Humps.decamelizeKeys(body),
rejectUnauthorized: service.rejectUnauthorized,
@ -22,10 +28,11 @@ function defaultRequest(service, endpoint, { body, query }: { body?: Object, que
];
}
async function getPaginated(service, endpoint, options: GetPaginatedOptions = {}) {
const { showPagination, maxPages, ...queryOptions } = options;
async function getPaginated(service, endpoint, options: PaginatedRequestOptions = {}) {
const { showPagination, maxPages, sudo, ...query } = options;
const requestOptions = defaultRequest(service, endpoint, {
query: queryOptions,
query,
sudo,
});
const response = await Request.get(...requestOptions);
@ -37,14 +44,14 @@ async function getPaginated(service, endpoint, options: GetPaginatedOptions = {}
// If not looking for a singular page and still under the max pages limit
// AND their is a next page, paginate
if (!queryOptions.page && underMaxPageLimit && links.next) {
if (!query.page && underMaxPageLimit && links.next) {
more = await getPaginated(service, links.next.url.replace(service.url, ''), options);
data = [...response.body, ...more];
} else {
data = response.body;
}
if (queryOptions.page && showPagination) {
if (query.page && showPagination) {
return {
data,
pagination: {
@ -62,11 +69,19 @@ async function getPaginated(service, endpoint, options: GetPaginatedOptions = {}
}
class RequestHelper {
static async get(service, endpoint, options = {}, { stream = false } = {}) {
static async get(
service,
endpoint,
options: DefaultRequestOptions = {},
{ stream = false } = {},
) {
const { sudo, ...query } = options;
if (stream) {
return Request.stream(
...defaultRequest(service, endpoint, {
query: options,
query,
sudo,
}),
);
}
@ -76,30 +91,39 @@ class RequestHelper {
return response.body;
}
static async post(service, endpoint, options = {}) {
static async post(service, endpoint, options: DefaultRequestOptions = {}) {
const { sudo, ...body } = options;
const response = await Request.post(
...defaultRequest(service, endpoint, {
body: options,
body,
sudo,
}),
);
return response.body;
}
static async put(service, endpoint, options = {}) {
static async put(service, endpoint, options: DefaultRequestOptions = {}) {
const { sudo, ...body } = options;
const response = await Request.put(
...defaultRequest(service, endpoint, {
body: options,
body,
sudo,
}),
);
return response.body;
}
static async delete(service, endpoint, options = {}) {
static async delete(service, endpoint, options: DefaultRequestOptions = {}) {
const { sudo, ...query } = options;
const response = await Request.delete(
...defaultRequest(service, endpoint, {
query: options,
query,
sudo,
}),
);