Skip to content

Commit

Permalink
feat: Adding the ability to add sudo to specific requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalrymple committed Oct 18, 2018
1 parent 9f59610 commit 780244f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/infrastructure/BaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class BaseModel {

// Set sudo
if (sudo) this.headers['Sudo'] = sudo;

// Freeze properties
Object.freeze(this);
}
}

Expand Down
56 changes: 40 additions & 16 deletions src/infrastructure/RequestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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: {
Expand All @@ -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,
}),
);
}
Expand All @@ -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,
}),
);

Expand Down

0 comments on commit 780244f

Please sign in to comment.