@rest-vir/define-service 0.3.4 → 0.4.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.
|
@@ -78,11 +78,16 @@ export type FetchEndpointOutput<EndpointToFetch extends Readonly<SelectFrom<Endp
|
|
|
78
78
|
requestDataShape: true;
|
|
79
79
|
responseDataShape: true;
|
|
80
80
|
}>> | NoParam> = Readonly<{
|
|
81
|
+
ok: true;
|
|
81
82
|
data: EndpointToFetch extends SelectFrom<EndpointDefinition, {
|
|
82
83
|
requestDataShape: true;
|
|
83
84
|
responseDataShape: true;
|
|
84
85
|
}> ? EndpointExecutorData<EndpointToFetch>['response'] : any;
|
|
85
86
|
response: Readonly<Response>;
|
|
87
|
+
}> | Readonly<{
|
|
88
|
+
ok: false;
|
|
89
|
+
data: string | undefined;
|
|
90
|
+
response: Readonly<Response>;
|
|
86
91
|
}>;
|
|
87
92
|
/**
|
|
88
93
|
* Extracts an array of all allowed methods for the given endpoint definition.
|
|
@@ -63,14 +63,25 @@ export async function fetchEndpoint(endpoint, ...params) {
|
|
|
63
63
|
const { requestInit, url } = buildEndpointRequestInit(endpoint, ...params);
|
|
64
64
|
/* node:coverage ignore next: all tests mock fetch so we're never going to have a fallback here. */
|
|
65
65
|
const response = await (fetch || defaultFetch)(url, requestInit, endpoint);
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
if (response.ok) {
|
|
67
|
+
const responseData = endpoint.responseDataShape ? await response.json() : undefined;
|
|
68
|
+
if (endpoint.responseDataShape) {
|
|
69
|
+
assertValidShape(responseData, endpoint.responseDataShape, { allowExtraKeys: true });
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
ok: true,
|
|
73
|
+
data: responseData,
|
|
74
|
+
response,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
return {
|
|
79
|
+
ok: false,
|
|
80
|
+
/** This will be an error message. */
|
|
81
|
+
data: (await response.text()) || undefined,
|
|
82
|
+
response,
|
|
83
|
+
};
|
|
69
84
|
}
|
|
70
|
-
return {
|
|
71
|
-
data: responseData,
|
|
72
|
-
response,
|
|
73
|
-
};
|
|
74
85
|
}
|
|
75
86
|
/**
|
|
76
87
|
* Build request init and URL for fetching an endpoint. Used in {@link fetchEndpoint}.
|