@rafads/plugin-openapi 1.5.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/README.md +83 -0
  2. package/dist/AddOpenApiIntegration-JPAEXE56.js +512 -0
  3. package/dist/AddOpenApiIntegration-JPAEXE56.js.map +1 -0
  4. package/dist/OpenApiAccountsPanel-GZFDSTAU.js +106 -0
  5. package/dist/OpenApiAccountsPanel-GZFDSTAU.js.map +1 -0
  6. package/dist/UpdateSpecSection-KUSNI44Q.js +106 -0
  7. package/dist/UpdateSpecSection-KUSNI44Q.js.map +1 -0
  8. package/dist/api/group.d.ts +239 -0
  9. package/dist/api/handlers.d.ts +7 -0
  10. package/dist/api/index.d.ts +237 -0
  11. package/dist/chunk-6V45G2XQ.js +199 -0
  12. package/dist/chunk-6V45G2XQ.js.map +1 -0
  13. package/dist/chunk-CURJJXB3.js +132 -0
  14. package/dist/chunk-CURJJXB3.js.map +1 -0
  15. package/dist/chunk-CXFO6TLJ.js +39 -0
  16. package/dist/chunk-CXFO6TLJ.js.map +1 -0
  17. package/dist/chunk-HTHEUUJA.js +2873 -0
  18. package/dist/chunk-HTHEUUJA.js.map +1 -0
  19. package/dist/chunk-IG6QTTZB.js +79 -0
  20. package/dist/chunk-IG6QTTZB.js.map +1 -0
  21. package/dist/chunk-O2LMSZ2Z.js +120 -0
  22. package/dist/chunk-O2LMSZ2Z.js.map +1 -0
  23. package/dist/chunk-OR27BCXQ.js +956 -0
  24. package/dist/chunk-OR27BCXQ.js.map +1 -0
  25. package/dist/chunk-R7TVM3XD.js +275 -0
  26. package/dist/chunk-R7TVM3XD.js.map +1 -0
  27. package/dist/client.js +38 -0
  28. package/dist/client.js.map +1 -0
  29. package/dist/core.js +331 -0
  30. package/dist/core.js.map +1 -0
  31. package/dist/index.js +19 -0
  32. package/dist/index.js.map +1 -0
  33. package/dist/providers/google/config.d.ts +34 -0
  34. package/dist/providers/google/discovery.d.ts +49 -0
  35. package/dist/providers/google/discovery.test.d.ts +1 -0
  36. package/dist/providers/google/index.d.ts +5 -0
  37. package/dist/providers/google/oauth-scopes.d.ts +3 -0
  38. package/dist/providers/google/oauth-scopes.test.d.ts +1 -0
  39. package/dist/providers/google/openapi-ownership-migration.d.ts +7 -0
  40. package/dist/providers/google/openapi-ownership-migration.test.d.ts +1 -0
  41. package/dist/providers/google/presets.d.ts +31 -0
  42. package/dist/providers/google/presets.test.d.ts +1 -0
  43. package/dist/providers/google/spec-format-adapter.d.ts +3 -0
  44. package/dist/providers/google/spec-format-adapter.test.d.ts +1 -0
  45. package/dist/providers/google.js +1446 -0
  46. package/dist/providers/google.js.map +1 -0
  47. package/dist/providers/microsoft/graph.d.ts +76 -0
  48. package/dist/providers/microsoft/graph.test.d.ts +1 -0
  49. package/dist/providers/microsoft/index.d.ts +3 -0
  50. package/dist/providers/microsoft/presets.d.ts +46 -0
  51. package/dist/providers/microsoft/presets.test.d.ts +1 -0
  52. package/dist/providers/microsoft/spec-format-adapter.d.ts +2 -0
  53. package/dist/providers/microsoft/spec-format-adapter.test.d.ts +1 -0
  54. package/dist/providers/microsoft.js +1056 -0
  55. package/dist/providers/microsoft.js.map +1 -0
  56. package/dist/react/AddOpenApiIntegration.d.ts +22 -0
  57. package/dist/react/AddOpenApiIntegration.test.d.ts +1 -0
  58. package/dist/react/OpenApiAccountsPanel.d.ts +6 -0
  59. package/dist/react/OpenApiIntegrationDetailsFields.d.ts +25 -0
  60. package/dist/react/UpdateSpecSection.d.ts +2 -0
  61. package/dist/react/atoms.d.ts +317 -0
  62. package/dist/react/auth-method-config.d.ts +18 -0
  63. package/dist/react/auth-method-config.test.d.ts +1 -0
  64. package/dist/react/client.d.ts +233 -0
  65. package/dist/react/index.d.ts +5 -0
  66. package/dist/react/integration-plugin.d.ts +6 -0
  67. package/dist/react/plugin-client.d.ts +2 -0
  68. package/dist/sdk/backing.d.ts +188 -0
  69. package/dist/sdk/client-credentials-oauth.test.d.ts +1 -0
  70. package/dist/sdk/compiled-spec-cache.test.d.ts +1 -0
  71. package/dist/sdk/config.d.ts +81 -0
  72. package/dist/sdk/configure.test.d.ts +1 -0
  73. package/dist/sdk/definitions.d.ts +57 -0
  74. package/dist/sdk/derive-auth.d.ts +11 -0
  75. package/dist/sdk/describe-auth-methods.test.d.ts +1 -0
  76. package/dist/sdk/errors.d.ts +45 -0
  77. package/dist/sdk/extract.d.ts +157 -0
  78. package/dist/sdk/extract.test.d.ts +1 -0
  79. package/dist/sdk/form-urlencoded-body.test.d.ts +1 -0
  80. package/dist/sdk/index.d.ts +19 -0
  81. package/dist/sdk/index.test.d.ts +1 -0
  82. package/dist/sdk/invoke.d.ts +150 -0
  83. package/dist/sdk/migrate-config.d.ts +4 -0
  84. package/dist/sdk/migrate-config.test.d.ts +1 -0
  85. package/dist/sdk/multi-scope-bearer.test.d.ts +1 -0
  86. package/dist/sdk/multi-scope-oauth.test.d.ts +1 -0
  87. package/dist/sdk/ndjson-output-migration.d.ts +13 -0
  88. package/dist/sdk/ndjson-output-migration.test.d.ts +1 -0
  89. package/dist/sdk/non-json-body.test.d.ts +1 -0
  90. package/dist/sdk/oauth-refresh.test.d.ts +1 -0
  91. package/dist/sdk/openapi-utils.d.ts +65 -0
  92. package/dist/sdk/output-schema-migration.d.ts +21 -0
  93. package/dist/sdk/output-schema-migration.test.d.ts +1 -0
  94. package/dist/sdk/parse.d.ts +33 -0
  95. package/dist/sdk/parse.test.d.ts +1 -0
  96. package/dist/sdk/plugin.d.ts +109 -0
  97. package/dist/sdk/plugin.test.d.ts +1 -0
  98. package/dist/sdk/presets.d.ts +15 -0
  99. package/dist/sdk/preview-oauth2.test.d.ts +1 -0
  100. package/dist/sdk/preview.d.ts +514 -0
  101. package/dist/sdk/query-serialization.test.d.ts +1 -0
  102. package/dist/sdk/real-specs.test.d.ts +1 -0
  103. package/dist/sdk/request-user-agent.test.d.ts +1 -0
  104. package/dist/sdk/response-headers-timeout.test.d.ts +1 -0
  105. package/dist/sdk/server-url-resolution.test.d.ts +1 -0
  106. package/dist/sdk/spec-blob-migration.d.ts +7 -0
  107. package/dist/sdk/spec-blob.test.d.ts +1 -0
  108. package/dist/sdk/spec-format.d.ts +36 -0
  109. package/dist/sdk/spec-format.test.d.ts +1 -0
  110. package/dist/sdk/split.d.ts +84 -0
  111. package/dist/sdk/store.d.ts +49 -0
  112. package/dist/sdk/store.test.d.ts +1 -0
  113. package/dist/sdk/streaming-response.test.d.ts +1 -0
  114. package/dist/sdk/tool-row-projection.test.d.ts +1 -0
  115. package/dist/sdk/types.d.ts +352 -0
  116. package/dist/sdk/upstream-failures.test.d.ts +1 -0
  117. package/dist/sdk/usage-scope-isolation.test.d.ts +1 -0
  118. package/dist/testing/index.d.ts +177 -0
  119. package/dist/testing.js +454 -0
  120. package/dist/testing.js.map +1 -0
  121. package/dist/testing.test.d.ts +1 -0
  122. package/package.json +103 -0
@@ -0,0 +1,177 @@
1
+ import { Context, Effect, Layer, Scope } from "effect";
2
+ import { HttpClient, HttpServerRequest, HttpServerResponse } from "effect/unstable/http";
3
+ import { HttpApi } from "effect/unstable/httpapi";
4
+ import { OAuthTestServer } from "@rafads/sdk/testing";
5
+ import { IntegrationSlug, type Connection, type CreateConnectionInput, type Owner, type ToolAddress as ToolAddressType } from "@rafads/sdk/core";
6
+ import type { OpenApiPluginExtension, OpenApiSpecConfig } from "../sdk/plugin";
7
+ declare const OpenApiTestServerAddressError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
8
+ readonly _tag: "OpenApiTestServerAddressError";
9
+ } & Readonly<A>;
10
+ export declare class OpenApiTestServerAddressError extends OpenApiTestServerAddressError_base<{
11
+ readonly address: unknown;
12
+ }> {
13
+ }
14
+ declare const OpenApiTestServerSpecError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
15
+ readonly _tag: "OpenApiTestServerSpecError";
16
+ } & Readonly<A>;
17
+ export declare class OpenApiTestServerSpecError extends OpenApiTestServerSpecError_base<{
18
+ readonly cause: unknown;
19
+ }> {
20
+ }
21
+ export interface OpenApiTestServerShape {
22
+ readonly baseUrl: string;
23
+ readonly specJson: string;
24
+ readonly httpClientLayer: Layer.Layer<HttpClient.HttpClient, never, never>;
25
+ }
26
+ export interface OpenApiHttpApiTestServerOptions {
27
+ readonly api: HttpApi.Any;
28
+ readonly handlersLayer: Layer.Layer<any, any, any>;
29
+ readonly specPath?: `/${string}`;
30
+ readonly transformSpec?: (spec: Record<string, unknown>) => Record<string, unknown>;
31
+ readonly captureSpecRequest?: (request: HttpServerRequest.HttpServerRequest) => Effect.Effect<void>;
32
+ readonly guardSpecRequest?: (request: HttpServerRequest.HttpServerRequest) => Effect.Effect<HttpServerResponse.HttpServerResponse | null>;
33
+ }
34
+ export interface OpenApiHttpApiTestServerShape extends OpenApiTestServerShape {
35
+ readonly specUrl: string;
36
+ }
37
+ export interface MutableOpenApiSpecTestServerShape extends OpenApiTestServerShape {
38
+ readonly specUrl: string;
39
+ readonly setApi: (api: HttpApi.Any) => Effect.Effect<void, OpenApiTestServerSpecError>;
40
+ readonly requestCount: Effect.Effect<number>;
41
+ }
42
+ export interface OpenApiTestRequest {
43
+ readonly method: string;
44
+ readonly url: string;
45
+ readonly path: string;
46
+ readonly headers: Readonly<Record<string, string>>;
47
+ readonly body: string;
48
+ }
49
+ export interface OpenApiEchoTestServerOptions {
50
+ readonly transformSpec?: (spec: Record<string, unknown>) => Record<string, unknown>;
51
+ readonly oauth2?: {
52
+ readonly authorizationUrl: string;
53
+ readonly tokenUrl: string;
54
+ readonly scopes?: Readonly<Record<string, string>>;
55
+ readonly validateAuthorization?: (authorization: string | null) => Effect.Effect<boolean>;
56
+ readonly wwwAuthenticate?: string;
57
+ };
58
+ }
59
+ export interface OpenApiEchoTestServerShape extends OpenApiTestServerShape {
60
+ readonly specUrl: string;
61
+ readonly requests: Effect.Effect<readonly OpenApiTestRequest[]>;
62
+ readonly clearRequests: Effect.Effect<void>;
63
+ }
64
+ export type OpenApiTestIntegrationOptions = Omit<OpenApiSpecConfig, "spec" | "baseUrl" | "slug"> & {
65
+ readonly baseUrl?: string | null;
66
+ readonly slug?: string;
67
+ };
68
+ export type OpenApiHttpApiTestIntegrationOptions = Omit<OpenApiSpecConfig, "spec" | "slug" | "baseUrl"> & {
69
+ readonly slug?: string;
70
+ readonly baseUrl?: string | null;
71
+ readonly specBaseUrl?: string;
72
+ readonly transformSpec?: (spec: Record<string, unknown>) => Record<string, unknown>;
73
+ };
74
+ type OpenApiHttpApiAddSpecHeadersInput = {
75
+ readonly headers?: Record<string, string>;
76
+ readonly queryParams?: Record<string, string>;
77
+ };
78
+ export type OpenApiHttpApiTestAddSpecPayloadOptions = Omit<OpenApiHttpApiTestIntegrationOptions, "headers" | "queryParams"> & OpenApiHttpApiAddSpecHeadersInput;
79
+ export type OpenApiTestIntegrationExecutor = {
80
+ readonly openapi: Pick<OpenApiPluginExtension, "addSpec">;
81
+ };
82
+ export type OpenApiTestConnectionExecutor = {
83
+ readonly openapi: Pick<OpenApiPluginExtension, "addSpec">;
84
+ readonly connections: {
85
+ readonly create: (input: CreateConnectionInput) => Effect.Effect<Connection, unknown>;
86
+ };
87
+ };
88
+ export interface OpenApiTestConnectionOptions {
89
+ readonly slug?: string;
90
+ readonly owner?: Owner;
91
+ readonly connection?: string;
92
+ readonly template?: string;
93
+ /** Inline credential value. Defaults to a throwaway token via the in-memory
94
+ * provider's `from` path is avoided — pass `value` to store a real secret. */
95
+ readonly value?: string;
96
+ readonly baseUrl?: string | null;
97
+ }
98
+ export interface OpenApiTestConnection {
99
+ readonly slug: string;
100
+ readonly owner: Owner;
101
+ readonly connection: string;
102
+ readonly address: (tool: string) => ToolAddressType;
103
+ }
104
+ /** addSpec + connections.create from a raw server spec; returns an address
105
+ * builder for `executor.execute`. */
106
+ export declare const addOpenApiTestConnection: (executor: OpenApiTestConnectionExecutor, server: OpenApiTestServerShape, integrationOptions: OpenApiTestIntegrationOptions, connectionOptions?: OpenApiTestConnectionOptions) => Effect.Effect<OpenApiTestConnection, unknown>;
107
+ export interface OpenApiTestSpecOptions {
108
+ readonly baseUrl?: string;
109
+ readonly transformSpec?: (spec: Record<string, unknown>) => Record<string, unknown>;
110
+ }
111
+ export declare const makeOpenApiTestSpecJson: (api: HttpApi.Any, options?: OpenApiTestSpecOptions) => string;
112
+ export declare const makeOpenApiTestIntegrationConfig: (server: OpenApiTestServerShape, options: OpenApiTestIntegrationOptions) => OpenApiSpecConfig;
113
+ export declare const addOpenApiTestIntegration: (executor: OpenApiTestIntegrationExecutor, server: OpenApiTestServerShape, options: OpenApiTestIntegrationOptions) => Effect.Effect<{
114
+ readonly slug: IntegrationSlug;
115
+ readonly toolCount: number;
116
+ }, import("@rafads/sdk/core").StorageFailure | import("@rafads/sdk/core").IntegrationAlreadyExistsError | import("../sdk").OpenApiParseError | import("../sdk").OpenApiExtractionError | import("../sdk").OpenApiOAuthError, never>;
117
+ export declare const makeOpenApiHttpApiTestIntegrationConfig: (api: HttpApi.Any, options: OpenApiHttpApiTestIntegrationOptions) => OpenApiSpecConfig;
118
+ export declare const addOpenApiHttpApiTestIntegration: (executor: OpenApiTestIntegrationExecutor, api: HttpApi.Any, options: OpenApiHttpApiTestIntegrationOptions) => Effect.Effect<{
119
+ readonly slug: IntegrationSlug;
120
+ readonly toolCount: number;
121
+ }, import("@rafads/sdk/core").StorageFailure | import("@rafads/sdk/core").IntegrationAlreadyExistsError | import("../sdk").OpenApiParseError | import("../sdk").OpenApiExtractionError | import("../sdk").OpenApiOAuthError, never>;
122
+ export declare const makeOpenApiHttpApiTestAddSpecPayload: (api: HttpApi.Any, options: OpenApiHttpApiTestAddSpecPayloadOptions) => {
123
+ authenticationTemplate?: readonly import("../sdk").AuthenticationInput[] | undefined;
124
+ queryParams?: Record<string, string> | undefined;
125
+ headers?: Record<string, string> | undefined;
126
+ spec: {
127
+ readonly kind: "url";
128
+ readonly url: string;
129
+ } | {
130
+ readonly value: string;
131
+ readonly kind: "blob";
132
+ };
133
+ slug: string | undefined;
134
+ baseUrl: string | undefined;
135
+ };
136
+ export declare const makeOpenApiHttpApiTestSpecPayload: (api: HttpApi.Any, options?: OpenApiTestSpecOptions) => {
137
+ spec: string;
138
+ };
139
+ export declare const serveOpenApiHttpApiTestServer: (options: OpenApiHttpApiTestServerOptions) => Effect.Effect<OpenApiHttpApiTestServerShape, OpenApiTestServerAddressError | OpenApiTestServerSpecError, Scope.Scope>;
140
+ declare const OpenApiHttpApiTestServer_base: Context.ServiceClass<OpenApiHttpApiTestServer, "@rafads/plugin-openapi/testing/OpenApiHttpApiTestServer", OpenApiHttpApiTestServerShape>;
141
+ export declare class OpenApiHttpApiTestServer extends OpenApiHttpApiTestServer_base {
142
+ static readonly layer: (options: OpenApiHttpApiTestServerOptions) => Layer.Layer<OpenApiHttpApiTestServer, OpenApiTestServerAddressError | OpenApiTestServerSpecError, never>;
143
+ }
144
+ export declare const serveMutableOpenApiSpecTestServer: (options: {
145
+ readonly initialApi: HttpApi.Any;
146
+ readonly specPath?: `/${string}`;
147
+ readonly transformSpec?: (spec: Record<string, unknown>) => Record<string, unknown>;
148
+ }) => Effect.Effect<MutableOpenApiSpecTestServerShape, OpenApiTestServerAddressError | OpenApiTestServerSpecError, Scope.Scope>;
149
+ export declare const serveOpenApiEchoTestServer: (options?: OpenApiEchoTestServerOptions) => Effect.Effect<OpenApiEchoTestServerShape, OpenApiTestServerAddressError | OpenApiTestServerSpecError, Scope.Scope>;
150
+ export declare const serveOpenApiEchoTestServerWithOAuth: (options?: Omit<OpenApiEchoTestServerOptions, "oauth2"> & {
151
+ readonly scopes?: Readonly<Record<string, string>>;
152
+ readonly wwwAuthenticate?: string;
153
+ }) => Effect.Effect<OpenApiEchoTestServerShape, OpenApiTestServerAddressError | OpenApiTestServerSpecError, Scope.Scope | OAuthTestServer>;
154
+ declare const OpenApiEchoTestServer_base: Context.ServiceClass<OpenApiEchoTestServer, "@rafads/plugin-openapi/testing/OpenApiEchoTestServer", OpenApiEchoTestServerShape>;
155
+ export declare class OpenApiEchoTestServer extends OpenApiEchoTestServer_base {
156
+ static readonly layer: (options?: OpenApiEchoTestServerOptions) => Layer.Layer<OpenApiEchoTestServer, OpenApiTestServerAddressError | OpenApiTestServerSpecError, never>;
157
+ static readonly layerWithOAuth: (options?: Omit<OpenApiEchoTestServerOptions, "oauth2"> & {
158
+ readonly scopes?: Readonly<Record<string, string>>;
159
+ readonly wwwAuthenticate?: string;
160
+ }) => Layer.Layer<OpenApiEchoTestServer, OpenApiTestServerAddressError | OpenApiTestServerSpecError, OAuthTestServer>;
161
+ }
162
+ export declare const TestLayers: {
163
+ httpApi: (options: OpenApiHttpApiTestServerOptions) => Layer.Layer<OpenApiHttpApiTestServer, OpenApiTestServerAddressError | OpenApiTestServerSpecError, never>;
164
+ echo: (options?: OpenApiEchoTestServerOptions) => Layer.Layer<OpenApiEchoTestServer, OpenApiTestServerAddressError | OpenApiTestServerSpecError, never>;
165
+ echoWithOAuth: (options?: Omit<OpenApiEchoTestServerOptions, "oauth2"> & {
166
+ readonly scopes?: Readonly<Record<string, string>>;
167
+ readonly wwwAuthenticate?: string;
168
+ }) => Layer.Layer<OpenApiEchoTestServer, OpenApiTestServerAddressError | OpenApiTestServerSpecError, OAuthTestServer>;
169
+ };
170
+ export interface OpenApiInvocationResult<TData = Record<string, unknown> | unknown[] | null> {
171
+ readonly status: number | null;
172
+ readonly headers: Record<string, string> | null;
173
+ readonly data: TData;
174
+ readonly error: unknown;
175
+ }
176
+ export declare const unwrapInvocation: <TData = Record<string, unknown> | null>(raw: unknown) => OpenApiInvocationResult<TData>;
177
+ export {};
@@ -0,0 +1,454 @@
1
+ // src/testing/index.ts
2
+ import { Context, Data, Effect, Layer, Predicate, Ref, Schema } from "effect";
3
+ import {
4
+ HttpRouter,
5
+ HttpServerRequest,
6
+ HttpServerResponse
7
+ } from "effect/unstable/http";
8
+ import {
9
+ HttpApi,
10
+ HttpApiBuilder,
11
+ HttpApiEndpoint,
12
+ HttpApiGroup,
13
+ OpenApi
14
+ } from "effect/unstable/httpapi";
15
+ import { OAuthTestServer, serveTestHttpServerLayer } from "@rafads/sdk/testing";
16
+ import {
17
+ AuthTemplateSlug,
18
+ ConnectionName,
19
+ IntegrationSlug,
20
+ ProviderItemId,
21
+ ProviderKey,
22
+ ToolAddress,
23
+ isToolResult
24
+ } from "@rafads/sdk/core";
25
+ var OpenApiTestServerAddressError = class extends Data.TaggedError(
26
+ "OpenApiTestServerAddressError"
27
+ ) {
28
+ };
29
+ var OpenApiTestServerSpecError = class extends Data.TaggedError("OpenApiTestServerSpecError") {
30
+ };
31
+ var addOpenApiTestConnection = (executor, server, integrationOptions, connectionOptions = {}) => Effect.gen(function* () {
32
+ const config = makeOpenApiTestIntegrationConfig(server, integrationOptions);
33
+ const result = yield* executor.openapi.addSpec(config);
34
+ const slug = String(result.slug);
35
+ const owner = connectionOptions.owner ?? "org";
36
+ const connection = connectionOptions.connection ?? "main";
37
+ const template = connectionOptions.template ?? "apiKey";
38
+ yield* executor.connections.create({
39
+ owner,
40
+ name: ConnectionName.make(connection),
41
+ integration: IntegrationSlug.make(slug),
42
+ template: AuthTemplateSlug.make(template),
43
+ ...connectionOptions.value !== void 0 ? { value: connectionOptions.value } : {
44
+ from: {
45
+ provider: ProviderKey.make("memory"),
46
+ id: ProviderItemId.make(`${slug}-token`)
47
+ }
48
+ }
49
+ });
50
+ return {
51
+ slug,
52
+ owner,
53
+ connection,
54
+ address: (tool) => ToolAddress.make(`tools.${slug}.${owner}.${connection}.${tool}`)
55
+ };
56
+ });
57
+ var makeOpenApiTestSpecJson = (api, options = {}) => {
58
+ const annotations = OpenApi.annotations({
59
+ ...options.baseUrl !== void 0 ? { servers: [{ url: options.baseUrl }] } : {},
60
+ transform: options.transformSpec
61
+ });
62
+ const annotated = api.annotateMerge(annotations);
63
+ return JSON.stringify(OpenApi.fromApi(annotated));
64
+ };
65
+ var makeOpenApiTestIntegrationConfig = (server, options) => {
66
+ const { baseUrl, slug, ...rest } = options;
67
+ return {
68
+ ...rest,
69
+ spec: { kind: "blob", value: server.specJson },
70
+ slug: slug ?? "test_api",
71
+ ...baseUrl === null ? {} : { baseUrl: baseUrl ?? server.baseUrl }
72
+ };
73
+ };
74
+ var addOpenApiTestIntegration = (executor, server, options) => executor.openapi.addSpec(makeOpenApiTestIntegrationConfig(server, options));
75
+ var makeOpenApiHttpApiTestIntegrationConfig = (api, options) => {
76
+ const { baseUrl, specBaseUrl, transformSpec, slug, ...config } = options;
77
+ return {
78
+ ...config,
79
+ spec: {
80
+ kind: "blob",
81
+ value: makeOpenApiTestSpecJson(api, { baseUrl: specBaseUrl, transformSpec })
82
+ },
83
+ slug: slug ?? "test_api",
84
+ ...baseUrl === null ? {} : { baseUrl: baseUrl ?? specBaseUrl ?? "https://api.example.test" }
85
+ };
86
+ };
87
+ var addOpenApiHttpApiTestIntegration = (executor, api, options) => executor.openapi.addSpec(makeOpenApiHttpApiTestIntegrationConfig(api, options));
88
+ var makeOpenApiHttpApiTestAddSpecPayload = (api, options) => {
89
+ const { headers, queryParams, ...integrationOptions } = options;
90
+ const config = makeOpenApiHttpApiTestIntegrationConfig(api, integrationOptions);
91
+ return {
92
+ spec: config.spec,
93
+ slug: config.slug,
94
+ baseUrl: config.baseUrl,
95
+ ...headers !== void 0 ? { headers } : {},
96
+ ...queryParams !== void 0 ? { queryParams } : {},
97
+ ...config.authenticationTemplate !== void 0 ? { authenticationTemplate: config.authenticationTemplate } : {}
98
+ };
99
+ };
100
+ var makeOpenApiHttpApiTestSpecPayload = (api, options = {}) => ({
101
+ spec: makeOpenApiTestSpecJson(api, options)
102
+ });
103
+ var isJsonObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
104
+ var OpenApiEchoItem = Schema.Struct({
105
+ id: Schema.Number,
106
+ name: Schema.String
107
+ });
108
+ var OpenApiEchoHeaders = Schema.Struct({
109
+ authorization: Schema.optional(Schema.String),
110
+ "x-static": Schema.optional(Schema.String)
111
+ });
112
+ var OpenApiEchoMessage = Schema.Struct({
113
+ message: Schema.String,
114
+ suffix: Schema.optional(Schema.String),
115
+ path: Schema.String
116
+ });
117
+ var OpenApiEchoItemsGroup = HttpApiGroup.make("items").add(
118
+ HttpApiEndpoint.get("listItems", "/items", {
119
+ success: Schema.Array(OpenApiEchoItem)
120
+ })
121
+ ).add(
122
+ HttpApiEndpoint.get("echoHeaders", "/echo-headers", {
123
+ success: OpenApiEchoHeaders
124
+ })
125
+ );
126
+ var OpenApiEchoGroup = HttpApiGroup.make("echo").add(
127
+ HttpApiEndpoint.get("echoMessage", "/echo/:message", {
128
+ params: Schema.Struct({ message: Schema.String }),
129
+ query: Schema.Struct({ suffix: Schema.optional(Schema.String) }),
130
+ success: OpenApiEchoMessage
131
+ })
132
+ );
133
+ var OpenApiEchoApi = HttpApi.make("executorOpenApiTest").add(OpenApiEchoItemsGroup).add(OpenApiEchoGroup).annotateMerge(
134
+ OpenApi.annotations({
135
+ title: "Executor OpenAPI Test Server",
136
+ version: "1.0.0"
137
+ })
138
+ );
139
+ var openApiSpecJsonFromHttpApi = (api, baseUrl, transformSpec) => Effect.try({
140
+ try: () => makeOpenApiTestSpecJson(api, { baseUrl, transformSpec }),
141
+ catch: (cause) => new OpenApiTestServerSpecError({ cause })
142
+ });
143
+ var serveOpenApiHttpApiTestServer = (options) => Effect.gen(function* () {
144
+ const specPath = options.specPath ?? "/spec.json";
145
+ let specJson = "";
146
+ const SpecRoute = HttpRouter.addAll([
147
+ HttpRouter.route(
148
+ "GET",
149
+ specPath,
150
+ Effect.gen(function* () {
151
+ const request = yield* HttpServerRequest.HttpServerRequest;
152
+ if (options.captureSpecRequest) {
153
+ yield* options.captureSpecRequest(request);
154
+ }
155
+ const guardResponse = options.guardSpecRequest ? yield* options.guardSpecRequest(request) : null;
156
+ if (guardResponse) return guardResponse;
157
+ return HttpServerResponse.text(specJson, {
158
+ status: 200,
159
+ contentType: "application/json"
160
+ });
161
+ })
162
+ )
163
+ ]);
164
+ const ApiLive = HttpApiBuilder.layer(options.api).pipe(
165
+ Layer.provide(options.handlersLayer)
166
+ );
167
+ const ServerLayer = HttpRouter.serve(Layer.mergeAll(ApiLive, SpecRoute), {
168
+ disableListenLog: true,
169
+ disableLogger: true
170
+ });
171
+ const server = yield* serveTestHttpServerLayer(ServerLayer).pipe(
172
+ Effect.mapError(
173
+ (error) => Predicate.isTagged(error, "TestHttpServerAddressError") ? new OpenApiTestServerAddressError({ address: error.address }) : new OpenApiTestServerSpecError({ cause: error.cause })
174
+ )
175
+ );
176
+ specJson = yield* openApiSpecJsonFromHttpApi(
177
+ options.api,
178
+ server.baseUrl,
179
+ options.transformSpec
180
+ );
181
+ return {
182
+ baseUrl: server.baseUrl,
183
+ specUrl: server.url(specPath),
184
+ specJson,
185
+ httpClientLayer: server.httpClientLayer
186
+ };
187
+ });
188
+ var OpenApiHttpApiTestServer = class _OpenApiHttpApiTestServer extends Context.Service()("@rafads/plugin-openapi/testing/OpenApiHttpApiTestServer") {
189
+ static layer = (options) => Layer.effect(_OpenApiHttpApiTestServer, serveOpenApiHttpApiTestServer(options));
190
+ };
191
+ var serveMutableOpenApiSpecTestServer = (options) => Effect.gen(function* () {
192
+ const specPath = options.specPath ?? "/spec.json";
193
+ const specJson = yield* Ref.make("");
194
+ const requests = yield* Ref.make(0);
195
+ const SpecRoute = HttpRouter.addAll([
196
+ HttpRouter.route(
197
+ "GET",
198
+ specPath,
199
+ Effect.gen(function* () {
200
+ yield* Ref.update(requests, (count) => count + 1);
201
+ const current = yield* Ref.get(specJson);
202
+ return HttpServerResponse.text(current, {
203
+ status: 200,
204
+ contentType: "application/json"
205
+ });
206
+ })
207
+ )
208
+ ]);
209
+ const server = yield* serveTestHttpServerLayer(
210
+ HttpRouter.serve(SpecRoute, {
211
+ disableListenLog: true,
212
+ disableLogger: true
213
+ })
214
+ ).pipe(
215
+ Effect.mapError(
216
+ (error) => Predicate.isTagged(error, "TestHttpServerAddressError") ? new OpenApiTestServerAddressError({ address: error.address }) : new OpenApiTestServerSpecError({ cause: error.cause })
217
+ )
218
+ );
219
+ const renderSpec = (api) => openApiSpecJsonFromHttpApi(api, server.baseUrl, options.transformSpec);
220
+ yield* Ref.set(specJson, yield* renderSpec(options.initialApi));
221
+ return {
222
+ baseUrl: server.baseUrl,
223
+ specUrl: server.url(specPath),
224
+ specJson: yield* Ref.get(specJson),
225
+ httpClientLayer: server.httpClientLayer,
226
+ setApi: (api) => renderSpec(api).pipe(Effect.flatMap((next) => Ref.set(specJson, next))),
227
+ requestCount: Ref.get(requests)
228
+ };
229
+ });
230
+ var openApiOperationMethods = /* @__PURE__ */ new Set([
231
+ "get",
232
+ "put",
233
+ "post",
234
+ "delete",
235
+ "options",
236
+ "head",
237
+ "patch"
238
+ ]);
239
+ var withOAuth2Security = (oauth2) => (spec) => {
240
+ const scopes = oauth2.scopes ?? { read: "Read test resources" };
241
+ const security = [{ oauth2: Object.keys(scopes) }];
242
+ const paths = isJsonObject(spec.paths) ? Object.fromEntries(
243
+ Object.entries(spec.paths).map(([path, pathItem]) => [
244
+ path,
245
+ isJsonObject(pathItem) ? Object.fromEntries(
246
+ Object.entries(pathItem).map(([method, operation]) => [
247
+ method,
248
+ openApiOperationMethods.has(method) && isJsonObject(operation) ? { ...operation, security } : operation
249
+ ])
250
+ ) : pathItem
251
+ ])
252
+ ) : spec.paths;
253
+ const components = isJsonObject(spec.components) ? spec.components : {};
254
+ const securitySchemes = isJsonObject(components.securitySchemes) ? components.securitySchemes : {};
255
+ return {
256
+ ...spec,
257
+ paths,
258
+ components: {
259
+ ...components,
260
+ securitySchemes: {
261
+ ...securitySchemes,
262
+ oauth2: {
263
+ type: "oauth2",
264
+ flows: {
265
+ authorizationCode: {
266
+ authorizationUrl: oauth2.authorizationUrl,
267
+ tokenUrl: oauth2.tokenUrl,
268
+ scopes
269
+ }
270
+ }
271
+ }
272
+ }
273
+ }
274
+ };
275
+ };
276
+ var composeSpecTransforms = (...transforms) => (spec) => transforms.reduce((current, transform) => transform ? transform(current) : current, spec);
277
+ var recordOpenApiRequest = (requests, request) => Effect.gen(function* () {
278
+ const url = new URL(request.url, "http://executor.test");
279
+ const body = yield* request.text.pipe(Effect.catch(() => Effect.succeed("")));
280
+ yield* Ref.update(requests, (all) => [
281
+ ...all,
282
+ {
283
+ method: request.method,
284
+ url: request.url,
285
+ path: url.pathname,
286
+ headers: request.headers,
287
+ body
288
+ }
289
+ ]);
290
+ return request;
291
+ });
292
+ var captureOpenApiRequest = (requests) => Effect.gen(function* () {
293
+ const request = yield* HttpServerRequest.HttpServerRequest;
294
+ return yield* recordOpenApiRequest(requests, request);
295
+ });
296
+ var openApiUnauthorizedResponse = (options, authorization) => options.oauth2?.validateAuthorization ? options.oauth2.validateAuthorization(authorization).pipe(
297
+ Effect.map(
298
+ (accepted) => accepted ? null : HttpServerResponse.jsonUnsafe(
299
+ { error: "invalid_token" },
300
+ options.oauth2?.wwwAuthenticate ? {
301
+ status: 401,
302
+ headers: {
303
+ "www-authenticate": options.oauth2.wwwAuthenticate
304
+ }
305
+ } : { status: 401 }
306
+ )
307
+ )
308
+ ) : Effect.succeed(null);
309
+ var makeOpenApiEchoItemsGroupLive = (requests, options) => HttpApiBuilder.group(
310
+ OpenApiEchoApi,
311
+ "items",
312
+ (handlers) => handlers.handle(
313
+ "listItems",
314
+ () => Effect.gen(function* () {
315
+ const request = yield* captureOpenApiRequest(requests);
316
+ const unauthorized = yield* openApiUnauthorizedResponse(
317
+ options,
318
+ request.headers.authorization ?? null
319
+ );
320
+ if (unauthorized) return unauthorized;
321
+ return [
322
+ OpenApiEchoItem.make({ id: 1, name: "Widget" }),
323
+ OpenApiEchoItem.make({ id: 2, name: "Gadget" })
324
+ ];
325
+ })
326
+ ).handle(
327
+ "echoHeaders",
328
+ () => Effect.gen(function* () {
329
+ const request = yield* captureOpenApiRequest(requests);
330
+ const unauthorized = yield* openApiUnauthorizedResponse(
331
+ options,
332
+ request.headers.authorization ?? null
333
+ );
334
+ if (unauthorized) return unauthorized;
335
+ return OpenApiEchoHeaders.make({
336
+ authorization: request.headers.authorization,
337
+ "x-static": request.headers["x-static"]
338
+ });
339
+ })
340
+ )
341
+ );
342
+ var makeOpenApiEchoGroupLive = (requests, options) => HttpApiBuilder.group(
343
+ OpenApiEchoApi,
344
+ "echo",
345
+ (handlers) => handlers.handle(
346
+ "echoMessage",
347
+ ({ params, query }) => Effect.gen(function* () {
348
+ const request = yield* captureOpenApiRequest(requests);
349
+ const unauthorized = yield* openApiUnauthorizedResponse(
350
+ options,
351
+ request.headers.authorization ?? null
352
+ );
353
+ if (unauthorized) return unauthorized;
354
+ const path = `/echo/${encodeURIComponent(params.message)}`;
355
+ return OpenApiEchoMessage.make({
356
+ message: params.message,
357
+ ...query.suffix ? { suffix: query.suffix } : {},
358
+ path
359
+ });
360
+ })
361
+ )
362
+ );
363
+ var serveOpenApiEchoTestServer = (options = {}) => Effect.gen(function* () {
364
+ const requests = yield* Ref.make([]);
365
+ let specJson = "";
366
+ const server = yield* serveOpenApiHttpApiTestServer({
367
+ api: OpenApiEchoApi,
368
+ handlersLayer: Layer.mergeAll(
369
+ makeOpenApiEchoItemsGroupLive(requests, options),
370
+ makeOpenApiEchoGroupLive(requests, options)
371
+ ),
372
+ transformSpec: composeSpecTransforms(
373
+ options.oauth2 ? withOAuth2Security(options.oauth2) : void 0,
374
+ options.transformSpec
375
+ ),
376
+ captureSpecRequest: (request) => recordOpenApiRequest(requests, request).pipe(Effect.asVoid)
377
+ });
378
+ specJson = server.specJson;
379
+ return {
380
+ baseUrl: server.baseUrl,
381
+ specUrl: server.specUrl,
382
+ specJson,
383
+ httpClientLayer: server.httpClientLayer,
384
+ requests: Ref.get(requests),
385
+ clearRequests: Ref.set(requests, [])
386
+ };
387
+ });
388
+ var serveOpenApiEchoTestServerWithOAuth = (options = {}) => Effect.gen(function* () {
389
+ const oauth = yield* OAuthTestServer;
390
+ return yield* serveOpenApiEchoTestServer({
391
+ transformSpec: options.transformSpec,
392
+ oauth2: {
393
+ authorizationUrl: oauth.authorizationEndpoint,
394
+ tokenUrl: oauth.tokenEndpoint,
395
+ scopes: options.scopes,
396
+ validateAuthorization: oauth.acceptsAuthorizationHeader,
397
+ wwwAuthenticate: options.wwwAuthenticate
398
+ }
399
+ });
400
+ });
401
+ var OpenApiEchoTestServer = class _OpenApiEchoTestServer extends Context.Service()("@rafads/plugin-openapi/testing/OpenApiEchoTestServer") {
402
+ static layer = (options) => Layer.effect(_OpenApiEchoTestServer, serveOpenApiEchoTestServer(options));
403
+ static layerWithOAuth = (options) => Layer.effect(_OpenApiEchoTestServer, serveOpenApiEchoTestServerWithOAuth(options));
404
+ };
405
+ var TestLayers = {
406
+ httpApi: OpenApiHttpApiTestServer.layer,
407
+ echo: OpenApiEchoTestServer.layer,
408
+ echoWithOAuth: OpenApiEchoTestServer.layerWithOAuth
409
+ };
410
+ var unwrapInvocation = (raw) => {
411
+ if (!isToolResult(raw)) {
412
+ return {
413
+ status: null,
414
+ headers: null,
415
+ data: raw,
416
+ error: null
417
+ };
418
+ }
419
+ if (raw.ok) {
420
+ return {
421
+ status: raw.http?.status ?? null,
422
+ headers: raw.http?.headers ?? null,
423
+ data: raw.data,
424
+ error: null
425
+ };
426
+ }
427
+ return {
428
+ status: raw.error.status ?? null,
429
+ headers: null,
430
+ data: null,
431
+ error: raw.error.details ?? raw.error
432
+ };
433
+ };
434
+ export {
435
+ OpenApiEchoTestServer,
436
+ OpenApiHttpApiTestServer,
437
+ OpenApiTestServerAddressError,
438
+ OpenApiTestServerSpecError,
439
+ TestLayers,
440
+ addOpenApiHttpApiTestIntegration,
441
+ addOpenApiTestConnection,
442
+ addOpenApiTestIntegration,
443
+ makeOpenApiHttpApiTestAddSpecPayload,
444
+ makeOpenApiHttpApiTestIntegrationConfig,
445
+ makeOpenApiHttpApiTestSpecPayload,
446
+ makeOpenApiTestIntegrationConfig,
447
+ makeOpenApiTestSpecJson,
448
+ serveMutableOpenApiSpecTestServer,
449
+ serveOpenApiEchoTestServer,
450
+ serveOpenApiEchoTestServerWithOAuth,
451
+ serveOpenApiHttpApiTestServer,
452
+ unwrapInvocation
453
+ };
454
+ //# sourceMappingURL=testing.js.map