Skip to content

Commit

Permalink
chore: changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryAnansky committed Jan 20, 2025
1 parent 6fb553c commit 120bb18
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
53 changes: 26 additions & 27 deletions packages/cli/src/cms/api/__tests__/api.client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,17 @@ describe('ApiClient', () => {

const result = await apiClient.remotes.push(testOrg, testProject, pushPayload, filesMock);

expect([...passedFormData.entries()]).toEqual([...formData.entries()]);
expect(fetch).toHaveBeenCalledWith(
`${testDomain}/api/orgs/${testOrg}/projects/${testProject}/pushes`,
expect.objectContaining({
method: 'POST',
headers: {
Authorization: `Bearer ${testToken}`,
'user-agent': expectedUserAgent,
},
})
);
expect(passedFormData).toEqual(formData);
expect(result).toEqual(responseMock);
});

Expand Down Expand Up @@ -355,10 +365,9 @@ describe('ApiClient', () => {
mockFetchResponse({
ok: true,
json: jest.fn().mockResolvedValue(responseBody),
headers: {
get: (name: string) =>
name.toLowerCase() === 'sunset' ? sunsetDate.toISOString() : null,
},
headers: new Headers({
Sunset: sunsetDate.toISOString(),
}),
});

await requestFn();
Expand All @@ -381,10 +390,9 @@ describe('ApiClient', () => {
mockFetchResponse({
ok: true,
json: jest.fn().mockResolvedValue(responseBody),
headers: {
get: (name: string) =>
name.toLowerCase() === 'sunset' ? sunsetDate.toISOString() : null,
},
headers: new Headers({
Sunset: sunsetDate.toISOString(),
}),
});

await requestFn();
Expand All @@ -404,38 +412,29 @@ describe('ApiClient', () => {
mockFetchResponse({
ok: true,
json: jest.fn().mockResolvedValue(upsertRemoteMock.responseBody),
headers: {
get: (name: string) =>
name.toLowerCase() === 'sunset'
? new Date('2024-08-06T12:30:32.456Z').toISOString()
: null,
},
headers: new Headers({
Sunset: new Date('2024-08-06T12:30:32.456Z').toISOString(),
}),
});

await upsertRemoteMock.requestFn();

mockFetchResponse({
ok: true,
json: jest.fn().mockResolvedValue(getDefaultBranchMock.responseBody),
headers: {
get: (name: string) =>
name.toLowerCase() === 'sunset'
? new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString()
: null,
},
headers: new Headers({
Sunset: new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(),
}),
});

await getDefaultBranchMock.requestFn();

mockFetchResponse({
ok: true,
json: jest.fn().mockResolvedValue(pushMock.responseBody),
headers: {
get: (name: string) =>
name.toLowerCase() === 'sunset'
? new Date('2024-08-06T12:30:32.456Z').toISOString()
: null,
},
headers: new Headers({
Sunset: new Date('2024-08-06T12:30:32.456Z').toISOString(),
}),
});

await pushMock.requestFn();
Expand Down
10 changes: 7 additions & 3 deletions packages/cli/src/commands/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type { Config, BundleOutputFormat, Region } from '@redocly/openapi-core';
import type { CommandArgs } from '../wrapper';
import type { VerifyConfigOptions } from '../types';
import type { Readable } from 'node:stream';
import type { Agent } from 'node:http';

const DEFAULT_VERSION = 'latest';

Expand Down Expand Up @@ -446,19 +447,22 @@ async function uploadFileToS3(url: string, filePathOrBuffer: string | Buffer) {
const readStream =
typeof filePathOrBuffer === 'string' ? fs.createReadStream(filePathOrBuffer) : filePathOrBuffer;

const requestOptions = {
type NodeFetchRequestInit = RequestInit & {
dispatcher?: Agent;
};

const requestOptions: NodeFetchRequestInit = {
method: 'PUT',
headers: {
'Content-Length': fileSizeInBytes.toString(),
},
body: Buffer.isBuffer(readStream)
? new Blob([readStream])
: new Blob([await streamToBuffer(readStream as Readable)]),
} as RequestInit;
};

const proxyAgent = getProxyAgent();
if (proxyAgent) {
// @ts-expect-error Node.js fetch has different type for agent
requestOptions.dispatcher = proxyAgent;
}

Expand Down
3 changes: 0 additions & 3 deletions packages/cli/src/utils/fetch-with-timeout.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { getProxyAgent } from '@redocly/openapi-core';

import type { Agent } from 'node:http';

export const DEFAULT_FETCH_TIMEOUT = 3000;

export type FetchWithTimeoutOptions = RequestInit & {
timeout?: number;
dispatcher?: Agent;
};

export default async (url: string, { timeout, ...options }: FetchWithTimeoutOptions = {}) => {
Expand Down

0 comments on commit 120bb18

Please sign in to comment.