| @@ -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") {} |