-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate from node-fetch to native fetch
- Loading branch information
1 parent
085aaf2
commit 24ddb6d
Showing
16 changed files
with
283 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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'); | ||
}); | ||
}); |
Oops, something went wrong.