Skip to content

Commit

Permalink
Merge pull request #2532 from SelfKeyFoundation/2531-moonpat-sdk
Browse files Browse the repository at this point in the history
2531 moonpay sdk
  • Loading branch information
sk91 authored Dec 22, 2020
2 parents 91aa6fc + 35a5a1c commit f35b34f
Show file tree
Hide file tree
Showing 5 changed files with 2,049 additions and 121 deletions.
14 changes: 11 additions & 3 deletions src/main/common/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ export class Api {

async request(opt) {
let { url, method } = validate(opt, ['url', 'method']);
let { headers = {}, qs = {}, body } = opt;
let { headers = {}, qs = {}, body, formData, json = true } = opt;

qs = { ...this.opt.qs, ...qs };
headers = { ...this.opt.headers, ...headers };
url = urljoin(this.opt.endpoint, url);
if (!url.startsWith('https://') && !url.startsWith('http://')) {
url = urljoin(this.opt.endpoint, url);
}
method = method.toUpperCase();
const rpOpt = { url, method, json: true, headers, qs };

const rpOpt = { url, method, json, headers, qs };

if (body) {
rpOpt.body = body;
}

if (formData) {
rpOpt.formData = formData;
}
try {
const resp = await rp(rpOpt);
return resp;
Expand Down
29 changes: 29 additions & 0 deletions src/main/common/api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,25 @@ describe('Api', () => {
);
});

it('should call request with formData if defined', async () => {
const formData = { token: 'hi' };
await api.request({ url: '/hi', method: 'get', formData });
expect(rp).toHaveBeenCalledWith(
expect.objectContaining({
formData
})
);
});

it('should call request with json if defined', async () => {
await api.request({ url: '/hi', method: 'get', json: true });
expect(rp).toHaveBeenCalledWith(
expect.objectContaining({
json: true
})
);
});

it('should call request with empty qs if not defined', async () => {
await api.request({ url: '/hi', method: 'get' });
expect(rp).toHaveBeenCalledWith(
Expand Down Expand Up @@ -211,6 +230,16 @@ describe('Api', () => {
);
});

it('should not prepend endpoint if absolute url', async () => {
const url = 'http://selfkey.org';
await api.request({ url, method: 'get' });
expect(rp).toHaveBeenCalledWith(
expect.objectContaining({
url
})
);
});

it('should call onRequestError if error is thrown on request', async () => {
const error = new Error('Request Error');
const returnedResp = 'response';
Expand Down
Loading

0 comments on commit f35b34f

Please sign in to comment.