Skip to content

Commit 4727187

Browse files
committed
add some generics
1 parent 651d1bc commit 4727187

File tree

1 file changed

+46
-57
lines changed

1 file changed

+46
-57
lines changed

packages/storage/src/common/utils.ts

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ export function isBrowser() {
77
return typeof window !== "undefined";
88
}
99

10-
export function isString(data: any): data is string {
11-
return typeof data === "string";
12-
}
13-
1410
/**
1511
* @internal
1612
*/
@@ -95,34 +91,34 @@ export function replaceSchemeWithGatewayUrl(
9591
/**
9692
* @internal
9793
*/
98-
export function replaceObjectGatewayUrlsWithSchemes(
99-
data: unknown,
94+
export function replaceObjectGatewayUrlsWithSchemes<TData = unknown>(
95+
data: TData,
10096
gatewayUrls: GatewayUrls,
101-
): unknown {
102-
switch (typeof data) {
103-
case "string":
104-
return replaceGatewayUrlWithScheme(data, gatewayUrls);
105-
case "object":
106-
if (!data) {
107-
return data;
108-
}
97+
): TData {
98+
if (typeof data === "string") {
99+
return replaceGatewayUrlWithScheme(data, gatewayUrls) as TData;
100+
}
101+
if (typeof data === "object") {
102+
if (!data) {
103+
return data;
104+
}
109105

110-
if (isFileOrBuffer(data)) {
111-
return data;
112-
}
106+
if (isFileOrBuffer(data)) {
107+
return data;
108+
}
113109

114-
if (Array.isArray(data)) {
115-
return data.map((entry) =>
116-
replaceObjectGatewayUrlsWithSchemes(entry, gatewayUrls),
117-
);
118-
}
110+
if (Array.isArray(data)) {
111+
return data.map((entry) =>
112+
replaceObjectGatewayUrlsWithSchemes(entry, gatewayUrls),
113+
) as TData;
114+
}
119115

120-
return Object.fromEntries(
121-
Object.entries(data).map(([key, value]) => [
122-
key,
123-
replaceObjectGatewayUrlsWithSchemes(value, gatewayUrls),
124-
]),
125-
);
116+
return Object.fromEntries(
117+
Object.entries(data).map(([key, value]) => [
118+
key,
119+
replaceObjectGatewayUrlsWithSchemes(value, gatewayUrls),
120+
]),
121+
) as TData;
126122
}
127123

128124
return data;
@@ -131,38 +127,31 @@ export function replaceObjectGatewayUrlsWithSchemes(
131127
/**
132128
* @internal
133129
*/
134-
export function replaceObjectSchemesWithGatewayUrls<TData>(
130+
export function replaceObjectSchemesWithGatewayUrls<TData = unknown>(
135131
data: TData,
136132
gatewayUrls: GatewayUrls,
137-
): unknown {
138-
if (isString(data)) {
139-
return replaceSchemeWithGatewayUrl(data, gatewayUrls);
133+
): TData {
134+
if (typeof data === "string") {
135+
return replaceSchemeWithGatewayUrl(data, gatewayUrls) as TData;
140136
}
141-
142-
switch (typeof data) {
143-
case "string":
144-
return replaceSchemeWithGatewayUrl(data, gatewayUrls);
145-
case "object":
146-
if (!data) {
147-
return data;
148-
}
149-
150-
if (isFileOrBuffer(data)) {
151-
return data;
152-
}
153-
154-
if (Array.isArray(data)) {
155-
return data.map((entry) =>
156-
replaceObjectSchemesWithGatewayUrls(entry, gatewayUrls),
157-
);
158-
}
159-
160-
return Object.fromEntries(
161-
Object.entries(data).map(([key, value]) => [
162-
key,
163-
replaceObjectSchemesWithGatewayUrls(value, gatewayUrls),
164-
]),
165-
);
137+
if (typeof data === "object") {
138+
if (!data) {
139+
return data;
140+
}
141+
if (isFileOrBuffer(data)) {
142+
return data;
143+
}
144+
if (Array.isArray(data)) {
145+
return data.map((entry) =>
146+
replaceObjectSchemesWithGatewayUrls(entry, gatewayUrls),
147+
) as TData;
148+
}
149+
return Object.fromEntries(
150+
Object.entries(data).map(([key, value]) => [
151+
key,
152+
replaceObjectSchemesWithGatewayUrls(value, gatewayUrls),
153+
]),
154+
) as TData;
166155
}
167156

168157
return data;

0 commit comments

Comments
 (0)