Skip to content

Commit

Permalink
feat(utils/create-http-method): allows override global initOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
fzn0x committed Jun 18, 2024
1 parent b5f4edc commit b14cb99
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ and Browsers:

## Error Handling

No need to write `try..catch` ! hypf do it like this:
You can handle errors like [Error handling on Golang](https://go.dev/blog/error-handling-and-go)

```ts
const hypfRequest = hypf.init('https://jsonplaceholder.typicode.com')
Expand All @@ -116,6 +116,33 @@ if (postErr) {
}
```

or throw on error with `throwOnError` options sets `true`

```ts
const hypfRequest = hypf.init('https://jsonplaceholder.typicode.com')

// Example usage of POST method with retry and timeout
try {
const response = await hypfRequest.post(
'/posts',
{ retries: 3, timeout: 5000, initOptions: { throwOnError: true } },
{
title: 'foo',
body: 'bar',
userId: 1,
}
)

if (postErr) {
console.error('POST Error:', postErr)
} else {
console.log('POST Data:', postData)
}
} catch (err) {
console.log(err)
}
```

## Hooks

Hooks is supported and expected to not modifying the original result by design.
Expand Down
2 changes: 1 addition & 1 deletion browser/hyperfetch-browser.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion browser/hyperfetch-browser.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/types/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface RequestOptions extends RequestInit {
params?: Record<string, string | number> // URLSearchParams option
proxy?: string
unix?: string
initOptions?: InitOptions
}

export type RequestFunction = (
Expand Down
17 changes: 17 additions & 0 deletions src/utils/create-http-method.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,21 @@ describe('createHTTPError', () => {
const [error] = await createHTTPMethod('http://localhost', 'GET', {})
expect(error?.message).equals('fetch failed')
})

it('allows override global throwOnError', async () => {
try {
await createHTTPMethod(
'http://localhost',
'GET',
{
initOptions: {
throwOnError: true,
},
},
{}
)
} catch (err) {
expect((err as Error)?.message).equals('fetch failed')
}
})
})
8 changes: 6 additions & 2 deletions src/utils/create-http-method.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RequestMethod } from '../types/request.js'
import type { RequestMethod, RequestOptions } from '../types/request.js'
import type { InitOptions } from '../types/init.js'

import { createRequest } from './create-request.js'
Expand All @@ -17,9 +17,13 @@ import { createRequest } from './create-request.js'
export const createHTTPMethod = (
url: string,
method: RequestMethod = 'GET',
options = Object.create(null),
options: RequestOptions = Object.create(null),
data: { [key: string]: unknown } = Object.create(null),
initOptions?: InitOptions
) => {
if (options.initOptions) {
initOptions = options.initOptions
}

return createRequest(url, { method, ...options }, data, initOptions)
}

0 comments on commit b14cb99

Please sign in to comment.