Skip to content

Commit

Permalink
fix: using the environment's global fetch API instead of undici. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shahradelahi authored Oct 8, 2024
1 parent 779ec8a commit 3775bf3
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 24 deletions.
7 changes: 7 additions & 0 deletions .changeset/metal-games-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@litehex/node-vault': major
---

Fix: Using the environment's global `fetch` API instead of `undici`.

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.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"packageManager": "[email protected]",
"dependencies": {
"p-safe": "^1.0.0",
"undici": "^6.19.2",
"zod": "^3.23.8",
"zod-request": "^0.2.2"
},
Expand All @@ -56,7 +55,8 @@
"tsd": "^0.31.1",
"tsup": "^8.1.0",
"tsx": "^4.15.7",
"typescript": "^5.5.2"
"typescript": "^5.5.2",
"undici": "^6.19.8"
},
"type": "module",
"engines": {
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

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

7 changes: 0 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import { fetch } from 'undici';
import { setGlobalFetch } from 'zod-request';

setGlobalFetch(fetch);

// -------------------------------

export { Client } from '@/lib/client';
export { generateCommand } from '@/utils/generate-command';

Expand Down
1 change: 0 additions & 1 deletion src/lib/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { RequestInit } from 'undici';
import { z } from 'zod';

import { Aws } from '@/engine/aws';
Expand Down
5 changes: 2 additions & 3 deletions src/typings.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/* eslint-disable @typescript-eslint/ban-types */

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

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

export type ClientOptions = z.infer<typeof ClientOptionsSchema> & {
request?: Partial<RequestInit>;
request?: Partial<RequestInit & Record<string, unknown>>;
fetcher?: Fetcher;
};

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

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

export interface ExtendedRequestInit extends RequestInit {
export interface ExtendedRequestInit extends RequestInit, Record<string, unknown> {
strictSchema?: boolean;
}

Expand Down
1 change: 0 additions & 1 deletion src/utils/generate-command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import omit from 'lodash/omit';
import pick from 'lodash/pick';
import { SafeReturn, trySafe } from 'p-safe';
import { fetch, type RequestInit } from 'undici';
import { z } from 'zod';
import { generateRequest, ZodRequestInit, ZodResponse } from 'zod-request';

Expand Down
4 changes: 3 additions & 1 deletion tests/integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import { expectType } from 'tsd';
import { ProxyAgent } from 'undici';
import { fetch, ProxyAgent } from 'undici';
import { z } from 'zod';

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

Expand Down Expand Up @@ -181,6 +182,7 @@ describe('node-vault', () => {
}

const agent = new ProxyAgent(HTTP_PROXY);
vc.fetcher = fetch;

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

Expand Down
3 changes: 1 addition & 2 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { execSync } from 'node:child_process';
import { accessSync } from 'node:fs';
import { resolve } from 'node:path';
import { expect } from 'chai';

import { Client } from '@/index';

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

const { keys, root_token } = data!;

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"module": "esnext",
"target": "esnext",
"moduleResolution": "node",
Expand Down

0 comments on commit 3775bf3

Please sign in to comment.