|
| 1 | +import { FetchHttpClient, HttpClient } from '@effect/platform'; |
| 2 | +import * as BunContext from '@effect/platform-bun/BunContext'; |
| 3 | +import * as BunRuntime from '@effect/platform-bun/BunRuntime'; |
| 4 | +import { Effect, Layer } from 'effect'; |
| 5 | +import * as S from 'effect/Schema'; |
| 6 | +import { createInngest } from 'ff-effect/for/inngest'; |
| 7 | +import { basicHandler, createFetchHandler, Logger } from 'ff-serv'; |
| 8 | +import * as InngestSdk from 'inngest'; |
| 9 | + |
| 10 | +/** |
| 11 | + * # How to use |
| 12 | + * 1. Start inngest cli `npx --ignore-scripts=false inngest-cli@latest dev` |
| 13 | + * 2. Run this |
| 14 | + **/ |
| 15 | + |
| 16 | +class Nothing extends Effect.Service<Nothing>()('nothing', { |
| 17 | + effect: Effect.gen(function* () { |
| 18 | + return { |
| 19 | + say: (message: string) => Logger.info(message), |
| 20 | + }; |
| 21 | + }), |
| 22 | +}) {} |
| 23 | + |
| 24 | +const Inngest = createInngest( |
| 25 | + Effect.succeed( |
| 26 | + new InngestSdk.Inngest({ |
| 27 | + id: 'dev', |
| 28 | + schemas: new InngestSdk.EventSchemas().fromSchema({ |
| 29 | + 'say-hello': S.standardSchemaV1( |
| 30 | + S.Struct({ |
| 31 | + message: S.String, |
| 32 | + }), |
| 33 | + ), |
| 34 | + }), |
| 35 | + }), |
| 36 | + ), |
| 37 | +); |
| 38 | + |
| 39 | +const program = Effect.gen(function* () { |
| 40 | + const helloWorld = yield* Inngest.createFunction( |
| 41 | + { id: 'asdf' }, |
| 42 | + { event: 'say-hello' }, |
| 43 | + ({ event, step, runId }) => |
| 44 | + Effect.gen(function* () { |
| 45 | + yield* Logger.info('Workflow starting'); |
| 46 | + |
| 47 | + yield* step.run('one', () => |
| 48 | + Effect.gen(function* () { |
| 49 | + const svc = yield* Nothing; |
| 50 | + yield* svc.say(`Hello ${event.data.message}`); |
| 51 | + }), |
| 52 | + ); |
| 53 | + }).pipe(Effect.annotateLogs({ runId })), |
| 54 | + ); |
| 55 | + |
| 56 | + const server = Bun.serve({ |
| 57 | + fetch: yield* createFetchHandler( |
| 58 | + [ |
| 59 | + basicHandler( |
| 60 | + '/api/inngest', |
| 61 | + yield* Inngest.fetchHandler({ functions: [helloWorld] }), |
| 62 | + ), |
| 63 | + basicHandler('/invoke', () => |
| 64 | + Effect.gen(function* () { |
| 65 | + yield* Inngest.send({ |
| 66 | + name: 'say-hello', |
| 67 | + data: { message: 'world' }, |
| 68 | + }); |
| 69 | + return new Response('ok'); |
| 70 | + }), |
| 71 | + ), |
| 72 | + ], |
| 73 | + { debug: false }, |
| 74 | + ), |
| 75 | + }); |
| 76 | + yield* Effect.addFinalizer(() => Effect.promise(() => server.stop())); |
| 77 | + yield* Logger.info(`Server started in port ${server.port}`); |
| 78 | + |
| 79 | + yield* HttpClient.put(`http://localhost:${server.port}/api/inngest`); |
| 80 | + yield* Logger.info('Called inngest put'); |
| 81 | + |
| 82 | + yield* Effect.never; |
| 83 | +}); |
| 84 | + |
| 85 | +BunRuntime.runMain( |
| 86 | + program.pipe( |
| 87 | + Effect.provide( |
| 88 | + Layer.mergeAll( |
| 89 | + BunContext.layer, |
| 90 | + Nothing.Default, |
| 91 | + Inngest.layer, |
| 92 | + Layer.scope, |
| 93 | + FetchHttpClient.layer, |
| 94 | + ), |
| 95 | + ), |
| 96 | + ), |
| 97 | +); |
0 commit comments