Skip to content
This repository was archived by the owner on Nov 12, 2024. It is now read-only.

Error type is undefined #4

Closed
brunolca opened this issue Jul 15, 2024 · 1 comment
Closed

Error type is undefined #4

brunolca opened this issue Jul 15, 2024 · 1 comment

Comments

@brunolca
Copy link

brunolca commented Jul 15, 2024

When trying to use this lib, It seems that the type of error is undefined.

When using it:

function App() {
  const { 
    data, 
    error // const error: undefined
  } = usePetStore("/pets", {});
  return <pre>{JSON.stringify({ data, error }, null, 2)}</pre>;
}

I found a solution with the following patch:

diff --git a/node_modules/swr-openapi/dist/index.d.ts b/node_modules/swr-openapi/dist/index.d.ts
index 7ddd9e5..d705d15 100644
--- a/node_modules/swr-openapi/dist/index.d.ts
+++ b/node_modules/swr-openapi/dist/index.d.ts
@@ -7,12 +7,13 @@ import type { PartialDeep } from "type-fest";
 export type SwrOpenApiConfiguration = {
     matchKeyComparator?: (a: object, b: object) => boolean;
 };
+
 /**
  * Apply an optional configuration to the library at runtime
  */
 export declare function applySwrOpenApiConfiguration(config: SwrOpenApiConfiguration): void;
 export declare function createHooks<Paths extends {}>(api: ReturnType<typeof createClient<Paths>>, keyPrefix: string): {
-    use: <Path extends PathsWithMethod<Paths, "get">, Req extends FilterKeys<Paths[Path], "get">, Options extends FetchOptions<Req>, Data extends ParseAsResponse<FilterKeys<SuccessResponse<ResponseObjectMap<Req>>, MediaType>, Options>, Error extends FilterKeys<ErrorResponse<ResponseObjectMap<Req>>, MediaType>, Config extends SWRConfiguration<Data, Error>>(path: Path, options: Options | null, swrConfig?: Config) => import("swr").SWRResponse<Data, Error, Config>;
-    useInfinite: <Path extends PathsWithMethod<Paths, "get">, Req extends FilterKeys<Paths[Path], "get">, Options extends FetchOptions<Req>, Data extends ParseAsResponse<FilterKeys<SuccessResponse<ResponseObjectMap<Req>>, MediaType>, Options>, Error extends FilterKeys<ErrorResponse<ResponseObjectMap<Req>>, MediaType>, Config extends SWRConfiguration<Data, Error>>(path: Path, getOptionsFn: SWRInfiniteKeyLoader<Data, Options | null>, swrConfig?: Config) => import("swr/dist/infinite").SWRInfiniteResponse<Data, Error>;
+    use: <Path extends PathsWithMethod<Paths, "get">, Req extends FilterKeys<Paths[Path], "get">, Options extends FetchOptions<Req>, Data extends ParseAsResponse<FilterKeys<SuccessResponse<ResponseObjectMap<Req>>, MediaType>, Options>, Error extends ErrorResponse<ResponseObjectMap<Req>>, Config extends SWRConfiguration<Data, Error>>(path: Path, options: Options | null, swrConfig?: Config) => import("swr").SWRResponse<Data, Error, Config>;
+    useInfinite: <Path extends PathsWithMethod<Paths, "get">, Req extends FilterKeys<Paths[Path], "get">, Options extends FetchOptions<Req>, Data extends ParseAsResponse<FilterKeys<SuccessResponse<ResponseObjectMap<Req>>, MediaType>, Options>, Error extends ErrorResponse<ResponseObjectMap<Req>>, Config extends SWRConfiguration<Data, Error>>(path: Path, getOptionsFn: SWRInfiniteKeyLoader<Data, Options | null>, swrConfig?: Config) => import("swr/dist/infinite").SWRInfiniteResponse<Data, Error>;
     readonly matchKey: <Path extends PathsWithMethod<Paths, "get">, Req extends FilterKeys<Paths[Path], "get">, Options extends FetchOptions<Req>>(path: Path, pathOptions?: PartialDeep<Options>) => (key: unknown) => boolean;
 };

Note that I updated Error extends FilterKeys<ErrorResponse<ResponseObjectMap<Req>> to simply use Error extends ErrorResponse<ResponseObjectMap<Req>>

I made a minimal reproduction repository brunolca/reproduction-swr-openapi-error-type and the same example with the patch applied brunolca/reproduction-swr-openapi-error-type#1

Packages versions i used:

"dependencies": {
    "openapi-fetch": "^0.10.2",
    "react": "^18.3.1",
    "react-dom": "^18.3.1",
    "swr": "^2.2.5",
    "swr-openapi": "^4.0.0"
}
@htunnicliff
Copy link
Member

@brunolca I believe this issue should be resolved by updates made in #8 (v5).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants