Showing the changes to packages/plugins/openapi/src/testing (1 of 25 files). Show all
Unified · Split
| @@ -0,0 +1,86 @@ | |||
| 1 | import { Context, Data, Effect, Layer, Predicate, Schema } from "effect"; | ||
| 2 | import { HttpClient, HttpServer } from "effect/unstable/http"; | ||
| 3 | |||
| 4 | export class OpenApiTestServerAddressError extends Data.TaggedError( | ||
| 5 | "OpenApiTestServerAddressError", | ||
| 6 | )<{ | ||
| 7 | readonly address: unknown; | ||
| 8 | }> {} | ||
| 9 | |||
| 10 | export class OpenApiTestServerSpecError extends Data.TaggedError("OpenApiTestServerSpecError")<{ | ||
| 11 | readonly cause: unknown; | ||
| 12 | }> {} | ||
| 13 | |||
| 14 | export interface OpenApiTestServerOptions { | ||
| 15 | readonly spec: unknown; | ||
| 16 | } | ||
| 17 | |||
| 18 | export interface OpenApiTestServerShape { | ||
| 19 | readonly baseUrl: string; | ||
| 20 | readonly specJson: string; | ||
| 21 | readonly httpClientLayer: Layer.Layer<HttpClient.HttpClient, never, never>; | ||
| 22 | } | ||
| 23 | |||
| 24 | const decodeJsonSpec = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown)); | ||
| 25 | |||
| 26 | const isJsonObject = (value: unknown): value is Readonly<Record<string, unknown>> => | ||
| 27 | typeof value === "object" && value !== null && !Array.isArray(value); | ||
| 28 | |||
| 29 | export const openApiSpecJsonWithServer = ( | ||
| 30 | spec: unknown, | ||
| 31 | baseUrl: string, | ||
| 32 | ): Effect.Effect<string, OpenApiTestServerSpecError> => | ||
| 33 | Effect.gen(function* () { | ||
| 34 | const parsed = | ||
| 35 | typeof spec === "string" | ||
| 36 | ? yield* decodeJsonSpec(spec).pipe( | ||
| 37 | Effect.mapError((cause) => new OpenApiTestServerSpecError({ cause })), | ||
| 38 | ) | ||
| 39 | : spec; | ||
| 40 | const withServer = isJsonObject(parsed) | ||
| 41 | ? { | ||
| 42 | ...parsed, | ||
| 43 | servers: [{ url: baseUrl }], | ||
| 44 | } | ||
| 45 | : parsed; | ||
| 46 | return yield* Effect.try({ | ||
| 47 | try: () => JSON.stringify(withServer), | ||
| 48 | catch: (cause) => new OpenApiTestServerSpecError({ cause }), | ||
| 49 | }); | ||
| 50 | }); | ||
| 51 | |||
| 52 | export const makeOpenApiTestServer = ( | ||
| 53 | options: OpenApiTestServerOptions, | ||
| 54 | ): Effect.Effect< | ||
| 55 | OpenApiTestServerShape, | ||
| 56 | OpenApiTestServerAddressError | OpenApiTestServerSpecError, | ||
| 57 | HttpClient.HttpClient | HttpServer.HttpServer | ||
| 58 | > => | ||
| 59 | Effect.gen(function* () { | ||
| 60 | const server = yield* HttpServer.HttpServer; | ||
| 61 | const address = server.address; | ||
| 62 | if (!Predicate.isTagged(address, "TcpAddress")) { | ||
| 63 | return yield* new OpenApiTestServerAddressError({ address }); | ||
| 64 | } | ||
| 65 | |||
| 66 | const client = yield* HttpClient.HttpClient; | ||
| 67 | const baseUrl = `http://127.0.0.1:${address.port}`; | ||
| 68 | const specJson = yield* openApiSpecJsonWithServer(options.spec, baseUrl); | ||
| 69 | |||
| 70 | return { | ||
| 71 | baseUrl, | ||
| 72 | specJson, | ||
| 73 | httpClientLayer: Layer.succeed(HttpClient.HttpClient, client), | ||
| 74 | }; | ||
| 75 | }); | ||
| 76 | |||
| 77 | export class OpenApiTestServer extends Context.Service<OpenApiTestServer, OpenApiTestServerShape>()( | ||
| 78 | "@executor-js/plugin-openapi/testing/OpenApiTestServer", | ||
| 79 | ) { | ||
| 80 | static readonly layer = (options: OpenApiTestServerOptions) => | ||
| 81 | Layer.effect(OpenApiTestServer, makeOpenApiTestServer(options)); | ||
| 82 | } | ||
| 83 | |||
| 84 | export const TestLayers = { | ||
| 85 | server: OpenApiTestServer.layer, | ||
| 86 | }; | ||