Skip to content

Commit

Permalink
oops, we do need undici
Browse files Browse the repository at this point in the history
  • Loading branch information
max-stytch committed Oct 4, 2023
1 parent 0f14c81 commit 5d4d210
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 682 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,19 @@ Learn more about errors in the [docs](https://stytch.com/docs/api/errors).

## Customizing the HTTPS Agent

The Stytch client can be customized to use your own HTTPS agent.
The Stytch client uses [undici](https://github.com/nodejs/undici), the Node fetch implementation. You can pass a custom undici `Dispatcher` to the client for use in requests.
For example, you can enable HTTPS Keep-Alive to avoid the cost of establishing a new connection with the Stytch servers on every request.

```javascript
const agent = new https.Agent({
keepAlive: true,
const dispatcher = new undici.Agent({
keepAliveTimeout: 6e6, // 10 minutes in MS
keepAliveMaxTimeout: 6e6, // 10 minutes in MS
});

const client = new stytch.Client({
project_id: "project-live-c60c0abe-c25a-4472-a9ed-320c6667d317",
secret: "secret-live-80JASucyk7z_G8Z-7dVwZVGXL5NT_qGAQ2I=",
agent,
dispatcher,
});
```

Expand Down
2 changes: 1 addition & 1 deletion dist/shared/client.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
globals: {
"ts-jest": {
// Setting this greatly speeds up test execution time
// https://stackoverflow.com/questions/45087018/jest-simple-tests-are-slow
isolatedModules: true,
},
transform: {
".*\\.ts": [
"ts-jest",
{
// Setting this greatly speeds up test execution time
// https://stackoverflow.com/questions/45087018/jest-simple-tests-are-slow
isolatedModules: true,
},
],
},
};
6 changes: 3 additions & 3 deletions lib/shared/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as http from "http";
import type { Dispatcher } from "undici";
import * as envs from "./envs";
import { version } from "../../package.json";
import { fetchConfig } from ".";
Expand All @@ -11,7 +11,7 @@ export interface ClientConfig {
secret: string;
env?: string;
timeout?: number;
agent?: http.Agent;
dispatcher?: Dispatcher;
}

export class BaseClient {
Expand Down Expand Up @@ -56,7 +56,7 @@ export class BaseClient {
baseURL: config.env,
headers,
timeout: config.timeout || DEFAULT_TIMEOUT,
agent: config.agent,
dispatcher: config.dispatcher,
};

// Get a baseURL that ends with a slash to make building route URLs easier.
Expand Down
4 changes: 2 additions & 2 deletions lib/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as http from "http";
import type { Dispatcher } from "undici";
import { RequestError, StytchError, StytchErrorJSON } from "./errors";

export interface fetchConfig {
baseURL: string;
headers: Record<string, string>;
timeout: number;
agent?: http.Agent;
dispatcher?: Dispatcher;
}

export type requestConfig = {
Expand Down
Loading

0 comments on commit 5d4d210

Please sign in to comment.