|
| 1 | +--- |
| 2 | +name: uni-network |
| 3 | +description: Promise-based HTTP client for uni-app - axios-inspired API with uni.request as default transport |
| 4 | +--- |
| 5 | + |
| 6 | +# uni-network |
| 7 | + |
| 8 | +Promise-based HTTP client for uni-app, inspired by axios@0.27.2. Uses uni.request/uni.uploadFile/uni.downloadFile as the underlying transport. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +```bash |
| 13 | +npm i @uni-helper/uni-network |
| 14 | +``` |
| 15 | + |
| 16 | +## Features |
| 17 | + |
| 18 | +- Promise-based API |
| 19 | +- Request/response interceptors |
| 20 | +- Request cancellation |
| 21 | +- TypeScript support |
| 22 | +- Composition API integration |
| 23 | +- Upload/download support |
| 24 | + |
| 25 | +## Basic Usage |
| 26 | + |
| 27 | +```ts |
| 28 | +import { un } from '@uni-helper/uni-network' |
| 29 | + |
| 30 | +// GET request |
| 31 | +un('/user/123') |
| 32 | + |
| 33 | +// POST request |
| 34 | +un({ |
| 35 | + method: 'POST', |
| 36 | + url: '/user/123', |
| 37 | + data: { |
| 38 | + firstName: 'Fred', |
| 39 | + lastName: 'Flintstone', |
| 40 | + }, |
| 41 | +}) |
| 42 | +``` |
| 43 | + |
| 44 | +## Request Aliases |
| 45 | + |
| 46 | +```ts |
| 47 | +import { un } from '@uni-helper/uni-network' |
| 48 | + |
| 49 | +// HTTP verbs |
| 50 | +un.get('/users') |
| 51 | +un.post('/users', { name: 'John' }) |
| 52 | +un.put('/users/1', { name: 'Jane' }) |
| 53 | +un.patch('/users/1', { age: 30 }) |
| 54 | +un.delete('/users/1') |
| 55 | + |
| 56 | +// Upload/download |
| 57 | +un.upload({ |
| 58 | + url: '/upload', |
| 59 | + filePath: tempFilePath, |
| 60 | + name: 'file', |
| 61 | +}) |
| 62 | + |
| 63 | +un.download({ |
| 64 | + url: '/files/report.pdf', |
| 65 | +}) |
| 66 | +``` |
| 67 | + |
| 68 | +## Creating an Instance |
| 69 | + |
| 70 | +```ts |
| 71 | +import { createUniNetwork } from '@uni-helper/uni-network' |
| 72 | + |
| 73 | +const http = createUniNetwork({ |
| 74 | + baseURL: 'https://api.example.com', |
| 75 | + timeout: 10000, |
| 76 | + headers: { |
| 77 | + 'Content-Type': 'application/json', |
| 78 | + }, |
| 79 | +}) |
| 80 | + |
| 81 | +// Use the instance |
| 82 | +http.get('/users') |
| 83 | +``` |
| 84 | + |
| 85 | +## Interceptors |
| 86 | + |
| 87 | +```ts |
| 88 | +import { un } from '@uni-helper/uni-network' |
| 89 | + |
| 90 | +// Request interceptor |
| 91 | +un.interceptors.request.use( |
| 92 | + (config) => { |
| 93 | + // Add auth token |
| 94 | + const token = uni.getStorageSync('token') |
| 95 | + if (token) { |
| 96 | + config.headers = config.headers || {} |
| 97 | + config.headers.Authorization = `Bearer ${token}` |
| 98 | + } |
| 99 | + return config |
| 100 | + }, |
| 101 | + (error) => { |
| 102 | + return Promise.reject(error) |
| 103 | + } |
| 104 | +) |
| 105 | + |
| 106 | +// Response interceptor |
| 107 | +un.interceptors.response.use( |
| 108 | + (response) => { |
| 109 | + // Transform response data |
| 110 | + return response.data |
| 111 | + }, |
| 112 | + (error) => { |
| 113 | + // Handle errors globally |
| 114 | + if (error.statusCode === 401) { |
| 115 | + uni.redirectTo({ url: '/pages/login/index' }) |
| 116 | + } |
| 117 | + return Promise.reject(error) |
| 118 | + } |
| 119 | +) |
| 120 | +``` |
| 121 | + |
| 122 | +## Configuration |
| 123 | + |
| 124 | +```ts |
| 125 | +interface UniNetworkConfig { |
| 126 | + // URL |
| 127 | + url?: string |
| 128 | + // Base URL |
| 129 | + baseURL?: string |
| 130 | + // Method |
| 131 | + method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | ... |
| 132 | + // Headers |
| 133 | + headers?: Record<string, string> |
| 134 | + // Request data |
| 135 | + data?: any |
| 136 | + // Query params |
| 137 | + params?: Record<string, any> |
| 138 | + // Timeout in ms |
| 139 | + timeout?: number |
| 140 | + // Response type |
| 141 | + responseType?: 'text' | 'arraybuffer' |
| 142 | + // Data type |
| 143 | + dataType?: 'json' | 'string' |
| 144 | + // Enable http2 |
| 145 | + enableHttp2?: boolean |
| 146 | + // Enable quic |
| 147 | + enableQuic?: boolean |
| 148 | + // Enable cache |
| 149 | + enableCache?: boolean |
| 150 | + // SSL verify |
| 151 | + sslVerify?: boolean |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +## Request Cancellation |
| 156 | + |
| 157 | +```ts |
| 158 | +import { un } from '@uni-helper/uni-network' |
| 159 | + |
| 160 | +const controller = new AbortController() |
| 161 | + |
| 162 | +un.get('/long-request', { |
| 163 | + signal: controller.signal, |
| 164 | +}) |
| 165 | + |
| 166 | +// Cancel after 5 seconds |
| 167 | +setTimeout(() => { |
| 168 | + controller.abort() |
| 169 | +}, 5000) |
| 170 | +``` |
| 171 | + |
| 172 | +## Error Handling |
| 173 | + |
| 174 | +```ts |
| 175 | +import { un, UniNetworkError } from '@uni-helper/uni-network' |
| 176 | + |
| 177 | +try { |
| 178 | + const response = await un.get('/users') |
| 179 | +} catch (error) { |
| 180 | + if (error instanceof UniNetworkError) { |
| 181 | + console.log('Request failed:', error.message) |
| 182 | + console.log('Status:', error.statusCode) |
| 183 | + } |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +## Composition API |
| 188 | + |
| 189 | +```ts |
| 190 | +import { useUniNetwork } from '@uni-helper/uni-network' |
| 191 | + |
| 192 | +const { data, error, loading, execute } = useUniNetwork({ |
| 193 | + url: '/users', |
| 194 | + method: 'GET', |
| 195 | +}) |
| 196 | + |
| 197 | +// Execute manually |
| 198 | +await execute() |
| 199 | +``` |
| 200 | + |
| 201 | +## TypeScript Support |
| 202 | + |
| 203 | +```ts |
| 204 | +import { un } from '@uni-helper/uni-network' |
| 205 | + |
| 206 | +interface User { |
| 207 | + id: number |
| 208 | + name: string |
| 209 | + email: string |
| 210 | +} |
| 211 | + |
| 212 | +// Typed response |
| 213 | +const { data } = await un.get<User>('/users/1') |
| 214 | +console.log(data.name) // Type-safe access |
| 215 | +``` |
| 216 | + |
| 217 | +<!-- |
| 218 | +Source references: |
| 219 | +- https://github.com/uni-helper/uni-network |
| 220 | +- https://uni-helper.js.org/uni-network |
| 221 | +--> |
0 commit comments