Skip to content

Commit

Permalink
ready to publish (#271)
Browse files Browse the repository at this point in the history
* code fix

* removed: logs

* modified: made the code ready to be published

* added: edgechains to generated package.json

* modified: package.json
  • Loading branch information
Sadaf-A authored Nov 26, 2023
1 parent 1acdbd0 commit 4313b03
Show file tree
Hide file tree
Showing 36 changed files with 14,067 additions and 157 deletions.
86 changes: 37 additions & 49 deletions JS/edgechains/examples/htmljs.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,46 @@
import { html } from "hono/html";
import { html } from 'hono/html'

// These functions form the basis of the html.js framework and will be moved to a separate lib

export const view = (viewToRender) => {
return async (c) => {
const newBody = await viewToRender({ context: c });
return c.html(newBody);
};
};
return async (c) => {
const newBody = await viewToRender({ context: c })
return c.html(newBody)
}
}

export const rootLayout = (layoutToApply) => {
return async (c, next) => {
await next();
if (c.req.header("HX-Request") !== "true") {
// Req is a normal request, so we render the whole page which means adding the root layout
const curBody = await c.res.text();
c.res = undefined; // To overwrite res, set it to undefined before setting new value https://github.com/honojs/hono/pull/970 released in https://github.com/honojs/hono/releases/tag/v3.1.0
const newBody = await layoutToApply({ context: c, children: html(curBody) });
c.res = c.html(newBody);
}
// Else do nothing and let the original response be sent
};
};
return async (c, next) => {
await next()
if (c.req.header('HX-Request') !== 'true') {
// Req is a normal request, so we render the whole page which means adding the root layout
const curBody = await c.res.text()
c.res = undefined // To overwrite res, set it to undefined before setting new value https://github.com/honojs/hono/pull/970 released in https://github.com/honojs/hono/releases/tag/v3.1.0
const newBody = await layoutToApply({ context: c, children: html(curBody) })
c.res = c.html(newBody)
}
// Else do nothing and let the original response be sent
}
}

export const layout = (layoutToApply) => {
return async (c, next) => {
await next();
if (
(c.req.header("HX-Request") === "true" &&
(c.req.header("HX-Boosted") === "true" || !c.req.header("HX-Target"))) ||
c.req.header("HX-Request") !== "true"
) {
// Req is regular req or boosted link, so we apply layouts
const curBody = await c.res.text();
c.res = undefined; // To overwrite res, set it to undefined before setting new value https://github.com/honojs/hono/pull/970 released in https://github.com/honojs/hono/releases/tag/v3.1.0
const newBody = await layoutToApply({ context: c, children: html(curBody) });
c.res = c.html(newBody);
}
// Else do nothing and let the original response be sent, which will be a partial update applied to the page with hx-target
};
};

export const Link: any = ({ to, "hx-target": hxTarget, class: className, children }) => {
if (hxTarget) {
return html`<a
href="${to}"
class="${className}"
hx-get="${to}"
hx-target="${hxTarget}"
hx-push-url="true"
hx-swap="morph"
>${children}</a
>`;
} else {
return html`<a href="${to}" class="${className}" hx-boost="true">${children}</a>`;
return async (c, next) => {
await next()
if ((c.req.header('HX-Request') === 'true' && (c.req.header('HX-Boosted') === 'true' || !c.req.header('HX-Target'))) || c.req.header('HX-Request') !== 'true') {
// Req is regular req or boosted link, so we apply layouts
const curBody = await c.res.text()
c.res = undefined // To overwrite res, set it to undefined before setting new value https://github.com/honojs/hono/pull/970 released in https://github.com/honojs/hono/releases/tag/v3.1.0
const newBody = await layoutToApply({ context: c, children: html(curBody) })
c.res = c.html(newBody)
}
};
// Else do nothing and let the original response be sent, which will be a partial update applied to the page with hx-target
}
}

export const Link: any = ({ to, "hx-target": hxTarget, "class": className, children }) => {
if (hxTarget) {
return html`<a href="${to}" class="${className}" hx-get="${to}" hx-target="${hxTarget}" hx-push-url="true" hx-swap="morph">${children}</a>`
} else {
return html`<a href="${to}" class="${className}" hx-boost="true">${children}</a>`
}
}
19 changes: 0 additions & 19 deletions JS/edgechains/examples/src/config/db.ts

This file was deleted.

6 changes: 2 additions & 4 deletions JS/edgechains/examples/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import "dotenv/config";
import { serve } from "@hono/node-server";
import { Hono } from "hono";
import DatabaseConnection from "./config/db.js";
import { HydeSearchRouter } from "./routes/hydeSearch.route.js";
import { view } from "../htmljs.js";
import { view } from "../htmljs.js"
import ExampleLayout from "./layouts/ExampleLayout.js";
DatabaseConnection.establishDatabaseConnection();

const app = new Hono();

app.route("/", HydeSearchRouter);

app.get("/", view(ExampleLayout));
app.get("/", view(ExampleLayout))

serve(app, () => {
console.log("server running on port 3000");
Expand Down
8 changes: 4 additions & 4 deletions JS/edgechains/examples/src/layouts/ExampleLayout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { html } from "hono/html";
import { FC } from "hono/jsx";
import { html } from 'hono/html'
import { FC } from 'hono/jsx'

const ExampleLayout: FC = (props) => html`
<!DOCTYPE html>
Expand Down Expand Up @@ -277,6 +277,6 @@ const ExampleLayout: FC = (props) => html`
</script>
</body>
</html>
`;
`

export default ExampleLayout;
export default ExampleLayout
6 changes: 3 additions & 3 deletions JS/edgechains/examples/src/service/HydeSearchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum PostgresDistanceMetric {
async function hydeSearchAdaEmbedding(arkRequest: ArkRequest, apiKey: string, orgId: string) {
try {
const gpt3endpoint = new OpenAiEndpoint(
"https://api.openai.com/v1/chat/completions",
'https://api.openai.com/v1/chat/completions',
apiKey,
orgId,
"gpt-3.5-turbo",
Expand All @@ -29,8 +29,8 @@ async function hydeSearchAdaEmbedding(arkRequest: ArkRequest, apiKey: string, or
//
const jsonnet = new Jsonnet();

const promptPath = path.join(process.cwd(), "../src/jsonnet/prompts.jsonnet");
const hydePath = path.join(process.cwd(), "../src/jsonnet/hyde.jsonnet");
const promptPath = path.join(__dirname, "../src/jsonnet/prompts.jsonnet");
const hydePath = path.join(__dirname, "../src/jsonnet/hyde.jsonnet");
// Load Jsonnet to extract args..
const promptLoader = await jsonnet.evaluateFile(promptPath);

Expand Down
4 changes: 2 additions & 2 deletions JS/edgechains/examples/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"jsxImportSource": "hono/jsx",
"noImplicitAny": false,
"moduleResolution": "NodeNext",
"declaration": true
"declaration": true,
},
"include": [
"src/**/*.ts",
"dist/**/*.d.ts" // include the generated declaration file
]
]
}
45 changes: 45 additions & 0 deletions JS/edgechains/lib/create-edgechains/__common/esbuild.build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const esbuild = require("esbuild");
const path = require("path");
const fs = require("fs");
const { execSync } = require("child_process");

const outputDir = path.resolve(__dirname, "dist");

if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}

const distPath = path.join(process.cwd(), "dist");

fs.promises.mkdir(distPath, { recursive: true });

esbuild
.build({
entryPoints: ["./src/index.ts"],
bundle: true,
minify: true,
platform: "node",
outfile: "./dist/index.js",
tsconfig: "./tsconfig.json",
target: "node21.1.0",
external: [
"express",
"tsx",
"typescript",
"typeorm",
"react",
"react-dom",
"pg",
"jsdom",
"hono",
"@hanazuki/node-jsonnet",
"readline/promises",
],
format: "cjs",
loader: {
".html": "text",
".css": "css",
".jsonnet": "text",
},
})
.catch(() => process.exit(1));
46 changes: 46 additions & 0 deletions JS/edgechains/lib/create-edgechains/__common/htmljs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { html } from 'hono/html'

// These functions form the basis of the html.js framework and will be moved to a separate lib

export const view = (viewToRender) => {
return async (c) => {
const newBody = await viewToRender({ context: c })
return c.html(newBody)
}
}

export const rootLayout = (layoutToApply) => {
return async (c, next) => {
await next()
if (c.req.header('HX-Request') !== 'true') {
// Req is a normal request, so we render the whole page which means adding the root layout
const curBody = await c.res.text()
c.res = undefined // To overwrite res, set it to undefined before setting new value https://github.com/honojs/hono/pull/970 released in https://github.com/honojs/hono/releases/tag/v3.1.0
const newBody = await layoutToApply({ context: c, children: html(curBody) })
c.res = c.html(newBody)
}
// Else do nothing and let the original response be sent
}
}

export const layout = (layoutToApply) => {
return async (c, next) => {
await next()
if ((c.req.header('HX-Request') === 'true' && (c.req.header('HX-Boosted') === 'true' || !c.req.header('HX-Target'))) || c.req.header('HX-Request') !== 'true') {
// Req is regular req or boosted link, so we apply layouts
const curBody = await c.res.text()
c.res = undefined // To overwrite res, set it to undefined before setting new value https://github.com/honojs/hono/pull/970 released in https://github.com/honojs/hono/releases/tag/v3.1.0
const newBody = await layoutToApply({ context: c, children: html(curBody) })
c.res = c.html(newBody)
}
// Else do nothing and let the original response be sent, which will be a partial update applied to the page with hx-target
}
}

export const Link: any = ({ to, "hx-target": hxTarget, "class": className, children }) => {
if (hxTarget) {
return html`<a href="${to}" class="${className}" hx-get="${to}" hx-target="${hxTarget}" hx-push-url="true" hx-swap="morph">${children}</a>`
} else {
return html`<a href="${to}" class="${className}" hx-boost="true">${children}</a>`
}
}
11 changes: 11 additions & 0 deletions JS/edgechains/lib/create-edgechains/__common/ormconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type": "postgres",
"host": "db.rmzqtepwnzoxgkkzjctt.supabase.co",
"port": 5432,
"username": "postgres",
"password": "xaX0MYcf1YiJlChK",
"database": "postgres",
"entities": ["dist/entities/**/*.js"],
"synchronize": false,
"logging": false
}
Loading

0 comments on commit 4313b03

Please sign in to comment.