@covisint/cui-api-interceptor
v1.0.6
Published
API validator/logger for cui products.
Downloads
4
Readme
cuiApiInterceptor
Lightweight logger / validator / adapter for $.ajax calls.
How it works
GET requests
Every time we get a response to a GET request we look at the accepts header and validate that against an existing JSON schema. Based on the response status and validity we issue a callback with different options to make it easier for you to log those requests in any fashion you'd like.
In the future there will be an option to set to which version of the schemas you want to validate and adapt to (have responses converted to).
Basic usage
const interceptorOptions = {
apiUri: [ appConfig.serviceUrl ],
stopIfInvalidPayload: false
};
window.cuiApiInterceptor.startGetInterceptor(interceptorOptions);
window.cuiApiInterceptor.startPrePutInterceptor(interceptorOptions);
window.cuiApiInterceptor.startPrePostInterceptor(interceptorOptions);
window.cuiApiInterceptor.startPostPutInterceptor(interceptorOptions);
window.cuiApiInterceptor.startPostPostInterceptor(interceptorOptions);
Advanced usage
Get / Post Put / Post Post interceptors
You can optionally set a method on the options object you pass the interceptor called logCallback
.
The logCallback
function gets passed a single object, with the following properties:
e <object>
: The jquery event matching the request. You can use this to access information like timestamp.xhr <object>
: Information on the responseresult <object>
: Information on the validity of the request
And, in the object result
, you will have the following properties:
settings <object>
: Information on the request and sources. Use this to see which domain the request was made from.endpoint <string>
: The endpoint which the call was made to.success <true || undefined>
: If the call got back a 200 and the response was validated.error <true || undefined>
: Wether or not there was an error (undefined if no error).errorType <only defined if error is true - string>
: The type of error the response gave us. Could be'http error'
,'response inconsistency'
or'missing schema'
.errors <only defined if error is true - array>
: An array of errors (objects or strings) associated with the error. If the errorType isresponse inconsistency
then the errors will be an array of objects returned by the AVJ library. Use this to access the data properties on the response that do not match against the schema.status <only defined if errorType is http error - string>
: The error status received.response <only defined if no error or errorType different than http error - any>
: The response that was received.missing schema <only defined if errorType is missing schema - string>
: The accepts header on that call.
Pre Put / Post Interceptor
For the prePut
and prePost
interceptors you can pass an extra property to the options object, stopIfInvalidPayload
, that will abort the ajax request if the payload sent is not valid against the corresponding json schema.
The properties passed to result object in logCallback for these interceptors are similar to those of the post request interceptors, with the exception of the error types and response.
response
is no longer defined, instead, payload
will contain the data that we are attempting to PUT/POST
errorType
is now one of the following: 'missing schema', 'invalid payload'