@hyper-fetch/core 7.1.2 → 7.2.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyper-fetch/core",
3
- "version": "7.1.2",
3
+ "version": "7.2.1",
4
4
  "description": "Cache, Queue and Persist your requests no matter if you are online or offline!",
5
5
  "author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
6
6
  "homepage": "https://hyperfetch.bettertyped.com/",
@@ -103,6 +103,7 @@ export type QueryParamsType = Record<string, QueryParamType>;
103
103
 
104
104
  // Responses
105
105
 
106
+ // It cannot be a union type because graphql may return both data and error
106
107
  export type RequestResponseType<Request extends RequestInstance> = {
107
108
  data: ExtractResponseType<Request> | null;
108
109
  error: ExtractErrorType<Request> | null;
@@ -112,6 +113,7 @@ export type RequestResponseType<Request extends RequestInstance> = {
112
113
  responseTimestamp: number;
113
114
  requestTimestamp: number;
114
115
  };
116
+
115
117
  export type ResponseType<GenericDataType, GenericErrorType, Adapter extends AdapterInstance> = {
116
118
  data: GenericDataType | null;
117
119
  error: GenericErrorType | null;
@@ -10,7 +10,9 @@ export function createClient<
10
10
  ClientProperties extends ClientGenericType = {
11
11
  error?: ClientErrorType;
12
12
  },
13
- >(options: ClientOptionsType<Client<NonNullable<TypeWithDefaults<ClientProperties, "error", ClientErrorType>>>>) {
13
+ >(
14
+ options: ClientOptionsType<Client<NonNullable<TypeWithDefaults<ClientProperties, "error", ClientErrorType>>>>,
15
+ ): Client<NonNullable<TypeWithDefaults<ClientProperties, "error", ClientErrorType>>, HttpAdapterType> {
14
16
  return new Client<NonNullable<TypeWithDefaults<ClientProperties, "error", ClientErrorType>>, HttpAdapterType>(
15
17
  options,
16
18
  );
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ export * from "./plugin";
4
4
  export * from "./client";
5
5
  export * from "./cache";
6
6
  export * from "./adapter";
7
+ export * from "./sdk";
7
8
  export * from "./http-adapter";
8
9
  export * from "./request";
9
10
  export * from "./constants/time.constants";
@@ -385,7 +385,7 @@ export class Request<
385
385
  let stringEndpoint = String(endpoint);
386
386
  if (params) {
387
387
  Object.entries(params).forEach(([key, value]) => {
388
- stringEndpoint = endpoint.replace(new RegExp(`:${key}`, "g"), String(value));
388
+ stringEndpoint = stringEndpoint.replace(new RegExp(`:${key}`, "g"), String(value));
389
389
  });
390
390
  }
391
391
 
@@ -1,8 +1,8 @@
1
- import { ProgressType, ResponseType, getErrorMessage } from "adapter";
1
+ import { ProgressType, RequestResponseType, ResponseType, getErrorMessage } from "adapter";
2
2
  import { ProgressEventType, RequestInstance, RequestJSON, RequestSendOptionsType } from "request";
3
3
  import { HttpMethods } from "constants/http.constants";
4
4
  import { canRetryRequest, Dispatcher } from "dispatcher";
5
- import { ExtractAdapterType, ExtractErrorType, ExtractResponseType } from "types";
5
+ import { ExtractAdapterType, ExtractErrorType } from "types";
6
6
 
7
7
  export const stringifyKey = (value: unknown): string => {
8
8
  try {
@@ -114,14 +114,12 @@ export const getRequestDispatcher = <Request extends RequestInstance>(
114
114
  export const sendRequest = <Request extends RequestInstance>(
115
115
  request: Request,
116
116
  options?: RequestSendOptionsType<Request>,
117
- ) => {
117
+ ): Promise<RequestResponseType<Request>> => {
118
118
  const { client } = request;
119
119
  const { requestManager } = client;
120
120
  const [dispatcher] = getRequestDispatcher(request, options?.dispatcherType);
121
121
 
122
- return new Promise<
123
- ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>>
124
- >((resolve) => {
122
+ return new Promise<RequestResponseType<Request>>((resolve) => {
125
123
  let isResolved = false;
126
124
  const requestId = dispatcher.add(request);
127
125
  options?.onBeforeSent?.({ requestId, request });
@@ -0,0 +1 @@
1
+ export * from "./sdk";
package/src/sdk/sdk.ts ADDED
@@ -0,0 +1,44 @@
1
+ import { ClientInstance } from "client";
2
+
3
+ export type RecursiveSchemaType = Record<
4
+ string, // for example users / $userId / posts / $postId
5
+ any
6
+ >;
7
+
8
+ const createRecursiveProxy = (client: ClientInstance, path: string[]): any => {
9
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
10
+ return new Proxy(() => {}, {
11
+ get: (_target, key: string) => {
12
+ if (typeof key === "symbol" || key === "inspect") {
13
+ return undefined;
14
+ }
15
+
16
+ // Assume the key is a method for the current path
17
+ const endpoint = `/${path.join("/")}`;
18
+ const method = key.toUpperCase();
19
+ const request = client.createRequest()({ endpoint, method });
20
+
21
+ // But also, assume the key is a new path segment for a deeper call
22
+ const newPath = [...path, key.startsWith("$") ? `:${key.slice(1)}` : key];
23
+ const deeperProxy = createRecursiveProxy(client, newPath);
24
+
25
+ // Return a new proxy that wraps both the request and the deeper proxy
26
+ return new Proxy(request, {
27
+ get: (reqTarget, reqKey: string) => {
28
+ // If the property exists on the request instance (like .send(), .setParams()), return it.
29
+ if (reqKey in reqTarget) {
30
+ return reqTarget[reqKey as keyof typeof reqTarget];
31
+ }
32
+ // Otherwise, it's a deeper path, so delegate to the deeper proxy.
33
+ return deeperProxy[reqKey];
34
+ },
35
+ });
36
+ },
37
+ });
38
+ };
39
+
40
+ export const createSdk = <Client extends ClientInstance, RecursiveSchema extends RecursiveSchemaType>(
41
+ client: Client,
42
+ ): RecursiveSchema => {
43
+ return createRecursiveProxy(client, []) as RecursiveSchema;
44
+ };