History

Replace repository contents with v4 snapshot

Rhys Sullivan committed 7 months ago

Commit 4fcdba41 parent be4bbf5865 files · +36247 −404676Browse files

Showing the changes to packages/plugins/openapi/src/api (3 of 865 files). Show all

Unified · Split

3 files
  1. packages/plugins/openapi/src/api/group.ts +73 −0
  2. packages/plugins/openapi/src/api/handlers.ts +53 −0
  3. packages/plugins/openapi/src/api/index.ts +2 −0
packages/plugins/openapi/src/api/group.ts+73−0
@@ -0,0 +1,73 @@
1import { HttpApiEndpoint, HttpApiGroup, HttpApiSchema } from "@effect/platform";
2import { Schema } from "effect";
3import { ScopeId } from "@executor/sdk";
4
5import {
6 OpenApiParseError,
7 OpenApiExtractionError,
8} from "../sdk/errors";
9import { SpecPreview } from "../sdk/preview";
10
11// ---------------------------------------------------------------------------
12// Params
13// ---------------------------------------------------------------------------
14
15const scopeIdParam = HttpApiSchema.param("scopeId", ScopeId);
16
17// ---------------------------------------------------------------------------
18// Payloads
19// ---------------------------------------------------------------------------
20
21const AddSpecPayload = Schema.Struct({
22 spec: Schema.String,
23 baseUrl: Schema.optional(Schema.String),
24 namespace: Schema.optional(Schema.String),
25 headers: Schema.optional(
26 Schema.Record({ key: Schema.String, value: Schema.Unknown }),
27 ),
28});
29
30const PreviewSpecPayload = Schema.Struct({
31 spec: Schema.String,
32});
33
34// ---------------------------------------------------------------------------
35// Responses
36// ---------------------------------------------------------------------------
37
38const AddSpecResponse = Schema.Struct({
39 toolCount: Schema.Number,
40 namespace: Schema.String,
41});
42
43// ---------------------------------------------------------------------------
44// Errors with HTTP status
45// ---------------------------------------------------------------------------
46
47const ParseError = OpenApiParseError.annotations(
48 HttpApiSchema.annotations({ status: 400 }),
49);
50const ExtractionError = OpenApiExtractionError.annotations(
51 HttpApiSchema.annotations({ status: 400 }),
52);
53
54// ---------------------------------------------------------------------------
55// Group
56// ---------------------------------------------------------------------------
57
58export class OpenApiGroup extends HttpApiGroup.make("openapi")
59 .add(
60 HttpApiEndpoint.post("previewSpec")`/scopes/${scopeIdParam}/openapi/preview`
61 .setPayload(PreviewSpecPayload)
62 .addSuccess(SpecPreview)
63 .addError(ParseError)
64 .addError(ExtractionError),
65 )
66 .add(
67 HttpApiEndpoint.post("addSpec")`/scopes/${scopeIdParam}/openapi/specs`
68 .setPayload(AddSpecPayload)
69 .addSuccess(AddSpecResponse)
70 .addError(ParseError)
71 .addError(ExtractionError),
72 )
73 .prefix("/v1") {}
packages/plugins/openapi/src/api/handlers.ts+53−0
@@ -0,0 +1,53 @@
1import { HttpApiBuilder } from "@effect/platform";
2import { Context, Effect } from "effect";
3
4import { addGroup } from "@executor/api";
5import type { OpenApiPluginExtension, HeaderValue } from "../sdk/plugin";
6import { OpenApiGroup } from "./group";
7
8// ---------------------------------------------------------------------------
9// Service tag — the server provides the OpenAPI extension
10// ---------------------------------------------------------------------------
11
12export class OpenApiExtensionService extends Context.Tag(
13 "OpenApiExtensionService",
14)<OpenApiExtensionService, OpenApiPluginExtension>() {}
15
16// ---------------------------------------------------------------------------
17// Composed API — core + openapi group
18// ---------------------------------------------------------------------------
19
20const ExecutorApiWithOpenApi = addGroup(OpenApiGroup);
21
22// ---------------------------------------------------------------------------
23// Handlers
24// ---------------------------------------------------------------------------
25
26export const OpenApiHandlers = HttpApiBuilder.group(
27 ExecutorApiWithOpenApi,
28 "openapi",
29 (handlers) =>
30 handlers
31 .handle("previewSpec", ({ payload }) =>
32 Effect.gen(function* () {
33 const ext = yield* OpenApiExtensionService;
34 return yield* ext.previewSpec(payload.spec);
35 }).pipe(Effect.orDie),
36 )
37 .handle("addSpec", ({ payload }) =>
38 Effect.gen(function* () {
39 const ext = yield* OpenApiExtensionService;
40 const result = yield* ext.addSpec({
41 spec: payload.spec,
42 baseUrl: payload.baseUrl,
43 namespace: payload.namespace,
44 headers: payload.headers as Record<string, HeaderValue> | undefined,
45 });
46 return {
47 toolCount: result.toolCount,
48 namespace: payload.namespace ?? "api",
49 };
50 }).pipe(Effect.orDie),
51 )
52 ,
53);
packages/plugins/openapi/src/api/index.ts+2−0
@@ -0,0 +1,2 @@
1export { OpenApiGroup } from "./group";
2export { OpenApiHandlers, OpenApiExtensionService } from "./handlers";