@hyper-fetch/core 8.0.0 → 8.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/__tests__/features/dispatcher/dispatcher.requests.spec.ts +1 -1
- package/__tests__/features/sdk/sdk.configuration.spec.ts +203 -0
- package/dist/index.mjs +62 -20
- package/dist/index.mjs.map +1 -1
- package/dist/sdk/sdk.d.ts +23 -7
- package/dist/sdk/sdk.d.ts.map +1 -1
- package/package.json +1 -9
- package/src/sdk/sdk.ts +106 -39
|
@@ -190,7 +190,7 @@ describe("Dispatcher [ Requests ]", () => {
|
|
|
190
190
|
describe("When stoping and starting particular requests", () => {
|
|
191
191
|
it("should allow to stop request", async () => {
|
|
192
192
|
const request = client.createRequest()({ endpoint: "shared-base-endpoint" });
|
|
193
|
-
mockRequest(request, { delay:
|
|
193
|
+
mockRequest(request, { delay: 500 });
|
|
194
194
|
|
|
195
195
|
const requestId = dispatcher.add(request);
|
|
196
196
|
await sleep(1);
|
|
@@ -259,4 +259,207 @@ describe("SDK [ Configuration ]", () => {
|
|
|
259
259
|
expect(request.method).toBe("GET");
|
|
260
260
|
});
|
|
261
261
|
});
|
|
262
|
+
|
|
263
|
+
describe("method-specific dot-path keys", () => {
|
|
264
|
+
it("should apply config to a specific method via dot-path key", () => {
|
|
265
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
266
|
+
const configured = sdk.$configure({
|
|
267
|
+
"users.$get": { retry: 7 },
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
const usersGet = configured.users.$get;
|
|
271
|
+
expect(usersGet.retry).toBe(7);
|
|
272
|
+
|
|
273
|
+
const postsGet = configured.posts.$get;
|
|
274
|
+
expect(postsGet.retry).toBe(0);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("should apply config to a nested dot-path key", () => {
|
|
278
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
279
|
+
const configured = sdk.$configure({
|
|
280
|
+
"users.$userId.$get": { cancelable: true },
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
const userByIdGet = configured.users.$userId.$get;
|
|
284
|
+
expect(userByIdGet.cancelable).toBe(true);
|
|
285
|
+
|
|
286
|
+
const usersGet = configured.users.$get;
|
|
287
|
+
expect(usersGet.cancelable).toBe(false);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("should not affect other methods on the same endpoint group", () => {
|
|
291
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
292
|
+
const configured = sdk.$configure({
|
|
293
|
+
"users.$get": { retry: 10 },
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
const usersGet = configured.users.$get;
|
|
297
|
+
expect(usersGet.retry).toBe(10);
|
|
298
|
+
|
|
299
|
+
// The nested $userId.$get shares the /users endpoint group but has a different dot-path
|
|
300
|
+
const userByIdGet = configured.users.$userId.$get;
|
|
301
|
+
expect(userByIdGet.retry).toBe(0);
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
describe("function-based configuration values", () => {
|
|
306
|
+
it("should apply a function value to configure a request", () => {
|
|
307
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
308
|
+
const configured = sdk.$configure({
|
|
309
|
+
"*": (request: RequestInstance) => request.setRetry(5).setCancelable(true),
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
const request = configured.users.$get;
|
|
313
|
+
expect(request.retry).toBe(5);
|
|
314
|
+
expect(request.cancelable).toBe(true);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it("should apply a function value on a dot-path key", () => {
|
|
318
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
319
|
+
const configured = sdk.$configure({
|
|
320
|
+
"users.$get": (request: RequestInstance) => request.setRetry(3).setHeaders({ "X-Custom": "test" }),
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
const usersGet = configured.users.$get;
|
|
324
|
+
expect(usersGet.retry).toBe(3);
|
|
325
|
+
expect(usersGet.headers).toStrictEqual({ "X-Custom": "test" });
|
|
326
|
+
|
|
327
|
+
const postsGet = configured.posts.$get;
|
|
328
|
+
expect(postsGet.retry).toBe(0);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("should allow setMock through function-based configuration", () => {
|
|
332
|
+
const mockFn = () => ({ data: [{ id: 1, name: "Test" }], error: null, status: 200 });
|
|
333
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
334
|
+
const configured = sdk.$configure({
|
|
335
|
+
"users.$get": (request: RequestInstance) => request.setMock(mockFn),
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
const usersGet = configured.users.$get;
|
|
339
|
+
expect(usersGet.isMockerEnabled).toBe(true);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("should allow setResponseMapper through function-based configuration", () => {
|
|
343
|
+
const mapper = (response: any) => ({ ...response, mapped: true });
|
|
344
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
345
|
+
const configured = sdk.$configure({
|
|
346
|
+
"users.$get": (request: RequestInstance) => request.setResponseMapper(mapper),
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
const usersGet = configured.users.$get;
|
|
350
|
+
expect(usersGet.unstable_responseMapper).toBe(mapper);
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
describe("configuration stacking", () => {
|
|
355
|
+
it("should stack properties from global and method-specific configs", () => {
|
|
356
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
357
|
+
const configured = sdk.$configure({
|
|
358
|
+
"*": { cache: true },
|
|
359
|
+
"users.$get": { deduplicate: true },
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
const usersGet = configured.users.$get;
|
|
363
|
+
expect(usersGet.cache).toBe(true);
|
|
364
|
+
expect(usersGet.deduplicate).toBe(true);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it("should stack properties across global, endpoint group, and dot-path", () => {
|
|
368
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
369
|
+
const configured = sdk.$configure({
|
|
370
|
+
"*": { retry: 3 },
|
|
371
|
+
"/users": { cache: false },
|
|
372
|
+
"users.$get": { deduplicate: true },
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
const usersGet = configured.users.$get;
|
|
376
|
+
expect(usersGet.retry).toBe(3);
|
|
377
|
+
expect(usersGet.cache).toBe(false);
|
|
378
|
+
expect(usersGet.deduplicate).toBe(true);
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it("should stack function-based values across levels", () => {
|
|
382
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
383
|
+
const configured = sdk.$configure({
|
|
384
|
+
"*": (request: RequestInstance) => request.setRetry(5),
|
|
385
|
+
"users.$get": (request: RequestInstance) => request.setCancelable(true),
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
const usersGet = configured.users.$get;
|
|
389
|
+
expect(usersGet.retry).toBe(5);
|
|
390
|
+
expect(usersGet.cancelable).toBe(true);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("should let a later level override a specific property while keeping others", () => {
|
|
394
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
395
|
+
const configured = sdk.$configure({
|
|
396
|
+
"*": { retry: 1, cache: true, cancelable: false },
|
|
397
|
+
"users.$get": { retry: 10 },
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
const usersGet = configured.users.$get;
|
|
401
|
+
expect(usersGet.retry).toBe(10);
|
|
402
|
+
expect(usersGet.cache).toBe(true);
|
|
403
|
+
expect(usersGet.cancelable).toBe(false);
|
|
404
|
+
});
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
describe("mixed plain-object and function values", () => {
|
|
408
|
+
it("should apply both plain objects and functions in the same configuration", () => {
|
|
409
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
410
|
+
const configured = sdk.$configure({
|
|
411
|
+
"*": { retry: 2 },
|
|
412
|
+
"users.$get": (request: RequestInstance) => request.setCancelable(true),
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
const usersGet = configured.users.$get;
|
|
416
|
+
expect(usersGet.retry).toBe(2);
|
|
417
|
+
expect(usersGet.cancelable).toBe(true);
|
|
418
|
+
|
|
419
|
+
const postsGet = configured.posts.$get;
|
|
420
|
+
expect(postsGet.retry).toBe(2);
|
|
421
|
+
expect(postsGet.cancelable).toBe(false);
|
|
422
|
+
});
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
describe("application order", () => {
|
|
426
|
+
it("should apply in order: global -> endpoint group -> method-specific", () => {
|
|
427
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
428
|
+
const configured = sdk.$configure({
|
|
429
|
+
"users.$get": { retry: 99 },
|
|
430
|
+
"*": { retry: 1 },
|
|
431
|
+
"/users": { retry: 10 },
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
// Method-specific (dot-path) should win since it's applied last
|
|
435
|
+
const usersGet = configured.users.$get;
|
|
436
|
+
expect(usersGet.retry).toBe(99);
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it("should apply endpoint group after global", () => {
|
|
440
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
441
|
+
const configured = sdk.$configure({
|
|
442
|
+
"/users": { retry: 10 },
|
|
443
|
+
"*": { retry: 1 },
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
const usersGet = configured.users.$get;
|
|
447
|
+
expect(usersGet.retry).toBe(10);
|
|
448
|
+
|
|
449
|
+
const postsGet = configured.posts.$get;
|
|
450
|
+
expect(postsGet.retry).toBe(1);
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
it("should apply function after plain-object in same category", () => {
|
|
454
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
455
|
+
const configured = sdk.$configure({
|
|
456
|
+
"*": { retry: 1, cancelable: false },
|
|
457
|
+
"users.$get": (request: RequestInstance) => request.setRetry(50),
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
const usersGet = configured.users.$get;
|
|
461
|
+
expect(usersGet.retry).toBe(50);
|
|
462
|
+
expect(usersGet.cancelable).toBe(false);
|
|
463
|
+
});
|
|
464
|
+
});
|
|
262
465
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -3999,30 +3999,58 @@ var endpointMatchesPattern = (endpoint, pattern) => {
|
|
|
3999
3999
|
}
|
|
4000
4000
|
return false;
|
|
4001
4001
|
};
|
|
4002
|
-
var
|
|
4002
|
+
var isDotPath = (key) => {
|
|
4003
|
+
return key.includes(".") && !key.startsWith("/");
|
|
4004
|
+
};
|
|
4005
|
+
var applyObjectDefaults = (request, config) => {
|
|
4006
|
+
let result = request;
|
|
4007
|
+
if (config.headers) result = result.setHeaders(config.headers);
|
|
4008
|
+
if (config.auth !== void 0) result = result.setAuth(config.auth);
|
|
4009
|
+
if (config.cache !== void 0) result = result.setCache(config.cache);
|
|
4010
|
+
if (config.cacheTime !== void 0) result = result.setCacheTime(config.cacheTime);
|
|
4011
|
+
if (config.staleTime !== void 0) result = result.setStaleTime(config.staleTime);
|
|
4012
|
+
if (config.retry !== void 0) result = result.setRetry(config.retry);
|
|
4013
|
+
if (config.retryTime !== void 0) result = result.setRetryTime(config.retryTime);
|
|
4014
|
+
if (config.cancelable !== void 0) result = result.setCancelable(config.cancelable);
|
|
4015
|
+
if (config.queued !== void 0) result = result.setQueued(config.queued);
|
|
4016
|
+
if (config.offline !== void 0) result = result.setOffline(config.offline);
|
|
4017
|
+
if (config.deduplicate !== void 0) result = result.setDeduplicate(config.deduplicate);
|
|
4018
|
+
if (config.deduplicateTime !== void 0 && config.deduplicateTime !== null) result = result.setDeduplicateTime(config.deduplicateTime);
|
|
4019
|
+
return result;
|
|
4020
|
+
};
|
|
4021
|
+
var applyDefaults = (request, context) => {
|
|
4022
|
+
const { endpoint, sdkPath, defaults } = context;
|
|
4003
4023
|
if (!defaults) return request;
|
|
4004
4024
|
let result = request;
|
|
4005
4025
|
const entries = Object.entries(defaults);
|
|
4026
|
+
const globalEntries = [];
|
|
4027
|
+
const groupEntries = [];
|
|
4028
|
+
const pathEntries = [];
|
|
4006
4029
|
for (let i = 0; i < entries.length; i += 1) {
|
|
4007
|
-
const [
|
|
4008
|
-
if (
|
|
4009
|
-
|
|
4010
|
-
|
|
4011
|
-
|
|
4012
|
-
|
|
4013
|
-
|
|
4014
|
-
|
|
4015
|
-
|
|
4016
|
-
|
|
4017
|
-
|
|
4018
|
-
|
|
4019
|
-
|
|
4020
|
-
|
|
4021
|
-
|
|
4030
|
+
const [key, value] = entries[i];
|
|
4031
|
+
if (!value) continue;
|
|
4032
|
+
const entry = [key, value];
|
|
4033
|
+
if (entry[0] === "*") globalEntries.push(entry);
|
|
4034
|
+
else if (isDotPath(entry[0])) pathEntries.push(entry);
|
|
4035
|
+
else groupEntries.push(entry);
|
|
4036
|
+
}
|
|
4037
|
+
const sorted = [
|
|
4038
|
+
...globalEntries,
|
|
4039
|
+
...groupEntries,
|
|
4040
|
+
...pathEntries
|
|
4041
|
+
];
|
|
4042
|
+
for (let i = 0; i < sorted.length; i += 1) {
|
|
4043
|
+
const [pattern, config] = sorted[i];
|
|
4044
|
+
let matches = false;
|
|
4045
|
+
if (isDotPath(pattern)) matches = pattern === sdkPath;
|
|
4046
|
+
else matches = endpointMatchesPattern(endpoint, pattern);
|
|
4047
|
+
if (matches) if (typeof config === "function") result = config(result);
|
|
4048
|
+
else result = applyObjectDefaults(result, config);
|
|
4022
4049
|
}
|
|
4023
4050
|
return result;
|
|
4024
4051
|
};
|
|
4025
|
-
var createRecursiveProxy = (client,
|
|
4052
|
+
var createRecursiveProxy = (client, context) => {
|
|
4053
|
+
const { path, sdkKeys, options } = context;
|
|
4026
4054
|
return new Proxy(() => {}, { get: (_target, key) => {
|
|
4027
4055
|
if (typeof key === "symbol" || key === "inspect") return;
|
|
4028
4056
|
let isMethod = false;
|
|
@@ -4033,14 +4061,24 @@ var createRecursiveProxy = (client, path, options) => {
|
|
|
4033
4061
|
methodName = key.slice(1);
|
|
4034
4062
|
pathSegment = `:${key.slice(1)}`;
|
|
4035
4063
|
} else if (options?.camelCaseToKebabCase) pathSegment = key.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
4064
|
+
const currentSdkKeys = [...sdkKeys, key];
|
|
4065
|
+
const sdkPath = currentSdkKeys.join(".");
|
|
4036
4066
|
const endpoint = `/${path.join("/")}`;
|
|
4037
4067
|
const method = getMethod(isMethod ? methodName : key, options);
|
|
4038
4068
|
let request = client.createRequest()({
|
|
4039
4069
|
endpoint,
|
|
4040
4070
|
method
|
|
4041
4071
|
});
|
|
4042
|
-
request = applyDefaults(request,
|
|
4043
|
-
|
|
4072
|
+
request = applyDefaults(request, {
|
|
4073
|
+
endpoint,
|
|
4074
|
+
sdkPath,
|
|
4075
|
+
defaults: options?.defaults
|
|
4076
|
+
});
|
|
4077
|
+
const deeperProxy = createRecursiveProxy(client, {
|
|
4078
|
+
path: [...path, pathSegment],
|
|
4079
|
+
sdkKeys: currentSdkKeys,
|
|
4080
|
+
options
|
|
4081
|
+
});
|
|
4044
4082
|
return new Proxy(request, { get: (reqTarget, reqKey) => {
|
|
4045
4083
|
if (reqKey in reqTarget) return reqTarget[reqKey];
|
|
4046
4084
|
return deeperProxy[reqKey];
|
|
@@ -4054,7 +4092,11 @@ var createSdk = (client, options) => {
|
|
|
4054
4092
|
methodTransform,
|
|
4055
4093
|
...rest
|
|
4056
4094
|
};
|
|
4057
|
-
const proxy = createRecursiveProxy(client,
|
|
4095
|
+
const proxy = createRecursiveProxy(client, {
|
|
4096
|
+
path: [],
|
|
4097
|
+
sdkKeys: [],
|
|
4098
|
+
options: mergedOptions
|
|
4099
|
+
});
|
|
4058
4100
|
return new Proxy(proxy, { get: (target, key) => {
|
|
4059
4101
|
if (key === "$configure") return (defaults) => {
|
|
4060
4102
|
const existingDefaults = mergedOptions.defaults || {};
|