@openrouter/sdk 0.1.17 → 0.1.18
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/README.md +0 -8
- package/esm/lib/config.d.ts +10 -2
- package/esm/lib/config.js +2 -2
- package/esm/lib/env.d.ts +13 -0
- package/esm/lib/env.js +16 -0
- package/esm/lib/sdks.js +2 -2
- package/jsr.json +1 -1
- package/package.json +2 -16
- package/REACT_QUERY.md +0 -296
- package/esm/react-query/_context.d.ts +0 -8
- package/esm/react-query/_context.js +0 -14
- package/esm/react-query/_types.d.ts +0 -27
- package/esm/react-query/_types.js +0 -5
- package/esm/react-query/analyticsGetUserActivity.d.ts +0 -36
- package/esm/react-query/analyticsGetUserActivity.js +0 -77
- package/esm/react-query/apiKeysCreate.d.ts +0 -20
- package/esm/react-query/apiKeysCreate.js +0 -39
- package/esm/react-query/apiKeysDelete.d.ts +0 -20
- package/esm/react-query/apiKeysDelete.js +0 -39
- package/esm/react-query/apiKeysGet.d.ts +0 -24
- package/esm/react-query/apiKeysGet.js +0 -66
- package/esm/react-query/apiKeysGetCurrentKeyMetadata.d.ts +0 -29
- package/esm/react-query/apiKeysGetCurrentKeyMetadata.js +0 -66
- package/esm/react-query/apiKeysList.d.ts +0 -37
- package/esm/react-query/apiKeysList.js +0 -69
- package/esm/react-query/apiKeysUpdate.d.ts +0 -20
- package/esm/react-query/apiKeysUpdate.js +0 -39
- package/esm/react-query/betaResponsesSend.d.ts +0 -24
- package/esm/react-query/betaResponsesSend.js +0 -42
- package/esm/react-query/chatSend.d.ts +0 -24
- package/esm/react-query/chatSend.js +0 -42
- package/esm/react-query/completionsGenerate.d.ts +0 -23
- package/esm/react-query/completionsGenerate.js +0 -42
- package/esm/react-query/creditsCreateCoinbaseCharge.d.ts +0 -25
- package/esm/react-query/creditsCreateCoinbaseCharge.js +0 -42
- package/esm/react-query/creditsGetCredits.d.ts +0 -29
- package/esm/react-query/creditsGetCredits.js +0 -66
- package/esm/react-query/embeddingsGenerate.d.ts +0 -23
- package/esm/react-query/embeddingsGenerate.js +0 -42
- package/esm/react-query/embeddingsListModels.d.ts +0 -29
- package/esm/react-query/embeddingsListModels.js +0 -66
- package/esm/react-query/endpointsList.d.ts +0 -24
- package/esm/react-query/endpointsList.js +0 -66
- package/esm/react-query/endpointsListZdrEndpoints.d.ts +0 -23
- package/esm/react-query/endpointsListZdrEndpoints.js +0 -60
- package/esm/react-query/generationsGetGeneration.d.ts +0 -30
- package/esm/react-query/generationsGetGeneration.js +0 -71
- package/esm/react-query/index.d.ts +0 -27
- package/esm/react-query/index.js +0 -30
- package/esm/react-query/modelsCount.d.ts +0 -23
- package/esm/react-query/modelsCount.js +0 -60
- package/esm/react-query/modelsList.d.ts +0 -38
- package/esm/react-query/modelsList.js +0 -69
- package/esm/react-query/modelsListForUser.d.ts +0 -24
- package/esm/react-query/modelsListForUser.js +0 -60
- package/esm/react-query/oAuthCreateAuthCode.d.ts +0 -23
- package/esm/react-query/oAuthCreateAuthCode.js +0 -42
- package/esm/react-query/oAuthExchangeAuthCodeForAPIKey.d.ts +0 -23
- package/esm/react-query/oAuthExchangeAuthCodeForAPIKey.js +0 -42
- package/esm/react-query/parametersGetParameters.d.ts +0 -38
- package/esm/react-query/parametersGetParameters.js +0 -80
- package/esm/react-query/providersList.d.ts +0 -23
- package/esm/react-query/providersList.js +0 -60
package/README.md
CHANGED
|
@@ -19,32 +19,24 @@ The SDK can be installed with either [npm](https://www.npmjs.com/), [pnpm](https
|
|
|
19
19
|
|
|
20
20
|
```bash
|
|
21
21
|
npm add @openrouter/sdk
|
|
22
|
-
# Install optional peer dependencies if you plan to use React hooks
|
|
23
|
-
npm add @tanstack/react-query react react-dom
|
|
24
22
|
```
|
|
25
23
|
|
|
26
24
|
### PNPM
|
|
27
25
|
|
|
28
26
|
```bash
|
|
29
27
|
pnpm add @openrouter/sdk
|
|
30
|
-
# Install optional peer dependencies if you plan to use React hooks
|
|
31
|
-
pnpm add @tanstack/react-query react react-dom
|
|
32
28
|
```
|
|
33
29
|
|
|
34
30
|
### Bun
|
|
35
31
|
|
|
36
32
|
```bash
|
|
37
33
|
bun add @openrouter/sdk
|
|
38
|
-
# Install optional peer dependencies if you plan to use React hooks
|
|
39
|
-
bun add @tanstack/react-query react react-dom
|
|
40
34
|
```
|
|
41
35
|
|
|
42
36
|
### Yarn
|
|
43
37
|
|
|
44
38
|
```bash
|
|
45
39
|
yarn add @openrouter/sdk
|
|
46
|
-
# Install optional peer dependencies if you plan to use React hooks
|
|
47
|
-
yarn add @tanstack/react-query react react-dom
|
|
48
40
|
```
|
|
49
41
|
|
|
50
42
|
> [!NOTE]
|
package/esm/lib/config.d.ts
CHANGED
|
@@ -13,6 +13,14 @@ export declare const ServerList: {
|
|
|
13
13
|
};
|
|
14
14
|
export type SDKOptions = {
|
|
15
15
|
apiKey?: string | (() => Promise<string>) | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Allows setting the httpReferer parameter for all supported operations
|
|
18
|
+
*/
|
|
19
|
+
httpReferer?: string | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Allows setting the xTitle parameter for all supported operations
|
|
22
|
+
*/
|
|
23
|
+
xTitle?: string | undefined;
|
|
16
24
|
httpClient?: HTTPClient;
|
|
17
25
|
/**
|
|
18
26
|
* Allows overriding the default server used by the SDK
|
|
@@ -37,8 +45,8 @@ export declare function serverURLFromOptions(options: SDKOptions): URL | null;
|
|
|
37
45
|
export declare const SDK_METADATA: {
|
|
38
46
|
readonly language: "typescript";
|
|
39
47
|
readonly openapiDocVersion: "1.0.0";
|
|
40
|
-
readonly sdkVersion: "0.1.
|
|
48
|
+
readonly sdkVersion: "0.1.18";
|
|
41
49
|
readonly genVersion: "2.755.9";
|
|
42
|
-
readonly userAgent: "speakeasy-sdk/typescript 0.1.
|
|
50
|
+
readonly userAgent: "speakeasy-sdk/typescript 0.1.18 2.755.9 1.0.0 @openrouter/sdk";
|
|
43
51
|
};
|
|
44
52
|
//# sourceMappingURL=config.d.ts.map
|
package/esm/lib/config.js
CHANGED
|
@@ -25,8 +25,8 @@ export function serverURLFromOptions(options) {
|
|
|
25
25
|
export const SDK_METADATA = {
|
|
26
26
|
language: "typescript",
|
|
27
27
|
openapiDocVersion: "1.0.0",
|
|
28
|
-
sdkVersion: "0.1.
|
|
28
|
+
sdkVersion: "0.1.18",
|
|
29
29
|
genVersion: "2.755.9",
|
|
30
|
-
userAgent: "speakeasy-sdk/typescript 0.1.
|
|
30
|
+
userAgent: "speakeasy-sdk/typescript 0.1.18 2.755.9 1.0.0 @openrouter/sdk",
|
|
31
31
|
};
|
|
32
32
|
//# sourceMappingURL=config.js.map
|
package/esm/lib/env.d.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import * as z from "zod/v4";
|
|
2
|
+
import { SDKOptions } from "./config.js";
|
|
2
3
|
export interface Env {
|
|
3
4
|
OPENROUTER_API_KEY?: string | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* Sets the httpReferer parameter for all supported operations
|
|
7
|
+
*/
|
|
8
|
+
OPENROUTER_HTTP_REFERER?: string | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Sets the xTitle parameter for all supported operations
|
|
11
|
+
*/
|
|
12
|
+
OPENROUTER_X_TITLE?: string | undefined;
|
|
4
13
|
OPENROUTER_DEBUG?: boolean | undefined;
|
|
5
14
|
}
|
|
6
15
|
export declare const envSchema: z.ZodType<Env, unknown>;
|
|
@@ -12,4 +21,8 @@ export declare function env(): Env;
|
|
|
12
21
|
* Clears the cached env object. Useful for testing with a fresh environment.
|
|
13
22
|
*/
|
|
14
23
|
export declare function resetEnv(): void;
|
|
24
|
+
/**
|
|
25
|
+
* Populates global parameters with environment variables.
|
|
26
|
+
*/
|
|
27
|
+
export declare function fillGlobals(options: SDKOptions): SDKOptions;
|
|
15
28
|
//# sourceMappingURL=env.d.ts.map
|
package/esm/lib/env.js
CHANGED
|
@@ -5,6 +5,8 @@ import * as z from "zod/v4";
|
|
|
5
5
|
import { dlv } from "./dlv.js";
|
|
6
6
|
export const envSchema = z.object({
|
|
7
7
|
OPENROUTER_API_KEY: z.string().optional(),
|
|
8
|
+
OPENROUTER_HTTP_REFERER: z.string().optional(),
|
|
9
|
+
OPENROUTER_X_TITLE: z.string().optional(),
|
|
8
10
|
OPENROUTER_DEBUG: z.coerce.boolean().optional(),
|
|
9
11
|
});
|
|
10
12
|
/**
|
|
@@ -41,4 +43,18 @@ export function env() {
|
|
|
41
43
|
export function resetEnv() {
|
|
42
44
|
envMemo = undefined;
|
|
43
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Populates global parameters with environment variables.
|
|
48
|
+
*/
|
|
49
|
+
export function fillGlobals(options) {
|
|
50
|
+
const clone = { ...options };
|
|
51
|
+
const envVars = env();
|
|
52
|
+
if (typeof envVars.OPENROUTER_HTTP_REFERER !== "undefined") {
|
|
53
|
+
clone.httpReferer ?? (clone.httpReferer = envVars.OPENROUTER_HTTP_REFERER);
|
|
54
|
+
}
|
|
55
|
+
if (typeof envVars.OPENROUTER_X_TITLE !== "undefined") {
|
|
56
|
+
clone.xTitle ?? (clone.xTitle = envVars.OPENROUTER_X_TITLE);
|
|
57
|
+
}
|
|
58
|
+
return clone;
|
|
59
|
+
}
|
|
44
60
|
//# sourceMappingURL=env.js.map
|
package/esm/lib/sdks.js
CHANGED
|
@@ -19,7 +19,7 @@ import { ERR, OK } from "../types/fp.js";
|
|
|
19
19
|
import { stringToBase64 } from "./base64.js";
|
|
20
20
|
import { SDK_METADATA, serverURLFromOptions } from "./config.js";
|
|
21
21
|
import { encodeForm } from "./encodings.js";
|
|
22
|
-
import { env } from "./env.js";
|
|
22
|
+
import { env, fillGlobals } from "./env.js";
|
|
23
23
|
import { HTTPClient, isAbortError, isConnectionError, isTimeoutError, matchContentType, matchStatusCode, } from "./http.js";
|
|
24
24
|
import { retry } from "./retries.js";
|
|
25
25
|
const gt = typeof globalThis === "undefined" ? null : globalThis;
|
|
@@ -54,7 +54,7 @@ export class ClientSDK {
|
|
|
54
54
|
}
|
|
55
55
|
this._baseURL = url;
|
|
56
56
|
__classPrivateFieldSet(this, _ClientSDK_httpClient, options.httpClient || defaultHttpClient, "f");
|
|
57
|
-
this._options = { ...options, hooks: __classPrivateFieldGet(this, _ClientSDK_hooks, "f") };
|
|
57
|
+
this._options = { ...fillGlobals(options), hooks: __classPrivateFieldGet(this, _ClientSDK_hooks, "f") };
|
|
58
58
|
__classPrivateFieldSet(this, _ClientSDK_logger, this._options.debugLogger, "f");
|
|
59
59
|
if (!__classPrivateFieldGet(this, _ClientSDK_logger, "f") && env().OPENROUTER_DEBUG) {
|
|
60
60
|
__classPrivateFieldSet(this, _ClientSDK_logger, console, "f");
|
package/jsr.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openrouter/sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.18",
|
|
4
4
|
"author": "OpenRouter",
|
|
5
5
|
"description": "The OpenRouter TypeScript SDK is a type-safe toolkit for building AI applications with access to 300+ language models through a unified API.",
|
|
6
6
|
"keywords": [
|
|
@@ -48,11 +48,6 @@
|
|
|
48
48
|
"types": "./esm/models/operations/index.d.ts",
|
|
49
49
|
"default": "./esm/models/operations/index.js"
|
|
50
50
|
},
|
|
51
|
-
"./react-query": {
|
|
52
|
-
"source": "./src/react-query/index.ts",
|
|
53
|
-
"types": "./esm/react-query/index.d.ts",
|
|
54
|
-
"default": "./esm/react-query/index.js"
|
|
55
|
-
},
|
|
56
51
|
"./*.js": {
|
|
57
52
|
"source": "./src/*.ts",
|
|
58
53
|
"types": "./esm/*.d.ts",
|
|
@@ -75,20 +70,11 @@
|
|
|
75
70
|
"prepublishOnly": "npm run build"
|
|
76
71
|
},
|
|
77
72
|
"peerDependencies": {
|
|
78
|
-
|
|
79
|
-
"react": "^18 || ^19",
|
|
80
|
-
"react-dom": "^18 || ^19"
|
|
81
|
-
},
|
|
82
|
-
"peerDependenciesMeta": {
|
|
83
|
-
"@tanstack/react-query": {"optional":true},
|
|
84
|
-
"react": {"optional":true},
|
|
85
|
-
"react-dom": {"optional":true}
|
|
73
|
+
|
|
86
74
|
},
|
|
87
75
|
"devDependencies": {
|
|
88
76
|
"@eslint/js": "^9.19.0",
|
|
89
|
-
"@tanstack/react-query": "^5.61.4",
|
|
90
77
|
"@types/node": "^22.13.12",
|
|
91
|
-
"@types/react": "^18.3.12",
|
|
92
78
|
"dotenv": "^16.4.7",
|
|
93
79
|
"eslint": "^9.19.0",
|
|
94
80
|
"globals": "^15.14.0",
|
package/REACT_QUERY.md
DELETED
|
@@ -1,296 +0,0 @@
|
|
|
1
|
-
# React hooks
|
|
2
|
-
|
|
3
|
-
This SDK provides React hooks and utilies for making queries and mutations that
|
|
4
|
-
can take the pain out of building front-end applications for the web or React
|
|
5
|
-
Native.
|
|
6
|
-
|
|
7
|
-
They are built as a thin wrapper around [TanStack Query for React v5][rq], a
|
|
8
|
-
powerful, asynchronous state management library. A good understanding of that
|
|
9
|
-
library will be very helpful while using them. In addition to hooks, there are
|
|
10
|
-
several helper functions that can be used for cache management and data fetching
|
|
11
|
-
during server-rendering and in React Server Components.
|
|
12
|
-
|
|
13
|
-
## Getting started
|
|
14
|
-
|
|
15
|
-
To get started using React hooks, you will need to inject TanStack query and an
|
|
16
|
-
SDK instance into your application. Typically, this will be done high up in
|
|
17
|
-
your React app at the root or layout component. For example:
|
|
18
|
-
|
|
19
|
-
```tsx
|
|
20
|
-
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
21
|
-
import { OpenRouterCore } from "@openrouter/sdk";
|
|
22
|
-
import { OpenRouterProvider } from "@openrouter/sdk/react-query";
|
|
23
|
-
|
|
24
|
-
const queryClient = new QueryClient();
|
|
25
|
-
const openRouter = new OpenRouterCore({
|
|
26
|
-
apiKey: process.env["OPENROUTER_API_KEY"] ?? "",
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
// Retries are handled by the underlying SDK.
|
|
30
|
-
queryClient.setQueryDefaults(["@openrouter/sdk"], { retry: false });
|
|
31
|
-
queryClient.setMutationDefaults(["@openrouter/sdk"], { retry: false });
|
|
32
|
-
|
|
33
|
-
export function App() {
|
|
34
|
-
return (
|
|
35
|
-
<QueryClientProvider client={queryClient}>
|
|
36
|
-
<OpenRouterProvider client={openRouter}>
|
|
37
|
-
{/* Your app logic starts here */}
|
|
38
|
-
</OpenRouterProvider>
|
|
39
|
-
</QueryClientProvider>
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
## Queries
|
|
45
|
-
|
|
46
|
-
Query hooks are the basic building block for fetching data. In addition to
|
|
47
|
-
request data, they take the same options as the [`useQuery` hook][use-query]
|
|
48
|
-
from TanStack Query.
|
|
49
|
-
|
|
50
|
-
[use-query]: https://tanstack.com/query/v5/docs/framework/react/reference/useQuery
|
|
51
|
-
|
|
52
|
-
```tsx
|
|
53
|
-
import { useAnalyticsGetUserActivity } from "@openrouter/sdk/react-query/analyticsGetUserActivity.js";
|
|
54
|
-
|
|
55
|
-
export function Example() {
|
|
56
|
-
const { data, error, status } = useAnalyticsGetUserActivity();
|
|
57
|
-
|
|
58
|
-
// Render the UI here...
|
|
59
|
-
}
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
### Query timeouts and retries
|
|
63
|
-
|
|
64
|
-
Since the underlying SDK handles request timeouts and retries, there are a few
|
|
65
|
-
more options provided by the query hooks to control these behaviors.
|
|
66
|
-
|
|
67
|
-
```tsx
|
|
68
|
-
import { useState } from "react";
|
|
69
|
-
import { useAnalyticsGetUserActivity } from "@openrouter/sdk/react-query/analyticsGetUserActivity.js";
|
|
70
|
-
|
|
71
|
-
export function ExampleWithOptions() {
|
|
72
|
-
const [enabled, setEnabled] = useState(true);
|
|
73
|
-
const { data, error, status } = useAnalyticsGetUserActivity(
|
|
74
|
-
{
|
|
75
|
-
// TanStack Query options:
|
|
76
|
-
enabled,
|
|
77
|
-
staleTime: 60 * 1000, // 1 minute
|
|
78
|
-
gcTime: 5 * 60 * 1000, // 5 minutes
|
|
79
|
-
|
|
80
|
-
// Request options for the underlying API call:
|
|
81
|
-
timeoutMs: 1000,
|
|
82
|
-
retryCodes: ["5XX"],
|
|
83
|
-
retries: {
|
|
84
|
-
strategy: "backoff",
|
|
85
|
-
backoff: {
|
|
86
|
-
initialInterval: 500,
|
|
87
|
-
maxInterval: 10 * 1000, // 10 seconds
|
|
88
|
-
exponent: 1.5,
|
|
89
|
-
maxElapsedTime: 60 * 1000, // 1 minute
|
|
90
|
-
},
|
|
91
|
-
},
|
|
92
|
-
}
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
// Render the UI here...
|
|
96
|
-
}
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
## Mutations
|
|
101
|
-
|
|
102
|
-
Operations that can have side-effects in this SDK are exposed as mutation hooks.
|
|
103
|
-
These can be integrated into HTML forms to submit data to the API. They also
|
|
104
|
-
take the same options as the [`useMutation` hook][use-mutation] from TanStack
|
|
105
|
-
Query.
|
|
106
|
-
|
|
107
|
-
[use-mutation]: https://tanstack.com/query/v5/docs/framework/react/reference/useMutation
|
|
108
|
-
|
|
109
|
-
```tsx
|
|
110
|
-
import { useBetaResponsesSendMutation } from "@openrouter/sdk/react-query/betaResponsesSend.js";
|
|
111
|
-
|
|
112
|
-
export function Example() {
|
|
113
|
-
const { mutate, status } = useBetaResponsesSendMutation();
|
|
114
|
-
|
|
115
|
-
return (
|
|
116
|
-
<form
|
|
117
|
-
onSubmit={(e) => {
|
|
118
|
-
e.preventDefault();
|
|
119
|
-
|
|
120
|
-
// Read form data here...
|
|
121
|
-
|
|
122
|
-
mutate({});
|
|
123
|
-
}}
|
|
124
|
-
>
|
|
125
|
-
{/* Form fields go here... */}
|
|
126
|
-
<button type="submit" disabled={status === "pending"}>Submit</button>
|
|
127
|
-
</form>
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
### Mutation timeouts and retries
|
|
133
|
-
|
|
134
|
-
Since the underlying SDK handles request timeouts and retries, there are a few
|
|
135
|
-
more options provided by the mutation hooks to control these behaviors.
|
|
136
|
-
|
|
137
|
-
```tsx
|
|
138
|
-
import { useBetaResponsesSendMutation } from "@openrouter/sdk/react-query/betaResponsesSend.js";
|
|
139
|
-
|
|
140
|
-
export function ExampleWithOptions() {
|
|
141
|
-
const { mutate, status } = useBetaResponsesSendMutation({
|
|
142
|
-
// TanStack Query options:
|
|
143
|
-
networkMode: "online",
|
|
144
|
-
gcTime: 5 * 60 * 1000, // 5 minutes
|
|
145
|
-
|
|
146
|
-
// Request options for the underlying API call:
|
|
147
|
-
timeoutMs: 1000,
|
|
148
|
-
retryCodes: ["5XX"],
|
|
149
|
-
retries: {
|
|
150
|
-
strategy: "backoff",
|
|
151
|
-
backoff: {
|
|
152
|
-
initialInterval: 500,
|
|
153
|
-
maxInterval: 10 * 1000, // 10 seconds
|
|
154
|
-
exponent: 1.5,
|
|
155
|
-
maxElapsedTime: 60 * 1000, // 1 minute
|
|
156
|
-
},
|
|
157
|
-
},
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
// Render the UI here...
|
|
161
|
-
}
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
## Cache invalidation
|
|
166
|
-
|
|
167
|
-
In many instances, triggering a mutation hook requires invalidating specific
|
|
168
|
-
query data currently residing in the TanStack Query's cache. Alongside every
|
|
169
|
-
query hook there are two functions that help invalidate cached data:
|
|
170
|
-
|
|
171
|
-
```tsx
|
|
172
|
-
import { useQueryClient } from "@tanstack/react-query";
|
|
173
|
-
import { invalidateAnalyticsGetUserActivity, invalidateAllAnalyticsGetUserActivity } from "@openrouter/sdk/react-query/analyticsGetUserActivity.js";
|
|
174
|
-
// Replace this with a real mutation
|
|
175
|
-
import { useExampleMutation } from "@openrouter/sdk/react-query/example.js";
|
|
176
|
-
|
|
177
|
-
export function Example() {
|
|
178
|
-
const { queryClient } = useQueryClient();
|
|
179
|
-
const { mutate, status } = useExampleMutation();
|
|
180
|
-
|
|
181
|
-
return (
|
|
182
|
-
<form
|
|
183
|
-
onSubmit={(e) => {
|
|
184
|
-
e.preventDefault();
|
|
185
|
-
|
|
186
|
-
const formData = new FormData(e.target);
|
|
187
|
-
|
|
188
|
-
mutate(formData, {
|
|
189
|
-
onSuccess: () => {
|
|
190
|
-
// Invalidate a single cache entry:
|
|
191
|
-
invalidateAnalyticsGetUserActivity(queryClient, /* ... arguments ... */);
|
|
192
|
-
// OR, invalidate all cache entries for the query targets:
|
|
193
|
-
invalidateAllAnalyticsGetUserActivity(queryClient);
|
|
194
|
-
},
|
|
195
|
-
});
|
|
196
|
-
}}
|
|
197
|
-
>
|
|
198
|
-
{/* Form fields go here... */}
|
|
199
|
-
|
|
200
|
-
<button type="submit" disabled={status === "pending"}>Submit</button>
|
|
201
|
-
</form>
|
|
202
|
-
);
|
|
203
|
-
}
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
## Integration with React Suspense
|
|
208
|
-
|
|
209
|
-
TanStack Query predates React Suspense and out of the box it does a great job at
|
|
210
|
-
exposing the lifecycle of asynchronous tasks. However, if you are already using
|
|
211
|
-
Suspense in your app, the default hooks will not trigger suspense boundaries.
|
|
212
|
-
This is why the library and, by extension, this SDK also provide equivalent
|
|
213
|
-
hooks that integrate neatly with React Suspense.
|
|
214
|
-
|
|
215
|
-
```tsx
|
|
216
|
-
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
217
|
-
import { ErrorBoundary } from "react-error-boundary";
|
|
218
|
-
|
|
219
|
-
import { OpenRouterCore } from "@openrouter/sdk";
|
|
220
|
-
import { OpenRouterProvider } from "@openrouter/sdk/react-query";
|
|
221
|
-
import { useAnalyticsGetUserActivitySuspense } from "@openrouter/sdk/react-query/analyticsGetUserActivity.js";
|
|
222
|
-
|
|
223
|
-
const queryClient = new QueryClient();
|
|
224
|
-
const openRouter = new OpenRouterCore({
|
|
225
|
-
apiKey: process.env["OPENROUTER_API_KEY"] ?? "",
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
export function App() {
|
|
229
|
-
return (
|
|
230
|
-
<QueryClientProvider client={queryClient}>
|
|
231
|
-
<OpenRouterProvider client={openRouter}>
|
|
232
|
-
<QueryErrorResetBoundary>
|
|
233
|
-
{({ reset }) => (
|
|
234
|
-
<ErrorBoundary
|
|
235
|
-
fallbackRender={({ error, resetErrorBoundary }) => (
|
|
236
|
-
<div>
|
|
237
|
-
There was an error!{' '}
|
|
238
|
-
<Button onClick={() => resetErrorBoundary()}>Try again</Button>
|
|
239
|
-
<pre>{error.message}</pre>
|
|
240
|
-
</div>
|
|
241
|
-
)}
|
|
242
|
-
onReset={reset}
|
|
243
|
-
>
|
|
244
|
-
<React.Suspense fallback={<h1>Loading...</h1>}>
|
|
245
|
-
<Example />
|
|
246
|
-
</React.Suspense>
|
|
247
|
-
</ErrorBoundary>
|
|
248
|
-
)}
|
|
249
|
-
</QueryErrorResetBoundary>
|
|
250
|
-
</OpenRouterProvider>
|
|
251
|
-
</QueryClientProvider>
|
|
252
|
-
);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
function Example() {
|
|
256
|
-
const { data } = useAnalyticsGetUserActivitySuspense();
|
|
257
|
-
|
|
258
|
-
// Render the UI here...
|
|
259
|
-
}
|
|
260
|
-
```
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
## Server-rendering and React Server Components
|
|
264
|
-
|
|
265
|
-
Query hooks are also side-loaded with prefetch helper functions. These functions
|
|
266
|
-
can be used to fetch data from the API during server-rendering and in React
|
|
267
|
-
Server Components so that it can be available immediately on page load to any
|
|
268
|
-
components that use the corresponding hooks:
|
|
269
|
-
```tsx
|
|
270
|
-
import {
|
|
271
|
-
dehydrate,
|
|
272
|
-
HydrationBoundary,
|
|
273
|
-
QueryClient,
|
|
274
|
-
} from "@tanstack/react-query";
|
|
275
|
-
import { OpenRouterCore } from "@openrouter/sdk";
|
|
276
|
-
import { prefetchAnalyticsGetUserActivity } from "@openrouter/sdk/react-query/analyticsGetUserActivity.js";
|
|
277
|
-
|
|
278
|
-
export default async function Page() {
|
|
279
|
-
const queryClient = new QueryClient();
|
|
280
|
-
const openRouter = new OpenRouterCore({
|
|
281
|
-
apiKey: process.env["OPENROUTER_API_KEY"] ?? "",
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
await prefetchAnalyticsGetUserActivity(openRouter);
|
|
285
|
-
|
|
286
|
-
return (
|
|
287
|
-
// HydrationBoundary is a Client Component, so hydration will happen there.
|
|
288
|
-
<HydrationBoundary state={dehydrate(queryClient)}>
|
|
289
|
-
{/* Client components under this point will also have data on page load. */}
|
|
290
|
-
</HydrationBoundary>
|
|
291
|
-
);
|
|
292
|
-
}
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
[rq]: https://tanstack.com/query/v5/docs/framework/react/overview
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import { OpenRouterCore } from "../core.js";
|
|
3
|
-
export declare function OpenRouterProvider(props: {
|
|
4
|
-
client: OpenRouterCore;
|
|
5
|
-
children: React.ReactNode;
|
|
6
|
-
}): React.ReactNode;
|
|
7
|
-
export declare function useOpenRouterContext(): OpenRouterCore;
|
|
8
|
-
//# sourceMappingURL=_context.d.ts.map
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import React from "react";
|
|
3
|
-
const OpenRouterContext = React.createContext(null);
|
|
4
|
-
export function OpenRouterProvider(props) {
|
|
5
|
-
return (_jsx(OpenRouterContext.Provider, { value: props.client, children: props.children }));
|
|
6
|
-
}
|
|
7
|
-
export function useOpenRouterContext() {
|
|
8
|
-
const value = React.useContext(OpenRouterContext);
|
|
9
|
-
if (value === null) {
|
|
10
|
-
throw new Error("SDK not initialized. Create an instance of OpenRouterCore and pass it to <OpenRouterProvider />.");
|
|
11
|
-
}
|
|
12
|
-
return value;
|
|
13
|
-
}
|
|
14
|
-
//# sourceMappingURL=_context.js.map
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { RequestOptions } from "../lib/sdks.js";
|
|
2
|
-
import { PageIterator } from "../types/operations.js";
|
|
3
|
-
import type { DefaultError, InfiniteData, InfiniteQueryPageParamsOptions, OmitKeyof, QueryKey, QueryObserverOptions, SkipToken, UseMutationOptions, UseQueryOptions, UseSuspenseQueryOptions } from "@tanstack/react-query";
|
|
4
|
-
interface UseInfiniteQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown> extends OmitKeyof<InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, "suspense"> {
|
|
5
|
-
/**
|
|
6
|
-
* Set this to `false` to unsubscribe this observer from updates to the query cache.
|
|
7
|
-
* Defaults to `true`.
|
|
8
|
-
*/
|
|
9
|
-
subscribed?: boolean;
|
|
10
|
-
}
|
|
11
|
-
interface InfiniteQueryObserverOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown> extends QueryObserverOptions<TQueryFnData, TError, TData, InfiniteData<TQueryFnData, TPageParam>, TQueryKey, TPageParam>, InfiniteQueryPageParamsOptions<TQueryFnData, TPageParam> {
|
|
12
|
-
}
|
|
13
|
-
interface UseSuspenseInfiniteQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown> extends OmitKeyof<UseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, "queryFn" | "enabled" | "throwOnError" | "placeholderData"> {
|
|
14
|
-
queryFn?: Exclude<UseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>["queryFn"], SkipToken>;
|
|
15
|
-
}
|
|
16
|
-
export type TupleToPrefixes<T extends any[]> = T extends [...infer Prefix, any] ? TupleToPrefixes<Prefix> | T : never;
|
|
17
|
-
export type QueryHookOptions<Data> = Omit<UseQueryOptions<Data, Error>, "queryKey" | "queryFn" | "select" | keyof RequestOptions> & RequestOptions;
|
|
18
|
-
export type SuspenseQueryHookOptions<Data> = Omit<UseSuspenseQueryOptions<Data, Error>, "queryKey" | "queryFn" | "select" | keyof RequestOptions> & RequestOptions;
|
|
19
|
-
export type InfiniteQueryHookOptions<Data extends PageIterator<unknown, unknown>> = Omit<UseInfiniteQueryOptions<Data, Error, InfiniteData<Data, Data["~next"]>, QueryKey, Data["~next"]>, "queryKey" | "queryFn" | "select" | "getNextPageParam" | "getPreviousPageParam" | "initialPageParam" | keyof RequestOptions> & RequestOptions & {
|
|
20
|
-
initialPageParam?: Data["~next"];
|
|
21
|
-
};
|
|
22
|
-
export type SuspenseInfiniteQueryHookOptions<Data extends PageIterator<unknown, unknown>> = Omit<UseSuspenseInfiniteQueryOptions<Data, Error, InfiniteData<Data, Data["~next"]>, QueryKey, Data["~next"]>, "queryKey" | "queryFn" | "select" | "getNextPageParam" | "getPreviousPageParam" | "initialPageParam" | keyof RequestOptions> & RequestOptions & {
|
|
23
|
-
initialPageParam?: Data["~next"];
|
|
24
|
-
};
|
|
25
|
-
export type MutationHookOptions<Data = unknown, Err = Error, Variables = unknown> = Omit<UseMutationOptions<Data, Err, Variables>, "mutationKey" | "mutationFn" | keyof RequestOptions> & RequestOptions;
|
|
26
|
-
export {};
|
|
27
|
-
//# sourceMappingURL=_types.d.ts.map
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { InvalidateQueryFilters, QueryClient, QueryFunctionContext, QueryKey, UseQueryResult, UseSuspenseQueryResult } from "@tanstack/react-query";
|
|
2
|
-
import { OpenRouterCore } from "../core.js";
|
|
3
|
-
import { RequestOptions } from "../lib/sdks.js";
|
|
4
|
-
import * as operations from "../models/operations/index.js";
|
|
5
|
-
import { QueryHookOptions, SuspenseQueryHookOptions, TupleToPrefixes } from "./_types.js";
|
|
6
|
-
export type AnalyticsGetUserActivityQueryData = operations.GetUserActivityResponse;
|
|
7
|
-
/**
|
|
8
|
-
* Get user activity grouped by endpoint
|
|
9
|
-
*
|
|
10
|
-
* @remarks
|
|
11
|
-
* Returns user activity data grouped by endpoint for the last 30 (completed) UTC days
|
|
12
|
-
*/
|
|
13
|
-
export declare function useAnalyticsGetUserActivity(request?: operations.GetUserActivityRequest | undefined, options?: QueryHookOptions<AnalyticsGetUserActivityQueryData>): UseQueryResult<AnalyticsGetUserActivityQueryData, Error>;
|
|
14
|
-
/**
|
|
15
|
-
* Get user activity grouped by endpoint
|
|
16
|
-
*
|
|
17
|
-
* @remarks
|
|
18
|
-
* Returns user activity data grouped by endpoint for the last 30 (completed) UTC days
|
|
19
|
-
*/
|
|
20
|
-
export declare function useAnalyticsGetUserActivitySuspense(request?: operations.GetUserActivityRequest | undefined, options?: SuspenseQueryHookOptions<AnalyticsGetUserActivityQueryData>): UseSuspenseQueryResult<AnalyticsGetUserActivityQueryData, Error>;
|
|
21
|
-
export declare function prefetchAnalyticsGetUserActivity(queryClient: QueryClient, client$: OpenRouterCore, request?: operations.GetUserActivityRequest | undefined): Promise<void>;
|
|
22
|
-
export declare function setAnalyticsGetUserActivityData(client: QueryClient, queryKeyBase: [parameters: {
|
|
23
|
-
date?: string | undefined;
|
|
24
|
-
}], data: AnalyticsGetUserActivityQueryData): AnalyticsGetUserActivityQueryData | undefined;
|
|
25
|
-
export declare function invalidateAnalyticsGetUserActivity(client: QueryClient, queryKeyBase: TupleToPrefixes<[parameters: {
|
|
26
|
-
date?: string | undefined;
|
|
27
|
-
}]>, filters?: Omit<InvalidateQueryFilters, "queryKey" | "predicate" | "exact">): Promise<void>;
|
|
28
|
-
export declare function invalidateAllAnalyticsGetUserActivity(client: QueryClient, filters?: Omit<InvalidateQueryFilters, "queryKey" | "predicate" | "exact">): Promise<void>;
|
|
29
|
-
export declare function buildAnalyticsGetUserActivityQuery(client$: OpenRouterCore, request?: operations.GetUserActivityRequest | undefined, options?: RequestOptions): {
|
|
30
|
-
queryKey: QueryKey;
|
|
31
|
-
queryFn: (context: QueryFunctionContext) => Promise<AnalyticsGetUserActivityQueryData>;
|
|
32
|
-
};
|
|
33
|
-
export declare function queryKeyAnalyticsGetUserActivity(parameters: {
|
|
34
|
-
date?: string | undefined;
|
|
35
|
-
}): QueryKey;
|
|
36
|
-
//# sourceMappingURL=analyticsGetUserActivity.d.ts.map
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
3
|
-
*/
|
|
4
|
-
import { useQuery, useSuspenseQuery, } from "@tanstack/react-query";
|
|
5
|
-
import { analyticsGetUserActivity } from "../funcs/analyticsGetUserActivity.js";
|
|
6
|
-
import { combineSignals } from "../lib/primitives.js";
|
|
7
|
-
import { unwrapAsync } from "../types/fp.js";
|
|
8
|
-
import { useOpenRouterContext } from "./_context.js";
|
|
9
|
-
/**
|
|
10
|
-
* Get user activity grouped by endpoint
|
|
11
|
-
*
|
|
12
|
-
* @remarks
|
|
13
|
-
* Returns user activity data grouped by endpoint for the last 30 (completed) UTC days
|
|
14
|
-
*/
|
|
15
|
-
export function useAnalyticsGetUserActivity(request, options) {
|
|
16
|
-
const client = useOpenRouterContext();
|
|
17
|
-
return useQuery({
|
|
18
|
-
...buildAnalyticsGetUserActivityQuery(client, request, options),
|
|
19
|
-
...options,
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Get user activity grouped by endpoint
|
|
24
|
-
*
|
|
25
|
-
* @remarks
|
|
26
|
-
* Returns user activity data grouped by endpoint for the last 30 (completed) UTC days
|
|
27
|
-
*/
|
|
28
|
-
export function useAnalyticsGetUserActivitySuspense(request, options) {
|
|
29
|
-
const client = useOpenRouterContext();
|
|
30
|
-
return useSuspenseQuery({
|
|
31
|
-
...buildAnalyticsGetUserActivityQuery(client, request, options),
|
|
32
|
-
...options,
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
export function prefetchAnalyticsGetUserActivity(queryClient, client$, request) {
|
|
36
|
-
return queryClient.prefetchQuery({
|
|
37
|
-
...buildAnalyticsGetUserActivityQuery(client$, request),
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
export function setAnalyticsGetUserActivityData(client, queryKeyBase, data) {
|
|
41
|
-
const key = queryKeyAnalyticsGetUserActivity(...queryKeyBase);
|
|
42
|
-
return client.setQueryData(key, data);
|
|
43
|
-
}
|
|
44
|
-
export function invalidateAnalyticsGetUserActivity(client, queryKeyBase, filters) {
|
|
45
|
-
return client.invalidateQueries({
|
|
46
|
-
...filters,
|
|
47
|
-
queryKey: [
|
|
48
|
-
"@openrouter/sdk",
|
|
49
|
-
"Analytics",
|
|
50
|
-
"getUserActivity",
|
|
51
|
-
...queryKeyBase,
|
|
52
|
-
],
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
export function invalidateAllAnalyticsGetUserActivity(client, filters) {
|
|
56
|
-
return client.invalidateQueries({
|
|
57
|
-
...filters,
|
|
58
|
-
queryKey: ["@openrouter/sdk", "Analytics", "getUserActivity"],
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
export function buildAnalyticsGetUserActivityQuery(client$, request, options) {
|
|
62
|
-
return {
|
|
63
|
-
queryKey: queryKeyAnalyticsGetUserActivity({ date: request?.date }),
|
|
64
|
-
queryFn: async function analyticsGetUserActivityQueryFn(ctx) {
|
|
65
|
-
const sig = combineSignals(ctx.signal, options?.fetchOptions?.signal);
|
|
66
|
-
const mergedOptions = {
|
|
67
|
-
...options,
|
|
68
|
-
fetchOptions: { ...options?.fetchOptions, signal: sig },
|
|
69
|
-
};
|
|
70
|
-
return unwrapAsync(analyticsGetUserActivity(client$, request, mergedOptions));
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
export function queryKeyAnalyticsGetUserActivity(parameters) {
|
|
75
|
-
return ["@openrouter/sdk", "Analytics", "getUserActivity", parameters];
|
|
76
|
-
}
|
|
77
|
-
//# sourceMappingURL=analyticsGetUserActivity.js.map
|