1 | import 'rxjs/add/operator/map';
|
2 | import { Log, Level } from 'ng2-logger/browser';
|
3 | import { Cookie } from './cookie';
|
4 | import { Mapping } from './mapping';
|
5 | import { Models as HelpersModels } from 'typescript-class-helpers/browser/models';
|
6 | import { JSON10 } from 'json10/browser';
|
7 | import { RequestCache } from './request-cache';
|
8 | import * as _ from 'lodash';
|
9 | const log = Log.create('rest namespace', Level.__NOTHING);
|
10 | export var Models;
|
11 | (function (Models) {
|
12 | Models.MethodConfig = HelpersModels.MethodConfig;
|
13 | Models.ClassConfig = HelpersModels.ClassConfig;
|
14 | Models.ParamConfig = HelpersModels.ParamConfig;
|
15 | ;
|
16 | ;
|
17 | [];
|
18 | class BaseBody {
|
19 | toJSON(data, isJSONArray = false) {
|
20 | let r = isJSONArray ? [] : {};
|
21 | if (typeof data === 'string') {
|
22 | try {
|
23 | r = JSON.parse(data);
|
24 | }
|
25 | catch (e) { }
|
26 | }
|
27 | else if (typeof data === 'object') {
|
28 | return data;
|
29 | }
|
30 | return r;
|
31 | }
|
32 | }
|
33 | Models.BaseBody = BaseBody;
|
34 | class HttpBody extends BaseBody {
|
35 | constructor(body, isArray = false, entity, circular) {
|
36 | super();
|
37 | this.body = body;
|
38 | this.isArray = isArray;
|
39 | this.entity = entity;
|
40 | this.circular = circular;
|
41 | }
|
42 | get json() {
|
43 | if (this.entity && typeof this.entity === 'object') {
|
44 | const json = this.toJSON(this.body, this.isArray);
|
45 | return Mapping.encode(json, this.entity, this.circular);
|
46 | }
|
47 | let res = this.toJSON(this.body, this.isArray);
|
48 | if (this.circular && Array.isArray(this.circular)) {
|
49 | res = JSON10.parse(JSON.stringify(res), this.circular);
|
50 | }
|
51 | return res;
|
52 | }
|
53 | get text() {
|
54 | return this.body.replace(/^\"/, '').replace(/\"$/, '');
|
55 | }
|
56 | }
|
57 | Models.HttpBody = HttpBody;
|
58 | class ErrorBody extends BaseBody {
|
59 | constructor(data) {
|
60 | super();
|
61 | this.data = data;
|
62 | }
|
63 | get json() {
|
64 | return this.toJSON(this.data);
|
65 | }
|
66 | get text() {
|
67 | return this.data;
|
68 | }
|
69 | }
|
70 | Models.ErrorBody = ErrorBody;
|
71 | class BaseResponse {
|
72 | constructor(responseText, headers, statusCode, isArray = false) {
|
73 | this.responseText = responseText;
|
74 | this.headers = headers;
|
75 | this.statusCode = statusCode;
|
76 | this.isArray = isArray;
|
77 | }
|
78 | get cookies() {
|
79 | return BaseResponse.cookies;
|
80 | }
|
81 | }
|
82 | BaseResponse.cookies = Cookie.Instance;
|
83 | Models.BaseResponse = BaseResponse;
|
84 | class HttpResponse extends BaseResponse {
|
85 | constructor(sourceRequest, responseText, headers, statusCode, entity, circular, isArray = false) {
|
86 | super(responseText, headers, statusCode, isArray);
|
87 | this.sourceRequest = sourceRequest;
|
88 | this.responseText = responseText;
|
89 | this.headers = headers;
|
90 | this.statusCode = statusCode;
|
91 | this.entity = entity;
|
92 | this.circular = circular;
|
93 | this.isArray = isArray;
|
94 | this.init();
|
95 | }
|
96 | init() {
|
97 | if (typeof this.entity === 'string') {
|
98 |
|
99 | let entityJSON = this.headers.getAll(this.entity);
|
100 | if (!!entityJSON) {
|
101 | this.entity = JSON.parse(entityJSON.join());
|
102 | }
|
103 | }
|
104 | if (typeof this.circular === 'string') {
|
105 |
|
106 | let circuralJSON = this.headers.getAll(this.circular);
|
107 | if (!!circuralJSON) {
|
108 | this.circular = JSON.parse(circuralJSON.join());
|
109 | }
|
110 | }
|
111 | this.body = new HttpBody(this.responseText, this.isArray, this.entity, this.circular);
|
112 | }
|
113 | get cache() {
|
114 | if (_.isUndefined(this.rq)) {
|
115 | this.rq = new RequestCache(this);
|
116 | }
|
117 | return new RequestCache(this);
|
118 | }
|
119 | }
|
120 | Models.HttpResponse = HttpResponse;
|
121 | class HttpResponseError extends BaseResponse {
|
122 |
|
123 |
|
124 | constructor(message, responseText, headers, statusCode) {
|
125 | super(responseText, headers, statusCode);
|
126 | this.message = message;
|
127 | this.body = new ErrorBody(responseText);
|
128 | }
|
129 | }
|
130 | Models.HttpResponseError = HttpResponseError;
|
131 | })(Models || (Models = {}));
|
132 |
|
\ | No newline at end of file |