-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rotorsoft
committed
Dec 9, 2023
1 parent
ae6c665
commit 160968d
Showing
18 changed files
with
1,143 additions
and
648 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "@rotorsoft/eventually-express", | ||
"version": "5.7.11", | ||
"description": "Eventually Framework Express Services", | ||
"description": "Eventually ExpressJs Application Adapter", | ||
"author": { | ||
"name": "rotorsoft", | ||
"email": "[email protected]" | ||
|
@@ -27,6 +27,6 @@ | |
"express": "^4.18.2" | ||
}, | ||
"devDependencies": { | ||
"@types/express": "^4.17.20" | ||
"@types/express": "^4.17.21" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "@rotorsoft/eventually-pg", | ||
"version": "5.5.19", | ||
"description": "Eventually Framework Postgres Services", | ||
"description": "Eventually Postgres Store Adapters", | ||
"author": { | ||
"name": "rotorsoft", | ||
"email": "[email protected]" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# [eventually-trpc](https://rotorsoft.github.io/eventually-monorepo/modules/eventually_trpc.html) | ||
|
||
[![NPM Version](https://img.shields.io/npm/v/@rotorsoft/eventually-trpc.svg)](https://www.npmjs.com/package/@rotorsoft/eventually-trpc) | ||
|
||
[Eventually](../../README.md) library implementing a [tRPC](https://trpc.io/) based application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "@rotorsoft/eventually-trpc", | ||
"version": "5.7.11", | ||
"description": "Eventually tRPC Application Adapter", | ||
"author": { | ||
"name": "rotorsoft", | ||
"email": "[email protected]" | ||
}, | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/rotorsoft/eventually-monorepo.git", | ||
"directory": "libs/eventually-trpc" | ||
}, | ||
"main": "dist/index", | ||
"types": "dist/index", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "npx tsc --build" | ||
}, | ||
"dependencies": { | ||
"@rotorsoft/eventually": "workspace:^5", | ||
"@rotorsoft/eventually-openapi": "workspace:^0", | ||
"@trpc/server": "^10.44.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import { | ||
Builder, | ||
broker, | ||
log, | ||
type AggregateFactory | ||
// type CommandAdapterFactory, | ||
// type EventHandlerFactory, | ||
// type ProjectorFactory, | ||
// CommandHandlerFactory | ||
} from "@rotorsoft/eventually"; | ||
import { httpGetPath, httpPostPath } from "@rotorsoft/eventually-openapi"; | ||
import { ProcedureRouterRecord, initTRPC } from "@trpc/server"; | ||
import { createHTTPServer } from "@trpc/server/adapters/standalone"; | ||
import { Server } from "http"; | ||
import { config } from "./config"; | ||
|
||
const trpc = initTRPC.create(); | ||
|
||
/** | ||
* tRPC app builder | ||
* | ||
* @remarks Exposes public interface as tRPC endpoints | ||
*/ | ||
export class TrpcApp extends Builder { | ||
private _procedures: ProcedureRouterRecord = {}; | ||
private _server?: Server; | ||
|
||
constructor() { | ||
super(); | ||
} | ||
|
||
private _withStreams(): void { | ||
// this._router.get("/all", queryHandler); | ||
log() | ||
.green() | ||
.info( | ||
"GET ", | ||
"/all?[stream=...][&names=...][&after=-1][&limit=1][&before=...][&created_after=...][&created_before=...]" | ||
); | ||
// this._router.get("/_stats", statsHandler); | ||
log().green().info("GET ", "/_stats"); | ||
// this._router.get("/_subscriptions", subscriptionsHandler); | ||
log().green().info("GET ", "/_subscriptions"); | ||
} | ||
|
||
private _withGets(factory: AggregateFactory): void { | ||
const path = httpGetPath(factory.name); | ||
// this._router.get(path, getHandler(factory)); | ||
log().green().info("GET ", path); | ||
const streamPath = path.concat("/stream"); | ||
// this._router.get(streamPath, getStreamHandler(factory)); | ||
log().green().info("GET ", streamPath); | ||
} | ||
|
||
private _withPosts(): void { | ||
this.artifacts.forEach(({ type, factory, inputs }) => { | ||
const endpoints = inputs | ||
.filter((input) => input.scope === "public") | ||
.map((input) => input.name); | ||
type === "aggregate" && this._withGets(factory as AggregateFactory); | ||
if (type === "policy" || type === "process-manager") { | ||
if (endpoints.length) { | ||
const path = httpPostPath(factory.name, type); | ||
//this._router.post(path, eventHandler(factory as EventHandlerFactory)); | ||
log().magenta().info("POST", path, endpoints); | ||
} | ||
} else if (type === "projector") { | ||
//const projector_factory = factory as ProjectorFactory; | ||
//const projector = projector_factory(); | ||
const path = httpPostPath(factory.name, type); | ||
if (endpoints.length) { | ||
// projectors expose a route to handle an array of events | ||
//this._router.post(path, projectHandler(projector_factory)); | ||
log().magenta().info("POST", path, inputs); | ||
} | ||
//this._router.get( | ||
// path, | ||
// readHandler(projector_factory, projector.schemas.state) | ||
//); | ||
log().green().info("GET ", path); | ||
} else | ||
endpoints.forEach((name) => { | ||
const path = httpPostPath(factory.name, type, name); | ||
// if (type === "command-adapter") | ||
// this._router.post( | ||
// path, | ||
// invokeHandler(factory as CommandAdapterFactory) | ||
// ); | ||
// else | ||
// this._router.post( | ||
// path, | ||
// commandHandler( | ||
// factory as CommandHandlerFactory, | ||
// name, | ||
// type === "aggregate" | ||
// ) | ||
// ); | ||
log().blue().info("POST", path); | ||
}); | ||
}); | ||
} | ||
|
||
//TODO: pass options | ||
build(): void { | ||
super.build(); | ||
|
||
// route artifacts | ||
this._withPosts(); | ||
this.hasStreams && this._withStreams(); | ||
|
||
// add middleware | ||
|
||
// use artifact routes | ||
//this._app.use(this._router); | ||
|
||
// ensure catch-all is last handler | ||
//this._app.use(errorHandler); | ||
|
||
// log sanitized config | ||
const { service, version, env, logLevel } = config; | ||
log().info("config", service, { env, logLevel, version }); | ||
} | ||
|
||
/** | ||
* Starts listening for requests | ||
* | ||
* WARNING! | ||
* - Serverless environments provide their own listening framework | ||
* - Use wrappers like serverless-http instead | ||
* | ||
* @param port to override port in config | ||
*/ | ||
async listen(port?: number): Promise<void> { | ||
port = port || config.port; | ||
this._server = await new Promise((resolve, reject) => { | ||
try { | ||
const server = createHTTPServer({ | ||
router: trpc.router(this._procedures) | ||
}); | ||
server.listen(port); | ||
log() | ||
.yellow() | ||
.underlined() | ||
.info(`tRPC server is listening on port ${port}`); | ||
|
||
// sync pending subscriptions | ||
void broker().drain(); | ||
|
||
resolve(server.server); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
} | ||
|
||
get name(): string { | ||
return "TrcpApp"; | ||
} | ||
|
||
async dispose(): Promise<void> { | ||
await super.dispose(); | ||
if (this._server) { | ||
await new Promise((resolve, reject) => { | ||
this._server && this._server.once("close", resolve); | ||
this._server && this._server.close(reject); | ||
}); | ||
this._server = undefined; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { extend, config as target } from "@rotorsoft/eventually"; | ||
import * as fs from "node:fs"; | ||
import { z } from "zod"; | ||
|
||
type Package = { | ||
name: string; | ||
version: string; | ||
description: string; | ||
author: { | ||
name: string; | ||
email: string; | ||
}; | ||
license: string; | ||
dependencies: Record<string, string>; | ||
}; | ||
|
||
const getPackage = (): Package => { | ||
const pkg = fs.readFileSync("package.json"); | ||
return JSON.parse(pkg.toString()) as Package; | ||
}; | ||
|
||
/** | ||
* Configuration zod schema | ||
*/ | ||
const Schema = z.object({ | ||
service: z.string().min(1), | ||
version: z.string().min(1), | ||
description: z.string().min(1), | ||
author: z.object({ name: z.string().min(1), email: z.string() }), | ||
license: z.string().min(1), | ||
dependencies: z.record(z.string()), | ||
port: z.number().int().min(1000).max(65535) | ||
}); | ||
export type Config = z.infer<typeof Schema>; | ||
|
||
const { PORT } = process.env; | ||
const pkg = getPackage(); | ||
const parts = pkg.name.split("/"); | ||
const service = parts.at(-1) || ""; | ||
|
||
export const config = extend( | ||
{ | ||
service, | ||
version: pkg.version, | ||
description: pkg.description, | ||
author: { name: pkg.author?.name, email: pkg.author?.email }, | ||
license: pkg.license, | ||
dependencies: pkg.dependencies, | ||
port: parseInt(PORT || "3000") || 3000 | ||
}, | ||
Schema, | ||
target() | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/** @module eventually-trpc */ | ||
export * from "./TrpcApp"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "./src", | ||
"outDir": "./dist", | ||
"tsBuildInfoFile": "./dist/.tsbuildinfo", | ||
"composite": true | ||
}, | ||
|
||
"references": [ | ||
{ "path": "../eventually" }, | ||
{ "path": "../eventually-openapi" } | ||
] | ||
} |
Oops, something went wrong.