Skip to content

Commit

Permalink
Merge pull request #26 from Mermaid-Chart/sidv/addRequestTimeout
Browse files Browse the repository at this point in the history
feat: Add requestTimeout to avoid long running queries
  • Loading branch information
sidharthv96 authored Aug 23, 2024
2 parents cee4d5b + 87e0131 commit de2b733
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
7 changes: 6 additions & 1 deletion packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [UNRELEASED]

### Changes

- Set a 30 second default timeout for all requests
- `MermaidChart#resetAccessToken()` no longer returns a `Promise`.

### Added

- Added `requestTimeout` option to configure timeout.

## [0.2.0] - 2024-04-11

### Added
Expand Down
21 changes: 13 additions & 8 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { v4 as uuid } from 'uuid';
import { OAuth2Client, generateCodeVerifier } from '@badgateway/oauth2-client';
import defaultAxios from 'axios';
import type { AxiosInstance, AxiosResponse } from 'axios';
import { RequiredParameterMissingError, OAuthError } from './errors.js';
import { URLS } from './urls.js';
import defaultAxios from 'axios';
import { v4 as uuid } from 'uuid';
import { OAuthError, RequiredParameterMissingError } from './errors.js';
import type {
AuthState,
AuthorizationData,
Document,
InitParams,
AuthorizationData,
MCUser,
MCProject,
MCDocument,
MCProject,
MCUser,
} from './types.js';
import { URLS } from './urls.js';

const defaultBaseURL = 'https://www.mermaidchart.com'; // "http://127.0.0.1:5174"
const authorizationURLTimeout = 60_000;
Expand All @@ -25,13 +25,17 @@ export class MermaidChart {
private pendingStates: Record<string, AuthState> = {};
private redirectURI!: string;
private accessToken?: string;
private requestTimeout = 30_000;

constructor({ clientID, baseURL, redirectURI }: InitParams) {
constructor({ clientID, baseURL, redirectURI, requestTimeout }: InitParams) {
this.clientID = clientID;
this.setBaseURL(baseURL || defaultBaseURL);
if (redirectURI) {
this.setRedirectURI(redirectURI);
}
if (requestTimeout) {
this.requestTimeout = requestTimeout;
}
}

public setRedirectURI(redirectURI: string) {
Expand All @@ -52,6 +56,7 @@ export class MermaidChart {
});
this.axios = defaultAxios.create({
baseURL: this.#baseURL,
timeout: this.requestTimeout,
});

this.axios.interceptors.response.use((res: AxiosResponse) => {
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface InitParams {
clientID: string;
redirectURI?: string;
baseURL?: string;
requestTimeout?: number;
}

export interface OAuthAuthorizationParams {
Expand Down

0 comments on commit de2b733

Please sign in to comment.