@nvisy/sdk 0.1.0 → 0.3.0
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.
- package/CHANGELOG.md +68 -4
- package/LICENSE.txt +1 -1
- package/README.md +16 -68
- package/dist/auth/index.d.ts +105 -0
- package/dist/auth/index.js +184 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth-CncOz2vx.d.ts +7420 -0
- package/dist/client-DQRisDAK.d.ts +808 -0
- package/dist/config-L7FKq_q5.d.ts +81 -0
- package/dist/datatypes/index.d.ts +9 -0
- package/dist/datatypes/index.js +3 -0
- package/dist/datatypes/index.js.map +1 -0
- package/dist/index.d.ts +140 -2
- package/dist/index.js +1303 -378
- package/dist/index.js.map +1 -1
- package/dist/services/index.d.ts +5 -0
- package/dist/services/index.js +1067 -0
- package/dist/services/index.js.map +1 -0
- package/dist/workspace-BjdbMWsa.d.ts +118 -0
- package/package.json +38 -32
- package/dist/builder-BEUIGfoZ.d.ts +0 -138
- package/dist/builder.d.ts +0 -2
- package/dist/builder.js +0 -424
- package/dist/builder.js.map +0 -1
- package/dist/client.d.ts +0 -2
- package/dist/client.js +0 -424
- package/dist/client.js.map +0 -1
- package/dist/errors.d.ts +0 -124
- package/dist/errors.js +0 -201
- package/dist/errors.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -8,20 +8,82 @@ and this project adheres to
|
|
|
8
8
|
|
|
9
9
|
## [Unreleased]
|
|
10
10
|
|
|
11
|
+
## [0.3.0] - 2026-01-09
|
|
12
|
+
|
|
11
13
|
### Added
|
|
12
14
|
|
|
15
|
+
- Logging middleware (`withLogging: true` in config) for request/response
|
|
16
|
+
logging
|
|
17
|
+
- Page types for all paginated responses: `ActivitiesPage`, `AnnotationsPage`,
|
|
18
|
+
`ApiTokensPage`, `CommentsPage`, `DocumentsPage`, `FilesPage`,
|
|
19
|
+
`IntegrationsPage`, `IntegrationRunsPage`, `InvitesPage`, `MembersPage`,
|
|
20
|
+
`NotificationsPage`, `WebhooksPage`, `WorkspacesPage`
|
|
21
|
+
|
|
13
22
|
### Changed
|
|
14
23
|
|
|
15
|
-
|
|
24
|
+
- Renamed `Client` class to `Nvisy`
|
|
25
|
+
- Renamed service classes from `*Service` (e.g. `WorkspacesService` →
|
|
26
|
+
`Workspaces`)
|
|
27
|
+
- Renamed service handler methods to include entity names (e.g. `list` →
|
|
28
|
+
`listWorkspaces`)
|
|
29
|
+
- Renamed `ClientError` to `NvisyError` and `ApiError` to `NvisyApiError`
|
|
30
|
+
- `NvisyApiError` now extends `NvisyError`
|
|
31
|
+
- **BREAKING**: API path parameters now use camelCase (`workspaceId` instead of
|
|
32
|
+
`workspace_id`)
|
|
33
|
+
- **BREAKING**: Pagination changed from offset-based to cursor-based
|
|
34
|
+
- Query params: `offset`/`limit` → `limit`/`after`
|
|
35
|
+
- Pagination type: `Pagination` → `CursorPagination`
|
|
36
|
+
- **BREAKING**: All list methods now return paginated response objects instead
|
|
37
|
+
of arrays
|
|
38
|
+
- Return type: `Promise<T[]>` → `Promise<TPage>` (e.g., `WorkspacesPage`)
|
|
39
|
+
- Response structure: `{ items: T[], total?: number, nextCursor?: string }`
|
|
40
|
+
- **BREAKING**: Comments service methods simplified
|
|
41
|
+
- `updateComment(fileId, commentId, ...)` → `updateComment(commentId, ...)`
|
|
42
|
+
- `deleteComment(fileId, commentId)` → `deleteComment(commentId)`
|
|
43
|
+
- Renamed query types to match schema (dropped `Query` suffix)
|
|
44
|
+
- Renamed request types (dropped `Request` suffix)
|
|
16
45
|
|
|
17
46
|
### Removed
|
|
18
47
|
|
|
48
|
+
- `ConfigError` and `NetworkError` classes (use `NvisyError` instead)
|
|
49
|
+
- Re-exports of datatypes and services from main entry point (use
|
|
50
|
+
`@nvisy/sdk/datatypes` and `@nvisy/sdk/services`)
|
|
51
|
+
- `DocumentStatus` type (removed from API)
|
|
52
|
+
- `ListIntegrationsQuery` type (integration type filter removed)
|
|
53
|
+
|
|
54
|
+
## [0.2.0] - 2025-06-15
|
|
55
|
+
|
|
56
|
+
### Added
|
|
57
|
+
|
|
58
|
+
- Generated OpenAPI schema types for type-safe API calls
|
|
59
|
+
- `Client.withApiToken()` method to create a new client with a different token
|
|
60
|
+
- Error middleware for automatic API error handling
|
|
61
|
+
- Full API coverage with services for all endpoints
|
|
62
|
+
- Comprehensive JSDoc documentation for all public APIs
|
|
63
|
+
|
|
64
|
+
### Changed
|
|
65
|
+
|
|
66
|
+
- Client now requires `apiToken` in configuration
|
|
67
|
+
- Refactored all services to use `openapi-fetch` for type-safe API calls
|
|
68
|
+
- Improved error handling with `ApiError`
|
|
69
|
+
- Services now throw `ApiError` automatically via middleware
|
|
70
|
+
- Datatypes now re-export schema types with convenient aliases
|
|
71
|
+
- Services are created on-demand via Client getters
|
|
72
|
+
|
|
73
|
+
### Removed
|
|
74
|
+
|
|
75
|
+
- `ClientBuilder` class and `Client.builder()` method
|
|
76
|
+
- `Client.fromEnvironment()` method
|
|
77
|
+
- Environment variable support (`NVISY_API_TOKEN`, `NVISY_BASE_URL`,
|
|
78
|
+
`NVISY_USER_AGENT`)
|
|
79
|
+
- Manual type definitions in datatypes (now use schema re-exports)
|
|
80
|
+
|
|
19
81
|
## [0.1.0] - 2025-10-15
|
|
20
82
|
|
|
21
83
|
### Added
|
|
22
84
|
|
|
23
85
|
- Initial release of the Nvisy SDK
|
|
24
|
-
- `Client` class for interacting with the Nvisy document
|
|
86
|
+
- `Client` class for interacting with the Nvisy document processing API
|
|
25
87
|
- `ClientBuilder` class for fluent configuration building
|
|
26
88
|
- Comprehensive error handling with `ClientError` and other error classes
|
|
27
89
|
- Configuration management with environment variable support
|
|
@@ -45,5 +107,7 @@ and this project adheres to
|
|
|
45
107
|
- Network error handling for timeouts, DNS resolution, and connection issues
|
|
46
108
|
- Configuration validation with detailed error messages
|
|
47
109
|
|
|
48
|
-
[Unreleased]: https://github.com/nvisycom/sdk/compare/v0.
|
|
49
|
-
[0.
|
|
110
|
+
[Unreleased]: https://github.com/nvisycom/sdk-ts/compare/v0.3.0...HEAD
|
|
111
|
+
[0.3.0]: https://github.com/nvisycom/sdk-ts/compare/v0.2.0...v0.3.0
|
|
112
|
+
[0.2.0]: https://github.com/nvisycom/sdk-ts/compare/v0.1.0...v0.2.0
|
|
113
|
+
[0.1.0]: https://github.com/nvisycom/sdk-ts/releases/tag/v0.1.0
|
package/LICENSE.txt
CHANGED
package/README.md
CHANGED
|
@@ -1,19 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Nvisy.com SDK for TypeScript/JavaScript
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@nvisy/sdk)
|
|
4
|
-
[](https://github.com/nvisycom/sdk/actions/workflows/build.yml)
|
|
5
|
-
[](https://nodejs.org/)
|
|
6
|
-
[](https://www.typescriptlang.org/)
|
|
4
|
+
[](https://github.com/nvisycom/sdk-ts/actions/workflows/build.yml)
|
|
7
5
|
|
|
8
|
-
Official
|
|
6
|
+
Official TypeScript SDK for the Nvisy AI-powered document processing platform.
|
|
9
7
|
|
|
10
8
|
## Features
|
|
11
9
|
|
|
12
|
-
- Modern ES2022+ JavaScript
|
|
10
|
+
- Modern ES2022+ JavaScript target
|
|
13
11
|
- Full TypeScript support with strict typing
|
|
14
|
-
-
|
|
15
|
-
- Built-in environment variable support
|
|
16
|
-
- Automatic retry logic with smart error handling
|
|
12
|
+
- Debug logging for development
|
|
17
13
|
- Individual module exports for optimal bundling
|
|
18
14
|
|
|
19
15
|
## Installation
|
|
@@ -24,71 +20,23 @@ npm install @nvisy/sdk
|
|
|
24
20
|
|
|
25
21
|
## Usage
|
|
26
22
|
|
|
27
|
-
### Direct Configuration
|
|
28
|
-
|
|
29
|
-
Create a client by passing configuration options directly to the constructor:
|
|
30
|
-
|
|
31
23
|
```typescript
|
|
32
|
-
import {
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
baseUrl: "https://api.nvisy.com", // Optional
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
headers: { // Optional
|
|
24
|
+
import { Nvisy } from "@nvisy/sdk";
|
|
25
|
+
|
|
26
|
+
const nvisy = new Nvisy({
|
|
27
|
+
apiToken: "your-api-token", // Required
|
|
28
|
+
baseUrl: "https://api.nvisy.com", // Optional
|
|
29
|
+
userAgent: "MyApp/1.0.0", // Optional
|
|
30
|
+
withLogging: true, // Optional
|
|
31
|
+
headers: { // Optional
|
|
40
32
|
"X-Custom-Header": "value",
|
|
41
33
|
},
|
|
42
34
|
});
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
### Builder Pattern
|
|
46
|
-
|
|
47
|
-
Use the fluent builder API for more readable configuration:
|
|
48
35
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const client = Client.builder()
|
|
53
|
-
.withApiKey("your-api-key") // Required: 10+ chars, alphanumeric with _ and -
|
|
54
|
-
.withBaseUrl("https://api.nvisy.com") // Optional: API endpoint (default shown)
|
|
55
|
-
.withTimeout(60000) // Optional: 1000-300000ms (default: 30000)
|
|
56
|
-
.withMaxRetries(5) // Optional: 0-5 attempts (default: 3)
|
|
57
|
-
.withHeader("X-Custom-Header", "value") // Optional: single custom header
|
|
58
|
-
.withHeaders({ "X-Another": "header" }) // Optional: multiple custom headers
|
|
59
|
-
.build();
|
|
36
|
+
const account = await nvisy.account.getAccount();
|
|
37
|
+
const workspaces = await nvisy.workspaces.listWorkspaces();
|
|
60
38
|
```
|
|
61
39
|
|
|
62
|
-
### From Environment Variables
|
|
63
|
-
|
|
64
|
-
Load configuration from environment variables:
|
|
65
|
-
|
|
66
|
-
```typescript
|
|
67
|
-
import { Client, ClientBuilder } from "@nvisy/sdk";
|
|
68
|
-
|
|
69
|
-
// Using builder pattern from environment (allows additional configuration)
|
|
70
|
-
const client = ClientBuilder.fromEnvironment()
|
|
71
|
-
.withTimeout(60000) // Override or add to env config
|
|
72
|
-
.build();
|
|
73
|
-
|
|
74
|
-
// Or using Client directly
|
|
75
|
-
const client = Client.fromEnvironment();
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
Set these environment variables:
|
|
79
|
-
|
|
80
|
-
| Variable | Description | Required |
|
|
81
|
-
| ------------------- | -------------------------------- | -------- |
|
|
82
|
-
| `NVISY_API_KEY` | API key for authentication | Yes |
|
|
83
|
-
| `NVISY_BASE_URL` | Custom API endpoint URL | No |
|
|
84
|
-
| `NVISY_TIMEOUT` | Request timeout in milliseconds | No |
|
|
85
|
-
| `NVISY_MAX_RETRIES` | Maximum number of retry attempts | No |
|
|
86
|
-
|
|
87
|
-
## Requirements
|
|
88
|
-
|
|
89
|
-
- Node.js 20.0.0 or higher
|
|
90
|
-
- TypeScript 5.9.0 or higher (for development)
|
|
91
|
-
|
|
92
40
|
## Changelog
|
|
93
41
|
|
|
94
42
|
See [CHANGELOG.md](CHANGELOG.md) for release notes and version history.
|
|
@@ -104,5 +52,5 @@ MIT License - see [LICENSE.txt](LICENSE.txt) for details.
|
|
|
104
52
|
## Support
|
|
105
53
|
|
|
106
54
|
- Documentation: [docs.nvisy.com](https://docs.nvisy.com)
|
|
107
|
-
- Issues: [GitHub Issues](https://github.com/nvisycom/sdk/issues)
|
|
55
|
+
- Issues: [GitHub Issues](https://github.com/nvisycom/sdk-ts/issues)
|
|
108
56
|
- Email: [support@nvisy.com](mailto:support@nvisy.com)
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { C as ClientConfig } from '../config-L7FKq_q5.js';
|
|
2
|
+
import { L as Login, A as AuthToken, S as Signup } from '../auth-CncOz2vx.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @fileoverview Configuration types for standalone auth functions.
|
|
6
|
+
*
|
|
7
|
+
* This module provides configuration options for authentication operations
|
|
8
|
+
* that don't require an existing API token.
|
|
9
|
+
*
|
|
10
|
+
* @module auth/config
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Configuration options for standalone authentication functions.
|
|
15
|
+
*
|
|
16
|
+
* This type omits `apiToken` from {@link ClientConfig} since auth functions
|
|
17
|
+
* are used to obtain a token in the first place.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { login } from "@nvisy/sdk/auth";
|
|
22
|
+
*
|
|
23
|
+
* const token = await login(
|
|
24
|
+
* { email: "user@example.com", password: "..." },
|
|
25
|
+
* { baseUrl: "https://api.nvisy.com" }
|
|
26
|
+
* );
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
type AuthConfig = Omit<ClientConfig, "apiToken">;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @fileoverview Standalone password-based authentication functions.
|
|
33
|
+
*
|
|
34
|
+
* This module provides functions for login and signup that don't require
|
|
35
|
+
* an existing API token. Use these to obtain an auth token, then create
|
|
36
|
+
* an authenticated {@link Client} instance.
|
|
37
|
+
*
|
|
38
|
+
* @module auth/password
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* import { login, signup } from "@nvisy/sdk/auth";
|
|
43
|
+
* import { Nvisy } from "@nvisy/sdk";
|
|
44
|
+
*
|
|
45
|
+
* // Login to get a token
|
|
46
|
+
* const token = await login({ email: "user@example.com", password: "..." });
|
|
47
|
+
*
|
|
48
|
+
* // Create authenticated client
|
|
49
|
+
* const nvisy = new Nvisy({ apiToken: token.accessToken });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Login with email and password to obtain an auth token.
|
|
55
|
+
*
|
|
56
|
+
* This is a standalone function that doesn't require an existing {@link Client}
|
|
57
|
+
* instance. Use the returned token to create an authenticated client.
|
|
58
|
+
*
|
|
59
|
+
* @param credentials - Login credentials (email and password)
|
|
60
|
+
* @param config - Optional configuration (baseUrl, headers, userAgent)
|
|
61
|
+
* @returns Promise that resolves with the auth token
|
|
62
|
+
* @throws {ApiError} If the credentials are invalid or the request fails
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* import { login } from "@nvisy/sdk/auth";
|
|
67
|
+
* import { Nvisy } from "@nvisy/sdk";
|
|
68
|
+
*
|
|
69
|
+
* const token = await login({
|
|
70
|
+
* email: "user@example.com",
|
|
71
|
+
* password: "your-password",
|
|
72
|
+
* });
|
|
73
|
+
*
|
|
74
|
+
* const nvisy = new Nvisy({ apiToken: token.accessToken });
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
declare function login(credentials: Login, config?: AuthConfig): Promise<AuthToken>;
|
|
78
|
+
/**
|
|
79
|
+
* Sign up a new account to obtain an auth token.
|
|
80
|
+
*
|
|
81
|
+
* This is a standalone function that doesn't require an existing {@link Client}
|
|
82
|
+
* instance. Use the returned token to create an authenticated client.
|
|
83
|
+
*
|
|
84
|
+
* @param details - Signup details (name, email, password, etc.)
|
|
85
|
+
* @param config - Optional configuration (baseUrl, headers, userAgent)
|
|
86
|
+
* @returns Promise that resolves with the auth token
|
|
87
|
+
* @throws {ApiError} If the signup fails (e.g., email already exists)
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* import { signup } from "@nvisy/sdk/auth";
|
|
92
|
+
* import { Nvisy } from "@nvisy/sdk";
|
|
93
|
+
*
|
|
94
|
+
* const token = await signup({
|
|
95
|
+
* name: "John Doe",
|
|
96
|
+
* email: "john@example.com",
|
|
97
|
+
* password: "secure-password",
|
|
98
|
+
* });
|
|
99
|
+
*
|
|
100
|
+
* const nvisy = new Nvisy({ apiToken: token.accessToken });
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
declare function signup(details: Signup, config?: AuthConfig): Promise<AuthToken>;
|
|
104
|
+
|
|
105
|
+
export { type AuthConfig, login, signup };
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import createClient from 'openapi-fetch';
|
|
2
|
+
|
|
3
|
+
// src/auth/password.ts
|
|
4
|
+
|
|
5
|
+
// src/config.ts
|
|
6
|
+
var VERSION = "0.3.0";
|
|
7
|
+
var DEFAULTS = {
|
|
8
|
+
/**
|
|
9
|
+
* Default base URL for the Nvisy API.
|
|
10
|
+
*/
|
|
11
|
+
BASE_URL: "https://api.nvisy.com",
|
|
12
|
+
/**
|
|
13
|
+
* Default user agent string.
|
|
14
|
+
*/
|
|
15
|
+
USER_AGENT: `@nvisy/sdk v.${VERSION}`
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/errors.ts
|
|
19
|
+
var NvisyError = class extends Error {
|
|
20
|
+
/**
|
|
21
|
+
* The error class name.
|
|
22
|
+
*/
|
|
23
|
+
name;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new NvisyError.
|
|
26
|
+
*
|
|
27
|
+
* @param message - The error message
|
|
28
|
+
*/
|
|
29
|
+
constructor(message) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.name = this.constructor.name;
|
|
32
|
+
if (Error.captureStackTrace) {
|
|
33
|
+
Error.captureStackTrace(this, this.constructor);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var NvisyApiError = class extends NvisyError {
|
|
38
|
+
/**
|
|
39
|
+
* The error type identifier (e.g., "ValidationError", "NotFoundError").
|
|
40
|
+
*/
|
|
41
|
+
name;
|
|
42
|
+
/**
|
|
43
|
+
* Human-readable error message safe for display to end users.
|
|
44
|
+
*/
|
|
45
|
+
message;
|
|
46
|
+
/**
|
|
47
|
+
* The resource type that the error relates to (e.g., "account", "project").
|
|
48
|
+
* May be null if the error is not resource-specific.
|
|
49
|
+
*/
|
|
50
|
+
resource;
|
|
51
|
+
/**
|
|
52
|
+
* A helpful suggestion for resolving the error.
|
|
53
|
+
* May be null if no suggestion is available.
|
|
54
|
+
*/
|
|
55
|
+
suggestion;
|
|
56
|
+
/**
|
|
57
|
+
* Field-specific validation errors.
|
|
58
|
+
* Present when the error is due to invalid input data.
|
|
59
|
+
*/
|
|
60
|
+
validation;
|
|
61
|
+
/**
|
|
62
|
+
* HTTP status code of the response (e.g., 400, 404, 500).
|
|
63
|
+
*/
|
|
64
|
+
statusCode;
|
|
65
|
+
/**
|
|
66
|
+
* Creates a new NvisyApiError from an API error response.
|
|
67
|
+
*
|
|
68
|
+
* @param response - The error response from the API
|
|
69
|
+
* @param statusCode - The HTTP status code of the response
|
|
70
|
+
*/
|
|
71
|
+
constructor(response, statusCode) {
|
|
72
|
+
super(response.message);
|
|
73
|
+
this.name = response.name;
|
|
74
|
+
this.message = response.message;
|
|
75
|
+
this.resource = response.resource;
|
|
76
|
+
this.suggestion = response.suggestion;
|
|
77
|
+
this.validation = response.validation;
|
|
78
|
+
this.statusCode = statusCode;
|
|
79
|
+
if (Error.captureStackTrace) {
|
|
80
|
+
Error.captureStackTrace(this, this.constructor);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Checks if this is a client error (4xx status code).
|
|
85
|
+
*
|
|
86
|
+
* Client errors indicate problems with the request itself, such as
|
|
87
|
+
* invalid input, missing authentication, or accessing non-existent resources.
|
|
88
|
+
*
|
|
89
|
+
* @returns True if the status code is in the 4xx range
|
|
90
|
+
*/
|
|
91
|
+
isClientError() {
|
|
92
|
+
return this.statusCode >= 400 && this.statusCode < 500;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Checks if this is a server error (5xx status code).
|
|
96
|
+
*
|
|
97
|
+
* Server errors indicate problems on the API side. These are typically
|
|
98
|
+
* transient and may succeed if retried.
|
|
99
|
+
*
|
|
100
|
+
* @returns True if the status code is in the 5xx range
|
|
101
|
+
*/
|
|
102
|
+
isServerError() {
|
|
103
|
+
return this.statusCode >= 500;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Determines if this error is safe to retry.
|
|
107
|
+
*
|
|
108
|
+
* An error is considered retryable if it's a server error (5xx),
|
|
109
|
+
* a request timeout (408), or rate limiting (429).
|
|
110
|
+
*
|
|
111
|
+
* @returns True if the request may succeed on retry
|
|
112
|
+
*/
|
|
113
|
+
isRetryable() {
|
|
114
|
+
return this.statusCode >= 500 || // Server errors
|
|
115
|
+
this.statusCode === 408 || // Request timeout
|
|
116
|
+
this.statusCode === 429;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Converts the error to a plain {@link ErrorResponse} object.
|
|
120
|
+
*
|
|
121
|
+
* Useful for serialization or logging.
|
|
122
|
+
*
|
|
123
|
+
* @returns A plain object representation of the error
|
|
124
|
+
*/
|
|
125
|
+
toJSON() {
|
|
126
|
+
return {
|
|
127
|
+
name: this.name,
|
|
128
|
+
message: this.message,
|
|
129
|
+
resource: this.resource,
|
|
130
|
+
suggestion: this.suggestion,
|
|
131
|
+
validation: this.validation
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// src/middleware/error.ts
|
|
137
|
+
var errorMiddleware = {
|
|
138
|
+
async onResponse({ response }) {
|
|
139
|
+
if (!response.ok) {
|
|
140
|
+
const error = await response.clone().json();
|
|
141
|
+
throw new NvisyApiError(error, response.status);
|
|
142
|
+
}
|
|
143
|
+
return response;
|
|
144
|
+
},
|
|
145
|
+
onError({ error }) {
|
|
146
|
+
if (error instanceof Error) {
|
|
147
|
+
throw new NvisyError(error.message);
|
|
148
|
+
}
|
|
149
|
+
throw new NvisyError("An unknown network error occurred");
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// src/auth/password.ts
|
|
154
|
+
function createAuthClient(config) {
|
|
155
|
+
const headers = {
|
|
156
|
+
"Content-Type": "application/json",
|
|
157
|
+
"User-Agent": config?.userAgent ?? DEFAULTS.USER_AGENT,
|
|
158
|
+
...config?.headers
|
|
159
|
+
};
|
|
160
|
+
const client = createClient({
|
|
161
|
+
baseUrl: config?.baseUrl ?? DEFAULTS.BASE_URL,
|
|
162
|
+
headers
|
|
163
|
+
});
|
|
164
|
+
client.use(errorMiddleware);
|
|
165
|
+
return client;
|
|
166
|
+
}
|
|
167
|
+
async function login(credentials, config) {
|
|
168
|
+
const client = createAuthClient(config);
|
|
169
|
+
const { data } = await client.POST("/auth/login", {
|
|
170
|
+
body: credentials
|
|
171
|
+
});
|
|
172
|
+
return data;
|
|
173
|
+
}
|
|
174
|
+
async function signup(details, config) {
|
|
175
|
+
const client = createAuthClient(config);
|
|
176
|
+
const { data } = await client.POST("/auth/signup", {
|
|
177
|
+
body: details
|
|
178
|
+
});
|
|
179
|
+
return data;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export { login, signup };
|
|
183
|
+
//# sourceMappingURL=index.js.map
|
|
184
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/config.ts","../../src/errors.ts","../../src/middleware/error.ts","../../src/auth/password.ts"],"names":[],"mappings":";;;;;AAcO,IAAM,OAAA,GAAU,OAAA;AA8DhB,IAAM,QAAA,GAAW;AAAA;AAAA;AAAA;AAAA,EAIvB,QAAA,EAAU,uBAAA;AAAA;AAAA;AAAA;AAAA,EAKV,UAAA,EAAY,gBAAgB,OAAO,CAAA;AACpC,CAAA;;;ACtDO,IAAM,UAAA,GAAN,cAAyB,KAAA,CAAM;AAAA;AAAA;AAAA;AAAA,EAIrB,IAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOhB,YAAY,OAAA,EAAiB;AAC5B,IAAA,KAAA,CAAM,OAAO,CAAA;AACb,IAAA,IAAA,CAAK,IAAA,GAAO,KAAK,WAAA,CAAY,IAAA;AAE7B,IAAA,IAAI,MAAM,iBAAA,EAAmB;AAC5B,MAAA,KAAA,CAAM,iBAAA,CAAkB,IAAA,EAAM,IAAA,CAAK,WAAW,CAAA;AAAA,IAC/C;AAAA,EACD;AACD,CAAA;AAwBO,IAAM,aAAA,GAAN,cAA4B,UAAA,CAAoC;AAAA;AAAA;AAAA;AAAA,EAItD,IAAA;AAAA;AAAA;AAAA;AAAA,EAKA,OAAA;AA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;AA0BA,eAAsB,KAAA,CACrB,aACA,MAAA,EACqB;AACrB,EAAA,MAAM,MAAA,GAAS,iBAAiB,MAAM,CAAA;AACtC,EAAA,MAAM,EAAE,IAAA,EAAK,GAAI,MAAM,MAAA,CAAO,KAAK,aAAA,EAAe;AAAA,IACjD,IAAA,EAAM;AAAA,GACN,CAAA;AACD,EAAA,OAAO,IAAA;AACR;AA2BA,eAAsB,MAAA,CACrB,SACA,MAAA,EACqB;AACrB,EAAA,MAAM,MAAA,GAAS,iBAAiB,MAAM,CAAA;AACtC,EAAA,MAAM,EAAE,IAAA,EAAK,GAAI,MAAM,MAAA,CAAO,KAAK,cAAA,EAAgB;AAAA,IAClD,IAAA,EAAM;AAAA,GACN,CAAA;AACD,EAAA,OAAO,IAAA;AACR","file":"index.js","sourcesContent":["/**\n * @fileoverview Configuration types and constants for the Nvisy SDK.\n *\n * This module provides configuration interfaces, default values, and environment\n * variable names used throughout the SDK.\n *\n * @module config\n */\n\n/**\n * Current SDK version.\n *\n * Used in the default user agent string and for version tracking.\n */\nexport const VERSION = \"0.3.0\";\n\n/**\n * Configuration options for creating a Nvisy client.\n *\n * The `apiToken` field is required for authentication. All other fields are optional\n * and will use sensible defaults when omitted.\n *\n * @example\n * ```typescript\n * const config: ClientConfig = {\n * apiToken: \"your-api-token\",\n * baseUrl: \"https://api.nvisy.com\",\n * headers: { \"X-Custom-Header\": \"value\" },\n * };\n * const client = new Client(config);\n * ```\n */\nexport interface ClientConfig {\n\t/**\n\t * API token for authentication.\n\t *\n\t * Tokens can be obtained from the Nvisy dashboard or via the auth endpoints.\n\t */\n\tapiToken: string;\n\n\t/**\n\t * Base URL for the Nvisy API.\n\t *\n\t * @default \"https://api.nvisy.com\"\n\t */\n\tbaseUrl?: string;\n\n\t/**\n\t * Custom headers to include with every request.\n\t *\n\t * These headers are merged with the default headers (Content-Type, User-Agent,\n\t * and Authorization). Custom headers take precedence over defaults if there\n\t * are conflicts.\n\t */\n\theaders?: Record<string, string>;\n\n\t/**\n\t * Custom user agent string to identify your application.\n\t *\n\t * @default \"@nvisy/sdk v.{version}\"\n\t */\n\tuserAgent?: string;\n\n\t/**\n\t * Enable logging for requests and responses.\n\t *\n\t * When enabled, logs request method, URL, status, and timing to console.\n\t *\n\t * @default false\n\t */\n\twithLogging?: boolean;\n}\n\n/**\n * Default configuration values used when options are not explicitly provided.\n */\nexport const DEFAULTS = {\n\t/**\n\t * Default base URL for the Nvisy API.\n\t */\n\tBASE_URL: \"https://api.nvisy.com\",\n\n\t/**\n\t * Default user agent string.\n\t */\n\tUSER_AGENT: `@nvisy/sdk v.${VERSION}`,\n} as const;\n","/**\n * @fileoverview Error types for the Nvisy SDK.\n *\n * This module defines the error hierarchy used throughout the SDK:\n * - {@link NvisyError} - Base error class for all SDK errors\n * - {@link NvisyApiError} - Errors returned by the Nvisy API (4xx/5xx responses)\n *\n * @module errors\n */\n\nimport type { ErrorResponse } from \"@/datatypes/index.js\";\n\n// Re-export ErrorResponse type for convenience\nexport type { ErrorResponse } from \"@/datatypes/index.js\";\n\n/**\n * Base error class for all Nvisy SDK errors.\n *\n * This class provides common functionality for all SDK errors.\n * It ensures proper error naming and stack traces.\n *\n * @example\n * ```typescript\n * try {\n * const client = new Client({ apiToken: \"\" });\n * } catch (error) {\n * if (error instanceof NvisyError) {\n * console.log(\"SDK error:\", error.message);\n * }\n * }\n * ```\n */\nexport class NvisyError extends Error {\n\t/**\n\t * The error class name.\n\t */\n\tpublic readonly name: string;\n\n\t/**\n\t * Creates a new NvisyError.\n\t *\n\t * @param message - The error message\n\t */\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = this.constructor.name;\n\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, this.constructor);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when the Nvisy API returns an error response.\n *\n * This error class wraps the API's {@link ErrorResponse} format and adds\n * the HTTP status code. It provides helper methods to categorize errors\n * and determine if they are retryable.\n *\n * @example\n * ```typescript\n * try {\n * await client.account.getAccount();\n * } catch (error) {\n * if (error instanceof NvisyApiError) {\n * console.log(`API Error: ${error.message}`);\n * console.log(`Status: ${error.statusCode}`);\n * if (error.isRetryable()) {\n * // Implement retry logic\n * }\n * }\n * }\n * ```\n */\nexport class NvisyApiError extends NvisyError implements ErrorResponse {\n\t/**\n\t * The error type identifier (e.g., \"ValidationError\", \"NotFoundError\").\n\t */\n\tpublic readonly name: string;\n\n\t/**\n\t * Human-readable error message safe for display to end users.\n\t */\n\tpublic readonly message: string;\n\n\t/**\n\t * The resource type that the error relates to (e.g., \"account\", \"project\").\n\t * May be null if the error is not resource-specific.\n\t */\n\tpublic readonly resource?: string | null;\n\n\t/**\n\t * A helpful suggestion for resolving the error.\n\t * May be null if no suggestion is available.\n\t */\n\tpublic readonly suggestion?: string | null;\n\n\t/**\n\t * Field-specific validation errors.\n\t * Present when the error is due to invalid input data.\n\t */\n\tpublic readonly validation?: ErrorResponse[\"validation\"];\n\n\t/**\n\t * HTTP status code of the response (e.g., 400, 404, 500).\n\t */\n\tpublic readonly statusCode: number;\n\n\t/**\n\t * Creates a new NvisyApiError from an API error response.\n\t *\n\t * @param response - The error response from the API\n\t * @param statusCode - The HTTP status code of the response\n\t */\n\tconstructor(response: ErrorResponse, statusCode: number) {\n\t\tsuper(response.message);\n\t\tthis.name = response.name;\n\t\tthis.message = response.message;\n\t\tthis.resource = response.resource;\n\t\tthis.suggestion = response.suggestion;\n\t\tthis.validation = response.validation;\n\t\tthis.statusCode = statusCode;\n\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, this.constructor);\n\t\t}\n\t}\n\n\t/**\n\t * Checks if this is a client error (4xx status code).\n\t *\n\t * Client errors indicate problems with the request itself, such as\n\t * invalid input, missing authentication, or accessing non-existent resources.\n\t *\n\t * @returns True if the status code is in the 4xx range\n\t */\n\tisClientError(): boolean {\n\t\treturn this.statusCode >= 400 && this.statusCode < 500;\n\t}\n\n\t/**\n\t * Checks if this is a server error (5xx status code).\n\t *\n\t * Server errors indicate problems on the API side. These are typically\n\t * transient and may succeed if retried.\n\t *\n\t * @returns True if the status code is in the 5xx range\n\t */\n\tisServerError(): boolean {\n\t\treturn this.statusCode >= 500;\n\t}\n\n\t/**\n\t * Determines if this error is safe to retry.\n\t *\n\t * An error is considered retryable if it's a server error (5xx),\n\t * a request timeout (408), or rate limiting (429).\n\t *\n\t * @returns True if the request may succeed on retry\n\t */\n\tisRetryable(): boolean {\n\t\treturn (\n\t\t\tthis.statusCode >= 500 || // Server errors\n\t\t\tthis.statusCode === 408 || // Request timeout\n\t\t\tthis.statusCode === 429 // Rate limited\n\t\t);\n\t}\n\n\t/**\n\t * Converts the error to a plain {@link ErrorResponse} object.\n\t *\n\t * Useful for serialization or logging.\n\t *\n\t * @returns A plain object representation of the error\n\t */\n\ttoJSON(): ErrorResponse {\n\t\treturn {\n\t\t\tname: this.name,\n\t\t\tmessage: this.message,\n\t\t\tresource: this.resource,\n\t\t\tsuggestion: this.suggestion,\n\t\t\tvalidation: this.validation,\n\t\t};\n\t}\n}\n","import type { Middleware } from \"openapi-fetch\";\nimport type { ErrorResponse } from \"@/datatypes/index.js\";\nimport { NvisyApiError, NvisyError } from \"@/errors.js\";\n\n/**\n * Middleware that automatically throws NvisyApiError for error responses.\n * This replaces the need for manual checks in services.\n */\nexport const errorMiddleware: Middleware = {\n\tasync onResponse({ response }) {\n\t\tif (!response.ok) {\n\t\t\tconst error = (await response.clone().json()) as ErrorResponse;\n\t\t\tthrow new NvisyApiError(error, response.status);\n\t\t}\n\t\treturn response;\n\t},\n\n\tonError({ error }) {\n\t\t// Wrap fetch errors (network failures, CORS, etc.) in NvisyError\n\t\tif (error instanceof Error) {\n\t\t\tthrow new NvisyError(error.message);\n\t\t}\n\t\tthrow new NvisyError(\"An unknown network error occurred\");\n\t},\n};\n","/**\n * @fileoverview Standalone password-based authentication functions.\n *\n * This module provides functions for login and signup that don't require\n * an existing API token. Use these to obtain an auth token, then create\n * an authenticated {@link Client} instance.\n *\n * @module auth/password\n *\n * @example\n * ```typescript\n * import { login, signup } from \"@nvisy/sdk/auth\";\n * import { Nvisy } from \"@nvisy/sdk\";\n *\n * // Login to get a token\n * const token = await login({ email: \"user@example.com\", password: \"...\" });\n *\n * // Create authenticated client\n * const nvisy = new Nvisy({ apiToken: token.accessToken });\n * ```\n */\n\nimport createClient from \"openapi-fetch\";\nimport type { AuthConfig } from \"@/auth/config.js\";\nimport { DEFAULTS } from \"@/config.js\";\nimport type { AuthToken, Login, Signup } from \"@/datatypes/index.js\";\nimport { errorMiddleware } from \"@/middleware/index.js\";\nimport type { paths } from \"@/schema/api.js\";\n\n/**\n * Creates an unauthenticated API client for auth operations.\n *\n * @param config - Optional configuration options\n * @returns A configured openapi-fetch client without authentication\n * @internal\n */\nfunction createAuthClient(config?: AuthConfig) {\n\tconst headers: Record<string, string> = {\n\t\t\"Content-Type\": \"application/json\",\n\t\t\"User-Agent\": config?.userAgent ?? DEFAULTS.USER_AGENT,\n\t\t...config?.headers,\n\t};\n\n\tconst client = createClient<paths>({\n\t\tbaseUrl: config?.baseUrl ?? DEFAULTS.BASE_URL,\n\t\theaders,\n\t});\n\n\tclient.use(errorMiddleware);\n\treturn client;\n}\n\n/**\n * Login with email and password to obtain an auth token.\n *\n * This is a standalone function that doesn't require an existing {@link Client}\n * instance. Use the returned token to create an authenticated client.\n *\n * @param credentials - Login credentials (email and password)\n * @param config - Optional configuration (baseUrl, headers, userAgent)\n * @returns Promise that resolves with the auth token\n * @throws {ApiError} If the credentials are invalid or the request fails\n *\n * @example\n * ```typescript\n * import { login } from \"@nvisy/sdk/auth\";\n * import { Nvisy } from \"@nvisy/sdk\";\n *\n * const token = await login({\n * email: \"user@example.com\",\n * password: \"your-password\",\n * });\n *\n * const nvisy = new Nvisy({ apiToken: token.accessToken });\n * ```\n */\nexport async function login(\n\tcredentials: Login,\n\tconfig?: AuthConfig,\n): Promise<AuthToken> {\n\tconst client = createAuthClient(config);\n\tconst { data } = await client.POST(\"/auth/login\", {\n\t\tbody: credentials,\n\t});\n\treturn data!;\n}\n\n/**\n * Sign up a new account to obtain an auth token.\n *\n * This is a standalone function that doesn't require an existing {@link Client}\n * instance. Use the returned token to create an authenticated client.\n *\n * @param details - Signup details (name, email, password, etc.)\n * @param config - Optional configuration (baseUrl, headers, userAgent)\n * @returns Promise that resolves with the auth token\n * @throws {ApiError} If the signup fails (e.g., email already exists)\n *\n * @example\n * ```typescript\n * import { signup } from \"@nvisy/sdk/auth\";\n * import { Nvisy } from \"@nvisy/sdk\";\n *\n * const token = await signup({\n * name: \"John Doe\",\n * email: \"john@example.com\",\n * password: \"secure-password\",\n * });\n *\n * const nvisy = new Nvisy({ apiToken: token.accessToken });\n * ```\n */\nexport async function signup(\n\tdetails: Signup,\n\tconfig?: AuthConfig,\n): Promise<AuthToken> {\n\tconst client = createAuthClient(config);\n\tconst { data } = await client.POST(\"/auth/signup\", {\n\t\tbody: details,\n\t});\n\treturn data!;\n}\n"]}
|