Skip to content

Commit b341712

Browse files
committed
improve error handling, fixed failing test cases, improve test coverage
1 parent eb71d24 commit b341712

12 files changed

+966
-225
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ oauth-jsclient.iml
1717
package-lock.json
1818
yarn.lock
1919
src/logs/*
20+
.qodo

README.md

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,197 @@
99
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=shield)](https://github.com/prettier/prettier)
1010
[![Known Vulnerabilities](https://snyk.io/test/github/intuit/oauth-jsclient/badge.svg)](https://snyk.io/test/github/intuit/oauth-jsclient)
1111

12+
# OAuth Client for Intuit
13+
14+
A Node.js client for Intuit's OAuth 2.0 implementation.
15+
16+
## Features
17+
18+
- OAuth 2.0 authentication flow
19+
- Token management and refresh
20+
- API request handling
21+
- Error handling with custom error types
22+
- Automatic retry for transient errors
23+
- Structured logging
24+
- Response validation
25+
26+
## Installation
27+
28+
```bash
29+
npm install oauth-jsclient
30+
```
31+
32+
## Usage
33+
34+
```javascript
35+
const OAuthClient = require('oauth-jsclient');
36+
37+
const oauthClient = new OAuthClient({
38+
clientId: 'your_client_id',
39+
clientSecret: 'your_client_secret',
40+
environment: 'sandbox', // or 'production'
41+
redirectUri: 'http://localhost:8000/callback',
42+
logging: true // Enable logging
43+
});
44+
```
45+
46+
## Error Handling
47+
48+
The client provides several custom error types for better error handling:
49+
50+
- `OAuthError`: Base error class for all OAuth related errors
51+
- `NetworkError`: For network related errors
52+
- `ValidationError`: For validation related errors
53+
- `TokenError`: For token related errors
54+
55+
Example error handling:
56+
57+
```javascript
58+
try {
59+
await oauthClient.makeApiCall({ url: 'https://api.example.com' });
60+
} catch (error) {
61+
if (error instanceof TokenError) {
62+
// Handle token errors
63+
console.error('Token error:', error.code, error.description);
64+
} else if (error instanceof NetworkError) {
65+
// Handle network errors
66+
console.error('Network error:', error.message);
67+
} else if (error instanceof ValidationError) {
68+
// Handle validation errors
69+
console.error('Validation error:', error.message);
70+
} else {
71+
// Handle other errors
72+
console.error('Unexpected error:', error);
73+
}
74+
}
75+
```
76+
77+
## Retry Logic
78+
79+
The client includes automatic retry logic for transient errors:
80+
81+
- Maximum 3 retries
82+
- Exponential backoff (1s, 2s, 4s)
83+
- Retries on specific status codes (408, 429, 500, 502, 503, 504)
84+
- Retries on network errors (ECONNRESET, ETIMEDOUT, ECONNREFUSED)
85+
86+
You can configure retry behavior:
87+
88+
```javascript
89+
OAuthClient.retryConfig = {
90+
maxRetries: 3,
91+
retryDelay: 1000,
92+
retryableStatusCodes: [408, 429, 500, 502, 503, 504],
93+
retryableErrors: ['ECONNRESET', 'ETIMEDOUT', 'ECONNREFUSED']
94+
};
95+
```
96+
97+
## Logging
98+
99+
The client provides structured logging when enabled:
100+
101+
```javascript
102+
const oauthClient = new OAuthClient({
103+
// ... other config
104+
logging: true
105+
});
106+
```
107+
108+
Log entries include:
109+
- Timestamp
110+
- Log level
111+
- Message
112+
- Request context (URL, method, headers)
113+
- Error details (for error logs)
114+
- Environment information
115+
- Client ID
116+
117+
Example log entry:
118+
```json
119+
{
120+
"timestamp": "2024-03-14T12:00:00.000Z",
121+
"level": "error",
122+
"message": "API call failed",
123+
"data": {
124+
"error": {
125+
"name": "TokenError",
126+
"code": "UNAUTHORIZED",
127+
"message": "Invalid or expired access token",
128+
"stack": "...",
129+
"intuit_tid": "1234-1234-1234-123"
130+
}
131+
},
132+
"environment": "sandbox",
133+
"clientId": "your_client_id",
134+
"request": {
135+
"url": "https://api.example.com",
136+
"method": "GET",
137+
"headers": {
138+
"Authorization": "Bearer ...",
139+
"Accept": "application/json"
140+
}
141+
}
142+
}
143+
```
144+
145+
## Response Validation
146+
147+
The client validates responses and throws appropriate errors for common scenarios:
148+
149+
- 401 Unauthorized: Invalid or expired access token
150+
- 403 Forbidden: Insufficient permissions
151+
- 429 Too Many Requests: Rate limit exceeded
152+
- Missing or invalid response data
153+
- Invalid content types
154+
155+
## API Reference
156+
157+
### OAuthClient
158+
159+
#### constructor(config)
160+
Creates a new OAuthClient instance.
161+
162+
```javascript
163+
const oauthClient = new OAuthClient({
164+
clientId: 'your_client_id',
165+
clientSecret: 'your_client_secret',
166+
environment: 'sandbox',
167+
redirectUri: 'http://localhost:8000/callback',
168+
logging: true
169+
});
170+
```
171+
172+
#### makeApiCall(params)
173+
Makes an API call with automatic retry and error handling.
174+
175+
```javascript
176+
const response = await oauthClient.makeApiCall({
177+
url: 'https://api.example.com',
178+
method: 'GET',
179+
headers: {
180+
'Custom-Header': 'value'
181+
},
182+
body: {
183+
key: 'value'
184+
}
185+
});
186+
```
187+
188+
#### validateResponse(response)
189+
Validates an API response and throws appropriate errors.
190+
191+
```javascript
192+
try {
193+
oauthClient.validateResponse(response);
194+
} catch (error) {
195+
// Handle validation errors
196+
}
197+
```
198+
199+
## License
200+
201+
Apache License 2.0
202+
12203
# Intuit OAuth2.0 NodeJS Library
13204

14205
The OAuth2 Nodejs Client library is meant to work with Intuit's

0 commit comments

Comments
 (0)