@congruent-stack/congruent-api-fetch 0.12.2 → 0.14.0
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/dist/index.cjs +19 -3
- package/dist/index.mjs +20 -4
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -28,7 +28,15 @@ function createFetchClient(contract, options) {
|
|
|
28
28
|
body: input.body ? JSON.stringify(input.body) : void 0
|
|
29
29
|
};
|
|
30
30
|
const finalRequestInit = options.enhanceRequestInit ? options.enhanceRequestInit(requestInit, input) : requestInit;
|
|
31
|
-
|
|
31
|
+
let response;
|
|
32
|
+
try {
|
|
33
|
+
response = await fetch(fullUrlAddress, finalRequestInit);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
return {
|
|
36
|
+
code: congruentApi.RequestFailureCode.ErrorThrown,
|
|
37
|
+
body: error instanceof Error ? error : new Error(String(error))
|
|
38
|
+
};
|
|
39
|
+
}
|
|
32
40
|
const responseCode = response.status;
|
|
33
41
|
const responseHeaders = Object.fromEntries(response.headers.entries());
|
|
34
42
|
if (responseCode === congruentApi.HttpStatusCode.NoContent_204 || responseCode === congruentApi.HttpStatusCode.NotModified_304) {
|
|
@@ -40,9 +48,17 @@ function createFetchClient(contract, options) {
|
|
|
40
48
|
}
|
|
41
49
|
const responseContentType = response.headers.get("content-type") || "";
|
|
42
50
|
if (!responseContentType.includes("application/json")) {
|
|
43
|
-
throw new Error(`Expected 'application/json' content-type
|
|
51
|
+
throw new Error(`Expected 'application/json' in 'content-type' response header, but got '${responseContentType}'`);
|
|
52
|
+
}
|
|
53
|
+
let responseBody;
|
|
54
|
+
try {
|
|
55
|
+
responseBody = await response.json();
|
|
56
|
+
} catch (error) {
|
|
57
|
+
return {
|
|
58
|
+
code: congruentApi.RequestFailureCode.ErrorThrown,
|
|
59
|
+
body: error instanceof Error ? error : new Error(String(error))
|
|
60
|
+
};
|
|
44
61
|
}
|
|
45
|
-
const responseBody = await response.json();
|
|
46
62
|
return {
|
|
47
63
|
code: responseCode,
|
|
48
64
|
headers: responseHeaders,
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createClient, HttpStatusCode } from '@congruent-stack/congruent-api';
|
|
1
|
+
import { createClient, RequestFailureCode, HttpStatusCode } from '@congruent-stack/congruent-api';
|
|
2
2
|
|
|
3
3
|
const EMPTY_OBJECT = Object.freeze({});
|
|
4
4
|
function createFetchClient(contract, options) {
|
|
@@ -26,7 +26,15 @@ function createFetchClient(contract, options) {
|
|
|
26
26
|
body: input.body ? JSON.stringify(input.body) : void 0
|
|
27
27
|
};
|
|
28
28
|
const finalRequestInit = options.enhanceRequestInit ? options.enhanceRequestInit(requestInit, input) : requestInit;
|
|
29
|
-
|
|
29
|
+
let response;
|
|
30
|
+
try {
|
|
31
|
+
response = await fetch(fullUrlAddress, finalRequestInit);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
return {
|
|
34
|
+
code: RequestFailureCode.ErrorThrown,
|
|
35
|
+
body: error instanceof Error ? error : new Error(String(error))
|
|
36
|
+
};
|
|
37
|
+
}
|
|
30
38
|
const responseCode = response.status;
|
|
31
39
|
const responseHeaders = Object.fromEntries(response.headers.entries());
|
|
32
40
|
if (responseCode === HttpStatusCode.NoContent_204 || responseCode === HttpStatusCode.NotModified_304) {
|
|
@@ -38,9 +46,17 @@ function createFetchClient(contract, options) {
|
|
|
38
46
|
}
|
|
39
47
|
const responseContentType = response.headers.get("content-type") || "";
|
|
40
48
|
if (!responseContentType.includes("application/json")) {
|
|
41
|
-
throw new Error(`Expected 'application/json' content-type
|
|
49
|
+
throw new Error(`Expected 'application/json' in 'content-type' response header, but got '${responseContentType}'`);
|
|
50
|
+
}
|
|
51
|
+
let responseBody;
|
|
52
|
+
try {
|
|
53
|
+
responseBody = await response.json();
|
|
54
|
+
} catch (error) {
|
|
55
|
+
return {
|
|
56
|
+
code: RequestFailureCode.ErrorThrown,
|
|
57
|
+
body: error instanceof Error ? error : new Error(String(error))
|
|
58
|
+
};
|
|
42
59
|
}
|
|
43
|
-
const responseBody = await response.json();
|
|
44
60
|
return {
|
|
45
61
|
code: responseCode,
|
|
46
62
|
headers: responseHeaders,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@congruent-stack/congruent-api-fetch",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.14.0",
|
|
5
5
|
"description": "Typescript schema-first tooling for agnostic REST APIs.",
|
|
6
6
|
"keywords": [],
|
|
7
7
|
"author": "congruent-stack",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
|
-
"@congruent-stack/congruent-api": "0.
|
|
23
|
+
"@congruent-stack/congruent-api": "0.14.0"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"pkgroll": "2.14.5",
|