Skip to content

Commit c50525d

Browse files
authored
Merge pull request #10 from hiddenboox/feature/refactor
Feature/refactor
2 parents 394a379 + ab7736e commit c50525d

File tree

7 files changed

+91
-31
lines changed

7 files changed

+91
-31
lines changed

src/authorize.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export default async ({ clientId, clientSecret, grantType } = {}) => {
1010
try {
1111
const response = await post(
1212
`${environment}/pl/standard/user/oauth/authorize`,
13-
null,
1413
{
1514
json: true,
1615
params: {
@@ -27,7 +26,7 @@ export default async ({ clientId, clientSecret, grantType } = {}) => {
2726
expiresIn: response.expires_in,
2827
grantType: response.grant_type
2928
}
30-
} catch (ex) {
31-
console.error(ex.message)
29+
} catch (err) {
30+
throw new Error(err.message)
3231
}
3332
}

src/consts.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ export const ContentType = {
22
JSON: 'application/json'
33
}
44

5-
export const HEADERS = {
5+
export const HEADER = {
66
Authorization: 'Authorization',
7-
'Content-Type': 'Content-Type'
7+
ContentType: 'Content-Type',
8+
ContentLength: 'Content-Length'
89
}
910

1011
export const HttpVerb = {

src/helpers/request.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,41 @@ import https from 'https'
33
import { URL } from 'url'
44

55
import { HttpVerb } from '../consts'
6+
import { required } from './validation'
67

78
export const request = (
89
{ url, json, params, body, ...rest } = { method: HttpVerb.GET, json: true }
910
) => {
11+
required('url', url)
12+
1013
const { pathname, search, host } = new URL(
1114
`${url}${params ? '?' + querystring.stringify(params) : ''}`
1215
)
1316
return new Promise((resolve, reject) => {
1417
const path = `${pathname}${search}`
1518
const req = https
16-
.request({ host, path, ...rest }, resp => {
19+
.request({ host, path, ...rest }, res => {
1720
let data = ''
18-
resp.setEncoding('utf8')
19-
resp.on('data', chunk => {
21+
res.setEncoding('utf8')
22+
res.on('data', chunk => {
2023
data += chunk
2124
})
22-
23-
resp.on('end', () => resolve(json ? JSON.parse(data) : data))
25+
res.on('end', () => {
26+
resolve(json ? JSON.parse(data) : data)
27+
})
2428
})
25-
.on('error', err => reject('Error: ' + err.message))
29+
30+
req.on('error', err => {
31+
reject(new Error(err.message))
32+
})
2633

2734
if (body) {
28-
req.write(json ? JSON.stringify(body) : body)
35+
req.write(typeof body === 'object' ? JSON.stringify(body) : body)
2936
}
3037

3138
req.end()
3239
})
3340
}
3441

35-
export const get = (url, params, options = {}) =>
36-
request({ ...options, url, params, method: HttpVerb.GET })
37-
export const post = (url, body, options = {}) =>
38-
request({ ...options, url, body, method: HttpVerb.POST })
42+
export const get = (url, options = {}) => request({ ...options, url, method: HttpVerb.GET })
43+
export const post = (url, options = {}) => request({ ...options, url, method: HttpVerb.POST })

src/order.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { post } from './helpers/request'
2-
import { ContentType, HEADERS } from './consts'
2+
import { ContentType, HEADER } from './consts'
33
import { environment } from './env'
44
import { required } from './helpers/validation'
55

@@ -16,18 +16,18 @@ export default async ({ accessToken, payment, cart, buyer, products } = {}) => {
1616
return await post(
1717
`${environment}/api/v2_1/orders`,
1818
{
19-
notifyUrl: PAYU_CLIENT_NOTIFY_SITE_URL,
20-
merchantPosId: PAYU_CLIENT_ID,
21-
...payment,
22-
...cart,
23-
...buyer,
24-
...products
25-
},
26-
{
19+
body: {
20+
notifyUrl: PAYU_CLIENT_NOTIFY_SITE_URL,
21+
merchantPosId: PAYU_CLIENT_ID,
22+
...payment,
23+
...cart,
24+
...buyer,
25+
...products
26+
},
2727
json: true,
2828
headers: {
29-
[HEADERS.Authorization]: `Bearer ${accessToken}`,
30-
[HEADERS.ContentType]: ContentType.JSON
29+
[HEADER.Authorization]: `Bearer ${accessToken}`,
30+
[HEADER.ContentType]: ContentType.JSON
3131
}
3232
}
3333
)

test/server.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ export const mockOrder = () =>
2121
status: {
2222
statusCode: 'SUCCESS'
2323
},
24-
redirectUri: '{url_do_przekierowania_na_stronę_podsumowania_płatności}',
24+
redirectUri: '{redirect_url_after_successfully_payment}',
2525
orderId: 'WZHF5FFDRJ140731GUEST000P01',
26-
extOrderId: '{twój_identyfikator_zamówienia}'
26+
extOrderId: '{internal_order_id}'
2727
})
28+
29+
export const mockFail = (status) =>
30+
nock('https://localhost')
31+
.get(`/${status}`)
32+
.reply(status, 'Internal Server Error 500')

test/unit/authorize.test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import nock from 'nock'
21
import assert from 'assert'
3-
import { expect } from 'chai'
2+
import nock from 'nock'
43

54
import { mockAuthorize } from '../server'
65
import { authorize, GrantType } from '../../src'

test/unit/request.test.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import assert from 'assert'
2+
import nock from 'nock'
3+
4+
import { post, get } from '../../src/helpers/request'
5+
import { mockAuthorize, mockFail } from '../server'
6+
import { Environment } from '../../src/consts'
7+
8+
describe('request', () => {
9+
afterEach(() => {
10+
nock.cleanAll()
11+
})
12+
13+
it('throw error on nullish arguments', async () => {
14+
15+
try {
16+
await post()
17+
} catch (err) {
18+
should.throw(() => { throw err }, assert.AssertionError, 'url should not be empty')
19+
}
20+
})
21+
22+
it('should response parsed JSON with opt { json: true }', async () => {
23+
mockAuthorize()
24+
25+
const response = await post(`${Environment.SANDBOX}/pl/standard/user/oauth/authorize`, { json: true })
26+
27+
response.should.have.property('access_token')
28+
response.should.have.property('token_type')
29+
response.should.have.property('expires_in')
30+
response.should.have.property('grant_type')
31+
})
32+
33+
it('should response with plain text without { json: true }', async () => {
34+
mockAuthorize()
35+
36+
const response = await post(`${Environment.SANDBOX}/pl/standard/user/oauth/authorize`)
37+
38+
response.should.be.a('string')
39+
})
40+
41+
it('should throw error when server respond with not ok status', async () => {
42+
mockFail(500)
43+
44+
try {
45+
const response = await get('https://localhost/500')
46+
} catch (err) {
47+
should.throw(() => { throw err }, Error, 'adddad')
48+
}
49+
50+
})
51+
})

0 commit comments

Comments
 (0)