Skip to content

Instantly share code, notes, and snippets.

@arvindkalra
Created August 8, 2020 13:58
Show Gist options
  • Save arvindkalra/a48a0be5f2957e44f9ce07474db467ce to your computer and use it in GitHub Desktop.
Save arvindkalra/a48a0be5f2957e44f9ce07474db467ce to your computer and use it in GitHub Desktop.
SDK which can be used to make get, post, put, patch, delete requests to other services using basic auth
const express = require("express");
const app = express();
const ServicesSDK = require("./ServicesSDK")
const serviceCredentials = {
uds: {
baseUrl: "http://localhost:3000",
auth: {
username: "user_uds",
password: "pass_uds",
},
},
pis: {
baseUrl: "http://localhost:5000",
auth: {
username: "user_pis",
password: "pass_pis",
},
},
};
app.services = new ServicesSDK(serviceCredentials);
// Can call other services as app.services.uds.get() or app.services.pis.post()
/**
* @author Arvind Kalra <kalarvind97@gmail.com>
* @profile https://github.com/arvindkalra
* @date 08/08/20
*/
import axios from "axios";
import _ from "lodash";
const reqFunctions = {
/**
* @name {serviceName}.post
* @description Make post request to the metrc api endpoint
* @param { Object } opts Options to make post request
* @param { String } opts.url Endpoint to make the request
* @param { Object } opts.headers Object having all the custom headers to be used to make this request
* @param { Object } opts.params Object having all the query params to be sent in the request
* @param { Object } opts.data Object to be sent as body to the request
* */
post: function (opts) {
const requestOptions = {
...validateRequestObject(opts),
method: "post",
baseURL: this.baseUrl,
auth: this.auth,
};
return axios
.request(requestOptions)
.then((res) => {
return res.data;
})
.catch((err) => {
throw err;
});
},
/**
* @name {serviceName}.get
* @description Make get request to the metrc api endpoint
* @param { Object } opts Options to make post request
* @param { String } opts.url Endpoint to make the request
* @param { Object } opts.headers Object having all the custom headers to be used to make this request
* @param { Object } opts.params Object having all the query params to be sent in the request
* @param { Object } opts.data Object to be sent as body to the request
* */
get: function (opts) {
const requestOptions = {
...validateRequestObject(opts),
method: "get",
baseURL: this.baseUrl,
auth: this.auth,
};
return axios
.request(requestOptions)
.then((res) => {
return res.data;
})
.catch((err) => {
throw err;
});
},
/**
* @name {serviceName}.put
* @description Make put request to the metrc api endpoint
* @param { Object } opts Options to make post request
* @param { String } opts.url Endpoint to make the request
* @param { Object } opts.headers Object having all the custom headers to be used to make this request
* @param { Object } opts.params Object having all the query params to be sent in the request
* @param { Object } opts.data Object to be sent as body to the request
* */
put: function (opts) {
const requestOptions = {
...validateRequestObject(opts),
method: "put",
baseURL: this.baseUrl,
auth: this.auth,
};
return axios
.request(requestOptions)
.then((res) => {
return res.data;
})
.catch((err) => {
throw err;
});
},
/**
* @name {serviceName}.delete
* @description Make delete request to the metrc api endpoint
* @param { Object } opts Options to make post request
* @param { String } opts.url Endpoint to make the request
* @param { Object } opts.headers Object having all the custom headers to be used to make this request
* @param { Object } opts.params Object having all the query params to be sent in the request
* @param { Object } opts.data Object to be sent as body to the request
* */
delete: function (opts) {
const requestOptions = {
...validateRequestObject(opts),
method: "delete",
baseURL: this.baseUrl,
auth: this.auth,
};
return axios
.request(requestOptions)
.then((res) => {
return res.data;
})
.catch((err) => {
throw err;
});
},
/**
* @name {serviceName}.patch
* @description Make patch request to the metrc api endpoint
* @param { Object } opts Options to make post request
* @param { String } opts.url Endpoint to make the request
* @param { Object } opts.headers Object having all the custom headers to be used to make this request
* @param { Object } opts.params Object having all the query params to be sent in the request
* @param { Object } opts.data Object to be sent as body to the request
* */
patch: function (opts) {
const requestOptions = {
...validateRequestObject(opts),
method: "patch",
baseURL: this.baseUrl,
auth: this.auth,
};
return axios
.request(requestOptions)
.then((res) => {
return res.data;
})
.catch((err) => {
throw err;
});
},
};
/**
* @name Metrc
* @description Create instance to make api requests to other services using basicAuth,
* returns an instance which has all the services from param object with their get, post methods.
* @param { Object } serviceCredentials All the services with their baseUrl and auth object, with username and password
* */
function ServicesSDK(serviceCredentials) {
const services = Object.keys(serviceCredentials);
for (let i = 0; i < services.length; i++) {
const serviceName = services[i];
this[serviceName] = {
baseUrl: serviceCredentials[serviceName].baseUrl,
auth: serviceCredentials[serviceName].auth,
__proto__: reqFunctions,
};
}
}
const validateRequestObject = (requestOptions) => {
const selectedOptions = _.pick(requestOptions, [
"url",
"headers",
"params",
"data",
]);
if (!_.has(selectedOptions, "url")) {
throw new Error(
"Url is required in request options for making the api request"
);
}
return selectedOptions;
};
export default ServicesSDK;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment