Skip to content

Commit 32e24c7

Browse files
committed
Drop support for overriding the fetch implementation, requiring native fetch
Change-type: major
1 parent 0c1efa8 commit 32e24c7

File tree

3 files changed

+4
-44
lines changed

3 files changed

+4
-44
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"balena-config-karma": "^4.0.2",
4848
"chai": "^6.0.1",
4949
"chai-as-promised": "^8.0.2",
50-
"fetch-ponyfill": "^7.1.0",
5150
"husky": "^9.1.7",
5251
"karma": "^6.4.4",
5352
"lint-staged": "^16.1.6",

src/index.ts

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
import type {
2-
Params,
3-
AnyObject,
4-
Resource,
5-
AnyResource,
6-
} from 'pinejs-client-core';
1+
import type { AnyObject, Resource, AnyResource } from 'pinejs-client-core';
72
import { PinejsClientCore } from 'pinejs-client-core';
83
export type { PinejsClientCore } from 'pinejs-client-core';
94

10-
interface BackendParams {
11-
/** The browser fetch API implementation or a compatible one */
12-
fetch?: typeof fetch;
13-
}
14-
155
export class RequestError extends Error {
166
public code = 'PineClientFetchRequestError';
177

@@ -31,21 +21,6 @@ export default class PineFetch<
3121
[key in string]: AnyResource;
3222
},
3323
> extends PinejsClientCore<Model> {
34-
constructor(
35-
params: Params,
36-
public backendParams: BackendParams,
37-
) {
38-
super(params);
39-
if (
40-
typeof backendParams?.fetch !== 'function' &&
41-
typeof fetch !== 'function'
42-
) {
43-
throw new Error(
44-
'No fetch implementation provided and native one not available',
45-
);
46-
}
47-
}
48-
4924
async _request({
5025
url,
5126
body,
@@ -61,10 +36,7 @@ export default class PineFetch<
6136
? JSON.stringify(body)
6237
: body;
6338

64-
// Assign to a variable first, otherwise browser fetch errors in case the context is different.
65-
const fetchImplementation = this.backendParams?.fetch ?? fetch;
66-
67-
const response = await fetchImplementation(url, {
39+
const response = await fetch(url, {
6840
...options,
6941
body: normalizedBody,
7042
});
@@ -78,6 +50,6 @@ export default class PineFetch<
7850
}
7951
throw new RequestError(responseBody, response.status, response.headers);
8052
}
81-
return response.json();
53+
return await response.json();
8254
}
8355
}

tests/setup.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
1-
import fetchPonyfillFactory from 'fetch-ponyfill';
21
import PineFetch from '../build/index.js';
32

43
const API_BASE_URL = 'https://api.balena-cloud.com/';
54
const API_VERSION = 'v5/';
65

7-
const IS_BROWSER = typeof window !== 'undefined';
8-
9-
// While testing on a browser, rely on the library to use the native `fetch`.
10-
const backendParams = IS_BROWSER
11-
? {}
12-
: { fetch: fetchPonyfillFactory({ Promise }).fetch };
13-
146
export interface TestSuiteContext extends Mocha.Context {
157
// So that TS lets us type the `this` of `it` calls.
168
pineClient?: PineFetch;
179
}
1810

1911
export const givenAPineClient = function (beforeFn: Mocha.HookFunction) {
2012
beforeFn(function (this: TestSuiteContext) {
21-
this.pineClient = new PineFetch(
22-
{ apiPrefix: API_BASE_URL + API_VERSION },
23-
backendParams,
24-
);
13+
this.pineClient = new PineFetch({ apiPrefix: API_BASE_URL + API_VERSION });
2514
});
2615
};

0 commit comments

Comments
 (0)