Skip to content

Commit 71d38a0

Browse files
authored
Merge pull request #1 from nstudio/fix-post-put
fix(http): post / put method handling
2 parents 33ff958 + 00a1df3 commit 71d38a0

File tree

13 files changed

+298
-250
lines changed

13 files changed

+298
-250
lines changed

demo-angular/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"@angular/platform-browser-dynamic": "~8.0.0",
2323
"@angular/router": "~8.0.0",
2424
"nativescript-angular": "~8.0.0",
25-
"nativescript-async": "file:../src",
25+
"nativescript-http-async": "file:../src",
2626
"nativescript-theme-core": "~1.0.4",
2727
"reflect-metadata": "~0.1.12",
2828
"rxjs": "~6.5.0",

demo-angular/src/app/app.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
22
import { NativeScriptModule } from 'nativescript-angular/nativescript.module';
3-
import { NativeScriptAsyncModule } from 'nativescript-async/angular';
3+
import { NativeScriptHttpAsyncModule } from 'nativescript-http-async/angular';
44
import { AppRoutingModule } from './app-routing.module';
55
import { AppComponent } from './app.component';
66
import { NativeScriptHttpClientModule } from 'nativescript-angular/http-client';
@@ -12,7 +12,7 @@ import { NativeScriptHttpClientModule } from 'nativescript-angular/http-client';
1212
imports: [
1313
NativeScriptModule,
1414
NativeScriptHttpClientModule,
15-
NativeScriptAsyncModule,
15+
NativeScriptHttpAsyncModule,
1616
AppRoutingModule
1717
],
1818
declarations: [

demo-angular/src/app/services/data.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ObservableArray } from 'tns-core-modules/data/observable-array';
55
import { from } from 'rxjs';
66
import { map, mergeMap , concatMap,switchMap} from 'rxjs/internal/operators';
77
import * as imageSrc from 'tns-core-modules/image-source';
8-
import { TNSXMLHttpRequest, FileManager } from 'nativescript-async';
8+
import { TNSXMLHttpRequest, FileManager } from 'nativescript-http-async';
99
import * as fs from 'tns-core-modules/file-system';
1010
import { releaseNativeObject } from 'tns-core-modules/utils/utils';
1111
declare var UIImageJPEGRepresentation, NSDataBase64EncodingOptions, android, java;

demo/app/home/home-page.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Async } from 'nativescript-async';
1+
import { Async } from 'nativescript-http-async';
22
console.log(new Async().message);
33
/*
44
In NativeScript, a file with the same name as an XML file is known as

demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"license": "SEE LICENSE IN <your-license-filename>",
1313
"repository": "<fill-your-repository-here>",
1414
"dependencies": {
15-
"nativescript-async": "file:../src",
15+
"nativescript-http-async": "file:../src",
1616
"nativescript-theme-core": "~1.0.4",
1717
"tns-core-modules": "~5.4.0"
1818
},

src/angular/index.ts

Lines changed: 29 additions & 6 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
})
11-
export class NativeScriptAsyncModule {
14+
export class NativeScriptHttpAsyncModule {
15+
static forRoot(options: { configuredProviders?: Array<any>; debug?: boolean; }): ModuleWithProviders {
16+
if (options.debug) {
17+
TNSHttpDebugging.enabled = true;
18+
}
19+
return {
20+
ngModule: NativeScriptHttpAsyncModule,
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: NativeScriptHttpAsyncModule
30+
) {
31+
if (parentModule) {
32+
throw new Error(`NativeScriptHttpAsyncModule has already been loaded. Import NativeScriptHttpAsyncModule in the AppModule only.`);
33+
}
34+
}
1235
}

src/file/file.android.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
declare var com;
2+
13
export class FileManager {
24

35
public static writeFile(bytes: any, path: string, callback: (...args) => void) {

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: 63 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,22 @@ 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 (data) {
102+
this._data.appendData(data);
103+
104+
const lastProgress: any = this._lastProgress || {
105+
lengthComputable: false,
106+
total: 0
107+
};
108+
lastProgress.loaded = this._data.length;
109+
if (this._onLoading && !this._loadingSent) {
110+
this._onLoading(lastProgress);
111+
this._loadingSent = true;
112+
}
113+
if (this._onProgress) {
114+
this._onProgress(lastProgress);
115+
}
127116
}
128117
}
129118

@@ -134,30 +123,20 @@ class NSURLSessionTaskDelegateImpl extends NSObject
134123
totalBytesSent,
135124
totalBytesExpectedToSend
136125
) {
126+
if (this._onLoading || this._onProgress) {
127+
this._lastProgress = {
128+
lengthComputable: totalBytesExpectedToSend > -1,
129+
loaded: totalBytesSent,
130+
total: totalBytesExpectedToSend > -1 ? totalBytesExpectedToSend : 0
131+
};
132+
if (this._onLoading && !this._loadingSent) {
133+
this._onLoading(this._lastProgress);
134+
this._loadingSent = true;
135+
}
137136
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-
}
137+
this._onProgress(this._lastProgress);
160138
}
139+
}
161140
}
162141

163142
public URLSessionDataTaskDidReceiveResponseCompletionHandler(
@@ -170,40 +149,33 @@ class NSURLSessionTaskDelegateImpl extends NSObject
170149
this._statusCode = (response as any).statusCode;
171150
this._url = response.URL.absoluteString;
172151
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
152+
if (this._onHeaders) {
153+
const headers = {};
154+
if (response && response.allHeaderFields) {
155+
const headerFields = response.allHeaderFields;
156+
headerFields.enumerateKeysAndObjectsUsingBlock(
157+
(key, value, stop) => {
158+
addHeader(headers, key, value);
189159
}
190160
);
191161
}
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-
}
162+
this._onHeaders(
163+
{
164+
headers,
165+
status: this._statusCode
166+
}
167+
);
168+
}
169+
if (this._onProgress) {
170+
const lengthComputable =
171+
response.expectedContentLength &&
172+
response.expectedContentLength > -1;
173+
this._lastProgress = {
174+
lengthComputable,
175+
loaded: 0,
176+
total: lengthComputable ? response.expectedContentLength : 0
177+
};
178+
this._onProgress(this._lastProgress);
207179
}
208180
}
209181

@@ -273,12 +245,12 @@ class NSURLSessionTaskDelegateImpl extends NSObject
273245
}
274246
const request = this._request as NSURLRequest;
275247
let contentType = request.allHTTPHeaderFields.objectForKey('Content-Type');
276-
if (contentType == null) {
277-
contentType = request.allHTTPHeaderFields.objectForKey('content-Type');
248+
if (!contentType) {
249+
contentType = request.allHTTPHeaderFields.objectForKey('content-type');
278250
}
279251
let acceptHeader;
280252

281-
if (contentType == null) {
253+
if (!contentType) {
282254
acceptHeader = request.allHTTPHeaderFields.objectForKey('Accept');
283255
} else {
284256
acceptHeader = contentType;
@@ -390,8 +362,13 @@ export class Http {
390362

391363
try {
392364

393-
const network = domainDebugger.getNetwork();
394-
const debugRequest = network && network.create();
365+
let domainDebugger;
366+
let debugRequest;
367+
if (TNSHttpDebugging.enabled) {
368+
domainDebugger = require('tns-core-modules/debugger');
369+
const network = domainDebugger.getNetwork();
370+
debugRequest = network && network.create();
371+
}
395372

396373
const urlRequest = NSMutableURLRequest.requestWithURL(
397374
NSURL.URLWithString(options.url)

0 commit comments

Comments
 (0)