diff options
Diffstat (limited to 'packages/server/src')
| -rw-r--r-- | packages/server/src/api/index.ts | 3 | ||||
| -rw-r--r-- | packages/server/src/api/question.ts | 37 | ||||
| -rw-r--r-- | packages/server/src/index.ts | 2 |
3 files changed, 41 insertions, 1 deletions
diff --git a/packages/server/src/api/index.ts b/packages/server/src/api/index.ts index 336ce12bb..375e3584b 100644 --- a/packages/server/src/api/index.ts +++ b/packages/server/src/api/index.ts @@ -1 +1,2 @@ -export {} +export { makeQuestionHandler } from "./question.js" +export type { QuestionOps } from "./question.js" diff --git a/packages/server/src/api/question.ts b/packages/server/src/api/question.ts new file mode 100644 index 000000000..f72c37aa1 --- /dev/null +++ b/packages/server/src/api/question.ts @@ -0,0 +1,37 @@ +import { Effect, Schema } from "effect" +import { HttpApiBuilder } from "effect/unstable/httpapi" +import { QuestionReply, QuestionRequest, questionApi } from "../definition/question.js" + +export interface QuestionOps<R = never> { + readonly list: () => Effect.Effect<ReadonlyArray<unknown>, never, R> + readonly reply: (input: { + requestID: string + answers: Schema.Schema.Type<typeof QuestionReply>["answers"] + }) => Effect.Effect<void, never, R> +} + +export const makeQuestionHandler = <R>(ops: QuestionOps<R>) => + HttpApiBuilder.group( + questionApi, + "question", + Effect.fn("QuestionHttpApi.handlers")(function* (handlers) { + const decode = Schema.decodeUnknownSync(Schema.Array(QuestionRequest)) + + const list = Effect.fn("QuestionHttpApi.list")(function* () { + return decode(yield* ops.list()) + }) + + const reply = Effect.fn("QuestionHttpApi.reply")(function* (ctx: { + params: { requestID: string } + payload: Schema.Schema.Type<typeof QuestionReply> + }) { + yield* ops.reply({ + requestID: ctx.params.requestID, + answers: ctx.payload.answers, + }) + return true + }) + + return handlers.handle("list", list).handle("reply", reply) + }), + ) diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index d5bdb6c8d..67b82a0be 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -1,4 +1,6 @@ export { openapi } from "./openapi.js" +export { makeQuestionHandler } from "./api/question.js" export { api } from "./definition/api.js" export { questionApi, QuestionReply, QuestionRequest } from "./definition/question.js" export type { OpenApiSpec, ServerApi } from "./types.js" +export type { QuestionOps } from "./api/question.js" |
