ceek
Ceek is an explicit request library for browser.
- Won't transform data without explicit config.
- Won't suppress expection silently (JSON parse error, config error, eg.).
- Explicitly error handling.
- Supports typescript for response data.
- Supports upload progress.
Usage
import { ceek } from 'ceek'
const { json } = await ceek.post('https://example.com', {
json: { foo: 'bar' }
})
API
ceek(options: CeekRequestOptions): CeekResponse
CeekRequestOptions
type CeekRequestOptions = {
method:
| 'get'
| 'GET'
| 'post'
| 'POST'
| 'put'
| 'PUT'
| 'patch'
| 'PATCH'
| 'head'
| 'delete'
url: string
timeout?: number
withCredentials?: boolean
headers?: Record<string, string>
responseType?: 'arraybuffer' | 'blob' | 'text'
body?: XMLHttpRequestBodyInit
json?: any
query?: Record<string, string>
baseUrl?: string
onUploadProgress?: (
progressEvent: ProgressEvent<XMLHttpRequestEventTarget>
) => void
onDownloadProgress?: (
progressEvent: ProgressEvent<XMLHttpRequestEventTarget>
) => void
}
CeekResponse
export type CeekResponse<T = any> = {
status: number
statusText: string
headers: Record<string, string>
body: string | ArrayBuffer | Blob
json: T
}
Error handling
import { catchError, matchError, CEEK_ERROR } from 'ceek'
ceek
.get('https://example.com')
.then((resp) => {
console.log(resp.json)
})
.catch(catchError(({ ceekError, error }) => {
if (ceekError) {
switch ceekError.type {
CEEK_ERROR.INVALID_JSON:
console.log('json parse error')
break
default:
console.log('some error')
}
}
}))
try {
const { json } = await ceek.get('https://example.com')
} catch (e) {
matchError(e, ({ ceekError, error }) => {
})
}
TODO global error handler
import type { CeekError } from 'ceek'
const request = ceek.extend({
onError(error: CeekError) {
switch error.type {
}
}
})