Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fetch.js - Copy header if it is windows.Headers object AND also added setTimeout #21

Merged
merged 5 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ MIT
The plugin conforms to the Cordova plugin specification, it can be installed
using the Cordova / Phonegap command line interface.

phonegap plugin add https://github.com/aporat/cordova-plugin-fetch.git

cordova plugin add https://github.com/aporat/cordova-plugin-fetch.git
cordova plugin add https://github.com/8bhsolutions/cordova-plugin-fetch.git

## Usage

Expand Down
19 changes: 17 additions & 2 deletions src/android/com/adobe/phonegap/fetch/FetchPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.json.JSONObject;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class FetchPlugin extends CordovaPlugin {

Expand All @@ -28,6 +29,8 @@ public class FetchPlugin extends CordovaPlugin {
private final OkHttpClient mClient = new OkHttpClient();
public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");

private static final long DEFAULT_TIMEOUT = 10;

@Override
public boolean execute(final String action, final JSONArray data, final CallbackContext callbackContext) {

Expand Down Expand Up @@ -143,12 +146,24 @@ public void onResponse(Response response) throws IOException {
callbackContext.error(e.getMessage());
}

} else {
}
else if (action.equals("setTimeout")) {
this.setTimeout(data.optLong(0, DEFAULT_TIMEOUT));
}
else {
Log.e(LOG_TAG, "Invalid action : " + action);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
return false;
}

return true;
}
}

private void setTimeout(long seconds) {
Log.v(LOG_TAG, "setTimeout: " + seconds);

mClient.setConnectTimeout(seconds, TimeUnit.SECONDS);
mClient.setReadTimeout(seconds, TimeUnit.SECONDS);
mClient.setWriteTimeout(seconds, TimeUnit.SECONDS);
}
}
10 changes: 9 additions & 1 deletion www/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
function Headers(headers) {
this.map = {}

if (headers instanceof Headers) {
if (headers instanceof Headers || headers instanceof window.Headers) {
headers.forEach(function(value, name) {
this.append(name, value)
}, this)
Expand Down Expand Up @@ -348,6 +348,14 @@
})
}

/**
* Set timeout of the underlying http request
* @param timeout in seconds
*/
cordovaFetch.fetch.setTimeout = function(timeout) {
exec(null, null, "FetchPlugin", "setTimeout", [timeout]);
}

cordovaFetch.fetch.polyfill = true

module.exports = cordovaFetch.fetch;
Expand Down