Skip to content

Commit

Permalink
feat: migrate from node-fetch to native fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryAnansky committed Jan 17, 2025
1 parent 085aaf2 commit 24ddb6d
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 194 deletions.
6 changes: 6 additions & 0 deletions .changeset/warm-tips-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@redocly/openapi-core": minor
"@redocly/cli": minor
---

Switched to using native `fetch` API instead of `node-fetch` dependency, improving performance and reducing bundle size.
99 changes: 21 additions & 78 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"private": true,
"engines": {
"node": ">=15.0.0",
"node": ">=18.17.0",
"npm": ">=7.0.0"
},
"engineStrict": true,
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"redocly": "bin/cli.js"
},
"engines": {
"node": ">=14.19.0",
"node": ">=18.17.0",
"npm": ">=7.0.0"
},
"engineStrict": true,
Expand Down Expand Up @@ -46,7 +46,6 @@
"glob": "^7.1.6",
"handlebars": "^4.7.6",
"mobx": "^6.0.4",
"node-fetch": "^2.6.1",
"pluralize": "^8.0.0",
"react": "^17.0.0 || ^18.2.0",
"react-dom": "^17.0.0 || ^18.2.0",
Expand Down
94 changes: 78 additions & 16 deletions packages/cli/src/__tests__/commands/push-region.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,90 @@ import { getMergedConfig } from '@redocly/openapi-core';
import { handlePush } from '../../commands/push';
import { promptClientToken } from '../../commands/login';
import { ConfigFixture } from '../fixtures/config';
import { Readable } from 'node:stream';

jest.mock('fs');
jest.mock('node-fetch', () => ({
default: jest.fn(() => ({
ok: true,
json: jest.fn().mockResolvedValue({}),
// Mock fs operations
jest.mock('fs', () => ({
...jest.requireActual('fs'),
createReadStream: () => {
const readable = new Readable();
readable.push('test data');
readable.push(null);
return readable;
},
statSync: () => ({ size: 9 }),
readFileSync: () => Buffer.from('test data'),
existsSync: () => false,
readdirSync: () => [],
}));

// Mock OpenAPI core
jest.mock('@redocly/openapi-core', () => ({
...jest.requireActual('@redocly/openapi-core'),
getMergedConfig: jest.fn().mockReturnValue({
styleguide: {
skipDecorators: jest.fn(),
extendPaths: [],
pluginPaths: [],
},
}),
bundle: jest.fn().mockResolvedValue({
bundle: { parsed: {} },
problems: {
totals: { errors: 0, warnings: 0 },
items: [],
[Symbol.iterator]: function* () {
yield* this.items;
},
},
}),
RedoclyClient: jest.fn().mockImplementation((region?: string) => ({
domain: region === 'eu' ? 'eu.redocly.com' : 'redoc.ly',
isAuthorizedWithRedoclyByRegion: jest.fn().mockResolvedValue(false),
login: jest.fn().mockResolvedValue({}),
registryApi: {
prepareFileUpload: jest.fn().mockResolvedValue({
signedUploadUrl: 'https://example.com',
filePath: 'test.yaml',
}),
pushApi: jest.fn().mockResolvedValue({}),
},
})),
}));
jest.mock('@redocly/openapi-core');

jest.mock('../../commands/login');
jest.mock('../../utils/miscellaneous');

(getMergedConfig as jest.Mock).mockImplementation((config) => config);
// Mock global fetch
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve({}),
headers: new Headers(),
statusText: 'OK',
redirected: false,
type: 'default',
url: '',
clone: () => ({} as Response),
body: new ReadableStream(),
bodyUsed: false,
arrayBuffer: async () => new ArrayBuffer(0),
blob: async () => new Blob(),
formData: async () => new FormData(),
text: async () => '',
} as Response)
);

const mockPromptClientToken = promptClientToken as jest.MockedFunction<typeof promptClientToken>;

describe('push-with-region', () => {
const redoclyClient = require('@redocly/openapi-core').__redoclyClient;
redoclyClient.isAuthorizedWithRedoclyByRegion = jest.fn().mockResolvedValue(false);

beforeAll(() => {
beforeEach(() => {
jest.clearAllMocks();
jest.spyOn(process.stdout, 'write').mockImplementation(() => true);
});

it('should call login with default domain when region is US', async () => {
redoclyClient.domain = 'redoc.ly';
await handlePush({
argv: {
upsert: true,
Expand All @@ -38,23 +96,27 @@ describe('push-with-region', () => {
config: ConfigFixture as any,
version: 'cli-version',
});

expect(mockPromptClientToken).toBeCalledTimes(1);
expect(mockPromptClientToken).toHaveBeenCalledWith(redoclyClient.domain);
expect(mockPromptClientToken).toHaveBeenCalledWith('redoc.ly');
});

it('should call login with EU domain when region is EU', async () => {
redoclyClient.domain = 'eu.redocly.com';
// Update config for EU region
const euConfig = { ...ConfigFixture, region: 'eu' };

await handlePush({
argv: {
upsert: true,
api: 'spec.json',
destination: '@org/[email protected]',
branchName: 'test',
},
config: ConfigFixture as any,
config: euConfig as any,
version: 'cli-version',
});

expect(mockPromptClientToken).toBeCalledTimes(1);
expect(mockPromptClientToken).toHaveBeenCalledWith(redoclyClient.domain);
expect(mockPromptClientToken).toHaveBeenCalledWith('eu.redocly.com');
});
});
Loading

0 comments on commit 24ddb6d

Please sign in to comment.