make-service 2.1.2 → 3.0.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/README.md +3 -6
- package/dist/index.js +9 -11
- package/dist/index.mjs +9 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -102,7 +102,6 @@ On the example above, the request will be sent with the following arguments:
|
|
|
102
102
|
// {
|
|
103
103
|
// method: 'GET',
|
|
104
104
|
// headers: {
|
|
105
|
-
// 'content-type': 'application/json',
|
|
106
105
|
// 'authorization': 'Bearer 123',
|
|
107
106
|
// }
|
|
108
107
|
// }
|
|
@@ -202,7 +201,7 @@ const service = makeService("https://example.com/api", {
|
|
|
202
201
|
})
|
|
203
202
|
|
|
204
203
|
const response = await service.get("/users", {
|
|
205
|
-
headers: new Headers({ authorization: 'undefined'
|
|
204
|
+
headers: new Headers({ authorization: 'undefined' }),
|
|
206
205
|
})
|
|
207
206
|
// headers will be empty.
|
|
208
207
|
```
|
|
@@ -344,7 +343,7 @@ const service = makeService("https://example.com/api")
|
|
|
344
343
|
const response = await service.get("/users/:id", {
|
|
345
344
|
params: { id: "2" },
|
|
346
345
|
query: { page: "2"},
|
|
347
|
-
headers: { Accept: "application/json" },
|
|
346
|
+
headers: { Accept: "application/json", "Content-type": "application/json" },
|
|
348
347
|
trace: (url, requestInit) => {
|
|
349
348
|
console.log("The request was sent to " + url)
|
|
350
349
|
console.log("with the following params: " + JSON.stringify(requestInit))
|
|
@@ -408,12 +407,9 @@ await enhancedFetch("https://example.com/api/users/:role", {
|
|
|
408
407
|
// {
|
|
409
408
|
// method: 'POST',
|
|
410
409
|
// body: '{"some":{"object":{"as":{"body":{}}}}}',
|
|
411
|
-
// headers: { 'content-type': 'application/json' }
|
|
412
410
|
// }
|
|
413
411
|
```
|
|
414
412
|
|
|
415
|
-
Notice: the `enhancedFetch` adds a `'content-type': 'application/json'` header by default.
|
|
416
|
-
|
|
417
413
|
## typedResponse
|
|
418
414
|
|
|
419
415
|
A type-safe wrapper around the `Response` object. It adds a `json` and `text` method that will parse the response with a given zod schema. If you don't provide a schema, it will return `unknown` instead of `any`, then you can also give it a generic to type cast the result.
|
|
@@ -596,6 +592,7 @@ The params will be **strongly-typed** which means they will be validated against
|
|
|
596
592
|
<td align="center" valign="top" width="14.28%"><a href="https://www.linkedin.com/in/danielweinmann"><img src="https://avatars.githubusercontent.com/u/204765?v=4?s=100" width="100px;" alt="Daniel Weinmann"/><br /><sub><b>Daniel Weinmann</b></sub></a><br /><a href="#code-danielweinmann" title="Code">💻</a> <a href="#promotion-danielweinmann" title="Promotion">📣</a> <a href="#ideas-danielweinmann" title="Ideas, Planning, & Feedback">🤔</a> <a href="#doc-danielweinmann" title="Documentation">📖</a> <a href="#bug-danielweinmann" title="Bug reports">🐛</a></td>
|
|
597
593
|
<td align="center" valign="top" width="14.28%"><a href="https://github.com/diogob"><img src="https://avatars.githubusercontent.com/u/20662?v=4?s=100" width="100px;" alt="Diogo Biazus"/><br /><sub><b>Diogo Biazus</b></sub></a><br /><a href="#code-diogob" title="Code">💻</a></td>
|
|
598
594
|
<td align="center" valign="top" width="14.28%"><a href="https://github.com/garusis"><img src="https://avatars.githubusercontent.com/u/15615652?v=4?s=100" width="100px;" alt="Marcos Javier Alvarez Maestre"/><br /><sub><b>Marcos Javier Alvarez Maestre</b></sub></a><br /><a href="#code-garusis" title="Code">💻</a> <a href="#bug-garusis" title="Bug reports">🐛</a></td>
|
|
595
|
+
<td align="center" valign="top" width="14.28%"><a href="https://luca.md"><img src="https://avatars.githubusercontent.com/u/1881266?v=4?s=100" width="100px;" alt="Andrei Luca"/><br /><sub><b>Andrei Luca</b></sub></a><br /><a href="#doc-iamandrewluca" title="Documentation">📖</a></td>
|
|
599
596
|
</tr>
|
|
600
597
|
</tbody>
|
|
601
598
|
</table>
|
package/dist/index.js
CHANGED
|
@@ -124,23 +124,21 @@ function typedResponse(response, options) {
|
|
|
124
124
|
return getJsonFn(target);
|
|
125
125
|
if (prop === "text")
|
|
126
126
|
return getTextFn(target);
|
|
127
|
-
|
|
127
|
+
const value = Reflect.get(target, prop);
|
|
128
|
+
if (typeof value === "function") {
|
|
129
|
+
return value.bind(target);
|
|
130
|
+
}
|
|
131
|
+
return value;
|
|
128
132
|
}
|
|
129
133
|
});
|
|
130
134
|
}
|
|
131
135
|
async function enhancedFetch(url, requestInit) {
|
|
132
|
-
var _a
|
|
136
|
+
var _a;
|
|
133
137
|
const { query, trace, ...reqInit } = requestInit != null ? requestInit : {};
|
|
134
|
-
const headers = mergeHeaders(
|
|
135
|
-
{
|
|
136
|
-
"content-type": "application/json"
|
|
137
|
-
},
|
|
138
|
-
(_a = reqInit.headers) != null ? _a : {}
|
|
139
|
-
);
|
|
140
|
-
const withParams = replaceURLParams(url, (_b = reqInit.params) != null ? _b : {});
|
|
141
|
-
const fullURL = addQueryToURL(withParams, query);
|
|
142
138
|
const body = ensureStringBody(reqInit.body);
|
|
143
|
-
const
|
|
139
|
+
const withParams = replaceURLParams(url, (_a = reqInit.params) != null ? _a : {});
|
|
140
|
+
const fullURL = addQueryToURL(withParams, query);
|
|
141
|
+
const enhancedReqInit = { ...reqInit, body };
|
|
144
142
|
trace == null ? void 0 : trace(fullURL, enhancedReqInit);
|
|
145
143
|
const response = await fetch(fullURL, enhancedReqInit);
|
|
146
144
|
return typedResponse(response);
|
package/dist/index.mjs
CHANGED
|
@@ -89,23 +89,21 @@ function typedResponse(response, options) {
|
|
|
89
89
|
return getJsonFn(target);
|
|
90
90
|
if (prop === "text")
|
|
91
91
|
return getTextFn(target);
|
|
92
|
-
|
|
92
|
+
const value = Reflect.get(target, prop);
|
|
93
|
+
if (typeof value === "function") {
|
|
94
|
+
return value.bind(target);
|
|
95
|
+
}
|
|
96
|
+
return value;
|
|
93
97
|
}
|
|
94
98
|
});
|
|
95
99
|
}
|
|
96
100
|
async function enhancedFetch(url, requestInit) {
|
|
97
|
-
var _a
|
|
101
|
+
var _a;
|
|
98
102
|
const { query, trace, ...reqInit } = requestInit != null ? requestInit : {};
|
|
99
|
-
const headers = mergeHeaders(
|
|
100
|
-
{
|
|
101
|
-
"content-type": "application/json"
|
|
102
|
-
},
|
|
103
|
-
(_a = reqInit.headers) != null ? _a : {}
|
|
104
|
-
);
|
|
105
|
-
const withParams = replaceURLParams(url, (_b = reqInit.params) != null ? _b : {});
|
|
106
|
-
const fullURL = addQueryToURL(withParams, query);
|
|
107
103
|
const body = ensureStringBody(reqInit.body);
|
|
108
|
-
const
|
|
104
|
+
const withParams = replaceURLParams(url, (_a = reqInit.params) != null ? _a : {});
|
|
105
|
+
const fullURL = addQueryToURL(withParams, query);
|
|
106
|
+
const enhancedReqInit = { ...reqInit, body };
|
|
109
107
|
trace == null ? void 0 : trace(fullURL, enhancedReqInit);
|
|
110
108
|
const response = await fetch(fullURL, enhancedReqInit);
|
|
111
109
|
return typedResponse(response);
|