servicestack-client
Advanced tools
Comparing version 0.0.36 to 0.0.37
{ | ||
"name": "servicestack-client", | ||
"title": "ServiceStack JavaScript Utils", | ||
"version": "0.0.36", | ||
"version": "0.0.37", | ||
"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", | ||
@@ -51,4 +51,4 @@ "homepage": "https://github.com/ServiceStack/servicestack-client", | ||
"mocha": "^2.5.3", | ||
"typescript": "^2.3.2" | ||
"typescript": "^2.4.1" | ||
} | ||
} |
@@ -25,13 +25,11 @@ # servicestack-client | ||
npm install servicestack-client --save | ||
npm install --save servicestack-client | ||
Or if using jspm: | ||
or if using jspm: | ||
jspm install servicestack-client | ||
npm install --save-dev servicestack-client | ||
The Type Definitions are contained in the above `servicestack-client` npm package, if using jspm it | ||
can be installed with: | ||
Where you'll also need to install the Type Definitions contained in the `servicestack-client` npm package separately. | ||
npm install servicestack-client --save-dev | ||
### Ideal Typed Message-based API | ||
@@ -298,3 +296,3 @@ | ||
the Type Definitions for W3C's `fetch` API, preferably by using the latest version of TypeScript and | ||
referencing the core **es2016** lib in TypeScript's `tsconfig.json`, e.g: | ||
referencing the core **es2016** and **dom** libs in TypeScript's `tsconfig.json`, e.g: | ||
@@ -304,5 +302,5 @@ ```json | ||
"compilerOptions": { | ||
"lib": [ | ||
"es2016" | ||
] | ||
"target": "es5", | ||
"module": "commonjs", | ||
"lib": [ "es2015", "dom" ] | ||
} | ||
@@ -309,0 +307,0 @@ } |
@@ -228,2 +228,12 @@ import 'fetch-everywhere'; | ||
} | ||
export interface ISendRequest { | ||
method: string; | ||
request: any | null; | ||
body?: any | null; | ||
args?: any; | ||
url?: string; | ||
returns?: { | ||
createResponse: () => any; | ||
}; | ||
} | ||
export declare class JsonServiceClient { | ||
@@ -257,12 +267,17 @@ baseUrl: string; | ||
postToUrl<T>(url: string, request: IReturn<T>, args?: any): Promise<T>; | ||
postBody<T>(request: IReturn<T>, body: string | any): Promise<T>; | ||
put<T>(request: IReturn<T>, args?: any): Promise<T>; | ||
putToUrl<T>(url: string, request: IReturn<T>, args?: any): Promise<T>; | ||
putBody<T>(request: IReturn<T>, body: string | any): Promise<T>; | ||
patch<T>(request: IReturn<T>, args?: any): Promise<T>; | ||
patchToUrl<T>(url: string, request: IReturn<T>, args?: any): Promise<T>; | ||
patchBody<T>(request: IReturn<T>, body: string | any): Promise<T>; | ||
createUrlFromDto<T>(method: string, request: IReturn<T>): string; | ||
toAbsoluteUrl(relativeOrAbsoluteUrl: string): string; | ||
private createRequest(method, request, args?, url?); | ||
private createRequest({method, request, args, url, body}); | ||
private createResponse<T>(res, request); | ||
private handleError(holdRes, res); | ||
send<T>(method: string, request: any | null, args?: any, url?: string): Promise<T>; | ||
private sendBody<T>(method, request, body); | ||
sendRequest<T>(info: ISendRequest): Promise<T>; | ||
raiseError(res: Response, error: any): any; | ||
@@ -286,2 +301,6 @@ } | ||
export declare const parseCookie: (setCookie: string) => Cookie; | ||
export declare const normalizeKey: (key: string) => string; | ||
export declare const normalize: (dto: any, deep?: boolean) => any; | ||
export declare const getField: (o: any, name: string) => any; | ||
export declare const parseResponseStatus: (json: string, defaultMsg?: any) => any; | ||
export declare const toDate: (s: string) => Date; | ||
@@ -288,0 +307,0 @@ export declare const toDateFmt: (s: string) => string; |
106
src/index.ts
@@ -312,3 +312,3 @@ import 'fetch-everywhere'; | ||
onError = (error?:any) => { | ||
onError = (error?:any):void => { | ||
if (this.stopped) return; | ||
@@ -561,3 +561,6 @@ if (!error) | ||
.then(r => r.map(x => this.toServerEventUser(x))) | ||
.catch(this.onError); | ||
.catch(e => { | ||
this.onError(e); | ||
return []; | ||
}); | ||
} | ||
@@ -666,2 +669,12 @@ | ||
export interface ISendRequest | ||
{ | ||
method:string; | ||
request:any|null; | ||
body?:any|null; | ||
args?:any; | ||
url?:string; | ||
returns?: { createResponse: () => any }; | ||
} | ||
export class JsonServiceClient { | ||
@@ -734,2 +747,6 @@ baseUrl: string; | ||
postBody<T>(request:IReturn<T>, body:string|any) { | ||
return this.sendBody<T>(HttpMethods.Post, request, body); | ||
} | ||
put<T>(request: IReturn<T>, args?:any): Promise<T> { | ||
@@ -743,2 +760,6 @@ return this.send<T>(HttpMethods.Put, request, args); | ||
putBody<T>(request:IReturn<T>, body:string|any) { | ||
return this.sendBody<T>(HttpMethods.Post, request, body); | ||
} | ||
patch<T>(request: IReturn<T>, args?:any): Promise<T> { | ||
@@ -752,2 +773,6 @@ return this.send<T>(HttpMethods.Patch, request, args); | ||
patchBody<T>(request:IReturn<T>, body:string|any) { | ||
return this.sendBody<T>(HttpMethods.Post, request, body); | ||
} | ||
createUrlFromDto<T>(method:string, request: IReturn<T>) : string { | ||
@@ -770,3 +795,4 @@ let url = combinePaths(this.replyBaseUrl, nameOf(request)); | ||
private createRequest(method:string, request:any|null, args?:any, url?:string) : [Request,IRequestFilterOptions] { | ||
private createRequest({ method, request, args, url, body } : ISendRequest) : [Request,IRequestFilterOptions] { | ||
if (!url) | ||
@@ -812,3 +838,3 @@ url = this.createUrlFromDto(method, request); | ||
if (HttpMethods.hasRequestBody(method)) | ||
(req as any).body = JSON.stringify(request); | ||
(req as any).body = body || JSON.stringify(request); | ||
@@ -904,12 +930,28 @@ var opt:IRequestFilterOptions = { url }; | ||
send<T>(method: string, request:any|null, args?:any, url?:string): Promise<T> { | ||
send<T>(method:string, request:any|null, args?:any, url?:string): Promise<T> { | ||
return this.sendRequest<T>({ method, request, args, url }); | ||
} | ||
var holdRes:Response = null; | ||
private sendBody<T>(method:string, request:IReturn<T>, body:string|any) { | ||
let url = combinePaths(this.replyBaseUrl, nameOf(request)); | ||
return this.sendRequest<T>({ | ||
method, | ||
request: body, | ||
body: typeof body == "string" ? body : JSON.stringify(body), | ||
url: appendQueryString(url, request), | ||
returns: request | ||
}); | ||
} | ||
const [req, opt] = this.createRequest(method, request, args, url); | ||
sendRequest<T>(info:ISendRequest): Promise<T> { | ||
const [req, opt] = this.createRequest(info); | ||
const returns = info.returns || info.request; | ||
let holdRes:Response = null; | ||
return fetch(opt.url || req.url, req) | ||
.then(res => { | ||
holdRes = res; | ||
const response = this.createResponse(res, request); | ||
const response = this.createResponse(res, returns); | ||
return response; | ||
@@ -923,9 +965,9 @@ }) | ||
jwtReq.refreshToken = this.refreshToken; | ||
var url = this.refreshTokenUri || this.createUrlFromDto(HttpMethods.Post, jwtReq); | ||
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(method, request, args); | ||
const [req, opt] = this.createRequest(info); | ||
return fetch(opt.url || req.url, req) | ||
.then(res => this.createResponse(res, request)) | ||
.then(res => this.createResponse(res, returns)) | ||
.catch(res => this.handleError(holdRes, res)); | ||
@@ -938,5 +980,5 @@ }) | ||
return this.onAuthenticationRequired().then(() => { | ||
const [req, opt] = this.createRequest(method, request, args); | ||
const [req, opt] = this.createRequest(info); | ||
return fetch(opt.url || req.url, req) | ||
.then(res => this.createResponse(res, request)) | ||
.then(res => this.createResponse(res, returns)) | ||
.catch(res => this.handleError(holdRes, res)); | ||
@@ -1219,2 +1261,40 @@ }); | ||
export const normalizeKey = (key: string) => key.toLowerCase().replace(/_/g, ''); | ||
const isArray = (o: any) => Object.prototype.toString.call(o) === '[object Array]'; | ||
export const normalize = (dto: any, deep?: boolean) => { | ||
if (isArray(dto)) { | ||
if (!deep) return dto; | ||
const to = []; | ||
for (let i = 0; i < dto.length; i++) { | ||
to[i] = normalize(dto[i], deep); | ||
} | ||
return to; | ||
} | ||
if (typeof dto != "object") return dto; | ||
var o = {}; | ||
for (let k in dto) { | ||
o[normalizeKey(k)] = deep ? normalize(dto[k], deep) : dto[k]; | ||
} | ||
return o; | ||
} | ||
export const getField = (o: any, name: string) => | ||
o == null || name == null ? null : | ||
o[name] || | ||
o[Object.keys(o).filter(k => normalizeKey(k) === normalizeKey(name))[0] || '']; | ||
export const parseResponseStatus = (json:string, defaultMsg=null) => { | ||
try { | ||
var err = JSON.parse(json); | ||
return sanitize(err.ResponseStatus || err.responseStatus); | ||
} catch (e) { | ||
return { | ||
message: defaultMsg || e.message || e, | ||
__error: { error: e, json: json } | ||
}; | ||
} | ||
}; | ||
export const toDate = (s: string) => new Date(parseFloat(/Date\(([^)]+)\)/.exec(s)[1])); | ||
@@ -1221,0 +1301,0 @@ export const toDateFmt = (s: string) => dateFmt(toDate(s)); |
151886
2409
309