servicestack-client
Advanced tools
Comparing version 0.0.39 to 0.0.40
{ | ||
"name": "servicestack-client", | ||
"title": "ServiceStack JavaScript Utils", | ||
"version": "0.0.39", | ||
"version": "0.0.40", | ||
"description": "ServiceStack's TypeScript library providing convenience utilities in developing web apps. Integrates with ServiceStack's Server features including ServiceClient, Server Events, Error Handling and Validation", | ||
@@ -47,8 +47,8 @@ "homepage": "https://github.com/ServiceStack/servicestack-client", | ||
"@types/chai": "^3.5.2", | ||
"@types/mocha": "^2.2.41", | ||
"chai": "^3.5.0", | ||
"@types/mocha": "^2.2.43", | ||
"chai": "^4.1.2", | ||
"fetch-everywhere": "^1.0.5", | ||
"mocha": "^2.5.3", | ||
"typescript": "^2.4.2" | ||
"mocha": "^4.0.1", | ||
"typescript": "^2.5.3" | ||
} | ||
} |
@@ -26,4 +26,6 @@ import 'fetch-everywhere'; | ||
export declare class ErrorResponse { | ||
type: ErrorResponseType; | ||
responseStatus: ResponseStatus; | ||
} | ||
export declare type ErrorResponseType = null | "RefreshTokenException"; | ||
export interface IResolver { | ||
@@ -278,3 +280,3 @@ tryResolve(Function: any): any; | ||
private createResponse<T>(res, request); | ||
private handleError(holdRes, res); | ||
private handleError(holdRes, res, type?); | ||
send<T>(method: string, request: any | null, args?: any, url?: string): Promise<T>; | ||
@@ -281,0 +283,0 @@ private sendBody<T>(method, request, body, args?); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
require("fetch-everywhere"); | ||
var ResponseStatus = (function () { | ||
var ResponseStatus = /** @class */ (function () { | ||
function ResponseStatus() { | ||
@@ -10,3 +10,3 @@ } | ||
exports.ResponseStatus = ResponseStatus; | ||
var ResponseError = (function () { | ||
var ResponseError = /** @class */ (function () { | ||
function ResponseError() { | ||
@@ -17,3 +17,3 @@ } | ||
exports.ResponseError = ResponseError; | ||
var ErrorResponse = (function () { | ||
var ErrorResponse = /** @class */ (function () { | ||
function ErrorResponse() { | ||
@@ -24,3 +24,3 @@ } | ||
exports.ErrorResponse = ErrorResponse; | ||
var NewInstanceResolver = (function () { | ||
var NewInstanceResolver = /** @class */ (function () { | ||
function NewInstanceResolver() { | ||
@@ -34,3 +34,3 @@ } | ||
exports.NewInstanceResolver = NewInstanceResolver; | ||
var SingletonInstanceResolver = (function () { | ||
var SingletonInstanceResolver = /** @class */ (function () { | ||
function SingletonInstanceResolver() { | ||
@@ -61,3 +61,3 @@ } | ||
})(ReadyState = exports.ReadyState || (exports.ReadyState = {})); | ||
var ServerEventsClient = (function () { | ||
var ServerEventsClient = /** @class */ (function () { | ||
function ServerEventsClient(baseUrl, channels, options, eventSource) { | ||
@@ -101,3 +101,3 @@ if (options === void 0) { options = {}; } | ||
var el = els && els[0]; | ||
var eventId = e.lastEventId; | ||
var eventId = parseInt(e.lastEventId); | ||
var data = e.data; | ||
@@ -125,2 +125,4 @@ var type = TypeMap[cmd] || "ServerEventMessage"; | ||
fn.call(el || document.body, _this.connectionInfo, request); | ||
if (_this.stopped) | ||
return; | ||
} | ||
@@ -262,2 +264,6 @@ if (opt.heartbeatUrl) { | ||
} | ||
var opt = this.options; | ||
if (opt && opt.heartbeat) { | ||
clearInterval(opt.heartbeat); | ||
} | ||
var hold = this.connectionInfo; | ||
@@ -468,3 +474,3 @@ if (hold == null || hold.unRegisterUrl == null) | ||
exports.ServerEventsClient = ServerEventsClient; | ||
var ServerEventReceiver = (function () { | ||
var ServerEventReceiver = /** @class */ (function () { | ||
function ServerEventReceiver() { | ||
@@ -476,3 +482,3 @@ } | ||
exports.ServerEventReceiver = ServerEventReceiver; | ||
var UpdateEventSubscriber = (function () { | ||
var UpdateEventSubscriber = /** @class */ (function () { | ||
function UpdateEventSubscriber() { | ||
@@ -485,3 +491,3 @@ } | ||
exports.UpdateEventSubscriber = UpdateEventSubscriber; | ||
var UpdateEventSubscriberResponse = (function () { | ||
var UpdateEventSubscriberResponse = /** @class */ (function () { | ||
function UpdateEventSubscriberResponse() { | ||
@@ -492,3 +498,3 @@ } | ||
exports.UpdateEventSubscriberResponse = UpdateEventSubscriberResponse; | ||
var GetEventSubscribers = (function () { | ||
var GetEventSubscribers = /** @class */ (function () { | ||
function GetEventSubscribers() { | ||
@@ -501,3 +507,3 @@ } | ||
exports.GetEventSubscribers = GetEventSubscribers; | ||
var ServerEventUser = (function () { | ||
var ServerEventUser = /** @class */ (function () { | ||
function ServerEventUser() { | ||
@@ -508,3 +514,3 @@ } | ||
exports.ServerEventUser = ServerEventUser; | ||
var HttpMethods = (function () { | ||
var HttpMethods = /** @class */ (function () { | ||
function HttpMethods() { | ||
@@ -525,3 +531,3 @@ } | ||
exports.HttpMethods = HttpMethods; | ||
var GetAccessToken = (function () { | ||
var GetAccessToken = /** @class */ (function () { | ||
function GetAccessToken() { | ||
@@ -533,3 +539,3 @@ } | ||
}()); | ||
var GetAccessTokenResponse = (function () { | ||
var GetAccessTokenResponse = /** @class */ (function () { | ||
function GetAccessTokenResponse() { | ||
@@ -540,3 +546,3 @@ } | ||
exports.GetAccessTokenResponse = GetAccessTokenResponse; | ||
var JsonServiceClient = (function () { | ||
var JsonServiceClient = /** @class */ (function () { | ||
function JsonServiceClient(baseUrl) { | ||
@@ -701,4 +707,5 @@ if (baseUrl == null) | ||
}; | ||
JsonServiceClient.prototype.handleError = function (holdRes, res) { | ||
JsonServiceClient.prototype.handleError = function (holdRes, res, type) { | ||
var _this = this; | ||
if (type === void 0) { type = null; } | ||
if (res instanceof Error) | ||
@@ -708,4 +715,5 @@ throw this.raiseError(holdRes, res); | ||
if (res.bodyUsed) | ||
throw this.raiseError(res, createErrorResponse(res.status, res.statusText)); | ||
if (typeof res.json == "undefined" && res.responseStatus) { | ||
throw this.raiseError(res, createErrorResponse(res.status, res.statusText, type)); | ||
var isErrorResponse = typeof res.json == "undefined" && res.responseStatus; | ||
if (isErrorResponse) { | ||
return new Promise(function (resolve, reject) { | ||
@@ -718,3 +726,5 @@ return reject(_this.raiseError(null, res)); | ||
if (!errorDto.responseStatus) | ||
throw createErrorResponse(res.status, res.statusText); | ||
throw createErrorResponse(res.status, res.statusText, type); | ||
if (type != null) | ||
errorDto.type = type; | ||
throw errorDto; | ||
@@ -724,3 +734,3 @@ }).catch(function (error) { | ||
if (error instanceof Error) | ||
throw _this.raiseError(res, createErrorResponse(res.status, res.statusText)); | ||
throw _this.raiseError(res, createErrorResponse(res.status, res.statusText, type)); | ||
throw _this.raiseError(res, error); | ||
@@ -748,2 +758,8 @@ }); | ||
var holdRes = null; | ||
var resendRequest = function () { | ||
var _a = _this.createRequest(info), req = _a[0], opt = _a[1]; | ||
return fetch(opt.url || req.url, req) | ||
.then(function (res) { return _this.createResponse(res, returns); }) | ||
.catch(function (res) { return _this.handleError(holdRes, res); }); | ||
}; | ||
return fetch(opt.url || req.url, req) | ||
@@ -758,22 +774,17 @@ .then(function (res) { | ||
if (_this.refreshToken) { | ||
var jwtReq = new GetAccessToken(); | ||
jwtReq.refreshToken = _this.refreshToken; | ||
var url = _this.refreshTokenUri || _this.createUrlFromDto(HttpMethods.Post, jwtReq); | ||
return _this.postToUrl(url, jwtReq) | ||
.then(function (r) { | ||
_this.bearerToken = r.accessToken; | ||
var _a = _this.createRequest(info), req = _a[0], opt = _a[1]; | ||
return fetch(opt.url || req.url, req) | ||
.then(function (res) { return _this.createResponse(res, returns); }) | ||
.catch(function (res) { return _this.handleError(holdRes, res); }); | ||
}) | ||
.catch(function (res) { return _this.handleError(holdRes, res); }); | ||
var jwtReq_1 = new GetAccessToken(); | ||
jwtReq_1.refreshToken = _this.refreshToken; | ||
var url = _this.refreshTokenUri || _this.createUrlFromDto(HttpMethods.Post, jwtReq_1); | ||
var _a = _this.createRequest({ method: HttpMethods.Post, request: jwtReq_1, args: null, url: url }), jwtRequest = _a[0], jwtOpt = _a[1]; | ||
return fetch(url, jwtRequest) | ||
.then(function (r) { return _this.createResponse(r, jwtReq_1).then(function (jwtResponse) { | ||
_this.bearerToken = jwtResponse.accessToken; | ||
return resendRequest(); | ||
}); }) | ||
.catch(function (res) { | ||
return _this.handleError(holdRes, res, "RefreshTokenException"); | ||
}); | ||
} | ||
if (_this.onAuthenticationRequired) { | ||
return _this.onAuthenticationRequired().then(function () { | ||
var _a = _this.createRequest(info), req = _a[0], opt = _a[1]; | ||
return fetch(opt.url || req.url, req) | ||
.then(function (res) { return _this.createResponse(res, returns); }) | ||
.catch(function (res) { return _this.handleError(holdRes, res); }); | ||
}); | ||
return _this.onAuthenticationRequired().then(resendRequest); | ||
} | ||
@@ -793,4 +804,7 @@ } | ||
exports.JsonServiceClient = JsonServiceClient; | ||
var createErrorResponse = function (errorCode, message) { | ||
var createErrorResponse = function (errorCode, message, type) { | ||
if (type === void 0) { type = null; } | ||
var error = new ErrorResponse(); | ||
if (type != null) | ||
error.type = type; | ||
error.responseStatus = new ResponseStatus(); | ||
@@ -797,0 +811,0 @@ error.responseStatus.errorCode = errorCode && errorCode.toString(); |
@@ -23,5 +23,8 @@ import 'fetch-everywhere'; | ||
export class ErrorResponse { | ||
type: ErrorResponseType; | ||
responseStatus: ResponseStatus; | ||
} | ||
export type ErrorResponseType = null | "RefreshTokenException"; | ||
export interface IResolver | ||
@@ -209,3 +212,3 @@ { | ||
const eventId = (e as any).lastEventId; | ||
const eventId = parseInt((e as any).lastEventId); | ||
const data = e.data; | ||
@@ -239,2 +242,4 @@ const type = TypeMap[cmd] || "ServerEventMessage"; | ||
fn.call(el || document.body, this.connectionInfo, request); | ||
if (this.stopped) | ||
return; | ||
} | ||
@@ -375,2 +380,7 @@ | ||
var opt = this.options; | ||
if (opt && opt.heartbeat) { | ||
clearInterval(opt.heartbeat); | ||
} | ||
var hold = this.connectionInfo; | ||
@@ -897,3 +907,3 @@ if (hold == null || hold.unRegisterUrl == null) | ||
private handleError(holdRes:Response, res) { | ||
private handleError(holdRes:Response, res, type:ErrorResponseType=null) { | ||
@@ -905,5 +915,6 @@ if (res instanceof Error) | ||
if (res.bodyUsed) | ||
throw this.raiseError(res, createErrorResponse(res.status, res.statusText)); | ||
throw this.raiseError(res, createErrorResponse(res.status, res.statusText, type)); | ||
if (typeof res.json == "undefined" && res.responseStatus) { | ||
let isErrorResponse = typeof res.json == "undefined" && res.responseStatus; | ||
if (isErrorResponse) { | ||
return new Promise((resolve,reject) => | ||
@@ -917,3 +928,5 @@ reject(this.raiseError(null, res)) | ||
if (!errorDto.responseStatus) | ||
throw createErrorResponse(res.status, res.statusText); | ||
throw createErrorResponse(res.status, res.statusText, type); | ||
if (type != null) | ||
errorDto.type = type; | ||
throw errorDto; | ||
@@ -923,3 +936,3 @@ }).catch(error => { | ||
if (error instanceof Error) | ||
throw this.raiseError(res, createErrorResponse(res.status, res.statusText)); | ||
throw this.raiseError(res, createErrorResponse(res.status, res.statusText, type)); | ||
throw this.raiseError(res, error); | ||
@@ -950,4 +963,11 @@ }); | ||
const returns = info.returns || info.request; | ||
let holdRes:Response = null; | ||
const resendRequest = () => { | ||
const [req, opt] = this.createRequest(info); | ||
return fetch(opt.url || req.url, req) | ||
.then(res => this.createResponse(res, returns)) | ||
.catch(res => this.handleError(holdRes, res)); | ||
} | ||
let holdRes:Response = null; | ||
return fetch(opt.url || req.url, req) | ||
@@ -966,20 +986,16 @@ .then(res => { | ||
let url = this.refreshTokenUri || this.createUrlFromDto(HttpMethods.Post, jwtReq); | ||
return this.postToUrl<GetAccessTokenResponse>(url, jwtReq) | ||
.then(r => { | ||
this.bearerToken = r.accessToken; | ||
const [req, opt] = this.createRequest(info); | ||
return fetch(opt.url || req.url, req) | ||
.then(res => this.createResponse(res, returns)) | ||
.catch(res => this.handleError(holdRes, res)); | ||
}) | ||
.catch(res => this.handleError(holdRes, res)); | ||
let [jwtRequest, jwtOpt] = this.createRequest({ method:HttpMethods.Post, request:jwtReq, args:null, url }); | ||
return fetch(url, jwtRequest) | ||
.then(r => this.createResponse(r, jwtReq).then(jwtResponse => { | ||
this.bearerToken = jwtResponse.accessToken; | ||
return resendRequest(); | ||
})) | ||
.catch(res => { | ||
return this.handleError(holdRes, res, "RefreshTokenException") | ||
}); | ||
} | ||
if (this.onAuthenticationRequired) { | ||
return this.onAuthenticationRequired().then(() => { | ||
const [req, opt] = this.createRequest(info); | ||
return fetch(opt.url || req.url, req) | ||
.then(res => this.createResponse(res, returns)) | ||
.catch(res => this.handleError(holdRes, res)); | ||
}); | ||
return this.onAuthenticationRequired().then(resendRequest); | ||
} | ||
@@ -1000,4 +1016,6 @@ } | ||
const createErrorResponse = (errorCode: string|number, message: string) => { | ||
const createErrorResponse = (errorCode: string|number, message: string, type:ErrorResponseType=null) => { | ||
const error = new ErrorResponse(); | ||
if (type != null) | ||
error.type = type; | ||
error.responseStatus = new ResponseStatus(); | ||
@@ -1004,0 +1022,0 @@ error.responseStatus.errorCode = errorCode && errorCode.toString(); |
Sorry, the diff of this file is not supported yet
159954
2504