@flyo/nitro-astro 2.0.3 → 2.0.4

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/cdn.ts ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Create an astro ExternalImageService for flyo where the base url with transormations looks like https://storage.flyo.cloud/image_7a158241.jpg/thumb/250x250?format=webp
3
+ */
4
+ import type { ExternalImageService, ImageTransform } from "astro";
5
+
6
+ const service: ExternalImageService = {
7
+ getURL(options: ImageTransform) {
8
+ // check if the options.src contains already https://storage.flyo.cloud
9
+ // if not we add it to the url
10
+
11
+ let url =
12
+ typeof options.src === "string" &&
13
+ options.src.includes("https://storage.flyo.cloud")
14
+ ? options.src
15
+ : `https://storage.flyo.cloud/${options.src}`;
16
+
17
+ // if either width or height are defined we add the /thumb/$widthx$height path to it.
18
+ let width: string | number | null = options.width ? options.width : null;
19
+ let height: string | number | null = options.height ? options.height : null;
20
+
21
+ if (width || height) {
22
+ if (width === null) {
23
+ width = "null";
24
+ }
25
+ if (height === null) {
26
+ height = "null";
27
+ }
28
+ url += `/thumb/${width}x${height}`;
29
+ }
30
+
31
+ const format = options.format ? options.format : "webp";
32
+
33
+ return `${url}?format=${format}`;
34
+ },
35
+
36
+ getHTMLAttributes(options) {
37
+ const { ...attributes } = options;
38
+ return {
39
+ ...attributes,
40
+ width: options.width ?? null, // width and height are required to prevent CLS and enable lazy loading for chrome.
41
+ height: options.height ?? null, // width and height are required to prevent CLS and enable lazy loading for chrome.
42
+ loading: options.loading ?? "lazy",
43
+ decoding: options.decoding ?? "async",
44
+ };
45
+ },
46
+ };
47
+
48
+ export default service;
package/middleware.ts ADDED
@@ -0,0 +1,45 @@
1
+ import { defineMiddleware } from "astro/middleware";
2
+ import { useConfigApi, useFlyoIntegration } from "@flyo/nitro-astro";
3
+
4
+ let resolvedValue;
5
+
6
+ async function getConfigPromise(context) {
7
+ if (resolvedValue) {
8
+ // If the value is already resolved, return a resolved promise with the value
9
+ return resolvedValue;
10
+ }
11
+
12
+ // Fetch the config and store the resolved value
13
+ const value = await useConfigApi().config({
14
+ lang: context.currentLocale,
15
+ });
16
+ resolvedValue = value;
17
+ return value;
18
+ }
19
+
20
+ export const onRequest = defineMiddleware(async (context, next) => {
21
+ context.locals.config = getConfigPromise(context);
22
+
23
+ const liveEditEnabled = useFlyoIntegration().options.liveEdit;
24
+
25
+ if (!liveEditEnabled) {
26
+ const response = await next();
27
+
28
+ response.headers.set(
29
+ "Vercel-CDN-Cache-Control",
30
+ `max-age=${useFlyoIntegration().options.serverCacheHeaderTtl}`
31
+ );
32
+ response.headers.set(
33
+ "CDN-Cache-Control",
34
+ `max-age=${useFlyoIntegration().options.serverCacheHeaderTtl}`
35
+ );
36
+ response.headers.set(
37
+ "Cache-Control",
38
+ `max-age=${useFlyoIntegration().options.clientCacheHeaderTtl}`
39
+ );
40
+
41
+ return response;
42
+ }
43
+
44
+ return next();
45
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flyo/nitro-astro",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "Nitro Astro connecting Flyo Headless to Astro",
5
5
  "main": "./dist/nitro-astro.js",
6
6
  "module": "./dist/nitro-astro.mjs",
@@ -66,7 +66,11 @@
66
66
  },
67
67
  "files": [
68
68
  "dist",
69
- "components"
69
+ "components",
70
+ "cdn.ts",
71
+ "middleware.ts",
72
+ "sitemap.ts",
73
+ "toolbar.ts"
70
74
  ],
71
75
  "types": "./dist/types/index.d.ts",
72
76
  "author": "Basil Suter <git@nadar.io>",
package/sitemap.ts ADDED
@@ -0,0 +1,34 @@
1
+ import { useSitemapApi } from "@flyo/nitro-astro";
2
+ import type { AstroGlobal } from "astro";
3
+
4
+ function buildUrl(path: string, domain: string) {
5
+ return `${domain.replace(/\/$/, "")}/${path.replace(/^\//, "")}`;
6
+ }
7
+
8
+ export async function GET(config: AstroGlobal) {
9
+ const sitemap = await useSitemapApi().sitemap();
10
+
11
+ let xml = '<?xml version="1.0" encoding="UTF-8"?>';
12
+ xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
13
+
14
+ const routes = [];
15
+ for (const item of sitemap) {
16
+ if (item.entity_type === "nitro-page") {
17
+ if (routes.includes(item.entity_slug)) {
18
+ continue;
19
+ }
20
+ routes.push(item.entity_slug);
21
+ xml += `<url><loc>${buildUrl(item.entity_slug, config.site.origin)}</loc></url>`;
22
+ } else if (item.routes?.detail) {
23
+ xml += `<url><loc>${buildUrl(item.routes["detail"], config.site.origin)}</loc></url>`;
24
+ }
25
+ }
26
+
27
+ xml += "</urlset>";
28
+
29
+ return new Response(xml, {
30
+ headers: {
31
+ "Content-Type": "application/xml",
32
+ },
33
+ });
34
+ }
package/toolbar.ts ADDED
@@ -0,0 +1,30 @@
1
+ import { defineToolbarApp } from "astro/toolbar";
2
+
3
+ export default defineToolbarApp({
4
+ init(canvas) {
5
+ const windowElement = document.createElement("astro-dev-toolbar-window");
6
+
7
+ const links = [
8
+ { link: "https://flyo.cloud", text: "Flyo Cloud Login" },
9
+ {
10
+ link: "https://dev.flyo.cloud/nitro/",
11
+ text: "Flyo Nitro Developer Portal",
12
+ },
13
+ {
14
+ link: "https://nitro-openapi.flyo.cloud/",
15
+ text: "Flyo Nitro API Documentation",
16
+ },
17
+ ];
18
+
19
+ const content = links
20
+ .map(
21
+ (link) =>
22
+ `<astro-dev-toolbar-card link="${link.link}" style="margin-bottom:8px;">
23
+ ${link.text}
24
+ </astro-dev-toolbar-card>`
25
+ )
26
+ .join("");
27
+ windowElement.innerHTML = content;
28
+ canvas.appendChild(windowElement);
29
+ },
30
+ });