@openapi-press/nuxt 0.1.0 → 0.1.2
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/module.mjs +3 -0
- package/.dist/runtime/composable.d.ts +24 -3
- package/.dist/runtime/composable.mjs +2 -1
- package/.dist/runtime/util.d.ts +16 -2
- package/.dist/runtime/util.mjs +25 -3
- package/README.md +17 -1
- package/package.json +2 -2
package/.dist/module.mjs
CHANGED
|
@@ -84,6 +84,9 @@ const module$1 = defineNuxtModule({
|
|
|
84
84
|
`export interface PressClients { ${entries.map(
|
|
85
85
|
(entry, i) => `${JSON.stringify(entry.name)}: ReturnType<typeof c${i}>;`
|
|
86
86
|
).join(" ")} }`,
|
|
87
|
+
`export interface PressConfigs { ${entries.map(
|
|
88
|
+
(entry, i) => `${JSON.stringify(entry.name)}: Parameters<typeof c${i}>;`
|
|
89
|
+
).join(" ")} }`,
|
|
87
90
|
`export type PressClientName = keyof PressClients;`,
|
|
88
91
|
`declare module "nuxt/schema" { interface RuntimeConfig { ${KEY}: { clients: Record<PressClientName, { host: string }> } } interface PublicRuntimeConfig { ${KEY}: { clients: Record<PressClientName, { prefix: string }> } } }`,
|
|
89
92
|
`export {};`
|
|
@@ -7,8 +7,29 @@
|
|
|
7
7
|
* composable:
|
|
8
8
|
*
|
|
9
9
|
* const useApi = () => usePress("api", { logger });
|
|
10
|
+
*
|
|
11
|
+
* When the registered Press requires something of its config — whatever its
|
|
12
|
+
* SDK's wrappers declared — the config argument must supply it, with one
|
|
13
|
+
* exception: the environment config provides `cache`, scoped per request on
|
|
14
|
+
* the server and per app in the browser, so a Press requiring a cache works
|
|
15
|
+
* with no wiring. The caller may still pass its own store, or `false`.
|
|
16
|
+
* Everything else — a refresh callback, a custom requirement — stays the
|
|
17
|
+
* app's to supply, and is mandatory.
|
|
10
18
|
*/
|
|
11
19
|
import type { ClientConfig } from "openapi-press";
|
|
12
|
-
import type { PressClients } from "#build/types/openapi-press";
|
|
13
|
-
/** The
|
|
14
|
-
|
|
20
|
+
import type { PressClients, PressConfigs } from "#build/types/openapi-press";
|
|
21
|
+
/** The requirement keys the environment config supplies. */
|
|
22
|
+
type Wired = "cache";
|
|
23
|
+
/** `Config` with the wired keys made optional. */
|
|
24
|
+
type Unwired<Config> = Omit<Config, Wired> & Partial<Pick<Config, Extract<keyof Config, Wired>>>;
|
|
25
|
+
/** The config argument as a tuple: omittable unless a key is still required. */
|
|
26
|
+
type ArgOf<Config> = Record<never, never> extends Config ? [config?: Config] : [config: Config];
|
|
27
|
+
/**
|
|
28
|
+
* The config argument for the client registered under `K`, as a tuple: the
|
|
29
|
+
* registered Press's own parameters, less what the environment supplies. A
|
|
30
|
+
* Press that requires nothing wired is passed through untouched.
|
|
31
|
+
*/
|
|
32
|
+
export type PressConfigArgs<K> = K extends keyof PressConfigs ? PressConfigs[K] extends infer Args extends unknown[] ? Wired extends keyof NonNullable<Args[0]> ? ArgOf<Unwired<NonNullable<Args[0]>>> : Args : never : [config?: ClientConfig];
|
|
33
|
+
/** The client registered under `name`, built with caller wiring. */
|
|
34
|
+
export declare const usePress: <K extends keyof PressClients & string>(name: K, ...args: PressConfigArgs<K>) => PressClients[K];
|
|
35
|
+
export {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { clients } from "#build/openapi-press.mjs";
|
|
2
2
|
import { mergeConfig, resolveConfig } from "./util.mjs";
|
|
3
|
-
export const usePress = (name,
|
|
3
|
+
export const usePress = (name, ...args) => {
|
|
4
|
+
const config = args[0] ?? {};
|
|
4
5
|
const cfg = resolveConfig(name);
|
|
5
6
|
const merged = mergeConfig(cfg, config);
|
|
6
7
|
return clients[name](merged);
|
package/.dist/runtime/util.d.ts
CHANGED
|
@@ -2,9 +2,23 @@
|
|
|
2
2
|
* The runtime's config plumbing behind the composable: resolving a named
|
|
3
3
|
* client's environment config, and layering caller wiring over it.
|
|
4
4
|
*/
|
|
5
|
-
import type { ClientConfig } from "openapi-press";
|
|
5
|
+
import type { CacheStore, ClientConfig } from "openapi-press";
|
|
6
|
+
/** The environment config: transport wiring plus the cache the module supplies. */
|
|
7
|
+
export type EnvironmentConfig = ClientConfig & {
|
|
8
|
+
cache: CacheStore;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* The cache a named client gets in the current environment, for a Press whose
|
|
12
|
+
* wrappers require one. Scoped so a result can only be served back to whoever
|
|
13
|
+
* could have fetched it: the browser keeps one store per client for the life
|
|
14
|
+
* of the app; the server keeps one per client per request, since requests act
|
|
15
|
+
* for different users and their forwarded credentials are not part of a cache
|
|
16
|
+
* key. Stores are per client name because keys are method + path + args,
|
|
17
|
+
* which two different APIs can share.
|
|
18
|
+
*/
|
|
19
|
+
export declare const resolveCache: (name: string) => CacheStore;
|
|
6
20
|
/** The config a named client resolves to in the current environment. */
|
|
7
|
-
export declare const resolveConfig: (name: string) =>
|
|
21
|
+
export declare const resolveConfig: (name: string) => EnvironmentConfig;
|
|
8
22
|
/**
|
|
9
23
|
* Layers caller wiring over the environment config. The caller wins on
|
|
10
24
|
* conflict, except headers, which merge — the environment's forwarded
|
package/.dist/runtime/util.mjs
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
import { KEY } from "@openapi-press/nuxt/constant";
|
|
2
|
-
import {
|
|
2
|
+
import { memoryCache } from "openapi-press";
|
|
3
|
+
import { useRequestEvent, useRequestHeaders, useRuntimeConfig } from "#imports";
|
|
4
|
+
const browserCaches = /* @__PURE__ */ new Map();
|
|
5
|
+
const requestCaches = /* @__PURE__ */ new WeakMap();
|
|
6
|
+
const cacheIn = (caches, name) => {
|
|
7
|
+
let cache = caches.get(name);
|
|
8
|
+
if (cache === void 0) {
|
|
9
|
+
cache = memoryCache();
|
|
10
|
+
caches.set(name, cache);
|
|
11
|
+
}
|
|
12
|
+
return cache;
|
|
13
|
+
};
|
|
14
|
+
export const resolveCache = (name) => {
|
|
15
|
+
if (!import.meta.server) return cacheIn(browserCaches, name);
|
|
16
|
+
const event = useRequestEvent();
|
|
17
|
+
if (event === void 0) return memoryCache();
|
|
18
|
+
let caches = requestCaches.get(event);
|
|
19
|
+
if (caches === void 0) {
|
|
20
|
+
caches = /* @__PURE__ */ new Map();
|
|
21
|
+
requestCaches.set(event, caches);
|
|
22
|
+
}
|
|
23
|
+
return cacheIn(caches, name);
|
|
24
|
+
};
|
|
3
25
|
export const resolveConfig = (name) => {
|
|
4
26
|
const runtime = useRuntimeConfig();
|
|
5
27
|
const prefix = runtime.public[KEY]?.clients?.[name]?.prefix;
|
|
@@ -20,9 +42,9 @@ export const resolveConfig = (name) => {
|
|
|
20
42
|
for (const [key, value] of Object.entries(incoming)) {
|
|
21
43
|
if (value !== void 0) headers[key] = value;
|
|
22
44
|
}
|
|
23
|
-
return { baseUrl: host, headers };
|
|
45
|
+
return { baseUrl: host, headers, cache: resolveCache(name) };
|
|
24
46
|
}
|
|
25
|
-
return { baseUrl: prefix };
|
|
47
|
+
return { baseUrl: prefix, cache: resolveCache(name) };
|
|
26
48
|
};
|
|
27
49
|
export const mergeConfig = (base, override) => {
|
|
28
50
|
const merged = { ...base, ...override };
|
package/README.md
CHANGED
|
@@ -80,6 +80,21 @@ export const useApi = () => usePress("api", { logger });
|
|
|
80
80
|
Every call builds a fresh client (builds are cheap — wrap in your own
|
|
81
81
|
composable if sharing matters, e.g. for stateful `.with` wrappers).
|
|
82
82
|
|
|
83
|
+
When the registered Press requires something of its config — its SDK applied
|
|
84
|
+
`withCache()`, `withReauth()`, or a custom wrapper with requirements — the
|
|
85
|
+
config argument must supply it, and omitting it is a type error.
|
|
86
|
+
|
|
87
|
+
The one exception is `cache`, which the module supplies: one store per client
|
|
88
|
+
per request on the server, so a result is never served across users, and one
|
|
89
|
+
per client for the life of the app in the browser. A Press that requires a
|
|
90
|
+
cache works with a bare `usePress("api")`. Pass your own store to replace it,
|
|
91
|
+
or `cache: false` to run uncached. Anything else — a `refresh` callback, a
|
|
92
|
+
custom requirement — is the app's to supply:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
export const useApi = () => usePress("api", { refresh });
|
|
96
|
+
```
|
|
97
|
+
|
|
83
98
|
## API
|
|
84
99
|
|
|
85
100
|
- `usePress(name, config?)` — auto-imported composable; builds the named
|
|
@@ -88,7 +103,8 @@ composable if sharing matters, e.g. for stateful `.with` wrappers).
|
|
|
88
103
|
- `press.clients` (nuxt.config) —
|
|
89
104
|
`Record<name, { client, host, prefix }>`: the Press module path, the
|
|
90
105
|
upstream origin, and the proxy mount.
|
|
91
|
-
- `#build/types/openapi-press` — generated `PressClients` / `
|
|
106
|
+
- `#build/types/openapi-press` — generated `PressClients` / `PressConfigs` /
|
|
107
|
+
`PressClientName`.
|
|
92
108
|
- `@openapi-press/nuxt/types` — the contract types.
|
|
93
109
|
- `@openapi-press/nuxt/config` — the `definePressConfig` identity helper.
|
|
94
110
|
- `@openapi-press/nuxt/constant` — `KEY`, the module's config namespace.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openapi-press/nuxt",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@nuxt/kit": "^4.4.6",
|
|
40
40
|
"h3": "^1.15.11",
|
|
41
|
-
"openapi-press": "0.1.
|
|
41
|
+
"openapi-press": "0.1.2"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@nuxt/nitro-server": "^4.4.6",
|