Skip to content

Commit e3cb9c4

Browse files
committed
fix(http): post / put method handling
1 parent d186054 commit e3cb9c4

File tree

6 files changed

+147
-136
lines changed

6 files changed

+147
-136
lines changed

src/angular/index.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
1-
import { NgModule } from '@angular/core';
1+
import { NgModule, ModuleWithProviders, Optional, SkipSelf } from '@angular/core';
22
import { TNSBrowserXhr } from './TNSBrowserXhr';
33
import { XhrFactory } from '@angular/common/http';
4+
import { TNSHttpDebugging } from '../http/http-request-common';
5+
6+
const BASE_PROVIDERS = [
7+
TNSBrowserXhr,
8+
{provide: XhrFactory, useExisting: TNSBrowserXhr}
9+
];
410

511
@NgModule({
6-
providers: [
7-
TNSBrowserXhr,
8-
{provide: XhrFactory, useExisting: TNSBrowserXhr}
9-
]
12+
providers: BASE_PROVIDERS
1013
})
1114
export class NativeScriptAsyncModule {
15+
static forRoot(options: { configuredProviders?: Array<any>; debug?: boolean; }): ModuleWithProviders {
16+
if (options.debug) {
17+
TNSHttpDebugging.enabled = true;
18+
}
19+
return {
20+
ngModule: NativeScriptAsyncModule,
21+
// Allow others to override if they need more control
22+
providers: [...BASE_PROVIDERS, ...(options.configuredProviders || [])]
23+
};
24+
}
25+
26+
constructor(
27+
@Optional()
28+
@SkipSelf()
29+
parentModule: NativeScriptAsyncModule
30+
) {
31+
if (parentModule) {
32+
throw new Error(`NativeScriptAsyncModule has already been loaded. Import NativeScriptAsyncModule in the AppModule only.`);
33+
}
34+
}
1235
}

src/http/http-request-common.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
export class TNSHttpDebugging {
2+
static enabled: boolean;
3+
// TODO: could add other debuggin options
4+
}
5+
16
export class ProgressEvent {
27
private _type: string;
38
private _lengthComputable: boolean;

src/http/http.ios.ts

Lines changed: 68 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as types from 'tns-core-modules/utils/types';
2-
import { Headers, HttpError, HttpRequestOptions } from './http-request-common';
3-
import * as domainDebugger from 'tns-core-modules/debugger';
2+
import { Headers, HttpError, HttpRequestOptions, TNSHttpDebugging } from './http-request-common';
43

54
export type CancellablePromise = Promise<any> & { cancel: () => void };
65

@@ -98,32 +97,26 @@ class NSURLSessionTaskDelegateImpl extends NSObject
9897
dataTask: NSURLSessionDataTask,
9998
data: NSData
10099
) {
101-
const method = this._request.HTTPMethod.toLowerCase();
102-
if (method !== 'post' && method !== 'put') {
103-
if (!this._loadingSent) {
104-
const lengthComputable = this._lastProgress.lengthComputable;
105-
this._onLoading({
106-
lengthComputable,
107-
loaded: this._data.length,
108-
total: this._lastProgress.total
109-
});
110-
this._loadingSent = true;
111-
}
112-
if (this._data) {
113-
this._data.appendData(data);
114-
}
115-
if (this._onProgress) {
116-
const lengthComputable = this._lastProgress.lengthComputable;
117-
this._onProgress({
118-
lengthComputable,
119-
loaded: this._data.length,
120-
total: this._lastProgress.total
121-
});
122-
}
123-
} else {
124-
if (this._data) {
125-
this._data.appendData(data);
126-
}
100+
// const method = this._request.HTTPMethod.toLowerCase();
101+
if (this._onLoading && !this._loadingSent) {
102+
const lengthComputable = this._lastProgress.lengthComputable;
103+
this._onLoading({
104+
lengthComputable,
105+
loaded: this._data.length,
106+
total: this._lastProgress.total
107+
});
108+
this._loadingSent = true;
109+
}
110+
if (this._onProgress) {
111+
const lengthComputable = this._lastProgress.lengthComputable;
112+
this._onProgress({
113+
lengthComputable,
114+
loaded: this._data.length,
115+
total: this._lastProgress.total
116+
});
117+
}
118+
if (this._data) {
119+
this._data.appendData(data);
127120
}
128121
}
129122

@@ -134,30 +127,21 @@ class NSURLSessionTaskDelegateImpl extends NSObject
134127
totalBytesSent,
135128
totalBytesExpectedToSend
136129
) {
130+
if (this._onLoading || this._onProgress) {
131+
const lengthComputable = totalBytesExpectedToSend > -1;
132+
this._lastProgress = {
133+
lengthComputable,
134+
loaded: totalBytesSent,
135+
total: lengthComputable ? totalBytesExpectedToSend : 0
136+
};
137+
if (this._onLoading && !this._loadingSent) {
138+
this._onLoading(this._lastProgress);
139+
this._loadingSent = true;
140+
}
137141
if (this._onProgress) {
138-
const method = this._request.HTTPMethod.toLowerCase();
139-
if (method === 'post' || method === 'put') {
140-
const lengthComputable = totalBytesExpectedToSend > -1;
141-
if (!this._loadingSent) {
142-
this._onLoading({
143-
lengthComputable,
144-
loaded: totalBytesSent,
145-
total: lengthComputable ? totalBytesExpectedToSend : 0
146-
});
147-
this._loadingSent = true;
148-
}
149-
this._onProgress({
150-
lengthComputable,
151-
loaded: totalBytesSent,
152-
total: lengthComputable ? totalBytesExpectedToSend : 0
153-
});
154-
this._lastProgress = {
155-
lengthComputable,
156-
loaded: totalBytesSent,
157-
total: lengthComputable ? totalBytesExpectedToSend : 0
158-
};
159-
}
142+
this._onProgress(this._lastProgress);
160143
}
144+
}
161145
}
162146

163147
public URLSessionDataTaskDidReceiveResponseCompletionHandler(
@@ -170,40 +154,33 @@ class NSURLSessionTaskDelegateImpl extends NSObject
170154
this._statusCode = (response as any).statusCode;
171155
this._url = response.URL.absoluteString;
172156
this._response = response;
173-
const method = this._request.HTTPMethod.toLowerCase();
174-
if (method !== 'post' && method !== 'put') {
175-
if (this._onHeaders) {
176-
const headers = {};
177-
if (response && response.allHeaderFields) {
178-
const headerFields = response.allHeaderFields;
179-
headerFields.enumerateKeysAndObjectsUsingBlock(
180-
(key, value, stop) => {
181-
addHeader(headers, key, value);
182-
}
183-
);
184-
}
185-
this._onHeaders(
186-
{
187-
headers,
188-
status: this._statusCode
157+
if (this._onHeaders) {
158+
const headers = {};
159+
if (response && response.allHeaderFields) {
160+
const headerFields = response.allHeaderFields;
161+
headerFields.enumerateKeysAndObjectsUsingBlock(
162+
(key, value, stop) => {
163+
addHeader(headers, key, value);
189164
}
190165
);
191166
}
192-
if (this._onProgress) {
193-
const lengthComputable =
194-
response.expectedContentLength &&
195-
response.expectedContentLength > -1;
196-
this._onProgress({
197-
lengthComputable,
198-
loaded: 0,
199-
total: lengthComputable ? response.expectedContentLength : 0
200-
});
201-
this._lastProgress = {
202-
lengthComputable,
203-
loaded: 0,
204-
total: lengthComputable ? response.expectedContentLength : 0
205-
};
206-
}
167+
this._onHeaders(
168+
{
169+
headers,
170+
status: this._statusCode
171+
}
172+
);
173+
}
174+
if (this._onProgress) {
175+
const lengthComputable =
176+
response.expectedContentLength &&
177+
response.expectedContentLength > -1;
178+
this._lastProgress = {
179+
lengthComputable,
180+
loaded: 0,
181+
total: lengthComputable ? response.expectedContentLength : 0
182+
};
183+
this._onProgress(this._lastProgress);
207184
}
208185
}
209186

@@ -273,12 +250,12 @@ class NSURLSessionTaskDelegateImpl extends NSObject
273250
}
274251
const request = this._request as NSURLRequest;
275252
let contentType = request.allHTTPHeaderFields.objectForKey('Content-Type');
276-
if (contentType == null) {
277-
contentType = request.allHTTPHeaderFields.objectForKey('content-Type');
253+
if (!contentType) {
254+
contentType = request.allHTTPHeaderFields.objectForKey('content-type');
278255
}
279256
let acceptHeader;
280257

281-
if (contentType == null) {
258+
if (!contentType) {
282259
acceptHeader = request.allHTTPHeaderFields.objectForKey('Accept');
283260
} else {
284261
acceptHeader = contentType;
@@ -390,8 +367,13 @@ export class Http {
390367

391368
try {
392369

393-
const network = domainDebugger.getNetwork();
394-
const debugRequest = network && network.create();
370+
let domainDebugger;
371+
let debugRequest;
372+
if (TNSHttpDebugging.enabled) {
373+
domainDebugger = require('tns-core-modules/debugger');
374+
const network = domainDebugger.getNetwork();
375+
debugRequest = network && network.create();
376+
}
395377

396378
const urlRequest = NSMutableURLRequest.requestWithURL(
397379
NSURL.URLWithString(options.url)

src/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
"typescript": "~3.4.5",
7070
"zone.js": "^0.8.26"
7171
},
72-
"dependencies": {},
72+
"dependencies": {
73+
"@types/node": "^12.0.10"
74+
},
7375
"bootstrapper": "nativescript-plugin-seed"
7476
}

0 commit comments

Comments
 (0)