-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
183 lines (166 loc) · 4.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const qs = require('querystring')
const Connection = require('./lib/Connection')
const Query = require('./lib/Query')
const { UploadQueue } = require('./Entity')
/**
* Nextengine API for Nodejs
*
* @see http://api.next-e.jp
*/
class Nextengine {
get accessToken () {
return this.connection.accessToken
}
get refreshToken () {
return this.connection.refreshToken
}
/**
* Constructor
*
* @param object opts
* @param string opts.clientId client id
* @param string opts.clientSecret client secret
* @param string [opts.redirectUri] (optional)redirect uri
* @param string [opts.accessToken] (optional)access token
* @param string [opts.refreshToken] (optional)refresh token
*/
constructor (opts) {
this.clientId = opts.clientId
this.clientSecret = opts.clientSecret
this.redirectUri = opts.redirectUri
this.connection = this.getConnection(opts.accessToken, opts.refreshToken)
}
/**
* Send request to Nextengine API
*
* @see http://api.next-e.jp/request_url.php
* @param string path path of api
* @param object params request body
* @return Promise
*/
request (path, params) {
return this.connection.request('POST', path, params)
}
/**
* Return Connection instance
*
* You can override this method to use custom connection
*
* @return Connection
*/
getConnection (accessToken, refreshToken) {
return new Connection(accessToken, refreshToken)
}
/**
* Start query building
*
* @param string|Entity pathOrEntity ex. 'receiveorder_base' or ReceiveOrder
* @return Query
*/
query (pathOrEntity) {
const query = new Query(this.connection, pathOrEntity)
return query
}
/**
* Send {xxx}/create request
*
* @param string|Entity pathOrEntity ex. 'receiveorder_base' or ReceiveOrder
* @param object params request body
* @return Promise
*/
create (pathOrEntity, params) {
return this.query(pathOrEntity).request(params, 'create')
}
/**
* Send {xxx}/update request
*
* @param string|Entity pathOrEntity ex. 'receiveorder_base' or ReceiveOrder
* @param object params request body
* @return Promise
*/
update (pathOrEntity, params) {
return this.query(pathOrEntity).request(params, 'update')
}
/**
* Send {xxx}/update request
*
* @param string|Entity pathOrEntity ex. 'receiveorder_base' or ReceiveOrder
* @param object params request body
* @return Promise
*/
upload (pathOrEntity, params) {
return this.query(pathOrEntity).request(params, 'upload')
}
/**
* Poll upload queue until specified status
*
* @param int queueId ID of upload queue
* @param int[] [statuses=[UploadQueue.COMPLETED, UploadQueue.FAILED]] ID of upload status
* @param int [interval=5000] Interval of polling
* @return Promise
*/
waitFor (queueId, statuses, interval) {
statuses = statuses || [UploadQueue.COMPLETED, UploadQueue]
interval = interval || 5000
return this.query(UploadQueue)
.where('que_id', '=', queueId)
.get(['que_status_id'])
.then(res => {
if (statuses.indexOf(res.que_status_id) >= 0) {
return Promise.resolve()
} else {
return new Promise((resolve, reject) => {
setTimeout(() => {
this.waitFor(queueId, statuses, interval)
.then(resolve)
.catch(reject)
}, interval)
})
}
})
}
/**
* utility of upload + waitFor
*
* @see upload
* @see waitFor
* @param string|Entity pathOrEntity
* @param int[] [statuses] ID of upload status
* @return Promise
*/
uploadAndWaitFor (pathOrEntity, params, statuses) {
return this.upload(pathOrEntity, params)
.then(res => this.waitFor(res.que_id, statuses))
}
/**
* Fetch access token and refresh token
*
* @param string uid
* @param string state
* @return Promise
*/
authorize (uid, state) {
return this.request('/api_neauth', {
uid: uid,
state: state,
client_id: this.clientId,
client_secret: this.clientSecret
})
}
/**
* Get authorize screen url
*
* @see http://api.next-e.jp/param_uid_state.php
* @return string url for authorize
*/
getAuthorizeURL () {
const params = {
client_id: this.clientId
}
if (this.redirectUri) {
params.redirect_uri = this.redirectUri
}
return `${this.connection.constructor.HOST_PF}/users/sign_in/?${qs.stringify(params)}`
}
}
module.exports = Nextengine