@workos-inc/node 7.69.1 → 7.69.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,4 +7,5 @@ export interface WorkOSOptions {
7
7
  appInfo?: AppInfo;
8
8
  fetchFn?: typeof fetch;
9
9
  clientId?: string;
10
+ timeout?: number;
10
11
  }
@@ -1,10 +1,13 @@
1
1
  import { HttpClientInterface, HttpClientResponseInterface, RequestOptions, ResponseHeaders } from '../interfaces/http-client.interface';
2
2
  import { HttpClient, HttpClientResponse } from './http-client';
3
+ interface FetchHttpClientOptions extends RequestInit {
4
+ timeout?: number;
5
+ }
3
6
  export declare class FetchHttpClient extends HttpClient implements HttpClientInterface {
4
7
  readonly baseURL: string;
5
- readonly options?: RequestInit | undefined;
8
+ readonly options?: FetchHttpClientOptions | undefined;
6
9
  private readonly _fetchFn;
7
- constructor(baseURL: string, options?: RequestInit | undefined, fetchFn?: typeof fetch);
10
+ constructor(baseURL: string, options?: FetchHttpClientOptions | undefined, fetchFn?: typeof fetch);
8
11
  /** @override */
9
12
  getClientName(): string;
10
13
  get(path: string, options: RequestOptions): Promise<HttpClientResponseInterface>;
@@ -22,3 +25,4 @@ export declare class FetchHttpClientResponse extends HttpClientResponse implemen
22
25
  toJSON(): Promise<any> | null;
23
26
  static _transformHeadersToObject(headers: Headers): ResponseHeaders;
24
27
  }
28
+ export {};
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.FetchHttpClientResponse = exports.FetchHttpClient = void 0;
13
13
  const http_client_1 = require("./http-client");
14
14
  const parse_error_1 = require("../exceptions/parse-error");
15
+ const DEFAULT_FETCH_TIMEOUT = 60000; // 60 seconds
15
16
  class FetchHttpClient extends http_client_1.HttpClient {
16
17
  constructor(baseURL, options, fetchFn) {
17
18
  super(baseURL, options);
@@ -75,47 +76,82 @@ class FetchHttpClient extends http_client_1.HttpClient {
75
76
  });
76
77
  }
77
78
  fetchRequest(url, method, body, headers) {
78
- var _a, _b, _c;
79
+ var _a, _b, _c, _d, _e;
79
80
  return __awaiter(this, void 0, void 0, function* () {
80
81
  // For methods which expect payloads, we should always pass a body value
81
82
  // even when it is empty. Without this, some JS runtimes (eg. Deno) will
82
83
  // inject a second Content-Length header.
83
84
  const methodHasPayload = method === 'POST' || method === 'PUT' || method === 'PATCH';
84
85
  const requestBody = body || (methodHasPayload ? '' : undefined);
85
- const { 'User-Agent': userAgent } = (_a = this.options) === null || _a === void 0 ? void 0 : _a.headers;
86
- const res = yield this._fetchFn(url, {
87
- method,
88
- headers: Object.assign(Object.assign(Object.assign({ Accept: 'application/json, text/plain, */*', 'Content-Type': 'application/json' }, (_b = this.options) === null || _b === void 0 ? void 0 : _b.headers), headers), { 'User-Agent': this.addClientToUserAgent(userAgent.toString()) }),
89
- body: requestBody,
90
- });
91
- if (!res.ok) {
92
- const requestID = (_c = res.headers.get('X-Request-ID')) !== null && _c !== void 0 ? _c : '';
93
- const rawBody = yield res.text();
94
- let responseJson;
95
- try {
96
- responseJson = JSON.parse(rawBody);
86
+ const { 'User-Agent': userAgent } = (((_a = this.options) === null || _a === void 0 ? void 0 : _a.headers) ||
87
+ {});
88
+ // Create AbortController for timeout if configured
89
+ let abortController;
90
+ let timeoutId;
91
+ // Access timeout from the options with default of 60 seconds
92
+ const timeout = (_c = (_b = this.options) === null || _b === void 0 ? void 0 : _b.timeout) !== null && _c !== void 0 ? _c : DEFAULT_FETCH_TIMEOUT; // Default 60 seconds
93
+ abortController = new AbortController();
94
+ timeoutId = setTimeout(() => {
95
+ abortController === null || abortController === void 0 ? void 0 : abortController.abort();
96
+ }, timeout);
97
+ try {
98
+ const res = yield this._fetchFn(url, {
99
+ method,
100
+ headers: Object.assign(Object.assign(Object.assign({ Accept: 'application/json, text/plain, */*', 'Content-Type': 'application/json' }, (_d = this.options) === null || _d === void 0 ? void 0 : _d.headers), headers), { 'User-Agent': this.addClientToUserAgent((userAgent || 'workos-node').toString()) }),
101
+ body: requestBody,
102
+ signal: abortController === null || abortController === void 0 ? void 0 : abortController.signal,
103
+ });
104
+ // Clear timeout if request completed successfully
105
+ if (timeoutId) {
106
+ clearTimeout(timeoutId);
97
107
  }
98
- catch (error) {
99
- if (error instanceof SyntaxError) {
100
- throw new parse_error_1.ParseError({
101
- message: error.message,
102
- rawBody,
103
- requestID,
104
- rawStatus: res.status,
105
- });
108
+ if (!res.ok) {
109
+ const requestID = (_e = res.headers.get('X-Request-ID')) !== null && _e !== void 0 ? _e : '';
110
+ const rawBody = yield res.text();
111
+ let responseJson;
112
+ try {
113
+ responseJson = JSON.parse(rawBody);
114
+ }
115
+ catch (error) {
116
+ if (error instanceof SyntaxError) {
117
+ throw new parse_error_1.ParseError({
118
+ message: error.message,
119
+ rawBody,
120
+ requestID,
121
+ rawStatus: res.status,
122
+ });
123
+ }
124
+ throw error;
106
125
  }
107
- throw error;
126
+ throw new http_client_1.HttpClientError({
127
+ message: res.statusText,
128
+ response: {
129
+ status: res.status,
130
+ headers: res.headers,
131
+ data: responseJson,
132
+ },
133
+ });
108
134
  }
109
- throw new http_client_1.HttpClientError({
110
- message: res.statusText,
111
- response: {
112
- status: res.status,
113
- headers: res.headers,
114
- data: responseJson,
115
- },
116
- });
135
+ return new FetchHttpClientResponse(res);
136
+ }
137
+ catch (error) {
138
+ // Clear timeout if request failed
139
+ if (timeoutId) {
140
+ clearTimeout(timeoutId);
141
+ }
142
+ // Handle timeout errors
143
+ if (error instanceof Error && error.name === 'AbortError') {
144
+ throw new http_client_1.HttpClientError({
145
+ message: `Request timeout after ${timeout}ms`,
146
+ response: {
147
+ status: 408,
148
+ headers: {},
149
+ data: { error: 'Request timeout' },
150
+ },
151
+ });
152
+ }
153
+ throw error;
117
154
  }
118
- return new FetchHttpClientResponse(res);
119
155
  });
120
156
  }
121
157
  fetchRequestWithRetry(url, method, body, headers) {
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const jest_fetch_mock_1 = __importDefault(require("jest-fetch-mock"));
16
16
  const test_utils_1 = require("../../common/utils/test-utils");
17
17
  const fetch_client_1 = require("./fetch-client");
18
+ const http_client_1 = require("./http-client");
18
19
  const parse_error_1 = require("../exceptions/parse-error");
19
20
  const fetchClient = new fetch_client_1.FetchHttpClient('https://test.workos.com', {
20
21
  headers: {
@@ -205,3 +206,73 @@ describe('Fetch client', () => {
205
206
  }));
206
207
  });
207
208
  });
209
+ describe('FetchHttpClient with timeout', () => {
210
+ let client;
211
+ let mockFetch;
212
+ beforeEach(() => {
213
+ mockFetch = jest.fn();
214
+ client = new fetch_client_1.FetchHttpClient('https://api.example.com', { timeout: 100 }, mockFetch);
215
+ });
216
+ it('should timeout requests that take too long', () => __awaiter(void 0, void 0, void 0, function* () {
217
+ // Mock a fetch that respects AbortController
218
+ mockFetch.mockImplementation((_, options) => {
219
+ return new Promise((_, reject) => {
220
+ if (options.signal) {
221
+ options.signal.addEventListener('abort', () => {
222
+ const error = new Error('AbortError');
223
+ error.name = 'AbortError';
224
+ reject(error);
225
+ });
226
+ }
227
+ // Never resolve - let the timeout trigger
228
+ });
229
+ });
230
+ yield expect(client.post('/test', { data: 'test' }, {})).rejects.toThrow(http_client_1.HttpClientError);
231
+ // Reset the mock for the second test
232
+ mockFetch.mockClear();
233
+ mockFetch.mockImplementation((_, options) => {
234
+ return new Promise((_, reject) => {
235
+ if (options.signal) {
236
+ options.signal.addEventListener('abort', () => {
237
+ const error = new Error('AbortError');
238
+ error.name = 'AbortError';
239
+ reject(error);
240
+ });
241
+ }
242
+ // Never resolve - let the timeout trigger
243
+ });
244
+ });
245
+ yield expect(client.post('/test', { data: 'test' }, {})).rejects.toMatchObject({
246
+ message: 'Request timeout after 100ms',
247
+ response: {
248
+ status: 408,
249
+ data: { error: 'Request timeout' },
250
+ },
251
+ });
252
+ }));
253
+ it('should not timeout requests that complete quickly', () => __awaiter(void 0, void 0, void 0, function* () {
254
+ const mockResponse = {
255
+ ok: true,
256
+ status: 200,
257
+ headers: new Map(),
258
+ json: () => Promise.resolve({ success: true }),
259
+ text: () => Promise.resolve('{"success": true}'),
260
+ };
261
+ mockFetch.mockResolvedValue(mockResponse);
262
+ const result = yield client.post('/test', { data: 'test' }, {});
263
+ expect(result).toBeDefined();
264
+ }));
265
+ it('should work without timeout configured', () => __awaiter(void 0, void 0, void 0, function* () {
266
+ const clientWithoutTimeout = new fetch_client_1.FetchHttpClient('https://api.example.com', {}, mockFetch);
267
+ const mockResponse = {
268
+ ok: true,
269
+ status: 200,
270
+ headers: new Map(),
271
+ json: () => Promise.resolve({ success: true }),
272
+ text: () => Promise.resolve('{"success": true}'),
273
+ };
274
+ mockFetch.mockResolvedValue(mockResponse);
275
+ const result = yield clientWithoutTimeout.post('/test', { data: 'test' }, {});
276
+ expect(result).toBeDefined();
277
+ }));
278
+ });
package/lib/index.js CHANGED
@@ -43,7 +43,7 @@ class WorkOSNode extends workos_1.WorkOS {
43
43
  /** @override */
44
44
  createHttpClient(options, userAgent) {
45
45
  var _a;
46
- const opts = Object.assign(Object.assign({}, options.config), { headers: Object.assign(Object.assign({}, (_a = options.config) === null || _a === void 0 ? void 0 : _a.headers), { Authorization: `Bearer ${this.key}`, 'User-Agent': userAgent }) });
46
+ const opts = Object.assign(Object.assign({}, options.config), { timeout: options.timeout, headers: Object.assign(Object.assign({}, (_a = options.config) === null || _a === void 0 ? void 0 : _a.headers), { Authorization: `Bearer ${this.key}`, 'User-Agent': userAgent }) });
47
47
  if (typeof fetch !== 'undefined' ||
48
48
  typeof options.fetchFn !== 'undefined') {
49
49
  return new fetch_client_1.FetchHttpClient(this.baseURL, opts, options.fetchFn);
@@ -2,12 +2,12 @@ export type WidgetScope = 'widgets:users-table:manage' | 'widgets:sso:manage' |
2
2
  export interface GetTokenOptions {
3
3
  organizationId: string;
4
4
  userId?: string;
5
- scopes?: [WidgetScope];
5
+ scopes?: WidgetScope[];
6
6
  }
7
7
  export interface SerializedGetTokenOptions {
8
8
  organization_id: string;
9
9
  user_id?: string;
10
- scopes?: [WidgetScope];
10
+ scopes?: WidgetScope[];
11
11
  }
12
12
  export declare const serializeGetTokenOptions: (options: GetTokenOptions) => SerializedGetTokenOptions;
13
13
  export interface GetTokenResponse {
package/lib/workos.js CHANGED
@@ -32,7 +32,7 @@ const actions_1 = require("./actions/actions");
32
32
  const vault_1 = require("./vault/vault");
33
33
  const conflict_exception_1 = require("./common/exceptions/conflict.exception");
34
34
  const parse_error_1 = require("./common/exceptions/parse-error");
35
- const VERSION = '7.69.1';
35
+ const VERSION = '7.69.2';
36
36
  const DEFAULT_HOSTNAME = 'api.workos.com';
37
37
  const HEADER_AUTHORIZATION = 'Authorization';
38
38
  const HEADER_IDEMPOTENCY_KEY = 'Idempotency-Key';
@@ -99,7 +99,7 @@ class WorkOS {
99
99
  }
100
100
  createHttpClient(options, userAgent) {
101
101
  var _a;
102
- return new fetch_client_1.FetchHttpClient(this.baseURL, Object.assign(Object.assign({}, options.config), { headers: Object.assign(Object.assign({}, (_a = options.config) === null || _a === void 0 ? void 0 : _a.headers), { Authorization: `Bearer ${this.key}`, 'User-Agent': userAgent }) }));
102
+ return new fetch_client_1.FetchHttpClient(this.baseURL, Object.assign(Object.assign({}, options.config), { timeout: options.timeout, headers: Object.assign(Object.assign({}, (_a = options.config) === null || _a === void 0 ? void 0 : _a.headers), { Authorization: `Bearer ${this.key}`, 'User-Agent': userAgent }) }));
103
103
  }
104
104
  createIronSessionProvider() {
105
105
  throw new Error('IronSessionProvider not implemented. Use WorkOSNode or WorkOSWorker instead.');
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "7.69.1",
2
+ "version": "7.69.2",
3
3
  "name": "@workos-inc/node",
4
4
  "author": "WorkOS",
5
5
  "description": "A Node wrapper for the WorkOS API",