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 @@
1+import { HttpApiEndpoint, HttpApiGroup, HttpApiSchema } from "@effect/platform";
2+import { Schema } from "effect";
3+import { ScopeId } from "@executor/sdk";
4+
5+import {
6+ OpenApiParseError,
7+ OpenApiExtractionError,
8+} from "../sdk/errors";
9+import { SpecPreview } from "../sdk/preview";
10+
11+// ---------------------------------------------------------------------------
12+// Params
13+// ---------------------------------------------------------------------------
14+
15+const scopeIdParam = HttpApiSchema.param("scopeId", ScopeId);
16+
17+// ---------------------------------------------------------------------------
18+// Payloads
19+// ---------------------------------------------------------------------------
20+
21+const 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+
30+const PreviewSpecPayload = Schema.Struct({
31+ spec: Schema.String,
32+});
33+
34+// ---------------------------------------------------------------------------
35+// Responses
36+// ---------------------------------------------------------------------------
37+
38+const AddSpecResponse = Schema.Struct({
39+ toolCount: Schema.Number,
40+ namespace: Schema.String,
41+});
42+
43+// ---------------------------------------------------------------------------
44+// Errors with HTTP status
45+// ---------------------------------------------------------------------------
46+
47+const ParseError = OpenApiParseError.annotations(
48+ HttpApiSchema.annotations({ status: 400 }),
49+);
50+const ExtractionError = OpenApiExtractionError.annotations(
51+ HttpApiSchema.annotations({ status: 400 }),
52+);
53+
54+// ---------------------------------------------------------------------------
55+// Group
56+// ---------------------------------------------------------------------------
57+
58+export 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 @@
1+import { HttpApiBuilder } from "@effect/platform";
2+import { Context, Effect } from "effect";
3+
4+import { addGroup } from "@executor/api";
5+import type { OpenApiPluginExtension, HeaderValue } from "../sdk/plugin";
6+import { OpenApiGroup } from "./group";
7+
8+// ---------------------------------------------------------------------------
9+// Service tag — the server provides the OpenAPI extension
10+// ---------------------------------------------------------------------------
11+
12+export class OpenApiExtensionService extends Context.Tag(
13+ "OpenApiExtensionService",
14+)<OpenApiExtensionService, OpenApiPluginExtension>() {}
15+
16+// ---------------------------------------------------------------------------
17+// Composed API — core + openapi group
18+// ---------------------------------------------------------------------------
19+
20+const ExecutorApiWithOpenApi = addGroup(OpenApiGroup);
21+
22+// ---------------------------------------------------------------------------
23+// Handlers
24+// ---------------------------------------------------------------------------
25+
26+export 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 @@
1+export { OpenApiGroup } from "./group";
2+export { OpenApiHandlers, OpenApiExtensionService } from "./handlers";