Skip to content

Commit d750e61

Browse files
committed
Replace axios by fetch, and simplify
1 parent 42a59c7 commit d750e61

15 files changed

+246
-562
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Purchase example
2828
const {default: GMOPG, ENUMS} = require('gmopg');
2929

3030
const gmopg = new GMOPG({
31-
axios: { baseURL: 'https://p01.mul-pay.jp' },
31+
baseUrl: 'https://p01.mul-pay.jp',
3232
SiteID: 'Your SiteID',
3333
SitePass: 'Your SitePass',
3434
ShopID: 'Your ShopID',
@@ -70,7 +70,7 @@ gmopg.entryTran({
7070
import GMOPG, {ENUMS} from 'gmopg'
7171

7272
const gmopg = new GMOPG({
73-
axios: { baseURL: 'https://p01.mul-pay.jp' },
73+
baseUrl: 'https://p01.mul-pay.jp',
7474
SiteID: 'Your SiteID',
7575
SitePass: 'Your SitePass',
7676
ShopID: 'Your ShopID',
@@ -107,15 +107,15 @@ const alterRes = await gmopg.alterTran({
107107
Config
108108
------
109109

110-
name | description | environ | default
111-
--- | --- | --- | ---
112-
axios.baseURL | baseurl for request | GMOPG_ENDPOINT | https://pt01.mul-pay.jp
113-
axios.timeout | timeout for request | GMOPG_TIMEOUT | 180000 (ms)
114-
axios.headers | headers for request | - | see code :eyes:
115-
SiteID | PG site id | GMOPG_SITEID | undefined
116-
SitePass | PG site pass | GMOPG_SITEPASS | undefined
117-
ShopID | PG shop id | GMOPG_SHOPID | undefined
118-
ShopPass | PG shop pass | GMOPG_SHOPPASS | undefined
110+
name | description | environ | default
111+
--- | --- | --- | ---
112+
baseUrl | baseurl for request | GMOPG_ENDPOINT | https://pt01.mul-pay.jp
113+
http.timeout | timeout for request | GMOPG_TIMEOUT | 180000 (ms)
114+
http.headers | headers for request | - | see code :eyes:
115+
SiteID | PG site id | GMOPG_SITEID | undefined
116+
SitePass | PG site pass | GMOPG_SITEPASS | undefined
117+
ShopID | PG shop id | GMOPG_SHOPID | undefined
118+
ShopPass | PG shop pass | GMOPG_SHOPPASS | undefined
119119

120120
Contribution
121121
------------

index.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,40 @@
1212
"payment"
1313
],
1414
"bugs": "https://github.com/pepabo/gmopg/issues",
15-
"main": "./index.js",
15+
"main": "./lib/gmopg.js",
1616
"types": "./lib/gmopg.d.ts",
1717
"files": [
1818
"MIT-LICENSE",
1919
"README.md",
20-
"index.js",
2120
"lib/"
2221
],
2322
"directories": {
2423
"lib": "./lib",
2524
"src": "./src"
2625
},
2726
"dependencies": {
28-
"axios": ">=0.19.0",
2927
"deepmerge": "^1.5.2",
3028
"encoding-japanese": "^1.0.29",
29+
"node-fetch": "^2.6.0",
3130
"qs": "^6.6.0"
3231
},
3332
"devDependencies": {
3433
"@types/deepmerge": "^1.3.2",
3534
"@types/encoding-japanese": "^1.0.15",
35+
"@types/nock": "^10.0.3",
3636
"@types/node": "^8.0.32",
37+
"@types/node-fetch": "^2.3.4",
3738
"@types/qs": "^6.5.1",
39+
"@types/sinon": "^7.0.11",
3840
"ava": "^1.2.1",
3941
"coveralls": "^3.0.0",
42+
"nock": "^10.0.6",
4043
"nyc": "^14.1.0",
44+
"sinon": "^7.3.2",
4145
"ts-node": "^6.0.3",
4246
"tslint": "^5.7.0",
4347
"tslint-microsoft-contrib": "^5.0.1",
44-
"typescript": "^3.3.3"
48+
"typescript": "^3.4.5"
4549
},
4650
"engines": {
4751
"node": ">=8.4.0"
@@ -56,7 +60,8 @@
5660
],
5761
"files": [
5862
"src/**/*.test.ts"
59-
]
63+
],
64+
"serial": true
6065
},
6166
"nyc": {
6267
"extension": [

src/client.test.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
import anyTest, {TestInterface} from 'ava'
2-
import Axios, {AxiosRequestConfig, AxiosResponse} from 'axios'
1+
import test from 'ava'
2+
import nock = require('nock');
33
import {PayType} from './client.enum'
44
import Client from './client'
55

6-
interface Context {
7-
client: Client
8-
}
6+
const baseUrl = 'https://x.y';
97

10-
const test = anyTest as TestInterface<Context>;
8+
const client = new Client({baseUrl})
119

12-
test.beforeEach((t) => {
13-
const client = new Client()
14-
client.client = Axios.create({})
15-
t.context.client = client
10+
test('.post is function', (t) => {
11+
t.is(typeof client.post, 'function')
1612
})
1713

1814
test('.post requests body correctly', async (t) => {
19-
t.context.client.config.axios = {
20-
adapter: async (config: AxiosRequestConfig) => {
21-
const response: AxiosResponse = {
22-
data: 'AccessID=accessid&AccessPass=accesspass',
23-
status: 200,
24-
statusText: 'OK',
25-
headers: {},
26-
config
27-
}
28-
29-
return Promise.resolve(response)
30-
}
31-
}
32-
33-
t.context.client.client.interceptors.request.use((req) => {
34-
t.is(req.data, 'Foo=aaa&Bar=0&Baz=true&Ja=日本語&Type=0')
35-
return req
36-
})
15+
nock(baseUrl)
16+
.post(/.*/, 'Foo=aaa&Bar=0&Baz=true&Ja=日本語&Type=0')
17+
.reply(200, 'AccessID=accessid&AccessPass=accesspass')
3718

38-
const res = await t.context.client.post('/test', {
19+
const res = await client.post('/test1', {
3920
Foo: 'aaa',
4021
Bar: 0,
4122
Baz: true,
4223
Ja: '日本語',
4324
Type: PayType.Credit
44-
})
25+
});
4526

4627
t.deepEqual(res, {
4728
AccessID: 'accessid',
4829
AccessPass: 'accesspass'
4930
})
5031
})
32+
33+
test('.post returns errors correctly', async (t) => {
34+
nock(baseUrl)
35+
.post(/.*/, 'Foo=aaa&Bar=0&Baz=true&Ja=日本語&Type=0')
36+
.reply(200, 'ErrCode=E01&ErrInfo=E01190001')
37+
38+
try {
39+
await client.post('/test2', {
40+
Foo: 'aaa',
41+
Bar: 0,
42+
Baz: true,
43+
Ja: '日本語',
44+
Type: PayType.Credit
45+
});
46+
t.fail()
47+
} catch (err) {
48+
t.deepEqual(err.errInfo, ["E01190001"])
49+
}
50+
})

src/client.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
import Axios, {AxiosInstance, AxiosResponse} from 'axios'
1+
import * as qs from 'qs'
2+
import * as merge from 'deepmerge'
3+
import fetch, {Response} from 'node-fetch';
24
import {BadRequest} from './errors'
35
import {IConfig} from './config.interface'
46
import {buildByEnv, defaults} from './config'
5-
import * as qs from 'qs'
6-
import * as merge from 'deepmerge'
77

88
export default class Client {
9-
public client: AxiosInstance
109
public config: IConfig
1110

1211
constructor(config: IConfig = {}) {
1312
this.config = merge(merge(defaults, config), buildByEnv())
14-
this.client = Axios.create(this.config.axios)
1513
}
1614

17-
public async post(endpoint: string, data: any): Promise<any> {
18-
const res: AxiosResponse = await this.client.post(endpoint, qs.stringify(data, {encode: false}), this.config.axios)
19-
const parsed: any = qs.parse(res.data)
15+
public async post(pathname: string, data: any): Promise<any> {
16+
const res: Response = await fetch(this.config.baseUrl + pathname, {
17+
method: 'POST',
18+
body: qs.stringify(data, {encode: false}),
19+
...this.config.http,
20+
})
21+
22+
const parsed: any = qs.parse(await res.text());
2023

21-
if (this.isError(parsed)) {
22-
throw new BadRequest(`Bad Request: ${endpoint}`).
24+
if (!res.ok || this.isError(parsed)) {
25+
throw new BadRequest(`Bad Request: ${pathname}`).
2326
setResponse(res).parseError(parsed)
2427
}
2528

src/client/cardable.test.ts

Lines changed: 33 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,58 @@
11
import test from 'ava'
2-
import Axios, {AxiosRequestConfig, AxiosResponse} from 'axios'
2+
import sinon = require('sinon')
33
import Client from '../client'
4-
import WithCardable from './cardable'
54
import {SeqMode} from '../client.enum'
5+
import WithCardable from './cardable'
66
import {IDeleteCardResult, ISaveCardResult, ISearchCardResult} from './cardable.interface'
77

88
const Cardable = WithCardable(Client)
9-
let card: any
10-
11-
test.beforeEach(() => {
12-
card = new Cardable()
13-
card.client = Axios.create({})
14-
})
9+
const cardable = new Cardable()
1510

16-
test('.defaultCardData returns default object', async (t) => {
17-
const res = await card.defaultCardData()
18-
const expect = {
19-
SiteID: undefined,
20-
SitePass: undefined,
21-
MemberID: undefined
22-
}
23-
t.deepEqual(res, expect)
11+
test.afterEach(() => {
12+
sinon.restore();
2413
})
2514

2615
test('.saveCard calls API and returns response', async (t) => {
27-
card.config.axios = {
28-
adapter: async (config: AxiosRequestConfig) => {
29-
const response: AxiosResponse = {
30-
data: 'CardSeq=cardseq&CardNo=cardno&Forward=forward&Brand=brand',
31-
status: 200,
32-
statusText: 'OK',
33-
headers: {},
34-
config
35-
}
36-
37-
return Promise.resolve(response)
38-
}
39-
}
16+
const expect: ISaveCardResult = {
17+
CardSeq: 'cardseq',
18+
CardNo: 'cardno',
19+
Forward: 'forward',
20+
Brand: 'brand'
21+
};
22+
23+
sinon.stub(cardable, 'post').resolves(expect)
4024

4125
const args = {
4226
SiteID: 'siteid',
4327
SitePass: 'sitepass',
4428
MemberID: 'memberid'
4529
}
46-
const res = await card.saveCard(args)
30+
const res = await cardable.saveCard(args)
4731

48-
const expect: ISaveCardResult = {
49-
CardSeq: 'cardseq',
50-
CardNo: 'cardno',
51-
Forward: 'forward',
52-
Brand: 'brand'
53-
}
5432
t.deepEqual(res, expect)
5533
})
5634

5735
test('.deleteCard calls API and returns response', async (t) => {
58-
card.config.axios = {
59-
adapter: async (config: AxiosRequestConfig) => {
60-
const response: AxiosResponse = {
61-
data: 'CardSeq=cardseq',
62-
status: 200,
63-
statusText: 'OK',
64-
headers: {},
65-
config
66-
}
67-
68-
return Promise.resolve(response)
69-
}
36+
37+
const expect: IDeleteCardResult = {
38+
CardSeq: 'cardseq'
7039
}
7140

41+
sinon.stub(cardable, 'post').resolves(expect)
42+
7243
const args = {
7344
SiteID: 'siteid',
7445
SitePass: 'sitepass',
7546
MemberID: 'memberid',
7647
SeqMode: SeqMode.Logic,
7748
CardSeq: 'cardseq'
7849
}
79-
const res = await card.deleteCard(args)
50+
const res = await cardable.deleteCard(args)
8051

81-
const expect: IDeleteCardResult = {
82-
CardSeq: 'cardseq'
83-
}
8452
t.deepEqual(res, expect)
8553
})
8654

8755
test('.searchCard calls API and returns response', async (t) => {
88-
card.config.axios = {
89-
adapter: async (config: AxiosRequestConfig) => {
90-
const response: AxiosResponse = {
91-
data: 'CardSeq=cardseq&DefaultFlag=1&CardName=cardname&CardNo=cardno&Expire=expire&HolderName=holdername&DeleteFlag=0',
92-
status: 200,
93-
statusText: 'OK',
94-
headers: {},
95-
config
96-
}
97-
98-
return Promise.resolve(response)
99-
}
100-
}
101-
102-
const args = {
103-
SiteID: 'siteid',
104-
SitePass: 'sitepass',
105-
MemberID: 'memberid',
106-
SeqMode: SeqMode.Logic,
107-
CardSeq: 'cardseq'
108-
}
109-
const res = await card.searchCard(args)
11056

11157
const result: ISearchCardResult = {
11258
CardSeq: 'cardseq',
@@ -117,6 +63,19 @@ test('.searchCard calls API and returns response', async (t) => {
11763
HolderName: 'holdername',
11864
DeleteFlag: '0'
11965
}
66+
67+
sinon.stub(cardable, 'post').resolves(result)
68+
12069
const expect = [result]
70+
71+
const args = {
72+
SiteID: 'siteid',
73+
SitePass: 'sitepass',
74+
MemberID: 'memberid',
75+
SeqMode: SeqMode.Logic,
76+
CardSeq: 'cardseq'
77+
}
78+
const res = await cardable.searchCard(args)
79+
12180
t.deepEqual(res, expect)
12281
})

0 commit comments

Comments
 (0)