make-service 2.0.0 → 2.1.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 +1 -0
- package/dist/index.d.ts +10 -4
- package/dist/index.js +6 -6
- package/dist/index.mjs +5 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -583,6 +583,7 @@ The params will be **strongly-typed** which means they will be validated against
|
|
|
583
583
|
# Acknowledgements
|
|
584
584
|
This library is part of a code I've been carrying around for a while through many projects.
|
|
585
585
|
|
|
586
|
+
- [Seasoned](https://github.com/seasonedcc) - for backing my work and allowing me testing it on big codebases when I started sketching this API.
|
|
586
587
|
- [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
588
|
- [zod](https://zod.dev/) by [@colinhacks](https://github.com/colinhacks) changed my mindset about how to deal with external data.
|
|
588
589
|
- [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;
|
|
@@ -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
|
|
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
|
|
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
|
};
|