diff --git a/lib/util/http/index.spec.ts b/lib/util/http/index.spec.ts index 8eda7e35e7e140c71d323580ab9fc9f8c27fb66b..36af769a86d7da8cb78ac435f25a822a35bf3194 100644 --- a/lib/util/http/index.spec.ts +++ b/lib/util/http/index.spec.ts @@ -367,14 +367,37 @@ describe('util/http/index', () => { }); }); - describe('getYaml', () => { + describe('getYamlUnchecked', () => { it('parses yaml response without schema', async () => { httpMock.scope(baseUrl).get('/').reply(200, 'x: 2\ny: 2'); - const res = await http.getYaml('http://renovate.com'); + const res = await http.getYamlUnchecked('http://renovate.com'); + expect(res.body).toEqual({ x: 2, y: 2 }); + }); + + it('parses yaml with options', async () => { + httpMock + .scope(baseUrl) + .get('/') + .matchHeader('custom', 'header') + .reply(200, 'x: 2\ny: 2'); + + const res = await http.getYamlUnchecked('http://renovate.com', { + headers: { custom: 'header' }, + }); expect(res.body).toEqual({ x: 2, y: 2 }); }); + it('throws on invalid yaml', async () => { + httpMock.scope(baseUrl).get('/').reply(200, '!@#$%^'); + + await expect( + http.getYamlUnchecked('http://renovate.com'), + ).rejects.toThrow('Failed to parse YAML file'); + }); + }); + + describe('getYaml', () => { it('parses yaml with schema validation', async () => { httpMock.scope(baseUrl).get('/').reply(200, 'x: 2\ny: 2'); @@ -397,12 +420,6 @@ describe('util/http/index', () => { expect(res.body).toBe('2 + 2 = 4'); }); - it('throws on invalid yaml', async () => { - httpMock.scope(baseUrl).get('/').reply(200, '!@#$%^'); - - await expect(http.getYaml('http://renovate.com')).rejects.toThrow(); - }); - it('throws on schema validation failure', async () => { httpMock.scope(baseUrl).get('/').reply(200, 'foo: bar'); @@ -410,6 +427,14 @@ describe('util/http/index', () => { http.getYaml('http://renovate.com', SomeSchema), ).rejects.toThrow(z.ZodError); }); + + it('throws on invalid yaml', async () => { + httpMock.scope(baseUrl).get('/').reply(200, '!@#$%^'); + + await expect( + http.getYaml('http://renovate.com', SomeSchema), + ).rejects.toThrow('Failed to parse YAML file'); + }); }); describe('getYamlSafe', () => { diff --git a/lib/util/http/index.ts b/lib/util/http/index.ts index b0968a15bb950f45f0c87a314fbd4d55fb1ec1f2..c99bfbaad9478b6554ced94a6f7c8413c9ef1e34 100644 --- a/lib/util/http/index.ts +++ b/lib/util/http/index.ts @@ -329,37 +329,48 @@ export class Http<Opts extends HttpOptions = HttpOptions> { }); } - async getYaml<ResT>(url: string, options?: Opts): Promise<HttpResponse<ResT>>; - async getYaml<ResT, Schema extends ZodType<ResT> = ZodType<ResT>>( + /** + * @deprecated use `getYaml` instead + */ + async getYamlUnchecked<ResT>( + url: string, + options?: Opts, + ): Promise<HttpResponse<ResT>> { + const res = await this.get(url, options); + const body = parseSingleYaml<ResT>(res.body); + return { ...res, body }; + } + + async getYaml<Schema extends ZodType<any, any, any>>( url: string, schema: Schema, ): Promise<HttpResponse<Infer<Schema>>>; - async getYaml<ResT, Schema extends ZodType<ResT> = ZodType<ResT>>( + async getYaml<Schema extends ZodType<any, any, any>>( url: string, options: Opts, schema: Schema, ): Promise<HttpResponse<Infer<Schema>>>; - async getYaml<ResT = unknown, Schema extends ZodType<ResT> = ZodType<ResT>>( + async getYaml<Schema extends ZodType<any, any, any>>( arg1: string, arg2?: Opts | Schema, arg3?: Schema, - ): Promise<HttpResponse<ResT>> { - const { url, httpOptions, schema } = this.resolveArgs<ResT>( - arg1, - arg2, - arg3, - ); + ): Promise<HttpResponse<Infer<Schema>>> { + const url = arg1; + let schema: Schema; + let httpOptions: Opts | undefined; + if (arg3) { + schema = arg3; + httpOptions = arg2 as Opts; + } else { + schema = arg2 as Schema; + } + const opts: InternalHttpOptions = { ...httpOptions, method: 'get', }; const res = await this.get(url, opts); - if (!schema) { - const body = parseSingleYaml<ResT>(res.body); - return { ...res, body }; - } - const body = await schema.parseAsync(parseSingleYaml(res.body)); return { ...res, body }; } @@ -396,9 +407,9 @@ export class Http<Opts extends HttpOptions = HttpOptions> { let res: AsyncResult<HttpResponse<ResT>, SafeJsonError>; if (httpOptions) { - res = Result.wrap(this.getYaml<ResT>(url, httpOptions, schema)); + res = Result.wrap(this.getYaml(url, httpOptions, schema)); } else { - res = Result.wrap(this.getYaml<ResT>(url, schema)); + res = Result.wrap(this.getYaml(url, schema)); } return res.transform((response) => Result.ok(response.body));