@flexacms/nuxt 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +57 -2
- package/dist/chunk-CPEHOITS.js +25 -0
- package/dist/chunk-DKK222XN.js +33 -0
- package/dist/module.d.ts +31 -0
- package/dist/module.js +63 -0
- package/dist/runtime/composables.js +51 -0
- package/dist/runtime/proxy-path.js +6 -0
- package/dist/runtime/server/client.js +6 -0
- package/dist/runtime/server/proxy.js +33 -0
- package/package.json +54 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ivan Mercedes
|
|
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
CHANGED
|
@@ -1,3 +1,58 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @flexacms/nuxt
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Flexa](https://github.com/flexacms) for Nuxt 3 and 4. Composables are auto-imported and fetch through a **read-only server proxy**, so the API key never reaches the browser.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @flexacms/nuxt
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
// nuxt.config.ts
|
|
11
|
+
export default defineNuxtConfig({
|
|
12
|
+
modules: ['@flexacms/nuxt'],
|
|
13
|
+
});
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```env
|
|
17
|
+
NUXT_FLEXA_URL=https://cms.example.com
|
|
18
|
+
NUXT_FLEXA_API_KEY=flx_...
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```vue
|
|
22
|
+
<script setup lang="ts">
|
|
23
|
+
const route = useRoute();
|
|
24
|
+
const { data: posts } = await useFlexaEntries('posts', { status: 'published' });
|
|
25
|
+
const { data: post, error } = await useFlexaEntry(
|
|
26
|
+
'posts',
|
|
27
|
+
() => route.params.slug as string,
|
|
28
|
+
);
|
|
29
|
+
</script>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Both are `useFetch` calls: they render during SSR, hydrate on the client and refetch when reactive arguments change. In server routes, `useFlexaClient()` gives the full client (writes included):
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
// server/api/subscribe.post.ts
|
|
36
|
+
export default defineEventHandler(async (event) => {
|
|
37
|
+
const body = await readBody(event);
|
|
38
|
+
|
|
39
|
+
return useFlexaClient()
|
|
40
|
+
.entity('subscribers')
|
|
41
|
+
.create({ title: body.email, values: { email: body.email } });
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The proxy (`/api/_flexa/**` by default, `flexa.proxyPath` to change it) only allows GET requests for entities, entries and schemas, and drops parameters such as `trashed`, whatever abilities the key has.
|
|
46
|
+
|
|
47
|
+
## Typed composables
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
// types/flexa-register.d.ts
|
|
51
|
+
import type { FlexaSchema } from './flexa'; // npx @flexacms/cli generate
|
|
52
|
+
|
|
53
|
+
declare module '@flexacms/nuxt' {
|
|
54
|
+
interface FlexaRegister {
|
|
55
|
+
schema: FlexaSchema;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/runtime/server/client.ts
|
|
2
|
+
import { createClient } from "@flexacms/client";
|
|
3
|
+
import { useRuntimeConfig } from "#imports";
|
|
4
|
+
var client;
|
|
5
|
+
function useFlexaClient() {
|
|
6
|
+
if (client) {
|
|
7
|
+
return client;
|
|
8
|
+
}
|
|
9
|
+
const config = useRuntimeConfig().flexa;
|
|
10
|
+
if (!config.url || !config.apiKey) {
|
|
11
|
+
throw new Error(
|
|
12
|
+
"Flexa: set NUXT_FLEXA_URL and NUXT_FLEXA_API_KEY (or flexa.url / flexa.apiKey in nuxt.config)."
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
client = createClient({
|
|
16
|
+
url: config.url,
|
|
17
|
+
apiKey: config.apiKey,
|
|
18
|
+
apiPath: config.apiPath
|
|
19
|
+
});
|
|
20
|
+
return client;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export {
|
|
24
|
+
useFlexaClient
|
|
25
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// src/runtime/proxy-path.ts
|
|
2
|
+
var ALLOWED_QUERY = [
|
|
3
|
+
"status",
|
|
4
|
+
"search",
|
|
5
|
+
"category",
|
|
6
|
+
"page",
|
|
7
|
+
"per_page"
|
|
8
|
+
];
|
|
9
|
+
function proxyRequestPath(path, query) {
|
|
10
|
+
const segments = path.split("/").filter(Boolean);
|
|
11
|
+
if (segments.length === 0 || segments.length > 2 || segments.some((segment) => segment === ".." || segment === ".")) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
if (segments[0] === "entities" && segments.length > 1) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
const search = new URLSearchParams();
|
|
18
|
+
for (const key of ALLOWED_QUERY) {
|
|
19
|
+
const value = query[key];
|
|
20
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
21
|
+
search.set(key, String(value));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (query.include === "relations" && segments[0] !== "entities") {
|
|
25
|
+
search.set("include", "relations");
|
|
26
|
+
}
|
|
27
|
+
const queryString = search.toString();
|
|
28
|
+
return `/${segments.map(encodeURIComponent).join("/")}${queryString ? `?${queryString}` : ""}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
proxyRequestPath
|
|
33
|
+
};
|
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
export * from '@flexacms/client';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Register your generated schema to type the composables:
|
|
6
|
+
*
|
|
7
|
+
* // types/flexa.d.ts
|
|
8
|
+
* import type { FlexaSchema } from './flexa';
|
|
9
|
+
*
|
|
10
|
+
* declare module '@flexacms/nuxt' {
|
|
11
|
+
* interface FlexaRegister {
|
|
12
|
+
* schema: FlexaSchema;
|
|
13
|
+
* }
|
|
14
|
+
* }
|
|
15
|
+
*/
|
|
16
|
+
interface FlexaRegister {
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface ModuleOptions {
|
|
20
|
+
/** Flexa URL. Prefer the NUXT_FLEXA_URL environment variable. */
|
|
21
|
+
url?: string;
|
|
22
|
+
/** API key, kept server-side. Prefer the NUXT_FLEXA_API_KEY environment variable. */
|
|
23
|
+
apiKey?: string;
|
|
24
|
+
/** Path of the content API. Defaults to `/api/v1`. */
|
|
25
|
+
apiPath?: string;
|
|
26
|
+
/** Route of the read-only proxy the composables call. Defaults to `/api/_flexa`. */
|
|
27
|
+
proxyPath?: string;
|
|
28
|
+
}
|
|
29
|
+
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
30
|
+
|
|
31
|
+
export { type FlexaRegister, type ModuleOptions, _default as default };
|
package/dist/module.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// src/module.ts
|
|
2
|
+
import {
|
|
3
|
+
addImports,
|
|
4
|
+
addServerHandler,
|
|
5
|
+
addServerImports,
|
|
6
|
+
createResolver,
|
|
7
|
+
defineNuxtModule
|
|
8
|
+
} from "@nuxt/kit";
|
|
9
|
+
import { defu } from "defu";
|
|
10
|
+
export * from "@flexacms/client";
|
|
11
|
+
var module_default = defineNuxtModule({
|
|
12
|
+
meta: {
|
|
13
|
+
name: "@flexacms/nuxt",
|
|
14
|
+
configKey: "flexa",
|
|
15
|
+
compatibility: { nuxt: ">=3.12.0" }
|
|
16
|
+
},
|
|
17
|
+
defaults: {
|
|
18
|
+
apiPath: "/api/v1",
|
|
19
|
+
proxyPath: "/api/_flexa"
|
|
20
|
+
},
|
|
21
|
+
setup(options, nuxt) {
|
|
22
|
+
const resolver = createResolver(import.meta.url);
|
|
23
|
+
const proxyPath = `/${(options.proxyPath ?? "/api/_flexa").replace(/^\/+|\/+$/g, "")}`;
|
|
24
|
+
nuxt.options.runtimeConfig.flexa = defu(
|
|
25
|
+
nuxt.options.runtimeConfig.flexa,
|
|
26
|
+
{
|
|
27
|
+
url: options.url ?? "",
|
|
28
|
+
apiKey: options.apiKey ?? "",
|
|
29
|
+
apiPath: options.apiPath ?? "/api/v1"
|
|
30
|
+
}
|
|
31
|
+
);
|
|
32
|
+
nuxt.options.runtimeConfig.public.flexa = defu(
|
|
33
|
+
nuxt.options.runtimeConfig.public.flexa,
|
|
34
|
+
{
|
|
35
|
+
proxyPath
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
addServerHandler({
|
|
39
|
+
route: `${proxyPath}/**`,
|
|
40
|
+
method: "get",
|
|
41
|
+
handler: resolver.resolve("./runtime/server/proxy")
|
|
42
|
+
});
|
|
43
|
+
addImports([
|
|
44
|
+
{
|
|
45
|
+
name: "useFlexaEntries",
|
|
46
|
+
from: resolver.resolve("./runtime/composables")
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
name: "useFlexaEntry",
|
|
50
|
+
from: resolver.resolve("./runtime/composables")
|
|
51
|
+
}
|
|
52
|
+
]);
|
|
53
|
+
addServerImports([
|
|
54
|
+
{
|
|
55
|
+
name: "useFlexaClient",
|
|
56
|
+
from: resolver.resolve("./runtime/server/client")
|
|
57
|
+
}
|
|
58
|
+
]);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
export {
|
|
62
|
+
module_default as default
|
|
63
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// src/runtime/composables.ts
|
|
2
|
+
import { useFetch, useRuntimeConfig } from "#imports";
|
|
3
|
+
import { computed, toValue } from "vue";
|
|
4
|
+
function proxyBase() {
|
|
5
|
+
return useRuntimeConfig().public.flexa.proxyPath;
|
|
6
|
+
}
|
|
7
|
+
function listQuery(params) {
|
|
8
|
+
const query = {};
|
|
9
|
+
for (const [key, value] of Object.entries({
|
|
10
|
+
status: params.status,
|
|
11
|
+
search: params.search,
|
|
12
|
+
category: params.category,
|
|
13
|
+
page: params.page,
|
|
14
|
+
per_page: params.perPage,
|
|
15
|
+
include: params.includeRelations ? "relations" : void 0
|
|
16
|
+
})) {
|
|
17
|
+
if (value !== void 0 && value !== "") {
|
|
18
|
+
query[key] = value;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return query;
|
|
22
|
+
}
|
|
23
|
+
function useFlexaEntries(entity, params = {}) {
|
|
24
|
+
const base = proxyBase();
|
|
25
|
+
const query = computed(() => listQuery(toValue(params)));
|
|
26
|
+
return useFetch(
|
|
27
|
+
() => `${base}/${encodeURIComponent(toValue(entity))}`,
|
|
28
|
+
{
|
|
29
|
+
query,
|
|
30
|
+
// Library composables need an explicit key: Nuxt's automatic keys are
|
|
31
|
+
// per call site, which would be shared by every use of this function.
|
|
32
|
+
key: `flexa:${toValue(entity)}:${JSON.stringify(query.value)}`,
|
|
33
|
+
watch: [query]
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
function useFlexaEntry(entity, idOrSlug, params = {}) {
|
|
38
|
+
const base = proxyBase();
|
|
39
|
+
return useFetch(
|
|
40
|
+
() => `${base}/${encodeURIComponent(toValue(entity))}/${encodeURIComponent(String(toValue(idOrSlug)))}`,
|
|
41
|
+
{
|
|
42
|
+
query: params.includeRelations ? { include: "relations" } : {},
|
|
43
|
+
key: `flexa:${toValue(entity)}:${String(toValue(idOrSlug))}`,
|
|
44
|
+
transform: (response) => response.data
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
export {
|
|
49
|
+
useFlexaEntries,
|
|
50
|
+
useFlexaEntry
|
|
51
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useFlexaClient
|
|
3
|
+
} from "../../chunk-CPEHOITS.js";
|
|
4
|
+
import {
|
|
5
|
+
proxyRequestPath
|
|
6
|
+
} from "../../chunk-DKK222XN.js";
|
|
7
|
+
|
|
8
|
+
// src/runtime/server/proxy.ts
|
|
9
|
+
import { isFlexaError } from "@flexacms/client";
|
|
10
|
+
import { createError, defineEventHandler, getQuery } from "h3";
|
|
11
|
+
var proxy_default = defineEventHandler(async (event) => {
|
|
12
|
+
const path = proxyRequestPath(
|
|
13
|
+
String(event.context.params?._ ?? ""),
|
|
14
|
+
getQuery(event)
|
|
15
|
+
);
|
|
16
|
+
if (path === null) {
|
|
17
|
+
throw createError({ statusCode: 404, statusMessage: "Not Found" });
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
return await useFlexaClient().request("GET", path);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
if (isFlexaError(error)) {
|
|
23
|
+
throw createError({
|
|
24
|
+
statusCode: error.status || 502,
|
|
25
|
+
statusMessage: error.message
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
export {
|
|
32
|
+
proxy_default as default
|
|
33
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@flexacms/nuxt",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Nuxt module for the Flexa headless CMS: auto-imported composables and a read-only server proxy that keeps your API key private.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cms",
|
|
7
|
+
"flexa",
|
|
8
|
+
"headless",
|
|
9
|
+
"nuxt",
|
|
10
|
+
"nuxt-module",
|
|
11
|
+
"vue"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/flexacms/sdk/tree/main/packages/nuxt#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/flexacms/sdk/issues"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/flexacms/sdk.git",
|
|
21
|
+
"directory": "packages/nuxt"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"README.md",
|
|
26
|
+
"LICENSE"
|
|
27
|
+
],
|
|
28
|
+
"type": "module",
|
|
29
|
+
"main": "./dist/module.js",
|
|
30
|
+
"types": "./dist/module.d.ts",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/module.d.ts",
|
|
34
|
+
"default": "./dist/module.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsup",
|
|
42
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@flexacms/client": "^0.2.0",
|
|
46
|
+
"@nuxt/kit": "^3.12.0 || ^4.0.0",
|
|
47
|
+
"defu": "^6.1.4"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@nuxt/kit": "^4.6.1",
|
|
51
|
+
"@nuxt/schema": "^4.6.1",
|
|
52
|
+
"h3": "^1.15.11",
|
|
53
|
+
"vue": "^3.5.0"
|
|
54
|
+
}
|
|
55
|
+
}
|