-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(presets): fetch presets from HTTP URLs (#27359)
- Loading branch information
Showing
5 changed files
with
158 additions
and
0 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
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,50 @@ | ||
import * as httpMock from '../../../../test/http-mock'; | ||
import { PRESET_DEP_NOT_FOUND, PRESET_INVALID_JSON } from '../util'; | ||
import * as http from '.'; | ||
|
||
const host = 'https://my.server/'; | ||
const filePath = '/test-preset.json'; | ||
const repo = 'https://my.server/test-preset.json'; | ||
|
||
describe('config/presets/http/index', () => { | ||
describe('getPreset()', () => { | ||
it('should return parsed JSON', async () => { | ||
httpMock.scope(host).get(filePath).reply(200, { foo: 'bar' }); | ||
|
||
expect(await http.getPreset({ repo })).toEqual({ foo: 'bar' }); | ||
}); | ||
|
||
it('should return parsed JSON5', async () => { | ||
httpMock | ||
.scope('https://my.server/') | ||
.get('/test-preset.json5') | ||
.reply(200, '{ foo: "bar" } // comment'); | ||
|
||
const repo = 'https://my.server/test-preset.json5'; | ||
|
||
expect(await http.getPreset({ repo })).toEqual({ foo: 'bar' }); | ||
}); | ||
|
||
it('throws if fails to parse', async () => { | ||
httpMock.scope(host).get(filePath).reply(200, 'not json'); | ||
|
||
await expect(http.getPreset({ repo })).rejects.toThrow( | ||
PRESET_INVALID_JSON, | ||
); | ||
}); | ||
|
||
it('throws if file not found', async () => { | ||
httpMock.scope(host).get(filePath).reply(404); | ||
|
||
await expect(http.getPreset({ repo })).rejects.toThrow( | ||
PRESET_DEP_NOT_FOUND, | ||
); | ||
}); | ||
|
||
it('throws on malformed URL', async () => { | ||
await expect(http.getPreset({ repo: 'malformed!' })).rejects.toThrow( | ||
PRESET_DEP_NOT_FOUND, | ||
); | ||
}); | ||
}); | ||
}); |
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,35 @@ | ||
import { logger } from '../../../logger'; | ||
import { ExternalHostError } from '../../../types/errors/external-host-error'; | ||
import { Http } from '../../../util/http'; | ||
import type { HttpResponse } from '../../../util/http/types'; | ||
import { parseUrl } from '../../../util/url'; | ||
import type { Preset, PresetConfig } from '../types'; | ||
import { PRESET_DEP_NOT_FOUND, parsePreset } from '../util'; | ||
|
||
const http = new Http('preset'); | ||
|
||
export async function getPreset({ | ||
repo: url, | ||
}: PresetConfig): Promise<Preset | null | undefined> { | ||
const parsedUrl = parseUrl(url); | ||
let response: HttpResponse; | ||
|
||
if (!parsedUrl) { | ||
logger.debug(`Preset URL ${url} is malformed`); | ||
throw new Error(PRESET_DEP_NOT_FOUND); | ||
} | ||
|
||
try { | ||
response = await http.get(url); | ||
} catch (err) { | ||
// istanbul ignore if: not testable with nock | ||
if (err instanceof ExternalHostError) { | ||
throw err; | ||
} | ||
|
||
logger.debug(`Preset file ${url} not found`); | ||
throw new Error(PRESET_DEP_NOT_FOUND); | ||
} | ||
|
||
return parsePreset(response.body, parsedUrl.pathname); | ||
} |
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