feat: export streams API and add SSE/WS example endpoints
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+37
-1
No files matched your search
+30
-1
@@ -1,4 +1,4 @@
|
|||||||
import { createApp, Provider, Step, Route, Get, Module, Transformer, JsonTransformer, Unauthorized, Plugin, needs, param } from '../src/index';
|
import { createApp, Provider, Step, Route, Get, Module, Transformer, JsonTransformer, Unauthorized, Plugin, needs, param, Sse, Ws, inbound, channel } from '../src/index';
|
||||||
|
|
||||||
interface User { id: string; name: string }
|
interface User { id: string; name: string }
|
||||||
|
|
||||||
@@ -37,7 +37,36 @@ const logger: Plugin = (api) => {
|
|||||||
|
|
||||||
export const app = createApp({ modules: [ApiModule], plugins: [logger] });
|
export const app = createApp({ modules: [ApiModule], plugins: [logger] });
|
||||||
|
|
||||||
|
@Route('/feed')
|
||||||
|
class FeedController {
|
||||||
|
@Sse('/ticks')
|
||||||
|
ticks() {
|
||||||
|
return (async function* () {
|
||||||
|
for (let n = 1; n <= 3; n++) yield { tick: n };
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route('/chat')
|
||||||
|
class ChatController {
|
||||||
|
@Ws('/echo')
|
||||||
|
echo(@inbound() incoming: AsyncIterable<string>) {
|
||||||
|
const out = channel<string>();
|
||||||
|
(async () => {
|
||||||
|
for await (const msg of incoming) out.push(`echo: ${msg}`);
|
||||||
|
out.close();
|
||||||
|
})();
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Module({ mountpoint: '/', controllers: [FeedController, ChatController] })
|
||||||
|
class StreamModule {}
|
||||||
|
|
||||||
|
export const streamApp = createApp({ modules: [StreamModule] });
|
||||||
|
|
||||||
if (require.main === module) {
|
if (require.main === module) {
|
||||||
console.log('chain for /api/users/:id ->', app.inspect('/api/users/:id'));
|
console.log('chain for /api/users/:id ->', app.inspect('/api/users/:id'));
|
||||||
app.listen(3000).then(() => console.log('green-tea on http://localhost:3000'));
|
app.listen(3000).then(() => console.log('green-tea on http://localhost:3000'));
|
||||||
|
streamApp.listen(3001).then(() => console.log('green-tea streams on http://localhost:3001'));
|
||||||
}
|
}
|
||||||
@@ -12,3 +12,10 @@ export { flow, Flow } from './flow';
|
|||||||
export type { StepFn, CompiledFlow } from './flow';
|
export type { StepFn, CompiledFlow } from './flow';
|
||||||
export { needs, ctx, param, query, body, headers } from './params';
|
export { needs, ctx, param, query, body, headers } from './params';
|
||||||
export type { Query, Headers, Context, ArgSpec } from './params';
|
export type { Query, Headers, Context, ArgSpec } from './params';
|
||||||
|
export { channel, isAsyncIterable } from './channel';
|
||||||
|
export type { Channel } from './channel';
|
||||||
|
export { Sse, Ws, Stream, Post, Put, Patch, Delete } from './metadata';
|
||||||
|
export type { Transport, HttpMethod } from './metadata';
|
||||||
|
export { inbound, abort } from './params';
|
||||||
|
export { sseEncoder, ndjsonEncoder } from './encoders';
|
||||||
|
export type { StreamEncoder } from './encoders';
|
||||||
Reference in new issue
Block a user