vovk 3.0.0-draft.331 → 3.0.0-draft.332
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/client/fetcher.d.ts +5 -3
- package/cjs/client/fetcher.js +74 -68
- package/mjs/client/fetcher.d.ts +5 -3
- package/mjs/client/fetcher.js +74 -68
- package/package.json +1 -1
package/cjs/client/fetcher.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { VovkDefaultFetcherOptions, VovkClientFetcher } from './types';
|
|
2
2
|
import { KnownAny } from '../types';
|
|
3
|
+
import { HttpException } from '../HttpException';
|
|
3
4
|
export declare const DEFAULT_ERROR_MESSAGE = "Unknown error at default fetcher";
|
|
4
|
-
export declare function createFetcher<T>({ prepareRequestInit, transformResponse, }?: {
|
|
5
|
-
prepareRequestInit?: (init: RequestInit, options: VovkDefaultFetcherOptions<T>) => RequestInit | Promise<RequestInit
|
|
6
|
-
transformResponse?: (respData: KnownAny, response: Response, options: VovkDefaultFetcherOptions<T>, init: RequestInit) => unknown
|
|
5
|
+
export declare function createFetcher<T>({ prepareRequestInit, transformResponse, onError, }?: {
|
|
6
|
+
prepareRequestInit?: (init: RequestInit, options: VovkDefaultFetcherOptions<T>) => RequestInit | Promise<RequestInit>;
|
|
7
|
+
transformResponse?: (respData: KnownAny, response: Response, options: VovkDefaultFetcherOptions<T>, init: RequestInit) => unknown | Promise<unknown>;
|
|
8
|
+
onError?: (error: HttpException, response: Response | null) => void | Promise<void>;
|
|
7
9
|
}): VovkClientFetcher<VovkDefaultFetcherOptions<T>>;
|
|
8
10
|
export declare const fetcher: VovkClientFetcher<{
|
|
9
11
|
apiRoot?: string;
|
package/cjs/client/fetcher.js
CHANGED
|
@@ -5,88 +5,94 @@ exports.createFetcher = createFetcher;
|
|
|
5
5
|
const types_1 = require("../types");
|
|
6
6
|
const HttpException_1 = require("../HttpException");
|
|
7
7
|
exports.DEFAULT_ERROR_MESSAGE = 'Unknown error at default fetcher';
|
|
8
|
-
function createFetcher({ prepareRequestInit, transformResponse, } = {}) {
|
|
8
|
+
function createFetcher({ prepareRequestInit, transformResponse, onError, } = {}) {
|
|
9
9
|
// fetcher uses HttpException class to throw errors of fake HTTP status 0 if client-side error occurs
|
|
10
10
|
// For normal HTTP errors, it uses message and status code from the response of VovkErrorResponse type
|
|
11
11
|
const newFetcher = async ({ httpMethod, getEndpoint, validate, defaultHandler, defaultStreamHandler, schema }, options) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
let response = null;
|
|
13
|
+
try {
|
|
14
|
+
const { params, query, body, meta, apiRoot, disableClientValidation, init, interpretAs } = options;
|
|
15
|
+
const endpoint = getEndpoint({ apiRoot, params, query });
|
|
16
|
+
const unusedParams = Array.from(new URL(endpoint.startsWith('/') ? `http://localhost${endpoint}` : endpoint).pathname.matchAll(/\{([^}]+)\}/g)).map((m) => m[1]);
|
|
17
|
+
if (unusedParams.length) {
|
|
18
|
+
throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, `Unused params: ${unusedParams.join(', ')} in ${endpoint}`, {
|
|
19
|
+
body,
|
|
20
|
+
query,
|
|
21
|
+
params,
|
|
22
|
+
endpoint,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
if (!disableClientValidation) {
|
|
26
|
+
try {
|
|
27
|
+
await validate({ body, query, params, endpoint });
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
// if HttpException is thrown, rethrow it
|
|
31
|
+
if (e instanceof HttpException_1.HttpException)
|
|
32
|
+
throw e;
|
|
33
|
+
// otherwise, throw HttpException with status 0
|
|
34
|
+
throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, e.message ?? exports.DEFAULT_ERROR_MESSAGE, {
|
|
35
|
+
body,
|
|
36
|
+
query,
|
|
37
|
+
params,
|
|
38
|
+
endpoint,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
let requestInit = {
|
|
43
|
+
method: httpMethod,
|
|
44
|
+
...init,
|
|
45
|
+
headers: {
|
|
46
|
+
accept: 'application/jsonl, application/json',
|
|
47
|
+
...(body instanceof FormData ? {} : { 'content-type': 'application/json' }),
|
|
48
|
+
...(meta ? { 'x-meta': JSON.stringify(meta) } : {}),
|
|
49
|
+
...init?.headers,
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
if (body instanceof FormData) {
|
|
53
|
+
requestInit.body = body;
|
|
54
|
+
}
|
|
55
|
+
else if (body) {
|
|
56
|
+
requestInit.body = JSON.stringify(body);
|
|
57
|
+
}
|
|
58
|
+
requestInit = prepareRequestInit
|
|
59
|
+
? await prepareRequestInit(requestInit, options)
|
|
60
|
+
: requestInit;
|
|
61
|
+
const controller = new AbortController();
|
|
62
|
+
requestInit.signal = controller.signal;
|
|
24
63
|
try {
|
|
25
|
-
await
|
|
64
|
+
response = await fetch(endpoint, requestInit);
|
|
26
65
|
}
|
|
27
66
|
catch (e) {
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
throw e;
|
|
31
|
-
// otherwise, throw HttpException with status 0
|
|
32
|
-
throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, e.message ?? exports.DEFAULT_ERROR_MESSAGE, {
|
|
67
|
+
// handle network errors
|
|
68
|
+
throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, (e?.message ?? exports.DEFAULT_ERROR_MESSAGE) + ' ' + endpoint, {
|
|
33
69
|
body,
|
|
34
70
|
query,
|
|
35
71
|
params,
|
|
36
72
|
endpoint,
|
|
37
73
|
});
|
|
38
74
|
}
|
|
75
|
+
const contentType = interpretAs ?? response.headers.get('content-type');
|
|
76
|
+
let resp;
|
|
77
|
+
if (contentType?.startsWith('application/jsonl')) {
|
|
78
|
+
resp = defaultStreamHandler({ response, controller, schema });
|
|
79
|
+
}
|
|
80
|
+
else if (contentType?.startsWith('application/json')) {
|
|
81
|
+
resp = defaultHandler({ response, schema });
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
resp = response;
|
|
85
|
+
}
|
|
86
|
+
resp = await resp;
|
|
87
|
+
resp = transformResponse
|
|
88
|
+
? await transformResponse(resp, response, options, requestInit)
|
|
89
|
+
: resp;
|
|
90
|
+
return [resp, response];
|
|
39
91
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
headers: {
|
|
44
|
-
accept: 'application/jsonl, application/json',
|
|
45
|
-
...(body instanceof FormData ? {} : { 'content-type': 'application/json' }),
|
|
46
|
-
...(meta ? { 'x-meta': JSON.stringify(meta) } : {}),
|
|
47
|
-
...init?.headers,
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
if (body instanceof FormData) {
|
|
51
|
-
requestInit.body = body;
|
|
52
|
-
}
|
|
53
|
-
else if (body) {
|
|
54
|
-
requestInit.body = JSON.stringify(body);
|
|
55
|
-
}
|
|
56
|
-
requestInit = prepareRequestInit
|
|
57
|
-
? ((await prepareRequestInit(requestInit, options)) ?? requestInit)
|
|
58
|
-
: requestInit;
|
|
59
|
-
let response;
|
|
60
|
-
const controller = new AbortController();
|
|
61
|
-
requestInit.signal = controller.signal;
|
|
62
|
-
try {
|
|
63
|
-
response = await fetch(endpoint, requestInit);
|
|
64
|
-
}
|
|
65
|
-
catch (e) {
|
|
66
|
-
// handle network errors
|
|
67
|
-
throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, (e?.message ?? exports.DEFAULT_ERROR_MESSAGE) + ' ' + endpoint, {
|
|
68
|
-
body,
|
|
69
|
-
query,
|
|
70
|
-
params,
|
|
71
|
-
endpoint,
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
const contentType = interpretAs ?? response.headers.get('content-type');
|
|
75
|
-
let resp;
|
|
76
|
-
if (contentType?.startsWith('application/jsonl')) {
|
|
77
|
-
resp = defaultStreamHandler({ response, controller, schema });
|
|
78
|
-
}
|
|
79
|
-
else if (contentType?.startsWith('application/json')) {
|
|
80
|
-
resp = defaultHandler({ response, schema });
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
resp = response;
|
|
92
|
+
catch (error) {
|
|
93
|
+
await onError?.(error, response);
|
|
94
|
+
throw error;
|
|
84
95
|
}
|
|
85
|
-
resp = await resp;
|
|
86
|
-
resp = transformResponse
|
|
87
|
-
? await transformResponse(resp, response, options, requestInit)
|
|
88
|
-
: resp;
|
|
89
|
-
return [resp, response];
|
|
90
96
|
};
|
|
91
97
|
return newFetcher;
|
|
92
98
|
}
|
package/mjs/client/fetcher.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { VovkDefaultFetcherOptions, VovkClientFetcher } from './types';
|
|
2
2
|
import { KnownAny } from '../types';
|
|
3
|
+
import { HttpException } from '../HttpException';
|
|
3
4
|
export declare const DEFAULT_ERROR_MESSAGE = "Unknown error at default fetcher";
|
|
4
|
-
export declare function createFetcher<T>({ prepareRequestInit, transformResponse, }?: {
|
|
5
|
-
prepareRequestInit?: (init: RequestInit, options: VovkDefaultFetcherOptions<T>) => RequestInit | Promise<RequestInit
|
|
6
|
-
transformResponse?: (respData: KnownAny, response: Response, options: VovkDefaultFetcherOptions<T>, init: RequestInit) => unknown
|
|
5
|
+
export declare function createFetcher<T>({ prepareRequestInit, transformResponse, onError, }?: {
|
|
6
|
+
prepareRequestInit?: (init: RequestInit, options: VovkDefaultFetcherOptions<T>) => RequestInit | Promise<RequestInit>;
|
|
7
|
+
transformResponse?: (respData: KnownAny, response: Response, options: VovkDefaultFetcherOptions<T>, init: RequestInit) => unknown | Promise<unknown>;
|
|
8
|
+
onError?: (error: HttpException, response: Response | null) => void | Promise<void>;
|
|
7
9
|
}): VovkClientFetcher<VovkDefaultFetcherOptions<T>>;
|
|
8
10
|
export declare const fetcher: VovkClientFetcher<{
|
|
9
11
|
apiRoot?: string;
|
package/mjs/client/fetcher.js
CHANGED
|
@@ -5,88 +5,94 @@ exports.createFetcher = createFetcher;
|
|
|
5
5
|
const types_1 = require("../types");
|
|
6
6
|
const HttpException_1 = require("../HttpException");
|
|
7
7
|
exports.DEFAULT_ERROR_MESSAGE = 'Unknown error at default fetcher';
|
|
8
|
-
function createFetcher({ prepareRequestInit, transformResponse, } = {}) {
|
|
8
|
+
function createFetcher({ prepareRequestInit, transformResponse, onError, } = {}) {
|
|
9
9
|
// fetcher uses HttpException class to throw errors of fake HTTP status 0 if client-side error occurs
|
|
10
10
|
// For normal HTTP errors, it uses message and status code from the response of VovkErrorResponse type
|
|
11
11
|
const newFetcher = async ({ httpMethod, getEndpoint, validate, defaultHandler, defaultStreamHandler, schema }, options) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
let response = null;
|
|
13
|
+
try {
|
|
14
|
+
const { params, query, body, meta, apiRoot, disableClientValidation, init, interpretAs } = options;
|
|
15
|
+
const endpoint = getEndpoint({ apiRoot, params, query });
|
|
16
|
+
const unusedParams = Array.from(new URL(endpoint.startsWith('/') ? `http://localhost${endpoint}` : endpoint).pathname.matchAll(/\{([^}]+)\}/g)).map((m) => m[1]);
|
|
17
|
+
if (unusedParams.length) {
|
|
18
|
+
throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, `Unused params: ${unusedParams.join(', ')} in ${endpoint}`, {
|
|
19
|
+
body,
|
|
20
|
+
query,
|
|
21
|
+
params,
|
|
22
|
+
endpoint,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
if (!disableClientValidation) {
|
|
26
|
+
try {
|
|
27
|
+
await validate({ body, query, params, endpoint });
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
// if HttpException is thrown, rethrow it
|
|
31
|
+
if (e instanceof HttpException_1.HttpException)
|
|
32
|
+
throw e;
|
|
33
|
+
// otherwise, throw HttpException with status 0
|
|
34
|
+
throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, e.message ?? exports.DEFAULT_ERROR_MESSAGE, {
|
|
35
|
+
body,
|
|
36
|
+
query,
|
|
37
|
+
params,
|
|
38
|
+
endpoint,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
let requestInit = {
|
|
43
|
+
method: httpMethod,
|
|
44
|
+
...init,
|
|
45
|
+
headers: {
|
|
46
|
+
accept: 'application/jsonl, application/json',
|
|
47
|
+
...(body instanceof FormData ? {} : { 'content-type': 'application/json' }),
|
|
48
|
+
...(meta ? { 'x-meta': JSON.stringify(meta) } : {}),
|
|
49
|
+
...init?.headers,
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
if (body instanceof FormData) {
|
|
53
|
+
requestInit.body = body;
|
|
54
|
+
}
|
|
55
|
+
else if (body) {
|
|
56
|
+
requestInit.body = JSON.stringify(body);
|
|
57
|
+
}
|
|
58
|
+
requestInit = prepareRequestInit
|
|
59
|
+
? await prepareRequestInit(requestInit, options)
|
|
60
|
+
: requestInit;
|
|
61
|
+
const controller = new AbortController();
|
|
62
|
+
requestInit.signal = controller.signal;
|
|
24
63
|
try {
|
|
25
|
-
await
|
|
64
|
+
response = await fetch(endpoint, requestInit);
|
|
26
65
|
}
|
|
27
66
|
catch (e) {
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
throw e;
|
|
31
|
-
// otherwise, throw HttpException with status 0
|
|
32
|
-
throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, e.message ?? exports.DEFAULT_ERROR_MESSAGE, {
|
|
67
|
+
// handle network errors
|
|
68
|
+
throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, (e?.message ?? exports.DEFAULT_ERROR_MESSAGE) + ' ' + endpoint, {
|
|
33
69
|
body,
|
|
34
70
|
query,
|
|
35
71
|
params,
|
|
36
72
|
endpoint,
|
|
37
73
|
});
|
|
38
74
|
}
|
|
75
|
+
const contentType = interpretAs ?? response.headers.get('content-type');
|
|
76
|
+
let resp;
|
|
77
|
+
if (contentType?.startsWith('application/jsonl')) {
|
|
78
|
+
resp = defaultStreamHandler({ response, controller, schema });
|
|
79
|
+
}
|
|
80
|
+
else if (contentType?.startsWith('application/json')) {
|
|
81
|
+
resp = defaultHandler({ response, schema });
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
resp = response;
|
|
85
|
+
}
|
|
86
|
+
resp = await resp;
|
|
87
|
+
resp = transformResponse
|
|
88
|
+
? await transformResponse(resp, response, options, requestInit)
|
|
89
|
+
: resp;
|
|
90
|
+
return [resp, response];
|
|
39
91
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
headers: {
|
|
44
|
-
accept: 'application/jsonl, application/json',
|
|
45
|
-
...(body instanceof FormData ? {} : { 'content-type': 'application/json' }),
|
|
46
|
-
...(meta ? { 'x-meta': JSON.stringify(meta) } : {}),
|
|
47
|
-
...init?.headers,
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
if (body instanceof FormData) {
|
|
51
|
-
requestInit.body = body;
|
|
52
|
-
}
|
|
53
|
-
else if (body) {
|
|
54
|
-
requestInit.body = JSON.stringify(body);
|
|
55
|
-
}
|
|
56
|
-
requestInit = prepareRequestInit
|
|
57
|
-
? ((await prepareRequestInit(requestInit, options)) ?? requestInit)
|
|
58
|
-
: requestInit;
|
|
59
|
-
let response;
|
|
60
|
-
const controller = new AbortController();
|
|
61
|
-
requestInit.signal = controller.signal;
|
|
62
|
-
try {
|
|
63
|
-
response = await fetch(endpoint, requestInit);
|
|
64
|
-
}
|
|
65
|
-
catch (e) {
|
|
66
|
-
// handle network errors
|
|
67
|
-
throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, (e?.message ?? exports.DEFAULT_ERROR_MESSAGE) + ' ' + endpoint, {
|
|
68
|
-
body,
|
|
69
|
-
query,
|
|
70
|
-
params,
|
|
71
|
-
endpoint,
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
const contentType = interpretAs ?? response.headers.get('content-type');
|
|
75
|
-
let resp;
|
|
76
|
-
if (contentType?.startsWith('application/jsonl')) {
|
|
77
|
-
resp = defaultStreamHandler({ response, controller, schema });
|
|
78
|
-
}
|
|
79
|
-
else if (contentType?.startsWith('application/json')) {
|
|
80
|
-
resp = defaultHandler({ response, schema });
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
resp = response;
|
|
92
|
+
catch (error) {
|
|
93
|
+
await onError?.(error, response);
|
|
94
|
+
throw error;
|
|
84
95
|
}
|
|
85
|
-
resp = await resp;
|
|
86
|
-
resp = transformResponse
|
|
87
|
-
? await transformResponse(resp, response, options, requestInit)
|
|
88
|
-
: resp;
|
|
89
|
-
return [resp, response];
|
|
90
96
|
};
|
|
91
97
|
return newFetcher;
|
|
92
98
|
}
|