-
Notifications
You must be signed in to change notification settings - Fork 36
SDK design
Welcome to the aftership-sdk-nodejs wiki!
lib/
|--- aftership.js
|--- handler.js
|--- payload.js
|--- error/
|--- error.js
|--- error_enum.js
This SDK make use of request.js
, to make the aftership API call simplier.
aftership.js
is the class to create the core instance and store the config. It has a public function aftership.call(method, path, options, callback)
. Everytime you call it, it will create a payload, by the payload.js
pass to handler.js
, which will handle the payload with its retry policy, and return the err/result to the callback.
- check error with
_errorHandling(api_key, options)
method, will throw error if wrong format - inject
request.js
to the instance - store the config in the instance, include
- api_key
- endpoint
- proxy
- retry
- rate
- set the rate limit property to null
- create Proxy Method
- GET(...)
- POST(...)
- PUT(...)
- DEL(...)
Public instance function for making request
- options is optional, shift the argument list if options is not defined
- construct the payload, by
Payload()
class - handle the payload, by
Handler.handlePayload()
Private instance function for verify the constructer params
- Throw SDK error if api_key is of incorrect type
- Throw SDK error if any fields of options is of incorrect type
- check error with
_errorHandling(method, path, options)
method, will throw error if wrong format - construct the
request_object
, which will be pass to therequest.js
- delete any
null
orundefined
value in therequest_object
- construct the
payload
with key-value belows:
- request_object:
request_object
- retry: options.retry OR aftership.retry
- raw: options.raw OR false
- Add
retry_count = 0
ifretry
is true - return the
payload
Private instance function for verify the constructer params
- Throw SDK error if method is of incorrect type
- Throw SDK error if path is of incorrect type
- Throw SDK error if any fields of options is of incorrect type
Public static function for handle the payload
- Use the
aftership.request
, call it withpayload.request_obejct
- if
request
return error
- if it is one of
ETIMEDOUT
/ECONNRESET
/ECONNREFUSED
, retry it withHandler._retryRequestError()
- else return error to the callback
- else, request do return response from API
- update the
aftership.rate_limit
- if code == 429, retry it with
Handler._retryTooManyRequestError()
- if code >= 500, retry it with
Handler._retryApiError()
- if code != 200 or 201, return error to the callback
- else, it is a OK response
- if
raw
is true, return the JSON.stringify response - else, return the object response
- if
Private static function for retry request error
- if
retry
is true && retry_count < 5
- payload.retry_count++
- call
Handler.handlePayload()
with the same payload after 1 second
- else, return
Request
error object to the callback
Private static function for retry 429 error
- if
aftership.rate
is true
- calculate the
timeout
, with the current time andaftership.rate_limit.reset
- call
Handler.handlePayload()
with the same payload aftertimeout
second
- else, return
API
error object to the callback
Private static function for retry API error (>=500)
- if
retry
is true && retry_count < 5
- payload.retry_count++
- call
Handler.handlePayload()
with the same payload after 1 second
- else, return
API
error object to the callback