Skip to content

Commit 3775bf3

Browse files
authored
fix: using the environment's global fetch API instead of undici. (#63)
1 parent 779ec8a commit 3775bf3

10 files changed

+22
-24
lines changed

.changeset/metal-games-beam.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@litehex/node-vault': major
3+
---
4+
5+
Fix: Using the environment's global `fetch` API instead of `undici`.
6+
7+
This change makes this library more compatible with JavaScript runtimes that do not support `Node.js` modules. If you want to use `undici` again, use the [Custom Fetcher](https://github.com/shahradelahi/node-vault/wiki/Usage#custom-fetcher) feature.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
"packageManager": "pnpm@8.15.7",
3535
"dependencies": {
3636
"p-safe": "^1.0.0",
37-
"undici": "^6.19.2",
3837
"zod": "^3.23.8",
3938
"zod-request": "^0.2.2"
4039
},
@@ -56,7 +55,8 @@
5655
"tsd": "^0.31.1",
5756
"tsup": "^8.1.0",
5857
"tsx": "^4.15.7",
59-
"typescript": "^5.5.2"
58+
"typescript": "^5.5.2",
59+
"undici": "^6.19.8"
6060
},
6161
"type": "module",
6262
"engines": {

pnpm-lock.yaml

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
import { fetch } from 'undici';
2-
import { setGlobalFetch } from 'zod-request';
3-
4-
setGlobalFetch(fetch);
5-
6-
// -------------------------------
7-
81
export { Client } from '@/lib/client';
92
export { generateCommand } from '@/utils/generate-command';
103

src/lib/client.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { RequestInit } from 'undici';
21
import { z } from 'zod';
32

43
import { Aws } from '@/engine/aws';

src/typings.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
/* eslint-disable @typescript-eslint/ban-types */
22

33
import { SafeReturn } from 'p-safe';
4-
import { RequestInit, Response } from 'undici';
54
import type { z } from 'zod';
65
import type { RequestSchema as ZodRequestSchema } from 'zod-request';
76

87
import { VaultError } from '@/errors';
98
import { ClientOptionsSchema } from '@/schema';
109

1110
export type ClientOptions = z.infer<typeof ClientOptionsSchema> & {
12-
request?: Partial<RequestInit>;
11+
request?: Partial<RequestInit & Record<string, unknown>>;
1312
fetcher?: Fetcher;
1413
};
1514

@@ -20,7 +19,7 @@ export type RequestSchema = Omit<ZodRequestSchema, 'path' | 'body'> & {
2019

2120
export type Fetcher = (input: any, init: any) => Promise<any>;
2221

23-
export interface ExtendedRequestInit extends RequestInit {
22+
export interface ExtendedRequestInit extends RequestInit, Record<string, unknown> {
2423
strictSchema?: boolean;
2524
}
2625

src/utils/generate-command.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import omit from 'lodash/omit';
22
import pick from 'lodash/pick';
33
import { SafeReturn, trySafe } from 'p-safe';
4-
import { fetch, type RequestInit } from 'undici';
54
import { z } from 'zod';
65
import { generateRequest, ZodRequestInit, ZodResponse } from 'zod-request';
76

tests/integration.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from 'chai';
22
import { expectType } from 'tsd';
3-
import { ProxyAgent } from 'undici';
3+
import { fetch, ProxyAgent } from 'undici';
44
import { z } from 'zod';
55

66
import { Client, generateCommand, VaultError } from '@/index';
@@ -129,6 +129,7 @@ describe('node-vault', () => {
129129
expect(url).to.be.instanceof(URL);
130130
expect(url.toString()).to.equal('http://127.0.0.1:8200/v1/secret-path/test');
131131
used = true;
132+
// @ts-expect-error Init type has some missing properties
132133
return fetch(url, init);
133134
};
134135

@@ -181,6 +182,7 @@ describe('node-vault', () => {
181182
}
182183

183184
const agent = new ProxyAgent(HTTP_PROXY);
185+
vc.fetcher = fetch;
184186

185187
const status = await vc.sealStatus(undefined, { dispatcher: agent });
186188

tests/utils.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { execSync } from 'node:child_process';
22
import { accessSync } from 'node:fs';
33
import { resolve } from 'node:path';
4-
import { expect } from 'chai';
54

65
import { Client } from '@/index';
76

@@ -25,7 +24,7 @@ export async function createInstance(unsealed: boolean = true): Promise<{
2524
secret_shares: 1,
2625
secret_threshold: 1
2726
});
28-
expect(error, error?.message).be.be.undefined;
27+
if (error) throw error;
2928

3029
const { keys, root_token } = data!;
3130

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"lib": ["ESNext"],
3+
"lib": ["ESNext", "DOM", "DOM.Iterable"],
44
"module": "esnext",
55
"target": "esnext",
66
"moduleResolution": "node",

0 commit comments

Comments
 (0)