@hyper-fetch/core 8.1.1 → 8.2.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.
Files changed (51) hide show
  1. package/README.md +137 -52
  2. package/__tests__/features/sdk/sdk.base.spec.ts +7 -11
  3. package/__tests__/features/sdk/sdk.configuration.spec.ts +5 -7
  4. package/__tests__/features/sdk/sdk.types.spec.ts +284 -0
  5. package/dist/adapter/adapter.d.ts +18 -1
  6. package/dist/adapter/adapter.d.ts.map +1 -1
  7. package/dist/adapter/adapter.types.d.ts +8 -0
  8. package/dist/adapter/adapter.types.d.ts.map +1 -1
  9. package/dist/cache/cache.d.ts +3 -6
  10. package/dist/cache/cache.d.ts.map +1 -1
  11. package/dist/cache/cache.events.d.ts +5 -2
  12. package/dist/cache/cache.events.d.ts.map +1 -1
  13. package/dist/cache/cache.types.d.ts +1 -0
  14. package/dist/cache/cache.types.d.ts.map +1 -1
  15. package/dist/client/client.d.ts +5 -1
  16. package/dist/client/client.d.ts.map +1 -1
  17. package/dist/dispatcher/dispatcher.d.ts +1 -1
  18. package/dist/index.mjs +74 -9
  19. package/dist/index.mjs.map +1 -1
  20. package/dist/managers/app/app.manager.events.d.ts +9 -0
  21. package/dist/managers/app/app.manager.events.d.ts.map +1 -1
  22. package/dist/managers/request/request.manager.events.d.ts +12 -1
  23. package/dist/managers/request/request.manager.events.d.ts.map +1 -1
  24. package/dist/mocker/mocker.types.d.ts +1 -0
  25. package/dist/mocker/mocker.types.d.ts.map +1 -1
  26. package/dist/plugin/plugin.d.ts +16 -0
  27. package/dist/plugin/plugin.d.ts.map +1 -1
  28. package/dist/plugin/plugin.types.d.ts +1 -0
  29. package/dist/plugin/plugin.types.d.ts.map +1 -1
  30. package/dist/request/request.d.ts +28 -0
  31. package/dist/request/request.d.ts.map +1 -1
  32. package/dist/request/request.types.d.ts +89 -2
  33. package/dist/request/request.types.d.ts.map +1 -1
  34. package/dist/sdk/sdk.d.ts +25 -5
  35. package/dist/sdk/sdk.d.ts.map +1 -1
  36. package/package.json +1 -1
  37. package/src/adapter/adapter.ts +18 -1
  38. package/src/adapter/adapter.types.ts +8 -0
  39. package/src/cache/cache.events.ts +5 -2
  40. package/src/cache/cache.ts +3 -6
  41. package/src/cache/cache.types.ts +1 -0
  42. package/src/client/client.ts +5 -1
  43. package/src/dispatcher/dispatcher.ts +1 -1
  44. package/src/managers/app/app.manager.events.ts +9 -0
  45. package/src/managers/request/request.manager.events.ts +12 -9
  46. package/src/mocker/mocker.types.ts +1 -0
  47. package/src/plugin/plugin.ts +16 -0
  48. package/src/plugin/plugin.types.ts +1 -0
  49. package/src/request/request.ts +28 -0
  50. package/src/request/request.types.ts +103 -2
  51. package/src/sdk/sdk.ts +55 -6
@@ -36,6 +36,38 @@ export type RequestInstanceProperties = {
36
36
  hasPayload?: boolean;
37
37
  };
38
38
 
39
+ /**
40
+ * The **constraint** form of a Request type. Mindset: "any Request that matches this partial shape".
41
+ *
42
+ * Use `RequestInstance` when you are writing a **reusable abstraction** that accepts requests from
43
+ * the outside - generic helpers (`<T extends RequestInstance>`), reusable UI components / hooks,
44
+ * `sdk.$configure({...})` callbacks, mocking utilities, interceptors, return types, etc.
45
+ *
46
+ * Unspecified generic parameters default to `any` **on purpose**: an omitted field means
47
+ * "I do not care about that field, the caller picks anything". This is exactly what you want
48
+ * for a constraint - any concrete `Request` satisfies it. Partial constraints work too:
49
+ * `RequestInstance<{ response: User[] }>` accepts every request that returns `User[]`,
50
+ * regardless of payload, params, queryParams, error, etc.
51
+ *
52
+ * For describing a **single endpoint** inside an SDK schema, use {@link RequestModel} instead.
53
+ * `RequestModel` uses safe non-`any` defaults (`unknown`, `undefined`, `Error`, `string`, `false`)
54
+ * so omitted fields stay strict instead of silently collapsing to `any`.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * // Reusable component: accepts any request whose response is User[]
59
+ * function UserList<T extends RequestInstance<{ response: User[] }>>(props: { request: T }) {
60
+ * // ...
61
+ * }
62
+ *
63
+ * // sdk.$configure callback: accepts any request the SDK happens to dispatch
64
+ * sdk.$configure({
65
+ * "users.$get": (request: RequestInstance) => request.setMock(() => ({ data: [] })),
66
+ * });
67
+ * ```
68
+ *
69
+ * @see {@link RequestModel} - definition counterpart for SDK schema modeling.
70
+ */
39
71
  export type RequestInstance<
40
72
  RequestProperties extends RequestInstanceProperties = {
41
73
  response?: any;
@@ -57,6 +89,75 @@ export type RequestInstance<
57
89
  any
58
90
  >;
59
91
 
92
+ /**
93
+ * The **definition** form of a Request type. Mindset: "**this** specific endpoint".
94
+ *
95
+ * Use `RequestModel` when you are **defining** a request inside an SDK schema or a request
96
+ * factory. Unlike {@link RequestInstance}, every unspecified field stays strict instead of
97
+ * falling back to `any` - because in a definition, an omitted field means "**there is no
98
+ * payload / no query / no params here**", not "anything goes". The type system will reject
99
+ * mismatches at the call site instead of silently erasing them.
100
+ *
101
+ * The `client` field is also injected automatically by `createSdk(client)` at the SDK boundary,
102
+ * so you never need to repeat it inside schema declarations.
103
+ *
104
+ * ### Defaults for omitted fields
105
+ *
106
+ * | Field | Default | Why |
107
+ * | ----------------- | ---------------- | ---------------------------------------------------------------------- |
108
+ * | `response` | `unknown` | Forces narrowing in consumer code; never silently widened. |
109
+ * | `payload` | `undefined` | "No body declared." `.setData()` is required for typed bodies. |
110
+ * | `queryParams` | `undefined` | Same as payload. |
111
+ * | `error` | `Error` | Sensible default; override per-endpoint when the API has a known shape.|
112
+ * | `endpoint` | `string` | Allows literal narrowing without erasure. Pass a string literal. |
113
+ * | `client` | `ClientInstance` | Injected by `createSdk(client)`; you do not need to set this manually. |
114
+ * | `hasPayload` | `false` | "Caller must call `.setData()`." Override only when payload is bound. |
115
+ * | `hasParams` | `false` | Same as `hasPayload` for params. |
116
+ * | `hasQueryParams` | `false` | Same as `hasPayload` for query params. |
117
+ * | `mutationContext` | `undefined` | Optional context; default is "no context". |
118
+ *
119
+ * @example
120
+ * ```ts
121
+ * // Schema describes shape only - client is injected by createSdk(client).
122
+ * type ApiSchema = {
123
+ * users: {
124
+ * $get: RequestModel<{
125
+ * response: User[];
126
+ * endpoint: "/users";
127
+ * }>;
128
+ * $userId: {
129
+ * $get: RequestModel<{
130
+ * response: User;
131
+ * endpoint: "/users/:userId";
132
+ * }>;
133
+ * };
134
+ * };
135
+ * };
136
+ * ```
137
+ *
138
+ * @see {@link RequestInstance} - constraint counterpart for accepting any Request.
139
+ */
140
+ export type RequestModel<
141
+ RequestProperties extends RequestInstanceProperties = {
142
+ response?: unknown;
143
+ payload?: undefined;
144
+ queryParams?: undefined;
145
+ error?: Error;
146
+ client?: ClientInstance;
147
+ },
148
+ > = Request<
149
+ TypeWithDefaults<RequestProperties, "response", unknown>,
150
+ TypeWithDefaults<RequestProperties, "payload", undefined>,
151
+ TypeWithDefaults<RequestProperties, "queryParams", undefined>,
152
+ TypeWithDefaults<RequestProperties, "error", Error>,
153
+ TypeWithDefaults<RequestProperties, "endpoint", string>,
154
+ TypeWithDefaults<RequestProperties, "client", ClientInstance>,
155
+ TypeWithDefaults<RequestProperties, "hasPayload", false>,
156
+ TypeWithDefaults<RequestProperties, "hasParams", false>,
157
+ TypeWithDefaults<RequestProperties, "hasQueryParams", false>,
158
+ undefined
159
+ >;
160
+
60
161
  // Progress
61
162
  export type ProgressEventType = { total: number; loaded: number };
62
163
 
@@ -108,7 +209,7 @@ export type RequestJSON<Request extends RequestInstance> = {
108
209
  */
109
210
  export type RequestOptionsType<GenericEndpoint, AdapterOptions, RequestMethods = HttpMethodsType> = {
110
211
  /**
111
- * Determine the endpoint for request request
212
+ * Determine the endpoint for the request
112
213
  */
113
214
  endpoint: GenericEndpoint;
114
215
  /**
@@ -149,7 +250,7 @@ export type RequestOptionsType<GenericEndpoint, AdapterOptions, RequestMethods =
149
250
  */
150
251
  staleTime?: number;
151
252
  /**
152
- * Should the requests from this request be send one-by-one
253
+ * Should the requests be queued and sent one-by-one
153
254
  */
154
255
  queued?: boolean;
155
256
  /**
package/src/sdk/sdk.ts CHANGED
@@ -1,12 +1,51 @@
1
1
  import type { ClientInstance } from "client";
2
- import type { RequestInstance } from "request";
3
- import type { ExtractEndpointType } from "types";
2
+ import type { Request, RequestInstance } from "request";
3
+ import type {
4
+ ExtractEndpointType,
5
+ ExtractResponseType,
6
+ ExtractPayloadType,
7
+ ExtractQueryParamsType,
8
+ ExtractLocalErrorType,
9
+ ExtractHasPayloadType,
10
+ ExtractHasParamsType,
11
+ ExtractHasQueryParamsType,
12
+ ExtractMutationContextType,
13
+ } from "types";
4
14
 
5
15
  export type RecursiveSchemaType = Record<
6
16
  string, // for example users / $userId / posts / $postId
7
17
  any
8
18
  >;
9
19
 
20
+ /**
21
+ * Recursively walks a schema and rebuilds every `Request` leaf with the SDK's actual client
22
+ * type injected into the `Client` generic slot. Lets users omit `client` from their
23
+ * `RequestModel<{...}>` declarations - the SDK fills it in from the client passed to
24
+ * `createSdk(client)`.
25
+ *
26
+ * Non-Request leaves (primitive values, plain types) are passed through unchanged. Nested
27
+ * schema objects are recursed into. Depth is guarded at 10 levels to keep the type checker
28
+ * within reasonable bounds.
29
+ */
30
+ export type InjectClient<T, TClient extends ClientInstance, Depth extends unknown[] = []> = Depth["length"] extends 10
31
+ ? T
32
+ : T extends RequestInstance
33
+ ? Request<
34
+ ExtractResponseType<T>,
35
+ ExtractPayloadType<T>,
36
+ ExtractQueryParamsType<T>,
37
+ ExtractLocalErrorType<T>,
38
+ ExtractEndpointType<T> extends string ? ExtractEndpointType<T> : string,
39
+ TClient,
40
+ ExtractHasPayloadType<T> extends boolean ? ExtractHasPayloadType<T> : false,
41
+ ExtractHasParamsType<T> extends boolean ? ExtractHasParamsType<T> : false,
42
+ ExtractHasQueryParamsType<T> extends boolean ? ExtractHasQueryParamsType<T> : false,
43
+ ExtractMutationContextType<T>
44
+ >
45
+ : T extends Record<string, any>
46
+ ? { [K in keyof T]: InjectClient<T[K], TClient, [...Depth, unknown]> }
47
+ : T;
48
+
10
49
  /**
11
50
  * Per-request defaults that can be applied via SDK configuration.
12
51
  * These mirror the chainable setters on Request (headers, cache, retry, etc.).
@@ -242,18 +281,28 @@ const createRecursiveProxy = (
242
281
  });
243
282
  };
244
283
 
245
- export type SdkInstance<Schema extends RecursiveSchemaType> = Schema & {
284
+ /**
285
+ * The fully-resolved SDK instance type returned by `createSdk`.
286
+ *
287
+ * The schema is rewritten via {@link InjectClient} so every `Request` leaf carries the
288
+ * actual client type passed to `createSdk(client)` - users do not need to repeat
289
+ * `client: AppClient` in every `RequestModel<{...}>` declaration.
290
+ */
291
+ export type SdkInstance<
292
+ Schema extends RecursiveSchemaType,
293
+ TClient extends ClientInstance = ClientInstance,
294
+ > = InjectClient<Schema, TClient> & {
246
295
  /**
247
296
  * Apply request defaults to the SDK. Returns a new SDK instance with the configuration applied.
248
297
  * Use "*" to match all endpoints, or specific endpoint strings / wildcard patterns.
249
298
  */
250
- $configure: (defaults: SdkConfigurationMap<Schema>) => SdkInstance<Schema>;
299
+ $configure: (defaults: SdkConfigurationMap<Schema>) => SdkInstance<Schema, TClient>;
251
300
  };
252
301
 
253
302
  export const createSdk = <Client extends ClientInstance, RecursiveSchema extends RecursiveSchemaType>(
254
303
  client: Client,
255
304
  options?: CreateSdkOptions<RecursiveSchema>,
256
- ): SdkInstance<RecursiveSchema> => {
305
+ ): SdkInstance<RecursiveSchema, Client> => {
257
306
  const {
258
307
  camelCaseToKebabCase = true,
259
308
  methodTransform = (method: string) => method.toUpperCase(),
@@ -277,7 +326,7 @@ export const createSdk = <Client extends ClientInstance, RecursiveSchema extends
277
326
  }
278
327
  return target[key];
279
328
  },
280
- }) as SdkInstance<RecursiveSchema>;
329
+ }) as SdkInstance<RecursiveSchema, Client>;
281
330
  };
282
331
 
283
332
  /**