Skip to content

Commit c56103c

Browse files
add ability to pass custom fetchFn, refactor code
1 parent 9fa7db8 commit c56103c

12 files changed

Lines changed: 74 additions & 74 deletions

src/orkes/ConductorClientWithAuth.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import type { OpenAPIConfig } from "../common";
1+
import type { BaseHttpRequest, OpenAPIConfig } from "../common";
22
import { ConductorClient } from "../common";
3-
import { HttpRequestConstructor } from "./types";
43

54
export class ConductorClientWithAuth extends ConductorClient {
65
private intervalId?: NodeJS.Timeout;
76

87
constructor(
98
config: Partial<OpenAPIConfig>,
10-
HttpRequest?: HttpRequestConstructor
9+
HttpRequest?: new (config: OpenAPIConfig) => BaseHttpRequest
1110
) {
1211
super(config, HttpRequest);
1312
}
1413

15-
private setToken(token: string) {
14+
private setToken(token: string | undefined) {
1615
this.request.config.TOKEN = token;
1716
}
1817

@@ -40,13 +39,9 @@ export class ConductorClientWithAuth extends ConductorClient {
4039
}
4140

4241
public deAuthorize(): void {
43-
this.clearTokenInterval();
44-
this.request.config.TOKEN = undefined;
45-
}
46-
47-
public clearTokenInterval(): void {
48-
if (this.intervalId != null) {
42+
if (this.intervalId) {
4943
clearInterval(this.intervalId);
5044
}
45+
this.setToken(undefined);
5146
}
5247
}

src/orkes/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const REFRESH_TOKEN_IN_MILLISECONDS = 30 * 60 * 1000;
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
import { ConductorClientWithAuth } from "./ConductorClientWithAuth";
2-
import { OrkesHttpRequest } from "./request/OrkesHttpRequest";
3-
import { HttpRequestConstructor, OrkesApiConfig } from "./types";
4-
5-
const REFRESH_TOKEN_IN_MILLISECONDS = 30 * 60 * 1000;
2+
import { resolveFetchFn, resolveOrkesConfig } from "./helpers";
3+
import { createOrkesHttpRequest } from "./request/createOrkesHttpRequest";
4+
import type { FetchFn, OrkesApiConfig } from "./types";
5+
import { REFRESH_TOKEN_IN_MILLISECONDS } from "./constants";
66

77
/**
88
* Takes a config with keyId and keySecret returns a promise with an instance of ConductorClient
99
*
1010
* @param config (optional) OrkesApiConfig with keyId and keySecret
11-
* @param HttpRequest (optional) Custom request class, could be used to pass in a custom fetch function
11+
* @param customFetch (optional) custom fetch function
1212
* @returns
1313
*/
1414
export const orkesConductorClient = async (
1515
config?: Partial<OrkesApiConfig>,
16-
HttpRequest: HttpRequestConstructor = OrkesHttpRequest
16+
customFetch?: FetchFn
1717
) => {
18-
const serverUrl = process.env.CONDUCTOR_SERVER_URL || config?.serverUrl;
18+
const { serverUrl, keyId, keySecret } = resolveOrkesConfig(config);
1919

2020
if (!serverUrl) throw new Error("Conductor server URL is not set");
2121

22-
const keyId = process.env.CONDUCTOR_AUTH_KEY || config?.keyId;
23-
const keySecret = process.env.CONDUCTOR_AUTH_SECRET || config?.keySecret;
24-
2522
const conductorClientWithAuth = new ConductorClientWithAuth(
2623
{ ...config, BASE: serverUrl },
27-
HttpRequest
24+
createOrkesHttpRequest(resolveFetchFn(customFetch))
2825
);
2926

3027
if (keyId && keySecret) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {
2+
fetch as undiciFetch,
3+
Agent as UndiciAgent,
4+
RequestInfo as UndiciRequestInfo,
5+
RequestInit as UndiciRequestInit,
6+
} from "undici";
7+
8+
export const createUndiciHttp2Fetch = () => {
9+
const undiciHttp2Agent = new UndiciAgent({ allowH2: true });
10+
11+
return async (input: UndiciRequestInfo, init?: UndiciRequestInit) => {
12+
return undiciFetch(input, {
13+
...init,
14+
dispatcher: undiciHttp2Agent,
15+
});
16+
};
17+
};

src/orkes/helpers/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./resolveFetchFn";
2+
export * from "./resolveOrkesConfig";
3+
export * from "./createUndiciHttp2Fetch";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { FetchFn } from "../types";
2+
import { createUndiciHttp2Fetch } from "./createUndiciHttp2Fetch";
3+
4+
export const resolveFetchFn = (customFetch?: FetchFn) =>
5+
customFetch ||
6+
(process?.release?.name === "node"
7+
? (createUndiciHttp2Fetch() as FetchFn)
8+
: fetch);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { OrkesApiConfig } from "../types";
2+
3+
export const resolveOrkesConfig = (config?: Partial<OrkesApiConfig>) => {
4+
return {
5+
serverUrl: process.env.CONDUCTOR_SERVER_URL || config?.serverUrl,
6+
keyId: process.env.CONDUCTOR_AUTH_KEY || config?.keyId,
7+
keySecret: process.env.CONDUCTOR_AUTH_SECRET || config?.keySecret,
8+
};
9+
};

src/orkes/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from "./OrkesConductorClient";
1+
export * from "./createOrkesConductorClient";
22
export * from "./ConductorClientWithAuth";
33
export * from "./request/request";
44

src/orkes/request/OrkesHttpRequest.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {
2+
ApiRequestOptions,
3+
BaseHttpRequest,
4+
CancelablePromise,
5+
OpenAPIConfig,
6+
} from "../../common";
7+
import { FetchFn } from "../types";
8+
import { request as orkesRequest } from "./request";
9+
10+
export function createOrkesHttpRequest(fetchFn?: FetchFn) {
11+
return class OrkesHttpRequest extends BaseHttpRequest {
12+
constructor(config: OpenAPIConfig) {
13+
super(config);
14+
}
15+
16+
public request<T>(options: ApiRequestOptions): CancelablePromise<T> {
17+
return orkesRequest(this.config, options, fetchFn);
18+
}
19+
};
20+
}

0 commit comments

Comments
 (0)