Skip to content

Commit

Permalink
Merge pull request #11 from caub/fetchify
Browse files Browse the repository at this point in the history
Replace axios by fetch, and simplify
  • Loading branch information
linyows authored Jun 12, 2019
2 parents e198318 + d750e61 commit 44e1a43
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 562 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Purchase example
const {default: GMOPG, ENUMS} = require('gmopg');

const gmopg = new GMOPG({
axios: { baseURL: 'https://p01.mul-pay.jp' },
baseUrl: 'https://p01.mul-pay.jp',
SiteID: 'Your SiteID',
SitePass: 'Your SitePass',
ShopID: 'Your ShopID',
Expand Down Expand Up @@ -70,7 +70,7 @@ gmopg.entryTran({
import GMOPG, {ENUMS} from 'gmopg'

const gmopg = new GMOPG({
axios: { baseURL: 'https://p01.mul-pay.jp' },
baseUrl: 'https://p01.mul-pay.jp',
SiteID: 'Your SiteID',
SitePass: 'Your SitePass',
ShopID: 'Your ShopID',
Expand Down Expand Up @@ -107,15 +107,15 @@ const alterRes = await gmopg.alterTran({
Config
------

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

Contribution
------------
Expand Down
3 changes: 0 additions & 3 deletions index.js

This file was deleted.

15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,40 @@
"payment"
],
"bugs": "https://github.com/pepabo/gmopg/issues",
"main": "./index.js",
"main": "./lib/gmopg.js",
"types": "./lib/gmopg.d.ts",
"files": [
"MIT-LICENSE",
"README.md",
"index.js",
"lib/"
],
"directories": {
"lib": "./lib",
"src": "./src"
},
"dependencies": {
"axios": ">=0.19.0",
"deepmerge": "^1.5.2",
"encoding-japanese": "^1.0.29",
"node-fetch": "^2.6.0",
"qs": "^6.6.0"
},
"devDependencies": {
"@types/deepmerge": "^1.3.2",
"@types/encoding-japanese": "^1.0.15",
"@types/nock": "^10.0.3",
"@types/node": "^8.0.32",
"@types/node-fetch": "^2.3.4",
"@types/qs": "^6.5.1",
"@types/sinon": "^7.0.11",
"ava": "^1.2.1",
"coveralls": "^3.0.0",
"nock": "^10.0.6",
"nyc": "^14.1.0",
"sinon": "^7.3.2",
"ts-node": "^6.0.3",
"tslint": "^5.7.0",
"tslint-microsoft-contrib": "^5.0.1",
"typescript": "^3.3.3"
"typescript": "^3.4.5"
},
"engines": {
"node": ">=8.4.0"
Expand All @@ -56,7 +60,8 @@
],
"files": [
"src/**/*.test.ts"
]
],
"serial": true
},
"nyc": {
"extension": [
Expand Down
60 changes: 30 additions & 30 deletions src/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
import anyTest, {TestInterface} from 'ava'
import Axios, {AxiosRequestConfig, AxiosResponse} from 'axios'
import test from 'ava'
import nock = require('nock');
import {PayType} from './client.enum'
import Client from './client'

interface Context {
client: Client
}
const baseUrl = 'https://x.y';

const test = anyTest as TestInterface<Context>;
const client = new Client({baseUrl})

test.beforeEach((t) => {
const client = new Client()
client.client = Axios.create({})
t.context.client = client
test('.post is function', (t) => {
t.is(typeof client.post, 'function')
})

test('.post requests body correctly', async (t) => {
t.context.client.config.axios = {
adapter: async (config: AxiosRequestConfig) => {
const response: AxiosResponse = {
data: 'AccessID=accessid&AccessPass=accesspass',
status: 200,
statusText: 'OK',
headers: {},
config
}

return Promise.resolve(response)
}
}

t.context.client.client.interceptors.request.use((req) => {
t.is(req.data, 'Foo=aaa&Bar=0&Baz=true&Ja=日本語&Type=0')
return req
})
nock(baseUrl)
.post(/.*/, 'Foo=aaa&Bar=0&Baz=true&Ja=日本語&Type=0')
.reply(200, 'AccessID=accessid&AccessPass=accesspass')

const res = await t.context.client.post('/test', {
const res = await client.post('/test1', {
Foo: 'aaa',
Bar: 0,
Baz: true,
Ja: '日本語',
Type: PayType.Credit
})
});

t.deepEqual(res, {
AccessID: 'accessid',
AccessPass: 'accesspass'
})
})

test('.post returns errors correctly', async (t) => {
nock(baseUrl)
.post(/.*/, 'Foo=aaa&Bar=0&Baz=true&Ja=日本語&Type=0')
.reply(200, 'ErrCode=E01&ErrInfo=E01190001')

try {
await client.post('/test2', {
Foo: 'aaa',
Bar: 0,
Baz: true,
Ja: '日本語',
Type: PayType.Credit
});
t.fail()
} catch (err) {
t.deepEqual(err.errInfo, ["E01190001"])
}
})
23 changes: 13 additions & 10 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import Axios, {AxiosInstance, AxiosResponse} from 'axios'
import * as qs from 'qs'
import * as merge from 'deepmerge'
import fetch, {Response} from 'node-fetch';
import {BadRequest} from './errors'
import {IConfig} from './config.interface'
import {buildByEnv, defaults} from './config'
import * as qs from 'qs'
import * as merge from 'deepmerge'

export default class Client {
public client: AxiosInstance
public config: IConfig

constructor(config: IConfig = {}) {
this.config = merge(merge(defaults, config), buildByEnv())
this.client = Axios.create(this.config.axios)
}

public async post(endpoint: string, data: any): Promise<any> {
const res: AxiosResponse = await this.client.post(endpoint, qs.stringify(data, {encode: false}), this.config.axios)
const parsed: any = qs.parse(res.data)
public async post(pathname: string, data: any): Promise<any> {
const res: Response = await fetch(this.config.baseUrl + pathname, {
method: 'POST',
body: qs.stringify(data, {encode: false}),
...this.config.http,
})

const parsed: any = qs.parse(await res.text());

if (this.isError(parsed)) {
throw new BadRequest(`Bad Request: ${endpoint}`).
if (!res.ok || this.isError(parsed)) {
throw new BadRequest(`Bad Request: ${pathname}`).
setResponse(res).parseError(parsed)
}

Expand Down
107 changes: 33 additions & 74 deletions src/client/cardable.test.ts
Original file line number Diff line number Diff line change
@@ -1,112 +1,58 @@
import test from 'ava'
import Axios, {AxiosRequestConfig, AxiosResponse} from 'axios'
import sinon = require('sinon')
import Client from '../client'
import WithCardable from './cardable'
import {SeqMode} from '../client.enum'
import WithCardable from './cardable'
import {IDeleteCardResult, ISaveCardResult, ISearchCardResult} from './cardable.interface'

const Cardable = WithCardable(Client)
let card: any

test.beforeEach(() => {
card = new Cardable()
card.client = Axios.create({})
})
const cardable = new Cardable()

test('.defaultCardData returns default object', async (t) => {
const res = await card.defaultCardData()
const expect = {
SiteID: undefined,
SitePass: undefined,
MemberID: undefined
}
t.deepEqual(res, expect)
test.afterEach(() => {
sinon.restore();
})

test('.saveCard calls API and returns response', async (t) => {
card.config.axios = {
adapter: async (config: AxiosRequestConfig) => {
const response: AxiosResponse = {
data: 'CardSeq=cardseq&CardNo=cardno&Forward=forward&Brand=brand',
status: 200,
statusText: 'OK',
headers: {},
config
}

return Promise.resolve(response)
}
}
const expect: ISaveCardResult = {
CardSeq: 'cardseq',
CardNo: 'cardno',
Forward: 'forward',
Brand: 'brand'
};

sinon.stub(cardable, 'post').resolves(expect)

const args = {
SiteID: 'siteid',
SitePass: 'sitepass',
MemberID: 'memberid'
}
const res = await card.saveCard(args)
const res = await cardable.saveCard(args)

const expect: ISaveCardResult = {
CardSeq: 'cardseq',
CardNo: 'cardno',
Forward: 'forward',
Brand: 'brand'
}
t.deepEqual(res, expect)
})

test('.deleteCard calls API and returns response', async (t) => {
card.config.axios = {
adapter: async (config: AxiosRequestConfig) => {
const response: AxiosResponse = {
data: 'CardSeq=cardseq',
status: 200,
statusText: 'OK',
headers: {},
config
}

return Promise.resolve(response)
}

const expect: IDeleteCardResult = {
CardSeq: 'cardseq'
}

sinon.stub(cardable, 'post').resolves(expect)

const args = {
SiteID: 'siteid',
SitePass: 'sitepass',
MemberID: 'memberid',
SeqMode: SeqMode.Logic,
CardSeq: 'cardseq'
}
const res = await card.deleteCard(args)
const res = await cardable.deleteCard(args)

const expect: IDeleteCardResult = {
CardSeq: 'cardseq'
}
t.deepEqual(res, expect)
})

test('.searchCard calls API and returns response', async (t) => {
card.config.axios = {
adapter: async (config: AxiosRequestConfig) => {
const response: AxiosResponse = {
data: 'CardSeq=cardseq&DefaultFlag=1&CardName=cardname&CardNo=cardno&Expire=expire&HolderName=holdername&DeleteFlag=0',
status: 200,
statusText: 'OK',
headers: {},
config
}

return Promise.resolve(response)
}
}

const args = {
SiteID: 'siteid',
SitePass: 'sitepass',
MemberID: 'memberid',
SeqMode: SeqMode.Logic,
CardSeq: 'cardseq'
}
const res = await card.searchCard(args)

const result: ISearchCardResult = {
CardSeq: 'cardseq',
Expand All @@ -117,6 +63,19 @@ test('.searchCard calls API and returns response', async (t) => {
HolderName: 'holdername',
DeleteFlag: '0'
}

sinon.stub(cardable, 'post').resolves(result)

const expect = [result]

const args = {
SiteID: 'siteid',
SitePass: 'sitepass',
MemberID: 'memberid',
SeqMode: SeqMode.Logic,
CardSeq: 'cardseq'
}
const res = await cardable.searchCard(args)

t.deepEqual(res, expect)
})
Loading

0 comments on commit 44e1a43

Please sign in to comment.