@nuxx/torn-fetch 0.1.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/COPYING +24 -0
- package/README.md +190 -0
- package/dist/index.d.ts +10636 -0
- package/dist/index.js +1 -0
- package/nuxx-torn-fetch-0.1.0.tgz +0 -0
- package/package.json +54 -0
package/COPYING
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
package/README.md
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# Torn Fetch
|
|
2
|
+
|
|
3
|
+
A TypeScript wrapper around `openapi-fetch` that provides a better developer experience when working with the Torn API. This library automatically handles authentication and error checking, throwing JavaScript errors when the API returns error responses.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Type-safe API calls**: Full TypeScript support with generated types from Torn's OpenAPI schema
|
|
8
|
+
- **Automatic error handling**: Throws JavaScript errors when the API returns error responses
|
|
9
|
+
- **Higher-order function pattern**: Create specialized fetchers for specific API endpoints
|
|
10
|
+
- **Clean API**: Simple and intuitive interface for making API calls
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @nuxx/torn-fetch
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### Basic Usage
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { tornFetch } from '@nuxx/torn-fetch'
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
const userAttacks = await tornFetch(
|
|
27
|
+
'your-api-key',
|
|
28
|
+
'/user/attacks'
|
|
29
|
+
)
|
|
30
|
+
console.log(userAttacks)
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.error('API Error:', error.message)
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { tornFetch } from '@nuxx/torn-fetch'
|
|
38
|
+
|
|
39
|
+
// Use with path parameters
|
|
40
|
+
const attacks = await tornFetch(
|
|
41
|
+
'your-api-key',
|
|
42
|
+
'/faction/{id}/chain',
|
|
43
|
+
{
|
|
44
|
+
path: {
|
|
45
|
+
id: 33458
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { tornFetch } from '@nuxx/torn-fetch'
|
|
53
|
+
|
|
54
|
+
// Use with query parameters
|
|
55
|
+
const attacks = await tornFetch(
|
|
56
|
+
'your-api-key',
|
|
57
|
+
'/user/attacks',
|
|
58
|
+
{
|
|
59
|
+
query: {
|
|
60
|
+
limit: 25,
|
|
61
|
+
from: 1753037683
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Error Handling
|
|
68
|
+
|
|
69
|
+
The library automatically throws JavaScript errors when the Torn API returns error responses:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { tornFetch } from '@nuxx/torn-fetch'
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
const userAttacks = await tornFetch(
|
|
76
|
+
'invalid-key-abc123',
|
|
77
|
+
'/user/attacks'
|
|
78
|
+
)
|
|
79
|
+
console.log(userAttacks)
|
|
80
|
+
} catch (error) {
|
|
81
|
+
console.error('API Error:', error.message)
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## API Reference
|
|
86
|
+
|
|
87
|
+
### `tornFetch<TPath>(apiKey: string, path: TPath, options?: TParams<TPath>): Promise<TResponse<TPath>>`
|
|
88
|
+
|
|
89
|
+
Creates a specialized fetcher function for a specific API endpoint.
|
|
90
|
+
|
|
91
|
+
**Parameters:**
|
|
92
|
+
- `apiKey`: Your Torn API key
|
|
93
|
+
- `path`: The API endpoint path (e.g., `/user`, `/faction/{id}/chain`)
|
|
94
|
+
- `options` (optional): An object conforming to the values expected by the Torn API, including:
|
|
95
|
+
- `query`: Query parameters to include in the request
|
|
96
|
+
- `path`: Path parameters to replace in the endpoint path
|
|
97
|
+
|
|
98
|
+
**Returns:** Promise resolving to the API response data
|
|
99
|
+
|
|
100
|
+
**Throws:** JavaScript error if the API returns an error response
|
|
101
|
+
|
|
102
|
+
## Development
|
|
103
|
+
|
|
104
|
+
### Prerequisites
|
|
105
|
+
|
|
106
|
+
- Node.js 18+
|
|
107
|
+
- pnpm
|
|
108
|
+
|
|
109
|
+
### Setup
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
# Install dependencies
|
|
113
|
+
pnpm install
|
|
114
|
+
|
|
115
|
+
# Get the latest Torn API schema and build
|
|
116
|
+
pnpm run build
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Scripts
|
|
120
|
+
|
|
121
|
+
- `pnpm run get-schema` - Downloads the latest Torn API OpenAPI schema and generates TypeScript types
|
|
122
|
+
- `pnpm run build` - Builds the project using Parcel
|
|
123
|
+
- `pnpm run lint` - Runs ESLint on the codebase
|
|
124
|
+
- `pnpm run type-check` - Runs TypeScript type checking
|
|
125
|
+
- `pnpm run test` - Runs the test suite
|
|
126
|
+
- `pnpm run test:watch` - Runs tests in watch mode
|
|
127
|
+
- `pnpm run test:coverage` - Runs tests with coverage reporting
|
|
128
|
+
- `pnpm run ci` - Runs the complete CI pipeline (schema, lint, type-check, test, build)
|
|
129
|
+
|
|
130
|
+
## Testing
|
|
131
|
+
|
|
132
|
+
This package includes comprehensive tests with 100% code coverage. The test suite covers:
|
|
133
|
+
|
|
134
|
+
- **Core functionality**: API calls, parameter handling, authentication
|
|
135
|
+
- **Error handling**: Torn API error detection and JavaScript error throwing
|
|
136
|
+
- **Edge cases**: Null values, undefined responses, non-object data
|
|
137
|
+
- **Type safety**: Proper TypeScript type checking
|
|
138
|
+
|
|
139
|
+
Run tests with:
|
|
140
|
+
```bash
|
|
141
|
+
pnpm test # Run tests once
|
|
142
|
+
pnpm run test:watch # Run tests in watch mode
|
|
143
|
+
pnpm run test:coverage # Run tests with coverage report
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
See [TESTING.md](./TESTING.md) for detailed testing documentation.
|
|
147
|
+
|
|
148
|
+
## Project Structure
|
|
149
|
+
|
|
150
|
+
```
|
|
151
|
+
torn-fetch/
|
|
152
|
+
├── coverage/ # Test coverage reports
|
|
153
|
+
├── dist/ # Compiled output
|
|
154
|
+
├── src/
|
|
155
|
+
│ ├── __tests__/ # Test files
|
|
156
|
+
│ │ ├── index.test.ts # Main functionality tests
|
|
157
|
+
│ │ ├── error-detection.test.ts # Error detection logic tests
|
|
158
|
+
│ │ └── error-handling.test.ts # Error handling tests
|
|
159
|
+
│ ├── index.ts # Main export (tornFetch)
|
|
160
|
+
│ ├── openapi.json # Torn API OpenAPI schema
|
|
161
|
+
│ └── torn-api.ts # Generated TypeScript types
|
|
162
|
+
├── eslint.config.js # ESLint configuration
|
|
163
|
+
├── package.json
|
|
164
|
+
├── pnpm-workspace.yaml # pnpm workspace configuration
|
|
165
|
+
├── tsconfig.json # TypeScript configuration
|
|
166
|
+
├── tsup.config.ts # Build configuration (legacy)
|
|
167
|
+
├── vitest.config.ts # Test configuration
|
|
168
|
+
├── TESTING.md # Testing documentation
|
|
169
|
+
└── README.md
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Contributing
|
|
173
|
+
|
|
174
|
+
1. Fork the repository
|
|
175
|
+
2. Create a feature branch
|
|
176
|
+
3. Make your changes
|
|
177
|
+
4. Run the full CI pipeline: `pnpm run ci`
|
|
178
|
+
5. Submit a pull request
|
|
179
|
+
|
|
180
|
+
All contributions should include appropriate tests and maintain 100% code coverage.
|
|
181
|
+
|
|
182
|
+
## License
|
|
183
|
+
|
|
184
|
+
Unlicense
|
|
185
|
+
|
|
186
|
+
## Author
|
|
187
|
+
|
|
188
|
+
nuxx [3054747]
|
|
189
|
+
nuxx@nuxx.lol
|
|
190
|
+
|