@@ -8,7 +8,8 @@ const debug = require('debug')('request');
88const md5 = require ( 'md5' ) ;
99const Promise = require ( './promise' ) ;
1010const Cache = require ( './cache' ) ;
11- const AVError = require ( './error' ) . error ;
11+ const AVError = require ( './error' ) ;
12+ const AV = require ( './av' ) ;
1213
1314// 计算 X-LC-Sign 的签名方法
1415const sign = ( key , isMasterKey ) => {
@@ -88,7 +89,7 @@ const ajax = (method, resourceUrl, data, headers = {}, onprogress) => {
8889 return promise ;
8990} ;
9091
91- const setHeaders = ( AV , sessionToken ) => {
92+ const setHeaders = ( sessionToken ) => {
9293 const headers = {
9394 'X-LC-Id' : AV . applicationId ,
9495 'Content-Type' : 'application/json;charset=UTF-8' ,
@@ -128,7 +129,7 @@ const setHeaders = (AV, sessionToken) => {
128129 return promise ;
129130} ;
130131
131- const createApiUrl = ( AV , route , className , objectId , method , dataObject ) => {
132+ const createApiUrl = ( route , className , objectId , method , dataObject ) => {
132133 // TODO: 兼容 AV.serverURL 旧方式设置 API Host,后续去掉
133134 if ( AV . serverURL ) {
134135 AV . _config . APIServerURL = AV . serverURL ;
@@ -182,7 +183,7 @@ const cacheServerURL = (serverURL, ttl) => {
182183} ;
183184
184185// handle AV._request Error
185- const handleError = ( AV , res ) => {
186+ const handleError = ( res ) => {
186187 const promise = new Promise ( ) ;
187188 /**
188189 When API request need to redirect to the right location,
@@ -212,7 +213,7 @@ const handleError = (AV, res) => {
212213 return promise ;
213214} ;
214215
215- const setServerUrlByRegion = ( AV , region = 'cn' ) => {
216+ const setServerUrlByRegion = ( region = 'cn' ) => {
216217 // 服务器请求的节点 host
217218 const API_HOST = {
218219 cn : 'https://api.leancloud.cn' ,
@@ -248,39 +249,37 @@ const setServerUrlByRegion = (AV, region = 'cn') => {
248249 }
249250} ;
250251
251- const init = ( AV ) => {
252- /**
253- * route is classes, users, login, etc.
254- * objectId is null if there is no associated objectId.
255- * method is the http method for the REST API.
256- * dataObject is the payload as an object, or null if there is none.
257- * @ignore
258- */
259- AV . _request = ( route , className , objectId , method , dataObject = { } , sessionToken ) => {
260- if ( ! AV . applicationId ) {
261- throw new Error ( 'You must specify your applicationId using AV.init()' ) ;
262- }
252+ /**
253+ * route is classes, users, login, etc.
254+ * objectId is null if there is no associated objectId.
255+ * method is the http method for the REST API.
256+ * dataObject is the payload as an object, or null if there is none.
257+ * @ignore
258+ */
259+ const AVRequest = ( route , className , objectId , method , dataObject = { } , sessionToken ) => {
260+ if ( ! AV . applicationId ) {
261+ throw new Error ( 'You must specify your applicationId using AV.init()' ) ;
262+ }
263263
264- if ( ! AV . applicationKey && ! AV . masterKey ) {
265- throw new Error ( 'You must specify a AppKey using AV.init()' ) ;
266- }
264+ if ( ! AV . applicationKey && ! AV . masterKey ) {
265+ throw new Error ( 'You must specify a AppKey using AV.init()' ) ;
266+ }
267267
268- checkRouter ( route ) ;
269- const apiURL = createApiUrl ( AV , route , className , objectId , method , dataObject ) ;
268+ checkRouter ( route ) ;
269+ const apiURL = createApiUrl ( route , className , objectId , method , dataObject ) ;
270270
271- return setHeaders ( AV , sessionToken ) . then (
272- headers => ajax ( method , apiURL , dataObject , headers )
273- . then (
274- null ,
275- res => handleError ( AV , res )
276- . then ( location => ajax ( method , location , dataObject , headers ) )
277- )
278- ) ;
279- } ;
271+ return setHeaders ( sessionToken ) . then (
272+ headers => ajax ( method , apiURL , dataObject , headers )
273+ . then (
274+ null ,
275+ res => handleError ( res )
276+ . then ( location => ajax ( method , location , dataObject , headers ) )
277+ )
278+ ) ;
280279} ;
281280
282281module . exports = {
283- init,
284282 ajax,
283+ request : AVRequest ,
285284 setServerUrlByRegion,
286285} ;
0 commit comments