Skip to content

Commit e9fa46d

Browse files
author
Ferenc Sárai
committed
fix: run with latest bee-js (v8.3.1) but utils/http.ts is patched
1 parent b0d6e8d commit e9fa46d

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"scripts": {
2222
"docusaurus": "docusaurus",
2323
"start": "docusaurus start",
24+
"prebuild": "cp patch-http.ts bee-js/src/utils/http.ts",
2425
"build": "docusaurus build",
2526
"swizzle": "docusaurus swizzle",
2627
"deploy": "docusaurus deploy",

patch-http.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
2+
import { Objects, Strings } from 'cafe-utility'
3+
import { BeeRequestOptions, BeeResponseError } from '../index'
4+
5+
const { AxiosError } = axios
6+
7+
export const DEFAULT_HTTP_CONFIG: AxiosRequestConfig = {
8+
headers: {
9+
accept: 'application/json, text/plain, */*',
10+
},
11+
maxBodyLength: Infinity,
12+
maxContentLength: Infinity,
13+
}
14+
15+
/**
16+
* Main function to make HTTP requests.
17+
* @param options User defined settings
18+
* @param config Internal settings and/or Bee settings
19+
*/
20+
export async function http<T>(options: BeeRequestOptions, config: AxiosRequestConfig): Promise<AxiosResponse<T>> {
21+
try {
22+
const requestConfig: AxiosRequestConfig = Objects.deepMerge3(DEFAULT_HTTP_CONFIG, config, options)
23+
maybeRunOnRequestHook(options, requestConfig)
24+
const response = await axios(requestConfig)
25+
26+
// TODO: https://github.com/axios/axios/pull/6253
27+
return response as unknown as AxiosResponse<T>
28+
} catch (e: unknown) {
29+
if (axios.isAxiosError(e)) {
30+
throw new BeeResponseError(
31+
config.method || 'get',
32+
config.url || '<unknown>',
33+
e.message,
34+
e.response?.data,
35+
e.response?.status,
36+
e.code,
37+
)
38+
}
39+
throw e
40+
}
41+
}
42+
43+
function maybeRunOnRequestHook(options: BeeRequestOptions, requestConfig: AxiosRequestConfig) {
44+
if (options.onRequest) {
45+
options.onRequest({
46+
method: requestConfig.method || 'GET',
47+
url: Strings.joinUrl(requestConfig.baseURL as string, requestConfig.url as string),
48+
headers: { ...requestConfig.headers } as Record<string, string>,
49+
params: requestConfig.params,
50+
})
51+
}
52+
}

tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./bee-js/tsconfig.json",
3+
"compilerOptions": {
4+
"module": "none", // Override the module option
5+
"moduleResolution": "node" // Ensure this is set to a valid value
6+
}
7+
}

typedoc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
"out": "api",
77
"allReflectionsHaveOwnDocument": true,
88
"entryPoints": ["./bee-js/src/index.ts"],
9-
"tsconfig": "./bee-js/tsconfig.json"
9+
"tsconfig": "./tsconfig.json"
1010
}

0 commit comments

Comments
 (0)