@flowgram.ai/runtime-nodejs 0.1.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.
@@ -0,0 +1,13 @@
1
+ import type { ServerParams } from '@server/type';
2
+
3
+ export const ServerConfig: ServerParams = {
4
+ name: 'flowgram-runtime',
5
+ title: 'FlowGram Runtime',
6
+ description: 'FlowGram Runtime Demo',
7
+ runtime: 'nodejs',
8
+ version: '0.0.1',
9
+ dev: false,
10
+ port: 4000,
11
+ basePath: '/api',
12
+ docsPath: '/docs',
13
+ };
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { createServer } from '@server/index';
2
+
3
+ async function main() {
4
+ const server = await createServer();
5
+ server.start();
6
+ }
7
+
8
+ main();
@@ -0,0 +1,8 @@
1
+ import type { CreateFastifyContextOptions } from '@trpc/server/adapters/fastify';
2
+
3
+ export function createContext(ctx: CreateFastifyContextOptions) {
4
+ const { req, res } = ctx;
5
+ return { req, res };
6
+ }
7
+
8
+ export type Context = Awaited<ReturnType<typeof createContext>>;
@@ -0,0 +1,14 @@
1
+ import { generateOpenApiDocument } from 'trpc-openapi';
2
+
3
+ import { ServerConfig } from '@config/index';
4
+ import { appRouter } from '@api/index';
5
+
6
+ // Generate OpenAPI schema document
7
+ export const serverDocument = generateOpenApiDocument(appRouter, {
8
+ title: ServerConfig.title,
9
+ description: ServerConfig.description,
10
+ version: ServerConfig.version,
11
+ baseUrl: `http://localhost:${ServerConfig.port}${ServerConfig.basePath}`,
12
+ docsUrl: 'https://flowgram.ai',
13
+ tags: ['Task'],
14
+ });
@@ -0,0 +1,89 @@
1
+ import { fastifyTRPCOpenApiPlugin } from 'trpc-openapi';
2
+ import fastify from 'fastify';
3
+ import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';
4
+ import { ServerInfoDefine, type ServerInfoOutput } from '@flowgram.ai/runtime-interface';
5
+ import ws from '@fastify/websocket';
6
+ import fastifySwaggerUI from '@fastify/swagger-ui';
7
+ import fastifySwagger from '@fastify/swagger';
8
+ import cors from '@fastify/cors';
9
+
10
+ import { ServerConfig } from '@config/index';
11
+ import { appRouter } from '@api/index';
12
+ import { serverDocument } from './docs';
13
+ import { createContext } from './context';
14
+
15
+ export async function createServer() {
16
+ const server = fastify({ logger: ServerConfig.dev });
17
+
18
+ await server.register(cors);
19
+ await server.register(ws);
20
+ await server.register(fastifyTRPCPlugin, {
21
+ prefix: '/trpc',
22
+ useWss: false,
23
+ trpcOptions: { router: appRouter, createContext },
24
+ });
25
+ await server.register(fastifyTRPCOpenApiPlugin, {
26
+ basePath: ServerConfig.basePath,
27
+ router: appRouter,
28
+ createContext,
29
+ } as any);
30
+
31
+ await server.register(fastifySwagger, {
32
+ mode: 'static',
33
+ specification: { document: serverDocument },
34
+ uiConfig: { displayOperationId: true },
35
+ exposeRoute: true,
36
+ } as any);
37
+
38
+ await server.register(fastifySwaggerUI, {
39
+ routePrefix: ServerConfig.docsPath,
40
+ uiConfig: {
41
+ docExpansion: 'full',
42
+ deepLinking: false,
43
+ },
44
+ uiHooks: {
45
+ onRequest: function (request, reply, next) {
46
+ next();
47
+ },
48
+ preHandler: function (request, reply, next) {
49
+ next();
50
+ },
51
+ },
52
+ staticCSP: true,
53
+ transformStaticCSP: (header) => header,
54
+ transformSpecification: (swaggerObject, request, reply) => swaggerObject,
55
+ transformSpecificationClone: true,
56
+ });
57
+
58
+ server.get(ServerInfoDefine.path, async (): Promise<ServerInfoOutput> => {
59
+ const serverTime = new Date();
60
+ const output: ServerInfoOutput = {
61
+ name: ServerConfig.name,
62
+ title: ServerConfig.title,
63
+ description: ServerConfig.description,
64
+ runtime: ServerConfig.runtime,
65
+ version: ServerConfig.version,
66
+ time: serverTime.toISOString(),
67
+ };
68
+ return output;
69
+ });
70
+
71
+ const stop = async () => {
72
+ await server.close();
73
+ };
74
+ const start = async () => {
75
+ try {
76
+ const address = await server.listen({ port: ServerConfig.port });
77
+ await server.ready();
78
+ server.swagger();
79
+ console.log(
80
+ `> Listen Port: ${ServerConfig.port}\n> Server Address: ${address}\n> API Docs: http://localhost:4000/docs`
81
+ );
82
+ } catch (err) {
83
+ server.log.error(err);
84
+ process.exit(1);
85
+ }
86
+ };
87
+
88
+ return { server, start, stop };
89
+ }
@@ -0,0 +1,8 @@
1
+ import { ServerInfoOutput } from '@flowgram.ai/runtime-interface';
2
+
3
+ export interface ServerParams extends Omit<ServerInfoOutput, 'time'> {
4
+ dev: boolean;
5
+ port: number;
6
+ basePath: string;
7
+ docsPath: string;
8
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "extends": "@flowgram.ai/ts-config/tsconfig.flow.path.json",
3
+ "compilerOptions": {
4
+ "target": "esnext",
5
+ "lib": [
6
+ "dom",
7
+ "dom.iterable",
8
+ "esnext"
9
+ ],
10
+ "allowJs": true,
11
+ "skipLibCheck": true,
12
+ "strict": true,
13
+ "noEmit": true,
14
+ "esModuleInterop": true,
15
+ "module": "esnext",
16
+ "moduleResolution": "bundler",
17
+ "resolveJsonModule": true,
18
+ "isolatedModules": true,
19
+ "jsx": "preserve",
20
+ "plugins": [],
21
+ "baseUrl": "src",
22
+ "paths": {
23
+ "@api/*": [
24
+ "api/*"
25
+ ],
26
+ "@application/*": [
27
+ "application/*"
28
+ ],
29
+ "@server/*": [
30
+ "server/*"
31
+ ],
32
+ "@config/*": [
33
+ "config/*"
34
+ ],
35
+ "@workflow/*": [
36
+ "workflow/*"
37
+ ]
38
+ }
39
+ },
40
+ "include": [
41
+ "**/*.ts",
42
+ "**/*.tsx",
43
+ "src/workflow/executor/condition/constant.ts"
44
+ ],
45
+ "exclude": [
46
+ "node_modules"
47
+ ]
48
+ }
@@ -0,0 +1,40 @@
1
+ import { config } from "dotenv";
2
+ import path from 'path';
3
+ import { defineConfig } from 'vitest/config';
4
+
5
+ export default defineConfig({
6
+ build: {
7
+ commonjsOptions: {
8
+ transformMixedEsModules: true,
9
+ },
10
+ },
11
+ resolve: {
12
+ alias: [
13
+ {find: "@api", replacement: path.resolve(__dirname, './src/api') },
14
+ {find: "@application", replacement: path.resolve(__dirname, './src/application') },
15
+ {find: "@server", replacement: path.resolve(__dirname, './src/server') },
16
+ {find: "@config", replacement: path.resolve(__dirname, './src/config') },
17
+ {find: "@workflow", replacement: path.resolve(__dirname, './src/workflow') },
18
+ ],
19
+ },
20
+ test: {
21
+ globals: true,
22
+ mockReset: false,
23
+ environment: 'jsdom',
24
+ testTimeout: 15000,
25
+ setupFiles: [path.resolve(__dirname, './src/workflow/__tests__/setup.ts')],
26
+ include: ['**/?(*.){test,spec}.?(c|m)[jt]s?(x)'],
27
+ exclude: [
28
+ '**/__mocks__**',
29
+ '**/node_modules/**',
30
+ '**/dist/**',
31
+ '**/lib/**', // lib 编译结果忽略掉
32
+ '**/cypress/**',
33
+ '**/.{idea,git,cache,output,temp}/**',
34
+ '**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*',
35
+ ],
36
+ env: {
37
+ ...config({ path: path.resolve(__dirname, './.env/.env.test') }).parsed
38
+ }
39
+ },
40
+ });