Skip to content

Commit

Permalink
Merge pull request #20 from phillipj/add-agent-support
Browse files Browse the repository at this point in the history
Allow Node.js HTTP/HTTPS agent to be provided
  • Loading branch information
meafmira authored Apr 15, 2019
2 parents 59ee73f + 57f9d2f commit 8282b4b
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 3 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,20 @@ let headersDict =
let headers = Axios.Headers.fromDict(headersDict);
Axios.getc("https://example.com", Axios.makeConfig(~headers, ()));
```

### Node.js HTTP/HTTPS Agent

Providing custom Node.js [`HTTP Agent`](https://nodejs.org/api/http.html#http_class_http_agent)
allows for configuring connection persistence and reuse. For secure connections,
[`HTTPS Agent`](https://nodejs.org/api/https.html#https_class_https_agent) allows security related
configuration to be provided.

```reason
let httpsAgent =
Axios.Agent.Https.(config(~rejectUnauthorized=false, ()) |> create);
Axios.getc(
"https://insecure-example.com",
Axios.makeConfig(~httpsAgent, ()),
);
```
11 changes: 10 additions & 1 deletion examples/request_examples.re
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ let headersDict =
}
);
let headers = Axios.Headers.fromDict(headersDict);
Axios.getc("https://example.com", Axios.makeConfig(~headers, ()));
Axios.getc("https://example.com", Axios.makeConfig(~headers, ()));

/* Node.js Agent */
let httpsAgent =
Axios.Agent.Https.(config(~rejectUnauthorized=false, ()) |> create);

Axios.getc(
"https://insecure-example.com",
Axios.makeConfig(~httpsAgent, ()),
);
7 changes: 6 additions & 1 deletion src/axios.re
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ external makeConfig:
~maxRedirects: int=?,
~socketPath: string=?,
~proxy: proxy=?,
~httpAgent: Axios_agent.Http.t=?,
~httpsAgent: Axios_agent.Https.t=?,
unit
) =>
config =
Expand Down Expand Up @@ -186,6 +188,8 @@ external makeConfigWithUrl:
~maxRedirects: int=?,
~socketPath: string=?,
~proxy: proxy=?,
~httpAgent: Axios_agent.Http.t=?,
~httpsAgent: Axios_agent.Https.t=?,
unit
) =>
configWithUrl =
Expand Down Expand Up @@ -453,4 +457,5 @@ external patchDatac:
(string, Js.t('a), config) => Js.Promise.t(response('b, 'c)) =
"patch";

module Instance = Axios_instance;
module Instance = Axios_instance;
module Agent = Axios_agent;
7 changes: 6 additions & 1 deletion src/axios.rei
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ external makeConfig:
~maxRedirects: int=?,
~socketPath: string=?,
~proxy: proxy=?,
~httpAgent: Axios_agent.Http.t=?,
~httpsAgent: Axios_agent.Https.t=?,
unit
) =>
config =
Expand Down Expand Up @@ -139,6 +141,8 @@ external makeConfigWithUrl:
~maxRedirects: int=?,
~socketPath: string=?,
~proxy: proxy=?,
~httpAgent: Axios_agent.Http.t=?,
~httpsAgent: Axios_agent.Https.t=?,
unit
) =>
configWithUrl =
Expand Down Expand Up @@ -406,4 +410,5 @@ external patchDatac:
(string, Js.t('a), config) => Js.Promise.t(response('b, 'c)) =
"patch";

module Instance = Axios_instance;
module Instance = Axios_instance;
module Agent = Axios_agent;
75 changes: 75 additions & 0 deletions src/axios_agent.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// https://nodejs.org/api/http.html#http_class_http_agent
module Http = {
type t;

[@bs.deriving abstract]
type config = {
[@bs.optional]
keepAlive: bool,
[@bs.optional]
keepAliveMsecs: int,
[@bs.optional]
maxSockets: int,
[@bs.optional]
maxFreeSockets: int,
[@bs.optional]
timeout: int,
};

[@bs.module "http"] [@bs.new] external create: config => t = "Agent";
};

// https://nodejs.org/api/https.html#https_class_https_agent
module Https = {
type t;

[@bs.deriving abstract]
type config = {
[@bs.optional]
ca: string,
[@bs.optional]
cert: string,
[@bs.optional]
ciphers: string,
[@bs.optional]
clientCertEngine: string,
[@bs.optional]
crl: string,
[@bs.optional]
dhparam: string,
[@bs.optional]
ecdhCurve: string,
[@bs.optional]
honorCipherOrder: bool,
[@bs.optional]
key: string,
[@bs.optional]
keepAlive: bool,
[@bs.optional]
keepAliveMsecs: int,
[@bs.optional]
maxSockets: int,
[@bs.optional]
maxFreeSockets: int,
[@bs.optional]
maxCachedSessions: int,
[@bs.optional]
passphrase: string,
[@bs.optional]
pfx: string,
[@bs.optional]
rejectUnauthorized: bool,
[@bs.optional]
secureOptions: int,
[@bs.optional]
secureProtocol: string,
[@bs.optional]
servername: string,
[@bs.optional]
sessionIdContext: string,
[@bs.optional]
timeout: int,
};

[@bs.module "https"] [@bs.new] external create: config => t = "Agent";
};

0 comments on commit 8282b4b

Please sign in to comment.