chore: support passing through token in fetchContents and fetchFile

Signed-off-by: blam <ben@blam.sh>

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2024-01-25 19:08:56 +01:00
parent d208877118
commit 75dd39d084
2 changed files with 48 additions and 4 deletions
@@ -124,6 +124,20 @@ describe('fetchContents helper', () => {
expect(fs.ensureDir).toHaveBeenCalledWith('foo');
expect(dirFunction).toHaveBeenCalledWith({ targetDir: 'foo' });
});
it('should pass through the token provided through to the URL reader', async () => {
await fetchContents({
...options,
outputPath: 'mydir/foo',
fetchUrl: 'https://github.com/backstage/foo',
token: 'mockToken',
});
expect(readTree).toHaveBeenCalledWith(
'https://github.com/backstage/foo',
expect.objectContaining({ token: 'mockToken' }),
);
});
});
describe('fetch file', () => {
@@ -211,5 +225,19 @@ describe('fetchContents helper', () => {
expect(fs.ensureDir).toHaveBeenCalledWith('mydir');
expect(fs.outputFile).toHaveBeenCalledWith('mydir/foo', 'test');
});
it('should pass through the token provided through to the URL reader', async () => {
await fetchFile({
...options,
outputPath: 'mydir/foo',
fetchUrl: 'https://github.com/backstage/foo',
token: 'mockToken',
});
expect(readUrl).toHaveBeenCalledWith(
'https://github.com/backstage/foo',
expect.objectContaining({ token: 'mockToken' }),
);
});
});
});
+20 -4
View File
@@ -32,8 +32,16 @@ export async function fetchContents(options: {
baseUrl?: string;
fetchUrl?: string;
outputPath: string;
token?: string;
}) {
const { reader, integrations, baseUrl, fetchUrl = '.', outputPath } = options;
const {
reader,
integrations,
baseUrl,
fetchUrl = '.',
outputPath,
token,
} = options;
const fetchUrlIsAbsolute = isFetchUrlAbsolute(fetchUrl);
@@ -45,7 +53,7 @@ export async function fetchContents(options: {
} else {
const readUrl = getReadUrl(fetchUrl, baseUrl, integrations);
const res = await reader.readTree(readUrl);
const res = await reader.readTree(readUrl, { token });
await fs.ensureDir(outputPath);
await res.dir({ targetDir: outputPath });
}
@@ -63,8 +71,16 @@ export async function fetchFile(options: {
baseUrl?: string;
fetchUrl?: string;
outputPath: string;
token?: string;
}) {
const { reader, integrations, baseUrl, fetchUrl = '.', outputPath } = options;
const {
reader,
integrations,
baseUrl,
fetchUrl = '.',
outputPath,
token,
} = options;
const fetchUrlIsAbsolute = isFetchUrlAbsolute(fetchUrl);
@@ -76,7 +92,7 @@ export async function fetchFile(options: {
} else {
const readUrl = getReadUrl(fetchUrl, baseUrl, integrations);
const res = await reader.readUrl(readUrl);
const res = await reader.readUrl(readUrl, { token });
await fs.ensureDir(path.dirname(outputPath));
const buffer = await res.buffer();
await fs.outputFile(outputPath, buffer.toString());