@unifiedcommerce/sdk 0.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/dist/src/client.d.ts +4433 -0
- package/dist/src/client.d.ts.map +1 -0
- package/dist/src/client.js +271 -0
- package/dist/src/generated/api-types.d.ts +14013 -0
- package/dist/src/generated/api-types.d.ts.map +1 -0
- package/dist/src/generated/api-types.js +5 -0
- package/dist/src/generator.d.ts +9 -0
- package/dist/src/generator.d.ts.map +1 -0
- package/dist/src/generator.js +90 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +2 -0
- package/dist/src/middleware.d.ts +17 -0
- package/dist/src/middleware.d.ts.map +1 -0
- package/dist/src/middleware.js +18 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +63 -0
- package/src/client.ts +305 -0
- package/src/generated/api-types.ts +14013 -0
- package/src/index.ts +3 -0
- package/src/middleware.ts +31 -0
- package/src/react.ts +43 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Middleware } from "openapi-fetch";
|
|
2
|
+
|
|
3
|
+
export interface ApiKeyAuth {
|
|
4
|
+
type: "api_key";
|
|
5
|
+
key: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface BearerAuth {
|
|
9
|
+
type: "bearer";
|
|
10
|
+
token: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type AuthCredential = ApiKeyAuth | BearerAuth;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* openapi-fetch middleware that injects authentication headers.
|
|
17
|
+
*
|
|
18
|
+
* Supports API key (x-api-key header) and Bearer token (Authorization header).
|
|
19
|
+
*/
|
|
20
|
+
export function authMiddleware(credential: AuthCredential): Middleware {
|
|
21
|
+
return {
|
|
22
|
+
onRequest({ request }) {
|
|
23
|
+
if (credential.type === "api_key") {
|
|
24
|
+
request.headers.set("x-api-key", credential.key);
|
|
25
|
+
} else if (credential.type === "bearer") {
|
|
26
|
+
request.headers.set("Authorization", `Bearer ${credential.token}`);
|
|
27
|
+
}
|
|
28
|
+
return request;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
package/src/react.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React Query integration for the UnifiedCommerce SDK.
|
|
3
|
+
*
|
|
4
|
+
* Wraps the openapi-fetch client in TanStack Query hooks (useQuery, useMutation,
|
|
5
|
+
* useSuspenseQuery) with the same generated path types.
|
|
6
|
+
*
|
|
7
|
+
* Requires peer dependencies: @tanstack/react-query >= 5.0.0, openapi-react-query >= 0.3.0
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { createSDK } from "@unifiedcommerce/sdk";
|
|
12
|
+
* import { createCommerceHooks } from "@unifiedcommerce/sdk/react";
|
|
13
|
+
*
|
|
14
|
+
* const sdk = createSDK({ baseUrl: "http://localhost:4000", auth: { type: "api_key", key: "..." } });
|
|
15
|
+
* const commerce = createCommerceHooks(sdk.raw);
|
|
16
|
+
*
|
|
17
|
+
* function ProductList() {
|
|
18
|
+
* const { data, isLoading } = commerce.useQuery("get", "/api/catalog/entities", {
|
|
19
|
+
* params: { query: { type: "product", limit: "20" } },
|
|
20
|
+
* });
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import createQueryHooks from "openapi-react-query";
|
|
26
|
+
import type createClient from "openapi-fetch";
|
|
27
|
+
import type { paths } from "./generated/api-types";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Type alias for the hooks object returned by createCommerceHooks.
|
|
31
|
+
* Plugin hook factories accept this type as their parameter.
|
|
32
|
+
*/
|
|
33
|
+
export type CommerceHooks = ReturnType<typeof createCommerceHooks>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Creates TanStack Query hooks from the typed openapi-fetch client.
|
|
37
|
+
*
|
|
38
|
+
* @param client - The raw openapi-fetch client (sdk.raw)
|
|
39
|
+
* @returns Typed useQuery, useMutation, useSuspenseQuery hooks for all API paths
|
|
40
|
+
*/
|
|
41
|
+
export function createCommerceHooks(client: ReturnType<typeof createClient<paths>>) {
|
|
42
|
+
return createQueryHooks(client);
|
|
43
|
+
}
|