|
| 1 | +/* eslint-disable @typescript-eslint/no-unnecessary-type-arguments */ |
1 | 2 | import type {Buffer} from 'node:buffer'; |
2 | 3 | import {expectTypeOf} from 'expect-type'; |
3 | 4 | import got, {type CancelableRequest, type Response} from '../source/index.js'; |
@@ -74,3 +75,45 @@ expectTypeOf(gotBodyOnly('https://example.com', {responseType: 'buffer'})).toEqu |
74 | 75 | expectTypeOf(gotBodyOnly('https://example.com', {resolveBodyOnly: false})).toEqualTypeOf<CancelableRequest<Response<string>>>(); |
75 | 76 | expectTypeOf(gotBodyOnly<{test: 'test'}>('https://example.com', {resolveBodyOnly: false})).toEqualTypeOf<CancelableRequest<Response<{test: 'test'}>>>(); |
76 | 77 | expectTypeOf(gotBodyOnly('https://example.com', {responseType: 'buffer', resolveBodyOnly: false})).toEqualTypeOf<CancelableRequest<Response<Buffer>>>(); |
| 78 | + |
| 79 | +// |
| 80 | +// Test got.extend() with responseType correctly infers types (fix for issue #2427) |
| 81 | +// |
| 82 | +const gotJson = got.extend({responseType: 'json'}); |
| 83 | +const gotJsonBodyOnly = got.extend({responseType: 'json', resolveBodyOnly: true}); |
| 84 | +const gotBuffer = got.extend({responseType: 'buffer'}); |
| 85 | +const gotBufferBodyOnly = got.extend({responseType: 'buffer', resolveBodyOnly: true}); |
| 86 | +const gotText = got.extend({responseType: 'text'}); |
| 87 | +const gotTextBodyOnly = got.extend({responseType: 'text', resolveBodyOnly: true}); |
| 88 | + |
| 89 | +// Test URL-first syntax without options - should infer correct type based on extended responseType |
| 90 | +expectTypeOf(gotJson('https://example.com')).toEqualTypeOf<CancelableRequest<Response<unknown>>>(); |
| 91 | +expectTypeOf(gotJsonBodyOnly('https://example.com')).toEqualTypeOf<CancelableRequest<unknown>>(); |
| 92 | +expectTypeOf(gotBuffer('https://example.com')).toEqualTypeOf<CancelableRequest<Response<Buffer>>>(); |
| 93 | +expectTypeOf(gotBufferBodyOnly('https://example.com')).toEqualTypeOf<CancelableRequest<Buffer>>(); |
| 94 | +expectTypeOf(gotText('https://example.com')).toEqualTypeOf<CancelableRequest<Response<string>>>(); |
| 95 | +expectTypeOf(gotTextBodyOnly('https://example.com')).toEqualTypeOf<CancelableRequest<string>>(); |
| 96 | + |
| 97 | +// Test options-only syntax with URL in options - should infer correct type based on extended responseType |
| 98 | +expectTypeOf(gotJson({url: 'https://example.com'})).toEqualTypeOf<CancelableRequest<Response<unknown>>>(); |
| 99 | +expectTypeOf(gotJsonBodyOnly({url: 'https://example.com'})).toEqualTypeOf<CancelableRequest<unknown>>(); |
| 100 | +expectTypeOf(gotBuffer({url: 'https://example.com'})).toEqualTypeOf<CancelableRequest<Response<Buffer>>>(); |
| 101 | +expectTypeOf(gotBufferBodyOnly({url: 'https://example.com'})).toEqualTypeOf<CancelableRequest<Buffer>>(); |
| 102 | +expectTypeOf(gotText({url: 'https://example.com'})).toEqualTypeOf<CancelableRequest<Response<string>>>(); |
| 103 | +expectTypeOf(gotTextBodyOnly({url: 'https://example.com'})).toEqualTypeOf<CancelableRequest<string>>(); |
| 104 | + |
| 105 | +// Test that generic type parameter still works with extended responseType |
| 106 | +expectTypeOf(gotJson<{data: string}>('https://example.com')).toEqualTypeOf<CancelableRequest<Response<{data: string}>>>(); |
| 107 | +expectTypeOf(gotJsonBodyOnly<{data: string}>('https://example.com')).toEqualTypeOf<CancelableRequest<{data: string}>>(); |
| 108 | + |
| 109 | +// Test that explicit responseType in call overrides extended responseType |
| 110 | +expectTypeOf(gotJson('https://example.com', {responseType: 'buffer'})).toEqualTypeOf<CancelableRequest<Response<Buffer>>>(); |
| 111 | +expectTypeOf(gotJson('https://example.com', {responseType: 'text'})).toEqualTypeOf<CancelableRequest<Response<string>>>(); |
| 112 | +expectTypeOf(gotBuffer('https://example.com', {responseType: 'json'})).toEqualTypeOf<CancelableRequest<Response<unknown>>>(); |
| 113 | +expectTypeOf(gotBuffer('https://example.com', {responseType: 'text'})).toEqualTypeOf<CancelableRequest<Response<string>>>(); |
| 114 | + |
| 115 | +// Test that resolveBodyOnly can be overridden with explicit responseType |
| 116 | +expectTypeOf(gotJson('https://example.com', {responseType: 'json', resolveBodyOnly: true})).toEqualTypeOf<CancelableRequest<unknown>>(); |
| 117 | +expectTypeOf(gotJsonBodyOnly('https://example.com', {responseType: 'json', resolveBodyOnly: false})).toEqualTypeOf<CancelableRequest<Response<unknown>>>(); |
| 118 | +expectTypeOf(gotBuffer('https://example.com', {responseType: 'buffer', resolveBodyOnly: true})).toEqualTypeOf<CancelableRequest<Buffer>>(); |
| 119 | +expectTypeOf(gotBufferBodyOnly('https://example.com', {responseType: 'buffer', resolveBodyOnly: false})).toEqualTypeOf<CancelableRequest<Response<Buffer>>>(); |
0 commit comments