Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix): decorate exports use parse-advanced, fixes #36 #54

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/eleven-deers-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@vinxi/plugin-directives": patch
"vinxi": patch
---

(fix): decorate exports use parse-advanced, fixes #36
113 changes: 113 additions & 0 deletions packages/vinxi-directives/fixtures/decorate-exports.snapshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { createReference } from '~/runtime';

import { cache } from "@solidjs/router";
import { consola } from "consola";
import { createStorage } from "unstorage";
import redisDriver from "unstorage/drivers/redis";
import type { MenuItem, Settings } from "~/types/common";

const debugFetcher = import.meta.env.VITE_COG_DEBUG_FETCHER == 1;
const cacheEnabled = import.meta.env.VITE_COG_CACHE_ENABLED == 1;
const cacheDebug = import.meta.env.VITE_COG_CACHE_DEBUG == 1;

const storage = createStorage({
driver: redisDriver({
base: import.meta.env.VITE_KB_REDIS_BASE ?? "cog:",
host: import.meta.env.VITE_KB_REDIS_HOST ?? "localhost",
port: import.meta.env.VITE_KB_REDIS_PORT ?? 6379,
}),
});

async function fetchAPI(path: string) {
const headers = new Headers();
headers.append("User-Agent", "chrome");

if (import.meta.env.VITE_COG_BACKEND_AUTH_LOGIN.length > 0) {
headers.append(
"Authorization",
"Basic " +
btoa(
import.meta.env.VITE_KB_BACKEND_AUTH_LOGIN +
":" +
import.meta.env.VITE_KB_BACKEND_AUTH_PASSWORD,
),
);
}

let url = `${import.meta.env.VITE_COG_BACKEND_BASE_URL}/${path}/`;

if (url.indexOf("?") > -1) {
// remove trailing slash
url = url.replace(/\/$/, "");
}

try {
debugFetcher && consola.info(`Fetching ${url}`);
const response = await fetch(url, { headers });

// @TODO we should probably cache error responses for a short period of time
if ("error" in response) {
return response;
}

if (response.status !== 200) {
// @TODO we should probably cache error responses for a short period of time
return {
code: response.status,
error: `Server responded with status ${response.status}`,
};
}

const json = await response.json().catch((error: string): void => {
throw new Error(`Was Fetching ${url}, got ${error}`);
});

if (!("data" in json)) {
return {
code: 500,
error: `JSON response does not contain data`,
};
}

if (cacheEnabled) {
await storage.setItem(path, json.data);
}

return json.data;
} catch (error) {
return { error };
}
}

export const getDataAtPath = async (path: string) => {
"use runtime";

if (cacheEnabled) {
const data = await storage.getItem(path);
if (data) {
cacheDebug && consola.info(`Cache hit: ${path.slice(0, 80)}…`);
return data;
}
}

cacheDebug && consola.info(`Cache miss: ${path.slice(0, 80)}…`);
return fetchAPI(path);
};

export const getSettings = cache(async (): Promise<Settings> => {
return getDataAtPath("solid/settings");
}, "settings");

export const getMenuMain = cache(async (): Promise<MenuItem[]> => {
return getDataAtPath("solid/menu/main");
}, "menu:main");

export const getMenuFooter = cache(async (): Promise<MenuItem[]> => {
return getDataAtPath("solid/menu/footer");
}, "menu:footer");


;if (typeof getDataAtPath === "function") createReference(getDataAtPath,"test", "getDataAtPath");
if (typeof getSettings === "function") createReference(getSettings,"test", "getSettings");
if (typeof getMenuMain === "function") createReference(getMenuMain,"test", "getMenuMain");
if (typeof getMenuFooter === "function") createReference(getMenuFooter,"test", "getMenuFooter");
107 changes: 107 additions & 0 deletions packages/vinxi-directives/fixtures/decorate-exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"use runtime";

import { cache } from "@solidjs/router";
import { consola } from "consola";
import { createStorage } from "unstorage";
import redisDriver from "unstorage/drivers/redis";
import type { MenuItem, Settings } from "~/types/common";

const debugFetcher = import.meta.env.VITE_COG_DEBUG_FETCHER == 1;
const cacheEnabled = import.meta.env.VITE_COG_CACHE_ENABLED == 1;
const cacheDebug = import.meta.env.VITE_COG_CACHE_DEBUG == 1;

const storage = createStorage({
driver: redisDriver({
base: import.meta.env.VITE_KB_REDIS_BASE ?? "cog:",
host: import.meta.env.VITE_KB_REDIS_HOST ?? "localhost",
port: import.meta.env.VITE_KB_REDIS_PORT ?? 6379,
}),
});

async function fetchAPI(path: string) {
const headers = new Headers();
headers.append("User-Agent", "chrome");

if (import.meta.env.VITE_COG_BACKEND_AUTH_LOGIN.length > 0) {
headers.append(
"Authorization",
"Basic " +
btoa(
import.meta.env.VITE_KB_BACKEND_AUTH_LOGIN +
":" +
import.meta.env.VITE_KB_BACKEND_AUTH_PASSWORD,
),
);
}

let url = `${import.meta.env.VITE_COG_BACKEND_BASE_URL}/${path}/`;

if (url.indexOf("?") > -1) {
// remove trailing slash
url = url.replace(/\/$/, "");
}

try {
debugFetcher && consola.info(`Fetching ${url}`);
const response = await fetch(url, { headers });

// @TODO we should probably cache error responses for a short period of time
if ("error" in response) {
return response;
}

if (response.status !== 200) {
// @TODO we should probably cache error responses for a short period of time
return {
code: response.status,
error: `Server responded with status ${response.status}`,
};
}

const json = await response.json().catch((error: string): void => {
throw new Error(`Was Fetching ${url}, got ${error}`);
});

if (!("data" in json)) {
return {
code: 500,
error: `JSON response does not contain data`,
};
}

if (cacheEnabled) {
await storage.setItem(path, json.data);
}

return json.data;
} catch (error) {
return { error };
}
}

export const getDataAtPath = async (path: string) => {
"use runtime";

if (cacheEnabled) {
const data = await storage.getItem(path);
if (data) {
cacheDebug && consola.info(`Cache hit: ${path.slice(0, 80)}…`);
return data;
}
}

cacheDebug && consola.info(`Cache miss: ${path.slice(0, 80)}…`);
return fetchAPI(path);
};

export const getSettings = cache(async (): Promise<Settings> => {
return getDataAtPath("solid/settings");
}, "settings");

export const getMenuMain = cache(async (): Promise<MenuItem[]> => {
return getDataAtPath("solid/menu/main");
}, "menu:main");

export const getMenuFooter = cache(async (): Promise<MenuItem[]> => {
return getDataAtPath("solid/menu/footer");
}, "menu:footer");
9 changes: 6 additions & 3 deletions packages/vinxi-directives/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export { directives } from "./plugin.js";
export { decorateExportsPlugin } from "./transform.js";
export { wrapExports, wrapExportsPlugin } from "./wrap-exports.js";
export { shimExports, shimExportsPlugin } from "./shim-exports.js";
export {
decorateExportsPlugin,
decorateExports,
} from "./plugins/decorate-exports.js";
export { wrapExports, wrapExportsPlugin } from "./plugins/wrap-exports.js";
export { shimExports, shimExportsPlugin } from "./plugins/shim-exports.js";
Loading
Loading