make-service 2.0.0 → 2.1.1

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 CHANGED
@@ -1,3 +1,7 @@
1
+ [![NPM](https://img.shields.io/npm/v/make-service)](https://www.npmjs.org/package/make-service)
2
+ ![Library size](https://img.shields.io/bundlephobia/minzip/make-service)
3
+ [![All Contributors](https://img.shields.io/github/all-contributors/gustavoguichard/make-service?color=0375b6&style=flat-square)](#contributors)
4
+
1
5
  # make-service
2
6
 
3
7
  A type-safe thin wrapper around the `fetch` API to better interact with external APIs.
@@ -580,9 +584,31 @@ const url = replaceURLParams(
580
584
 
581
585
  The params will be **strongly-typed** which means they will be validated against the URL structure and will not type-check if the given object does not match that structure.
582
586
 
587
+ # Contributors
588
+
589
+ <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
590
+ <!-- prettier-ignore-start -->
591
+ <!-- markdownlint-disable -->
592
+ <table>
593
+ <tbody>
594
+ <tr>
595
+ <td align="center" valign="top" width="14.28%"><a href="https://github.com/gustavoguichard"><img src="https://avatars.githubusercontent.com/u/566971?v=4?s=100" width="100px;" alt="Guga Guichard"/><br /><sub><b>Guga Guichard</b></sub></a><br /><a href="#code-gustavoguichard" title="Code">💻</a> <a href="#projectManagement-gustavoguichard" title="Project Management">📆</a> <a href="#promotion-gustavoguichard" title="Promotion">📣</a> <a href="#maintenance-gustavoguichard" title="Maintenance">🚧</a> <a href="#doc-gustavoguichard" title="Documentation">📖</a> <a href="#bug-gustavoguichard" title="Bug reports">🐛</a> <a href="#infra-gustavoguichard" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="#question-gustavoguichard" title="Answering Questions">💬</a> <a href="#research-gustavoguichard" title="Research">🔬</a> <a href="#review-gustavoguichard" title="Reviewed Pull Requests">👀</a> <a href="#ideas-gustavoguichard" title="Ideas, Planning, & Feedback">🤔</a> <a href="#example-gustavoguichard" title="Examples">💡</a></td>
596
+ <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
+ <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
+ <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>
599
+ </tr>
600
+ </tbody>
601
+ </table>
602
+
603
+ <!-- markdownlint-restore -->
604
+ <!-- prettier-ignore-end -->
605
+
606
+ <!-- ALL-CONTRIBUTORS-LIST:END -->
607
+
583
608
  # Acknowledgements
584
609
  This library is part of a code I've been carrying around for a while through many projects.
585
610
 
611
+ - [Seasoned](https://github.com/seasonedcc) - for backing my work and allowing me testing it on big codebases when I started sketching this API.
586
612
  - [croods](https://github.com/seasonedcc/croods) by [@danielweinmann](https://github.com/danielweinmann) - a react data-layer library from pre-ReactQuery/pre-SWR era - gave me ideas and experience dealing with APIs after spending a lot of time in that codebase.
587
613
  - [zod](https://zod.dev/) by [@colinhacks](https://github.com/colinhacks) changed my mindset about how to deal with external data.
588
614
  - [zod-fetch](https://github.com/mattpocock/zod-fetch) by [@mattpocock](https://github.com/mattpocock) for the inspiration, when I realized I had a similar solution that could be extracted and be available for everyone to use.
package/dist/index.d.ts CHANGED
@@ -3,9 +3,9 @@ declare const HTTP_METHODS: readonly ["GET", "POST", "PUT", "DELETE", "PATCH", "
3
3
  type Schema<T> = {
4
4
  parse: (d: unknown) => T;
5
5
  };
6
- type JSONValue = string | number | boolean | {
7
- [x: string]: JSONValue;
8
- } | Array<JSONValue>;
6
+ type JSONValue = string | number | boolean | Date | {
7
+ [x: string]: JSONValue | undefined | null;
8
+ } | Array<JSONValue | undefined | null>;
9
9
  type SearchParams = ConstructorParameters<typeof URLSearchParams>[0];
10
10
  type TypedResponse = Omit<Response, 'json' | 'text'> & {
11
11
  json: TypedResponseJson;
@@ -110,7 +110,7 @@ declare function makeService(baseURL: string | URL, baseOptions?: BaseOptions):
110
110
  * @param searchParams the query parameters
111
111
  * @returns the url with the query parameters added with the same type as the url
112
112
  */
113
- declare function addQueryToURL(url: string | URL, searchParams?: SearchParams): string | URL;
113
+ declare function addQueryToURL<T extends string | URL>(url: T, searchParams?: SearchParams): T;
114
114
  /**
115
115
  * @param body the JSON-like body of the request
116
116
  * @returns the body is stringified if it is not a string and it is a JSON-like object. It also accepts other types of BodyInit such as Blob, ReadableStream, etc.
@@ -134,5 +134,11 @@ declare function mergeHeaders(...entries: (HeadersInit | [string, undefined][] |
134
134
  * @returns the url with the params replaced and with the same type as the given url
135
135
  */
136
136
  declare function replaceURLParams<T extends string | URL>(url: T, params: PathParams<T>): T;
137
+ /**
138
+ * This is an enhanced version of the typeof operator to check the type of more complex values.
139
+ * @param t the value to be checked
140
+ * @returns the type of the value
141
+ */
142
+ declare function typeOf(t: unknown): "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "url" | "blob" | "array" | "arraybuffer" | "formdata" | "null" | "readablestream" | "urlsearchparams";
137
143
 
138
- export { BaseOptions, EnhancedRequestInit, GetJson, GetText, HTTPMethod, JSONValue, PathParams, RequestTransformer, ResponseTransformer, Schema, SearchParams, ServiceRequestInit, TypedResponse, TypedResponseJson, TypedResponseText, addQueryToURL, enhancedFetch, ensureStringBody, makeFetcher, makeGetApiURL, makeService, mergeHeaders, replaceURLParams, typedResponse };
144
+ export { BaseOptions, EnhancedRequestInit, GetJson, GetText, HTTPMethod, JSONValue, PathParams, RequestTransformer, ResponseTransformer, Schema, SearchParams, ServiceRequestInit, TypedResponse, TypedResponseJson, TypedResponseText, addQueryToURL, enhancedFetch, ensureStringBody, makeFetcher, makeGetApiURL, makeService, mergeHeaders, replaceURLParams, typeOf, typedResponse };
package/dist/index.js CHANGED
@@ -28,6 +28,7 @@ __export(src_exports, {
28
28
  makeService: () => makeService,
29
29
  mergeHeaders: () => mergeHeaders,
30
30
  replaceURLParams: () => replaceURLParams,
31
+ typeOf: () => typeOf,
31
32
  typedResponse: () => typedResponse
32
33
  });
33
34
  module.exports = __toCommonJS(src_exports);
@@ -54,9 +55,6 @@ var getText = (response) => async (schema) => {
54
55
  const text = await response.text();
55
56
  return schema ? schema.parse(text) : text;
56
57
  };
57
- function typeOf(t) {
58
- return Object.prototype.toString.call(t).replace(/^\[object (.+)\]$/, "$1").toLowerCase();
59
- }
60
58
 
61
59
  // src/primitives.ts
62
60
  function addQueryToURL(url, searchParams) {
@@ -67,9 +65,7 @@ function addQueryToURL(url, searchParams) {
67
65
  return `${url}${separator}${new URLSearchParams(searchParams)}`;
68
66
  }
69
67
  if (searchParams && url instanceof URL) {
70
- for (const [key, value] of Object.entries(
71
- new URLSearchParams(searchParams)
72
- )) {
68
+ for (const [key, value] of new URLSearchParams(searchParams).entries()) {
73
69
  url.searchParams.set(key, value);
74
70
  }
75
71
  }
@@ -112,6 +108,9 @@ function replaceURLParams(url, params) {
112
108
  });
113
109
  return url instanceof URL ? new URL(urlString) : urlString;
114
110
  }
111
+ function typeOf(t) {
112
+ return Object.prototype.toString.call(t).replace(/^\[object (.+)\]$/, "$1").toLowerCase();
113
+ }
115
114
 
116
115
  // src/api.ts
117
116
  var identity = (value) => value;
@@ -192,5 +191,6 @@ function makeService(baseURL, baseOptions) {
192
191
  makeService,
193
192
  mergeHeaders,
194
193
  replaceURLParams,
194
+ typeOf,
195
195
  typedResponse
196
196
  });
package/dist/index.mjs CHANGED
@@ -20,9 +20,6 @@ var getText = (response) => async (schema) => {
20
20
  const text = await response.text();
21
21
  return schema ? schema.parse(text) : text;
22
22
  };
23
- function typeOf(t) {
24
- return Object.prototype.toString.call(t).replace(/^\[object (.+)\]$/, "$1").toLowerCase();
25
- }
26
23
 
27
24
  // src/primitives.ts
28
25
  function addQueryToURL(url, searchParams) {
@@ -33,9 +30,7 @@ function addQueryToURL(url, searchParams) {
33
30
  return `${url}${separator}${new URLSearchParams(searchParams)}`;
34
31
  }
35
32
  if (searchParams && url instanceof URL) {
36
- for (const [key, value] of Object.entries(
37
- new URLSearchParams(searchParams)
38
- )) {
33
+ for (const [key, value] of new URLSearchParams(searchParams).entries()) {
39
34
  url.searchParams.set(key, value);
40
35
  }
41
36
  }
@@ -78,6 +73,9 @@ function replaceURLParams(url, params) {
78
73
  });
79
74
  return url instanceof URL ? new URL(urlString) : urlString;
80
75
  }
76
+ function typeOf(t) {
77
+ return Object.prototype.toString.call(t).replace(/^\[object (.+)\]$/, "$1").toLowerCase();
78
+ }
81
79
 
82
80
  // src/api.ts
83
81
  var identity = (value) => value;
@@ -157,5 +155,6 @@ export {
157
155
  makeService,
158
156
  mergeHeaders,
159
157
  replaceURLParams,
158
+ typeOf,
160
159
  typedResponse
161
160
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "make-service",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "description": "Some utilities to extend the 'fetch' API to better interact with external APIs.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",