Skip to content
This repository was archived by the owner on Jul 4, 2024. It is now read-only.

Commit b97cd83

Browse files
committed
feat: add response type and auto parsing
1 parent 177fb2c commit b97cd83

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

__tests__/response-type.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { assertEquals } from '../mods/testing-assert.ts';
2+
import axiod from '../mod.ts';
3+
import { IAxiodResponse } from '../interfaces.ts';
4+
5+
Deno.test('Axiod request with ArrayBuffer as responseType', async () => {
6+
const { data } = await axiod.get<ArrayBuffer>(
7+
'https://picsum.photos/seed/picsum/200/300',
8+
{ responseType: 'arraybuffer' },
9+
);
10+
11+
assertEquals(data instanceof ArrayBuffer, true);
12+
});
13+
14+
Deno.test('Axiod request with Stream as responseType', async () => {
15+
const { data } = await axiod.get<ReadableStream>(
16+
'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerMeltdowns.mp4',
17+
{ responseType: 'stream' },
18+
);
19+
20+
assertEquals(data instanceof ReadableStream, true);
21+
});
22+
23+
Deno.test('Axiod request with Blob as responseType', async () => {
24+
const { data } = await axiod.get<Blob>(
25+
'https://picsum.photos/seed/picsum/200/300',
26+
{ responseType: 'blob' },
27+
);
28+
29+
assertEquals(data instanceof Blob, true);
30+
});

interfaces.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface IConfig {
2727
transformRequest?: Array<TransformRequest>;
2828
transformResponse?: Array<TransformResponse>;
2929
redirect?: RequestRedirect;
30+
responseType?: ResponseType;
3031
}
3132

3233
export interface IRequest extends IConfig {
@@ -84,3 +85,5 @@ export interface IAxiodInterceptors {
8485
request: IAxiodInterceptor<IAxiodRequestInterceptor, IAxiodRequestErrorInterceptor>;
8586
response: IAxiodInterceptor<IAxiodResponseInterceptor, IAxiodResponseErrorInterceptor>;
8687
}
88+
89+
export type ResponseType = 'arraybuffer' | 'blob' | 'json' | 'text' | 'stream';

mod.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ async function request<T = any>(this: typeof axiod, config: IRequest): Promise<I
126126
transformRequest,
127127
transformResponse,
128128
redirect,
129+
responseType = 'json',
129130
} = config;
130131

131132
// Url and Base url
@@ -260,9 +261,23 @@ async function request<T = any>(this: typeof axiod, config: IRequest): Promise<I
260261
// Data
261262
let _data: any = null;
262263

263-
// Try to convert to json
264+
// Try to auto parse data
264265
try {
265-
_data = await x.clone().json();
266+
const response = x.clone();
267+
268+
if (responseType === 'json') {
269+
_data = await response.json();
270+
} else if (responseType === 'text') {
271+
_data = await response.text();
272+
} else if (responseType === 'arraybuffer') {
273+
_data = await response.arrayBuffer();
274+
} else if (responseType === 'blob') {
275+
_data = await response.blob();
276+
} else if (responseType === 'stream') {
277+
_data = (await response.blob()).stream();
278+
} else {
279+
_data = await response.text();
280+
}
266281
} catch (ex) {
267282
_data = await x.clone().text();
268283
}
@@ -295,6 +310,7 @@ async function request<T = any>(this: typeof axiod, config: IRequest): Promise<I
295310
auth,
296311
paramsSerializer,
297312
redirect,
313+
responseType,
298314
};
299315

300316
// Validate the status code

0 commit comments

Comments
 (0)