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

How to use Forest with Elysia or Hono ? #1175

Open
GautierT opened this issue Sep 19, 2024 · 8 comments
Open

How to use Forest with Elysia or Hono ? #1175

GautierT opened this issue Sep 19, 2024 · 8 comments

Comments

@GautierT
Copy link

Hi.
We would like to use Forest with Elysia or Hono.
Is it possible ?

Maybe with https://elysiajs.com/patterns/mount.html ?

Thanks.

@GautierT GautierT changed the title How to use Forest with Elysia or Hono How to use Forest with Elysia or Hono ? Sep 19, 2024
@Scra3
Copy link
Member

Scra3 commented Sep 19, 2024

Hello,
Welcome on our repository :)

Currently we don't support Elysia or Hono but I can forward your feedback to our product board.

If you wish, you can propose a PR to support your Framework. You should update this file. I will happy to review your proposal.

Be careful, It's a Node.Js agent, Bun is not 100% currently compatible with Node.js.
Thanks

@Scra3
Copy link
Member

Scra3 commented Sep 19, 2024

If you wish, you can use the agent in standalone server and use the datasources you want. If you don't want to manage the server, you can use our cloud solution..

@GautierT
Copy link
Author

Thanks @Scra3
I tried to use the standalone agent but I can't find the way to get the onboarding on forest without the need to input my db info.
In my previous experience with forest I was able to get the 2 tokens and input my app url where forest was exposed...
Am I missing something?

Thanks.

@Scra3
Copy link
Member

Scra3 commented Sep 22, 2024

Hello,
It's little bit hidden currently :/
After you have created a new project on the onboarding page add the following queryParam: ?customHosting=true.
Example: https://app.forestadmin.com/MyProjectName/onboarding/framework?customHosting=true
Thanks

@GautierT
Copy link
Author

GautierT commented Sep 22, 2024 via email

@GautierT
Copy link
Author

GautierT commented Sep 22, 2024

I tried to bun run this script :

import { createAgent } from '@forestadmin/agent'
import { createSqlDataSource } from '@forestadmin/datasource-sql'

import { prisma } from './lib/prismaClient'
import { env } from './utils/env'

const agent = createAgent({
  authSecret: env.require('FOREST_AUTH_SECRET'),
  envSecret: env.require('FOREST_ENV_SECRET'),
  isProduction: env.require('NODE_ENV') === 'production',
  typingsPath: './typings.ts',
  typingsMaxDepth: 5,
})

// Utilisation de Prisma avec Forest Admin
agent.addDataSource(createSqlDataSource(env.require('DATABASE_URL')))

// Montage de l'agent sur un serveur autonome
const server = await agent.mountOnStandaloneServer(3005).start()

console.log('server', server)

And it gives me a timeout error :

warning: Error while registering the authentication client. Authentication might not work: The request to Forest Admin server has timeout
options:  {
  forestServerUrl: "https://api.forestadmin.com",
  permissionsCacheDurationInSeconds: 31560000,
  logger: [Function: defaultLogger],
  instantCacheRefresh: true,
  experimental: undefined,
  envSecret: "ENV_SECRET_REDACTED",
}
path:  /liana/v1/ip-whitelist-rules
headers:  {}
body:  undefined
error:  707 |
708 | RequestBase.prototype._timeoutError = function (reason, timeout, errno) {
709 |   if (this._aborted) {
710 |     return;
711 |   }
712 |   const error = new Error(`${reason + timeout}ms exceeded`);
                      ^
error: Timeout of 10000ms exceeded
 code: "ECONNABORTED"

      at /Users/gautier/Documents/Projets/back/node_modules/superagent/lib/request-base.js:712:17
      at /Users/gautier/Documents/Projets/back/node_modules/superagent/lib/request-base.js:727:12

16 |       console.log('path: ', path)
17 |       console.log('headers: ', headers)
18 |       console.log('body: ', body)
19 |       console.log('error: ', error)
20 |       if (error.timeout) {
21 |         throw new Error('The request to Forest Admin server has timeout')
                   ^
error: The request to Forest Admin server has timeout
      at /Users/gautier/Documents/Projets/back/node_modules/@forestadmin/forestadmin-client/dist/utils/server.js:21:15
16 |       console.log('path: ', path)
17 |       console.log('headers: ', headers)
18 |       console.log('body: ', body)
19 |       console.log('error: ', error)
20 |       if (error.timeout) {
21 |         throw new Error('The request to Forest Admin server has timeout')
                   ^
error: The request to Forest Admin server has timeout
      at /Users/gautier/Documents/Projets/back/node_modules/@forestadmin/forestadmin-client/dist/utils/server.js:21:15

@GautierT
Copy link
Author

It seems to be a problem with bun and superagent...
I replace superagent by fetch in forestadmin-client/utils/server.js by using bun patch and it's working now.

diff --git a/dist/utils/server.js b/dist/utils/server.js
index 5e0df8ca17feaa1746418f33fcbfd53c41567ba4..4184c3848680af52a0bbf38ae58660284bd0d200 100644
--- a/dist/utils/server.js
+++ b/dist/utils/server.js
@@ -1,7 +1,5 @@
-"use strict";
-var __importDefault = (this && this.__importDefault) || function (mod) {
-    return (mod && mod.__esModule) ? mod : { "default": mod };
-};
+
+var __importDefault = (this && this.__importDefault) || ((mod) => (mod && mod.__esModule) ? mod : { "default": mod });
 Object.defineProperty(exports, "__esModule", { value: true });
 const superagent_1 = __importDefault(require("superagent"));
 const __1 = require("..");
@@ -9,21 +7,38 @@ class ServerUtils {
     /** Query Forest-Admin server */
     static async query(options, method, path, headers = {}, body, maxTimeAllowed = 10000) {
         try {
-            const url = new URL(path, options.forestServerUrl).toString();
-            const request = superagent_1.default[method](url).timeout(maxTimeAllowed);
-            request.set('forest-secret-key', options.envSecret);
-            if (headers)
-                request.set(headers);
-            const response = await request.send(body);
-            return response.body;
+          const url = new URL(path, options.forestServerUrl).toString()
+          const fetchOptions = {
+            method,
+            headers: {
+              'forest-secret-key': options.envSecret,
+              ...headers,
+              'Content-Type': 'application/json',
+            },
+            body: body ? JSON.stringify(body) : undefined,
+          }
+    
+          const controller = new AbortController()
+          const timeoutId = setTimeout(() => controller.abort(), maxTimeAllowed)
+    
+          const response = await fetch(url, { ...fetchOptions, signal: controller.signal })
+          clearTimeout(timeoutId)
+    
+    
+          if (!response.ok) {
+            console.log('response: ', response)
+            throw new Error(`HTTP error! status: ${response.status}`)
+          }
+    
+          return await response.json()
+        } catch (error) {
+          console.log('error: ', error)
+          if (error.name === 'AbortError') {
+            throw new Error('The request to Forest Admin server has timeout')
+          }
+          this.handleResponseError(error)
         }
-        catch (error) {
-            if (error.timeout) {
-                throw new Error('The request to Forest Admin server has timeout');
-            }
-            this.handleResponseError(error);
-        }
-    }
+      }
     static handleResponseError(e) {
         if (/certificate/i.test(e.message)) {
             throw new Error('Forest Admin server TLS certificate cannot be verified. ' +

@Scra3
Copy link
Member

Scra3 commented Sep 23, 2024

Hello Gautier,
We did the choice to not support Bun for the moment.
If you want to continue to use it with Bun, you will probably force to maintain your patch :/
Have a good time with our Product,
Alban

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants