Skip to content

Commit 5b8ee00

Browse files
authored
refactor: Internal patches (#100)
2 parents a7811e9 + b17478b commit 5b8ee00

File tree

9 files changed

+26
-25
lines changed

9 files changed

+26
-25
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Releases
22

33
## Contents
4+
- [v0.3.4-alpha.2](#v0-3-4-alpha-2)
45
- [v0.3.4-alpha.1](#v0-3-4-alpha-1)
56
- [v0.3.4-alpha.0](#v0-3-4-alpha-0)
67
- [v0.3.3](#v0-3-3)
@@ -18,6 +19,13 @@
1819
- [v0.1.0](#v0-1-0)
1920
- [v0.0.8](#v0-0-8)
2021

22+
## v0.3.4-alpha.2
23+
24+
- Internal; Better handle path to string for constructing URLs.
25+
- Internal; Fix missing trailing `.js` from import in `tryParse.ts` for ESM compatibility.
26+
- Internal; Simplify retrier to not be recursive in-case heap goes wild.
27+
- Internal; Cache-client non-arrow functions.
28+
2129
## v0.3.4-alpha.1
2230

2331
- Remove unused shallow flag from isErrorType as it is totally unnecessary.

jsr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kasperrt/wiretyped",
3-
"version": "0.3.4-alpha.1",
3+
"version": "0.3.4-alpha.2",
44
"exports": {
55
".": "./src/index.ts"
66
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wiretyped",
3-
"version": "0.3.4-alpha.1",
3+
"version": "0.3.4-alpha.2",
44
"description": "Universal fetch-based HTTP client with robust error handling, retries, caching, SSE, and Standard Schema validation.",
55
"type": "module",
66
"keywords": [

src/cache/client.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class CacheClient {
108108
* @param {string} key - cache key
109109
* @param {Promise} request - http request to put in the pending object
110110
*/
111-
#addPendingRequest = <T = unknown>(key: string, request: () => SafeWrapAsync<Error, T>, ttl: number) => {
111+
#addPendingRequest<T = unknown>(key: string, request: () => SafeWrapAsync<Error, T>, ttl: number) {
112112
const pending = (async (): SafeWrapAsync<Error, T> => {
113113
const [errWrapped, wrapped] = await safeWrapAsync(() => request());
114114
if (errWrapped) {
@@ -129,18 +129,14 @@ export class CacheClient {
129129

130130
this.#pending.set(key, pending);
131131
return pending;
132-
};
132+
}
133133

134134
/**
135135
* Pass a request to the cache
136136
* @param {string} key - cache key
137137
* @param {Function} res - http request and data returned.
138138
*/
139-
public get = <T = unknown>(
140-
key: string,
141-
res: () => SafeWrapAsync<Error, T>,
142-
ttl = this.#ttl,
143-
): SafeWrapAsync<Error, T> => {
139+
public get<T = unknown>(key: string, res: () => SafeWrapAsync<Error, T>, ttl = this.#ttl): SafeWrapAsync<Error, T> {
144140
const cachedData = this.#getItem<CacheItem<T>>(key);
145141
if (cachedData) {
146142
return Promise.resolve([null, cachedData.data]);
@@ -152,7 +148,7 @@ export class CacheClient {
152148
}
153149

154150
return this.#addPendingRequest(key, res, ttl);
155-
};
151+
}
156152

157153
/**
158154
* Constructs a deterministic, unambiguous cache key based on URL + headers.
@@ -179,13 +175,13 @@ export class CacheClient {
179175
* cleanup that does housekeeping every 30 seconds, removing
180176
* invalid cache items to prevent unecessary memory usage;
181177
*/
182-
#cleanup = () => {
178+
#cleanup() {
183179
clearInterval(this.#intervalId);
184180

185181
this.#intervalId = setInterval(() => {
186182
for (const key of this.#cache.keys()) {
187183
this.#getItem(key);
188184
}
189185
}, this.#cleanupInterval);
190-
};
186+
}
191187
}

src/error/isErrorType.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { unwrapErrorType } from './unwrapErrorType';
1+
import { unwrapErrorType } from './unwrapErrorType.js';
22

33
/**
44
* Generic type guard to check if an unknown error matches a specific error class.

src/utils/constructUrl.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ export async function constructUrl<
1818
validation?: boolean,
1919
): SafeWrapAsync<Error, string> {
2020
const searchParams = new URLSearchParams();
21-
let result = path.toString();
21+
let result = String(path);
2222

2323
// Strip leading slash for clean concatenation with baseUrl
2424
if (result.startsWith('/')) {
25-
result = result.substring(1);
25+
result = result.slice(1);
2626
}
2727

2828
if (!params) {
@@ -68,7 +68,7 @@ export async function constructUrl<
6868
}
6969

7070
for (const [key, value] of Object.entries(data ?? {})) {
71-
result = result.replace(new RegExp(`{${key}}`, 'g'), encodeURIComponent(String(value)));
71+
result = result.split(`{${key}}`).join(encodeURIComponent(String(value)));
7272
}
7373
}
7474

@@ -80,7 +80,7 @@ export async function constructUrl<
8080
}
8181

8282
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
83-
result = result.replace(new RegExp(`{${key}}`, 'g'), encodeURIComponent(String(value)));
83+
result = result.split(`{${key}}`).join(encodeURIComponent(String(value)));
8484
}
8585
}
8686

src/utils/retry.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ export interface RetryOptions<R> {
3232
* @param timeout how long the wait-time should be
3333
* @param errFn optional errorFunction on whether we want to skip retrying and propagate the error (true = stop, false = retry and move on)
3434
*/
35-
export function retry<R = unknown>({
35+
export async function retry<R = unknown>({
3636
fn,
3737
attempts = 10,
3838
timeout = 1000,
3939
errFn,
4040
}: RetryOptions<R>): SafeWrapAsync<Error, R> {
41-
const retrier = async (fn: () => SafeWrapAsync<Error, R>, attempt = 1): SafeWrapAsync<Error, R> => {
41+
for (let attempt = 1; ; attempt += 1) {
4242
const [err, data] = await fn();
4343
if (!err) {
4444
return [null, data];
@@ -53,8 +53,5 @@ export function retry<R = unknown>({
5353
}
5454

5555
await sleep(timeout);
56-
return retrier(fn, attempt + 1);
57-
};
58-
59-
return retrier(fn);
56+
}
6057
}

src/utils/tryParse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { safeWrap } from './wrap';
1+
import { safeWrap } from './wrap.js';
22

33
/**
44
* Attempts to parse a string as JSON.

src/utils/validator.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,6 @@ describe('validate', () => {
9393
const [err, value] = await validator('test', schema);
9494

9595
expect(value).toBe('test');
96-
expect(err).toBeNull;
96+
expect(err).toBeNull();
9797
});
9898
});

0 commit comments

Comments
 (0)