@openapi-press/nuxt 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/config.d.mts +12 -0
- package/.dist/config.d.ts +12 -0
- package/.dist/config.mjs +3 -0
- package/.dist/constant.d.mts +7 -0
- package/.dist/constant.d.ts +7 -0
- package/.dist/constant.mjs +3 -0
- package/.dist/module.d.mts +19 -0
- package/.dist/module.d.ts +19 -0
- package/.dist/module.mjs +99 -0
- package/.dist/runtime/composable.d.ts +14 -0
- package/.dist/runtime/composable.mjs +7 -0
- package/.dist/runtime/server/proxy.d.ts +8 -0
- package/.dist/runtime/server/proxy.mjs +22 -0
- package/.dist/runtime/util.d.ts +13 -0
- package/.dist/runtime/util.mjs +37 -0
- package/.dist/types.d.mts +51 -0
- package/.dist/types.d.ts +51 -0
- package/.dist/types.mjs +1 -0
- package/.dist/util.d.mts +29 -0
- package/.dist/util.d.ts +29 -0
- package/.dist/util.mjs +41 -0
- package/LICENSE +21 -0
- package/README.md +96 -0
- package/package.json +56 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { NuxtPressConfig } from './types.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The authoring helper for the module's nuxt.config section. The module
|
|
5
|
+
* never sees a spec or a tree — SDKs are built by their authors as `Press`
|
|
6
|
+
* factories; the module's job is to make them usable in a Nuxt app.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Identity helper that types a Nuxt openapi-press configuration. */
|
|
10
|
+
declare const definePressConfig: (config: NuxtPressConfig) => NuxtPressConfig;
|
|
11
|
+
|
|
12
|
+
export { definePressConfig };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { NuxtPressConfig } from './types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The authoring helper for the module's nuxt.config section. The module
|
|
5
|
+
* never sees a spec or a tree — SDKs are built by their authors as `Press`
|
|
6
|
+
* factories; the module's job is to make them usable in a Nuxt app.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Identity helper that types a Nuxt openapi-press configuration. */
|
|
10
|
+
declare const definePressConfig: (config: NuxtPressConfig) => NuxtPressConfig;
|
|
11
|
+
|
|
12
|
+
export { definePressConfig };
|
package/.dist/config.mjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { NuxtModule } from '@nuxt/schema';
|
|
2
|
+
import { NuxtPressConfig } from './types.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Nuxt module for openapi-press.
|
|
6
|
+
*
|
|
7
|
+
* The module makes user-built clients usable; it never builds one. Each named
|
|
8
|
+
* client in `press.clients` points at a module whose default export is a
|
|
9
|
+
* `Press` factory. Setup resolves those paths, splits host (private) and
|
|
10
|
+
* prefix (public) across the runtime-config planes, mounts a proxy per
|
|
11
|
+
* prefix, and writes the client registry into build templates: a manifest
|
|
12
|
+
* `usePress` imports the factories through, and a type manifest deriving the
|
|
13
|
+
* name union and each client's type. The composable builds on demand — there
|
|
14
|
+
* is no plugin and nothing on the nuxt context.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
declare const module$1: NuxtModule<NuxtPressConfig>;
|
|
18
|
+
|
|
19
|
+
export { module$1 as default };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { NuxtModule } from '@nuxt/schema';
|
|
2
|
+
import { NuxtPressConfig } from './types.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Nuxt module for openapi-press.
|
|
6
|
+
*
|
|
7
|
+
* The module makes user-built clients usable; it never builds one. Each named
|
|
8
|
+
* client in `press.clients` points at a module whose default export is a
|
|
9
|
+
* `Press` factory. Setup resolves those paths, splits host (private) and
|
|
10
|
+
* prefix (public) across the runtime-config planes, mounts a proxy per
|
|
11
|
+
* prefix, and writes the client registry into build templates: a manifest
|
|
12
|
+
* `usePress` imports the factories through, and a type manifest deriving the
|
|
13
|
+
* name union and each client's type. The composable builds on demand — there
|
|
14
|
+
* is no plugin and nothing on the nuxt context.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
declare const module$1: NuxtModule<NuxtPressConfig>;
|
|
18
|
+
|
|
19
|
+
export { module$1 as default };
|
package/.dist/module.mjs
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { defineNuxtModule, createResolver, resolvePath, addServerHandler, addTemplate, addTypeTemplate, addImports } from '@nuxt/kit';
|
|
2
|
+
import { KEY } from './constant.mjs';
|
|
3
|
+
import { normalizeClients } from './util.mjs';
|
|
4
|
+
|
|
5
|
+
const module$1 = defineNuxtModule({
|
|
6
|
+
meta: {
|
|
7
|
+
name: "openapi-press",
|
|
8
|
+
configKey: KEY
|
|
9
|
+
},
|
|
10
|
+
setup: async (options, nuxt) => {
|
|
11
|
+
const resolver = createResolver(import.meta.url);
|
|
12
|
+
const clients = normalizeClients(options.clients ?? {});
|
|
13
|
+
const family = [
|
|
14
|
+
"openapi-press",
|
|
15
|
+
"@openapi-press/spec",
|
|
16
|
+
"@openapi-press/error",
|
|
17
|
+
"@openapi-press/call",
|
|
18
|
+
"@openapi-press/client"
|
|
19
|
+
];
|
|
20
|
+
nuxt.options.vite.ssr ||= {};
|
|
21
|
+
if (Array.isArray(nuxt.options.vite.ssr.external)) {
|
|
22
|
+
nuxt.options.vite.ssr.external.push(...family);
|
|
23
|
+
} else {
|
|
24
|
+
nuxt.options.vite.ssr.external = [...family];
|
|
25
|
+
}
|
|
26
|
+
nuxt.options.nitro.externals ||= {};
|
|
27
|
+
(nuxt.options.nitro.externals.external ||= []).push(...family);
|
|
28
|
+
const entries = await Promise.all(
|
|
29
|
+
Object.entries(clients).map(async ([name, client]) => {
|
|
30
|
+
const path = await resolvePath(client.client, {
|
|
31
|
+
cwd: nuxt.options.srcDir,
|
|
32
|
+
alias: nuxt.options.alias
|
|
33
|
+
});
|
|
34
|
+
return {
|
|
35
|
+
name,
|
|
36
|
+
host: client.host,
|
|
37
|
+
prefix: client.prefix,
|
|
38
|
+
path,
|
|
39
|
+
typePath: path.replace(/\.[cm]?[jt]sx?$/, "")
|
|
40
|
+
};
|
|
41
|
+
})
|
|
42
|
+
);
|
|
43
|
+
const hosts = {};
|
|
44
|
+
const prefixes = {};
|
|
45
|
+
for (const entry of entries) {
|
|
46
|
+
hosts[entry.name] = { host: entry.host };
|
|
47
|
+
prefixes[entry.name] = { prefix: entry.prefix };
|
|
48
|
+
}
|
|
49
|
+
nuxt.options.runtimeConfig[KEY] = { clients: hosts };
|
|
50
|
+
nuxt.options.runtimeConfig.public[KEY] = { clients: prefixes };
|
|
51
|
+
for (const entry of entries) {
|
|
52
|
+
addServerHandler({
|
|
53
|
+
route: `${entry.prefix}/**`,
|
|
54
|
+
handler: resolver.resolve("./runtime/server/proxy")
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
addTemplate({
|
|
58
|
+
filename: "openapi-press.mjs",
|
|
59
|
+
write: true,
|
|
60
|
+
getContents: () => [
|
|
61
|
+
...entries.map(
|
|
62
|
+
(entry, i) => `import c${i} from ${JSON.stringify(entry.path)};`
|
|
63
|
+
),
|
|
64
|
+
`export const clients = { ${entries.map((entry, i) => `${JSON.stringify(entry.name)}: c${i}`).join(", ")} };`
|
|
65
|
+
].join("\n")
|
|
66
|
+
});
|
|
67
|
+
addTemplate({
|
|
68
|
+
filename: "openapi-press.d.mts",
|
|
69
|
+
write: true,
|
|
70
|
+
getContents: () => [
|
|
71
|
+
...entries.map(
|
|
72
|
+
(entry, i) => `import type c${i} from ${JSON.stringify(entry.typePath)};`
|
|
73
|
+
),
|
|
74
|
+
`export declare const clients: { ${entries.map((entry, i) => `${JSON.stringify(entry.name)}: typeof c${i}`).join("; ")} };`
|
|
75
|
+
].join("\n")
|
|
76
|
+
});
|
|
77
|
+
addTypeTemplate({
|
|
78
|
+
filename: "types/openapi-press.d.ts",
|
|
79
|
+
write: true,
|
|
80
|
+
getContents: () => [
|
|
81
|
+
...entries.map(
|
|
82
|
+
(entry, i) => `import type c${i} from ${JSON.stringify(entry.typePath)};`
|
|
83
|
+
),
|
|
84
|
+
`export interface PressClients { ${entries.map(
|
|
85
|
+
(entry, i) => `${JSON.stringify(entry.name)}: ReturnType<typeof c${i}>;`
|
|
86
|
+
).join(" ")} }`,
|
|
87
|
+
`export type PressClientName = keyof PressClients;`,
|
|
88
|
+
`declare module "nuxt/schema" { interface RuntimeConfig { ${KEY}: { clients: Record<PressClientName, { host: string }> } } interface PublicRuntimeConfig { ${KEY}: { clients: Record<PressClientName, { prefix: string }> } } }`,
|
|
89
|
+
`export {};`
|
|
90
|
+
].join("\n")
|
|
91
|
+
});
|
|
92
|
+
addImports({
|
|
93
|
+
name: "usePress",
|
|
94
|
+
from: resolver.resolve("./runtime/composable")
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
export { module$1 as default };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `usePress(name, config?)` builds a registered client on demand: the
|
|
3
|
+
* environment config — host or proxy prefix, forwarded credentials — layered
|
|
4
|
+
* under whatever wiring the caller supplies (logger, hooks, mapError,
|
|
5
|
+
* anything from `ClientConfig`). Every call builds fresh; builds are cheap.
|
|
6
|
+
* An app that wants to share a wired client wraps this in its own
|
|
7
|
+
* composable:
|
|
8
|
+
*
|
|
9
|
+
* const useApi = () => usePress("api", { logger });
|
|
10
|
+
*/
|
|
11
|
+
import type { ClientConfig } from "openapi-press";
|
|
12
|
+
import type { PressClients } from "#build/types/openapi-press";
|
|
13
|
+
/** The client registered under `name`, optionally rebuilt with caller wiring. */
|
|
14
|
+
export declare const usePress: <K extends keyof PressClients & string>(name: K, config?: ClientConfig) => PressClients[K];
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { clients } from "#build/openapi-press.mjs";
|
|
2
|
+
import { mergeConfig, resolveConfig } from "./util.mjs";
|
|
3
|
+
export const usePress = (name, config = {}) => {
|
|
4
|
+
const cfg = resolveConfig(name);
|
|
5
|
+
const merged = mergeConfig(cfg, config);
|
|
6
|
+
return clients[name](merged);
|
|
7
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The per-client API proxy. Mounted at every configured prefix; resolves the
|
|
3
|
+
* matched client from runtime config at request time and forwards the request
|
|
4
|
+
* — method, remaining path, query, headers, body — to that client's upstream
|
|
5
|
+
* host. The browser only ever sees the prefix.
|
|
6
|
+
*/
|
|
7
|
+
declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<any>>;
|
|
8
|
+
export default _default;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { KEY } from "@openapi-press/nuxt/constant";
|
|
2
|
+
import { matchClient, proxyTarget } from "@openapi-press/nuxt/util";
|
|
3
|
+
import { createError, defineEventHandler, proxyRequest } from "h3";
|
|
4
|
+
import { useRuntimeConfig } from "#imports";
|
|
5
|
+
export default defineEventHandler((event) => {
|
|
6
|
+
const config = useRuntimeConfig(event);
|
|
7
|
+
const matched = matchClient(event.path, config.public[KEY]?.clients ?? {});
|
|
8
|
+
if (matched === null) {
|
|
9
|
+
throw createError({
|
|
10
|
+
statusCode: 404,
|
|
11
|
+
statusMessage: "No openapi-press client mounted at this path"
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
const host = config[KEY]?.clients?.[matched.name]?.host;
|
|
15
|
+
if (host === void 0) {
|
|
16
|
+
throw createError({
|
|
17
|
+
statusCode: 502,
|
|
18
|
+
statusMessage: `No upstream host configured for openapi-press client "${matched.name}"`
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return proxyRequest(event, proxyTarget(host, matched.prefix, event.path));
|
|
22
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The runtime's config plumbing behind the composable: resolving a named
|
|
3
|
+
* client's environment config, and layering caller wiring over it.
|
|
4
|
+
*/
|
|
5
|
+
import type { ClientConfig } from "openapi-press";
|
|
6
|
+
/** The config a named client resolves to in the current environment. */
|
|
7
|
+
export declare const resolveConfig: (name: string) => ClientConfig;
|
|
8
|
+
/**
|
|
9
|
+
* Layers caller wiring over the environment config. The caller wins on
|
|
10
|
+
* conflict, except headers, which merge — the environment's forwarded
|
|
11
|
+
* credentials survive a caller supplying its own headers.
|
|
12
|
+
*/
|
|
13
|
+
export declare const mergeConfig: (base: ClientConfig, override: ClientConfig) => ClientConfig;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { KEY } from "@openapi-press/nuxt/constant";
|
|
2
|
+
import { useRequestHeaders, useRuntimeConfig } from "#imports";
|
|
3
|
+
export const resolveConfig = (name) => {
|
|
4
|
+
const runtime = useRuntimeConfig();
|
|
5
|
+
const prefix = runtime.public[KEY]?.clients?.[name]?.prefix;
|
|
6
|
+
if (prefix === void 0) {
|
|
7
|
+
throw new Error(
|
|
8
|
+
`openapi-press: no client named "${name}" \u2014 configure it under \`${KEY}.clients\` in nuxt.config.`
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
if (import.meta.server) {
|
|
12
|
+
const host = runtime[KEY]?.clients?.[name]?.host;
|
|
13
|
+
if (host === void 0) {
|
|
14
|
+
throw new Error(
|
|
15
|
+
`openapi-press: no upstream host for client "${name}" in server runtime config.`
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
const incoming = useRequestHeaders(["cookie", "authorization"]);
|
|
19
|
+
const headers = {};
|
|
20
|
+
for (const [key, value] of Object.entries(incoming)) {
|
|
21
|
+
if (value !== void 0) headers[key] = value;
|
|
22
|
+
}
|
|
23
|
+
return { baseUrl: host, headers };
|
|
24
|
+
}
|
|
25
|
+
return { baseUrl: prefix };
|
|
26
|
+
};
|
|
27
|
+
export const mergeConfig = (base, override) => {
|
|
28
|
+
const merged = { ...base, ...override };
|
|
29
|
+
if (base.headers !== void 0 && override.headers !== void 0) {
|
|
30
|
+
const headers = new Headers(base.headers);
|
|
31
|
+
new Headers(override.headers).forEach((value, key) => {
|
|
32
|
+
headers.set(key, value);
|
|
33
|
+
});
|
|
34
|
+
merged.headers = headers;
|
|
35
|
+
}
|
|
36
|
+
return merged;
|
|
37
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The module's contract: the nuxt.config shape, the resolved-match shape the
|
|
3
|
+
* proxy routes by, and the two runtime-config planes the module writes and
|
|
4
|
+
* the runtime reads.
|
|
5
|
+
*/
|
|
6
|
+
/** Where a named client lives, where it points, and where its proxy mounts. */
|
|
7
|
+
interface PressClientConfig {
|
|
8
|
+
/**
|
|
9
|
+
* Resolution path to the module whose default export is the client's
|
|
10
|
+
* `Press` factory. Aliases are allowed (`~/shared/api`).
|
|
11
|
+
*/
|
|
12
|
+
client: string;
|
|
13
|
+
/**
|
|
14
|
+
* Upstream origin requests are forwarded to. Server-only — it lands in
|
|
15
|
+
* private runtime config and never ships to the browser. Overridable at
|
|
16
|
+
* deploy time via `NUXT_PRESS_CLIENTS_<NAME>_HOST`.
|
|
17
|
+
*/
|
|
18
|
+
host: string;
|
|
19
|
+
/**
|
|
20
|
+
* Route prefix the proxy mounts under. Public — it is the browser's
|
|
21
|
+
* `baseUrl`, so client-side requests stay same-origin and route through
|
|
22
|
+
* the proxy.
|
|
23
|
+
*/
|
|
24
|
+
prefix: string;
|
|
25
|
+
}
|
|
26
|
+
interface NuxtPressConfig {
|
|
27
|
+
/**
|
|
28
|
+
* Named clients. The name is the handle `usePress(name, press)` resolves
|
|
29
|
+
* config by; each entry stands up its own proxy.
|
|
30
|
+
*/
|
|
31
|
+
clients?: Record<string, PressClientConfig>;
|
|
32
|
+
}
|
|
33
|
+
/** A configured client a request path resolved to. */
|
|
34
|
+
interface MatchedClient {
|
|
35
|
+
name: string;
|
|
36
|
+
prefix: string;
|
|
37
|
+
}
|
|
38
|
+
/** The private runtime-config plane: upstream hosts, server-only. */
|
|
39
|
+
interface HostPlane {
|
|
40
|
+
clients?: Record<string, {
|
|
41
|
+
host: string;
|
|
42
|
+
}>;
|
|
43
|
+
}
|
|
44
|
+
/** The public runtime-config plane: proxy prefixes, shipped to the browser. */
|
|
45
|
+
interface PrefixPlane {
|
|
46
|
+
clients?: Record<string, {
|
|
47
|
+
prefix: string;
|
|
48
|
+
}>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type { HostPlane, MatchedClient, NuxtPressConfig, PrefixPlane, PressClientConfig };
|
package/.dist/types.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The module's contract: the nuxt.config shape, the resolved-match shape the
|
|
3
|
+
* proxy routes by, and the two runtime-config planes the module writes and
|
|
4
|
+
* the runtime reads.
|
|
5
|
+
*/
|
|
6
|
+
/** Where a named client lives, where it points, and where its proxy mounts. */
|
|
7
|
+
interface PressClientConfig {
|
|
8
|
+
/**
|
|
9
|
+
* Resolution path to the module whose default export is the client's
|
|
10
|
+
* `Press` factory. Aliases are allowed (`~/shared/api`).
|
|
11
|
+
*/
|
|
12
|
+
client: string;
|
|
13
|
+
/**
|
|
14
|
+
* Upstream origin requests are forwarded to. Server-only — it lands in
|
|
15
|
+
* private runtime config and never ships to the browser. Overridable at
|
|
16
|
+
* deploy time via `NUXT_PRESS_CLIENTS_<NAME>_HOST`.
|
|
17
|
+
*/
|
|
18
|
+
host: string;
|
|
19
|
+
/**
|
|
20
|
+
* Route prefix the proxy mounts under. Public — it is the browser's
|
|
21
|
+
* `baseUrl`, so client-side requests stay same-origin and route through
|
|
22
|
+
* the proxy.
|
|
23
|
+
*/
|
|
24
|
+
prefix: string;
|
|
25
|
+
}
|
|
26
|
+
interface NuxtPressConfig {
|
|
27
|
+
/**
|
|
28
|
+
* Named clients. The name is the handle `usePress(name, press)` resolves
|
|
29
|
+
* config by; each entry stands up its own proxy.
|
|
30
|
+
*/
|
|
31
|
+
clients?: Record<string, PressClientConfig>;
|
|
32
|
+
}
|
|
33
|
+
/** A configured client a request path resolved to. */
|
|
34
|
+
interface MatchedClient {
|
|
35
|
+
name: string;
|
|
36
|
+
prefix: string;
|
|
37
|
+
}
|
|
38
|
+
/** The private runtime-config plane: upstream hosts, server-only. */
|
|
39
|
+
interface HostPlane {
|
|
40
|
+
clients?: Record<string, {
|
|
41
|
+
host: string;
|
|
42
|
+
}>;
|
|
43
|
+
}
|
|
44
|
+
/** The public runtime-config plane: proxy prefixes, shipped to the browser. */
|
|
45
|
+
interface PrefixPlane {
|
|
46
|
+
clients?: Record<string, {
|
|
47
|
+
prefix: string;
|
|
48
|
+
}>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type { HostPlane, MatchedClient, NuxtPressConfig, PrefixPlane, PressClientConfig };
|
package/.dist/types.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/.dist/util.d.mts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { MatchedClient, PressClientConfig } from './types.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The pure logic behind the module and its proxy: client-config
|
|
5
|
+
* normalization and the path matching that routes a proxied request to the
|
|
6
|
+
* right upstream. Kept free of Nuxt so it is testable as plain functions.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Strips trailing slashes so host + path concatenation is unambiguous. */
|
|
10
|
+
declare const normalizeHost: (host: string) => string;
|
|
11
|
+
/** Strips trailing slashes; the prefix is always compared segment-wise. */
|
|
12
|
+
declare const normalizePrefix: (prefix: string) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Validates and normalizes the configured clients: hosts must be absolute
|
|
15
|
+
* http(s) origins, prefixes must be rooted paths.
|
|
16
|
+
*/
|
|
17
|
+
declare const normalizeClients: (clients: Record<string, PressClientConfig>) => Record<string, PressClientConfig>;
|
|
18
|
+
/**
|
|
19
|
+
* Resolves a request path to the configured client whose prefix matches it.
|
|
20
|
+
* Matches are segment-wise (the prefix itself, or the prefix followed by a
|
|
21
|
+
* path or query boundary); when prefixes nest, the longest wins.
|
|
22
|
+
*/
|
|
23
|
+
declare const matchClient: (path: string, clients: Record<string, {
|
|
24
|
+
prefix: string;
|
|
25
|
+
}>) => MatchedClient | null;
|
|
26
|
+
/** The upstream URL a proxied request forwards to: host + path minus prefix. */
|
|
27
|
+
declare const proxyTarget: (host: string, prefix: string, path: string) => string;
|
|
28
|
+
|
|
29
|
+
export { matchClient, normalizeClients, normalizeHost, normalizePrefix, proxyTarget };
|
package/.dist/util.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { MatchedClient, PressClientConfig } from './types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The pure logic behind the module and its proxy: client-config
|
|
5
|
+
* normalization and the path matching that routes a proxied request to the
|
|
6
|
+
* right upstream. Kept free of Nuxt so it is testable as plain functions.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Strips trailing slashes so host + path concatenation is unambiguous. */
|
|
10
|
+
declare const normalizeHost: (host: string) => string;
|
|
11
|
+
/** Strips trailing slashes; the prefix is always compared segment-wise. */
|
|
12
|
+
declare const normalizePrefix: (prefix: string) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Validates and normalizes the configured clients: hosts must be absolute
|
|
15
|
+
* http(s) origins, prefixes must be rooted paths.
|
|
16
|
+
*/
|
|
17
|
+
declare const normalizeClients: (clients: Record<string, PressClientConfig>) => Record<string, PressClientConfig>;
|
|
18
|
+
/**
|
|
19
|
+
* Resolves a request path to the configured client whose prefix matches it.
|
|
20
|
+
* Matches are segment-wise (the prefix itself, or the prefix followed by a
|
|
21
|
+
* path or query boundary); when prefixes nest, the longest wins.
|
|
22
|
+
*/
|
|
23
|
+
declare const matchClient: (path: string, clients: Record<string, {
|
|
24
|
+
prefix: string;
|
|
25
|
+
}>) => MatchedClient | null;
|
|
26
|
+
/** The upstream URL a proxied request forwards to: host + path minus prefix. */
|
|
27
|
+
declare const proxyTarget: (host: string, prefix: string, path: string) => string;
|
|
28
|
+
|
|
29
|
+
export { matchClient, normalizeClients, normalizeHost, normalizePrefix, proxyTarget };
|
package/.dist/util.mjs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const normalizeHost = (host) => host.replace(/\/+$/, "");
|
|
2
|
+
const normalizePrefix = (prefix) => prefix.replace(/\/+$/, "");
|
|
3
|
+
const normalizeClients = (clients) => {
|
|
4
|
+
const out = {};
|
|
5
|
+
for (const [name, client] of Object.entries(clients)) {
|
|
6
|
+
if (typeof client.client !== "string" || client.client.length === 0) {
|
|
7
|
+
throw new Error(
|
|
8
|
+
`openapi-press: client "${name}" must point at a module whose default export is its Press.`
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
if (!/^https?:\/\//.test(client.host)) {
|
|
12
|
+
throw new Error(
|
|
13
|
+
`openapi-press: client "${name}" host must be an absolute http(s) URL.`
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
if (!client.prefix.startsWith("/")) {
|
|
17
|
+
throw new Error(
|
|
18
|
+
`openapi-press: client "${name}" prefix must start with "/".`
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
out[name] = {
|
|
22
|
+
client: client.client,
|
|
23
|
+
host: normalizeHost(client.host),
|
|
24
|
+
prefix: normalizePrefix(client.prefix)
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
return out;
|
|
28
|
+
};
|
|
29
|
+
const matchClient = (path, clients) => {
|
|
30
|
+
let best = null;
|
|
31
|
+
for (const [name, { prefix }] of Object.entries(clients)) {
|
|
32
|
+
const hit = path === prefix || path.startsWith(`${prefix}/`) || path.startsWith(`${prefix}?`);
|
|
33
|
+
if (hit && (best === null || prefix.length > best.prefix.length)) {
|
|
34
|
+
best = { name, prefix };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return best;
|
|
38
|
+
};
|
|
39
|
+
const proxyTarget = (host, prefix, path) => host + path.slice(prefix.length);
|
|
40
|
+
|
|
41
|
+
export { matchClient, normalizeClients, normalizeHost, normalizePrefix, proxyTarget };
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alexander Thorwaldson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# @openapi-press/nuxt
|
|
2
|
+
|
|
3
|
+
Nuxt module that makes openapi-press-built clients usable: typed clients on the
|
|
4
|
+
nuxt context, SSR-aware config, and a per-client API proxy.
|
|
5
|
+
|
|
6
|
+
## The model
|
|
7
|
+
|
|
8
|
+
Each named client points at a module whose default export is a `Press` — the
|
|
9
|
+
config-accepting factory openapi-press produces. At build time the module
|
|
10
|
+
resolves those paths and generates a registry: a manifest the composable
|
|
11
|
+
imports the factories through, and a type manifest deriving the name union
|
|
12
|
+
and each client's type. `usePress(name)` builds on demand with
|
|
13
|
+
environment-appropriate config — names autocomplete, the client comes back
|
|
14
|
+
fully typed, no casts. There is no plugin and nothing on the nuxt context.
|
|
15
|
+
|
|
16
|
+
Config resolution is SSR-aware:
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
SSR: Nuxt server ────────────────→ host (+ cookie/authorization forwarded)
|
|
20
|
+
Client: browser ──→ prefix (proxy) ──→ host
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`host` lives in private runtime config (env-overridable via
|
|
24
|
+
`NUXT_PRESS_CLIENTS_<NAME>_HOST`); the browser only ever sees `prefix`,
|
|
25
|
+
where a catch-all proxy forwards to the host.
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
// shared/api.ts — plain openapi-press, default-exported Press
|
|
31
|
+
import { definePress } from "openapi-press";
|
|
32
|
+
import type { paths } from "./schema.gen";
|
|
33
|
+
|
|
34
|
+
const { op, client } = definePress<paths>();
|
|
35
|
+
|
|
36
|
+
export default client({
|
|
37
|
+
users: {
|
|
38
|
+
list: op("get", "/users"),
|
|
39
|
+
get: op("get", "/users/{user_id}"),
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
// nuxt.config.ts
|
|
46
|
+
export default defineNuxtConfig({
|
|
47
|
+
modules: ["@openapi-press/nuxt"],
|
|
48
|
+
press: {
|
|
49
|
+
clients: {
|
|
50
|
+
api: {
|
|
51
|
+
client: "~/shared/api",
|
|
52
|
+
host: "https://api.internal:8443",
|
|
53
|
+
prefix: "/api/core",
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```vue
|
|
61
|
+
<script setup lang="ts">
|
|
62
|
+
const api = usePress("api"); // typed from your Press's return type
|
|
63
|
+
const { data } = await useAsyncData("user", () =>
|
|
64
|
+
api.users.get(route.params.id),
|
|
65
|
+
);
|
|
66
|
+
</script>
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
App wiring — logger, hooks, `mapError`, anything from `ClientConfig` — is
|
|
70
|
+
supplied at the call site and layered over the environment config (caller
|
|
71
|
+
wins; headers merge, so SSR credential forwarding survives):
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
const api = usePress("api", { logger, hooks: { onError: report } });
|
|
75
|
+
|
|
76
|
+
// Or as the app's own composable:
|
|
77
|
+
export const useApi = () => usePress("api", { logger });
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Every call builds a fresh client (builds are cheap — wrap in your own
|
|
81
|
+
composable if sharing matters, e.g. for stateful `.with` wrappers).
|
|
82
|
+
|
|
83
|
+
## API
|
|
84
|
+
|
|
85
|
+
- `usePress(name, config?)` — auto-imported composable; builds the named
|
|
86
|
+
client with caller wiring layered over the environment config. Typed via
|
|
87
|
+
the generated registry.
|
|
88
|
+
- `press.clients` (nuxt.config) —
|
|
89
|
+
`Record<name, { client, host, prefix }>`: the Press module path, the
|
|
90
|
+
upstream origin, and the proxy mount.
|
|
91
|
+
- `#build/types/openapi-press` — generated `PressClients` / `PressClientName`.
|
|
92
|
+
- `@openapi-press/nuxt/types` — the contract types.
|
|
93
|
+
- `@openapi-press/nuxt/config` — the `definePressConfig` identity helper.
|
|
94
|
+
- `@openapi-press/nuxt/constant` — `KEY`, the module's config namespace.
|
|
95
|
+
- `@openapi-press/nuxt/util` — the pure plumbing (`normalizeClients`,
|
|
96
|
+
`matchClient`, `proxyTarget`).
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@openapi-press/nuxt",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/zoobzio/openapi-press.git",
|
|
8
|
+
"directory": "integrations/nuxt"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./.dist/module.mjs",
|
|
12
|
+
"types": "./.dist/module.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./.dist/module.d.ts",
|
|
16
|
+
"import": "./.dist/module.mjs"
|
|
17
|
+
},
|
|
18
|
+
"./config": {
|
|
19
|
+
"types": "./.dist/config.d.ts",
|
|
20
|
+
"import": "./.dist/config.mjs"
|
|
21
|
+
},
|
|
22
|
+
"./constant": {
|
|
23
|
+
"types": "./.dist/constant.d.ts",
|
|
24
|
+
"import": "./.dist/constant.mjs"
|
|
25
|
+
},
|
|
26
|
+
"./types": {
|
|
27
|
+
"types": "./.dist/types.d.ts",
|
|
28
|
+
"import": "./.dist/types.mjs"
|
|
29
|
+
},
|
|
30
|
+
"./util": {
|
|
31
|
+
"types": "./.dist/util.d.ts",
|
|
32
|
+
"import": "./.dist/util.mjs"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
".dist"
|
|
37
|
+
],
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@nuxt/kit": "^4.4.6",
|
|
40
|
+
"h3": "^1.15.11",
|
|
41
|
+
"openapi-press": "0.1.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@nuxt/nitro-server": "^4.4.6",
|
|
45
|
+
"@nuxt/schema": "^4.4.6",
|
|
46
|
+
"@types/node": "^25.9.1",
|
|
47
|
+
"nuxt": "^4.4.6"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "unbuild",
|
|
51
|
+
"stub": "unbuild --stub",
|
|
52
|
+
"lint": "eslint .",
|
|
53
|
+
"typecheck": "tsc --noEmit",
|
|
54
|
+
"test": "vitest run"
|
|
55
|
+
}
|
|
56
|
+
}
|