Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/openapi-generator-client-generics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/openapi-generator": minor
---

Generated `httpclient` clients now keep the error and requirement types of the `HttpClient` passed to `make`, so errors and services added by client middleware appear in each operation's signature. The generated interface gains `E` and `R` type parameters that default to `HttpClientError` and `never`, so existing clients are unchanged.
54 changes: 27 additions & 27 deletions packages/tools/openapi-generator/src/OpenApiTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ export const makeTransformerSchema = () => {
methods.push(operationToBinaryMethod(name, op))
}
}
return `export interface ${name} {
readonly httpClient: HttpClient.HttpClient
return `export interface ${name}<E = HttpClientError.HttpClientError, R = never> {
readonly httpClient: HttpClient.HttpClient.With<E, R>
${methods.join("\n ")}
}

Expand Down Expand Up @@ -163,7 +163,7 @@ ${clientErrorSource(name)}`
successTypes.add("void")
}
const success = successTypes.size > 0 ? Array.from(successTypes).join(" | ") : "void"
const errors = ["HttpClientError.HttpClientError", "SchemaError"]
const errors = ["E", "HttpClientError.HttpClientError", "SchemaError"]
if (responses.errorSchemas.size > 0) {
Utils.spreadElementsInto(
Array.from(responses.errorSchemas.values()).map(
Expand Down Expand Up @@ -212,7 +212,7 @@ ${clientErrorSource(name)}`
? responses.sseSchema
: `{ readonly event: string; readonly id: string | undefined; readonly data: ${responses.sseSchema} }`
const returnType =
`Stream.Stream<${value}, HttpClientError.HttpClientError | SchemaError | Sse.Retry | Sse.SseError, typeof ${responses.sseSchema}.DecodingServices>`
`Stream.Stream<${value}, E | HttpClientError.HttpClientError | SchemaError | Sse.Retry | Sse.SseError, R | typeof ${responses.sseSchema}.DecodingServices>`
return `${jsdoc}${methodKey}: (${parameters}) => ${returnType}`
}

Expand Down Expand Up @@ -242,7 +242,7 @@ ${clientErrorSource(name)}`
const jsdoc = Utils.toComment(operation.description)
const methodKey = `readonly "${operation.id}Stream"`
const parameters = args.join(", ")
const returnType = `Stream.Stream<Uint8Array, HttpClientError.HttpClientError>`
const returnType = `Stream.Stream<Uint8Array, E | HttpClientError.HttpClientError, R>`
return `${jsdoc}${methodKey}: (${parameters}) => ${returnType}`
}

Expand Down Expand Up @@ -307,12 +307,12 @@ export type WithOptionalResponse<A, Config extends OperationConfig | undefined>
: Config extends { readonly includeResponse?: false | undefined } | undefined ? A
: A | [A, HttpClientResponse.HttpClientResponse]

export const make = (
httpClient: HttpClient.HttpClient,
export const make = <E, R>(
httpClient: HttpClient.HttpClient.With<E, R>,
options: {
readonly transformClient?: ((client: HttpClient.HttpClient) => Effect.Effect<HttpClient.HttpClient>) | undefined
readonly transformClient?: ((client: HttpClient.HttpClient.With<E, R>) => Effect.Effect<HttpClient.HttpClient.With<E, R>>) | undefined
} = {}
): ${name} => {
): ${name}<E, R> => {
${helpers.join("\n ")}
const decodeSuccess =
<Schema extends ${importName}.Constraint>(schema: Schema) =>
Expand Down Expand Up @@ -560,8 +560,8 @@ export const makeTransformerTs = () => {
methods.push(operationToBinaryMethod(op))
}
}
return `export interface ${name} {
readonly httpClient: HttpClient.HttpClient
return `export interface ${name}<E = HttpClientError.HttpClientError, R = never> {
readonly httpClient: HttpClient.HttpClient.With<E, R>
Comment on lines +563 to +564

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Map the relevant file and nearby generator helpers
ast-grep outline packages/tools/openapi-generator/src/OpenApiTransformer.ts --view expanded || true
printf '\n---\n'
rg -n "operationToSseMethod|sseRequestSourceTs|HttpClientError|Stream.Stream<" packages/tools/openapi-generator/src/OpenApiTransformer.ts packages/tools/openapi-generator/src -n

Repository: Effect-TS/effect

Length of output: 8062


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Read the relevant slices around the reported lines and related helpers.
sed -n '430,560p' packages/tools/openapi-generator/src/OpenApiTransformer.ts
printf '\n---\n'
sed -n '560,760p' packages/tools/openapi-generator/src/OpenApiTransformer.ts

Repository: Effect-TS/effect

Length of output: 12091


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the relevant generator helpers and type signatures.
git ls-files packages/tools/openapi-generator/src | sed -n '1,120p'
printf '\n--- FILE OUTLINE ---\n'
ast-grep outline packages/tools/openapi-generator/src/OpenApiTransformer.ts --view expanded || true

Repository: Effect-TS/effect

Length of output: 1546


🏁 Script executed:

#!/bin/bash
set -euo pipefail

file=packages/tools/openapi-generator/src/OpenApiTransformer.ts

# Show the exact region around the reported lines and any SSE helper definitions.
nl -ba "$file" | sed -n '480,560p'
printf '\n---\n'
rg -n "sseRequestSourceTs|operationToSseMethod|Stream\\.Stream<.*HttpClientError|HttpClient\\.With<" "$file"

Repository: Effect-TS/effect

Length of output: 194


🏁 Script executed:

#!/bin/bash
set -euo pipefail

file=packages/tools/openapi-generator/src/OpenApiTransformer.ts

# Read the SSE method + helper implementation region.
sed -n '540,980p' "$file"

Repository: Effect-TS/effect

Length of output: 16517


🏁 Script executed:

#!/bin/bash
set -euo pipefail

python3 - <<'PY'
from pathlib import Path
p = Path('packages/tools/openapi-generator/src/OpenApiTransformer.ts')
text = p.read_text()
for needle in ['const operationToSseMethod = (operation: ParsedOperation) => {',
               'const operationToSseImpl = (operation: ParsedOperation) => {',
               'const sseRequestSourceTs =',
               'const sseRequestSource = (_importName: string) =>']:
    idx = text.find(needle)
    print(f"\n### {needle}\n")
    if idx == -1:
        print("NOT FOUND")
        continue
    start = max(0, idx - 200)
    end = min(len(text), idx + 1800)
    print(text[start:end])
PY

Repository: Effect-TS/effect

Length of output: 8383


🏁 Script executed:

#!/bin/bash
set -euo pipefail

sed -n '555,760p' packages/tools/openapi-generator/src/OpenApiTransformer.ts

Repository: Effect-TS/effect

Length of output: 7640


Propagate E and R through type-only SSE methods. operationToSseMethod and sseRequestSourceTs still hard-code HttpClientError.HttpClientError, so generated HttpClient.With<CustomError, CustomRequirements> clients lose those channels on *Sse methods.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/tools/openapi-generator/src/OpenApiTransformer.ts` around lines 502
- 503, Update operationToSseMethod and sseRequestSourceTs to propagate the
generated client’s generic E and R parameters instead of hard-coding
HttpClientError.HttpClientError. Ensure type-only SSE methods preserve the same
custom error and requirements channels as the surrounding HttpClient.With<E, R>
interface.

${methods.join("\n ")}
}

Expand Down Expand Up @@ -595,7 +595,7 @@ ${clientErrorSource(name)}`
}
const success = successTypes.size > 0 ? Array.from(successTypes).join(" | ") : "void"

const errors = ["HttpClientError.HttpClientError"]
const errors = ["E", "HttpClientError.HttpClientError"]
if (responses.errorSchemas.size > 0) {
for (const schema of responses.errorSchemas.values()) {
errors.push(`${name}Error<"${schema}", ${schema}>`)
Expand Down Expand Up @@ -637,7 +637,7 @@ ${clientErrorSource(name)}`
const jsdoc = Utils.toComment(operation.description)
const methodKey = `readonly "${operation.id}Sse"`
const parameters = args.join(", ")
const returnType = `Stream.Stream<${responses.sseSchema}, HttpClientError.HttpClientError>`
const returnType = `Stream.Stream<${responses.sseSchema}, E | HttpClientError.HttpClientError, R>`
return `${jsdoc}${methodKey}: (${parameters}) => ${returnType}`
}

Expand Down Expand Up @@ -667,7 +667,7 @@ ${clientErrorSource(name)}`
const jsdoc = Utils.toComment(operation.description)
const methodKey = `readonly "${operation.id}Stream"`
const parameters = args.join(", ")
const returnType = `Stream.Stream<Uint8Array, HttpClientError.HttpClientError>`
const returnType = `Stream.Stream<Uint8Array, E | HttpClientError.HttpClientError, R>`
return `${jsdoc}${methodKey}: (${parameters}) => ${returnType}`
}

Expand Down Expand Up @@ -731,12 +731,12 @@ export type WithOptionalResponse<A, Config extends OperationConfig | undefined>
: Config extends { readonly includeResponse?: false | undefined } | undefined ? A
: A | [A, HttpClientResponse.HttpClientResponse]

export const make = (
httpClient: HttpClient.HttpClient,
export const make = <E, R>(
httpClient: HttpClient.HttpClient.With<E, R>,
options: {
readonly transformClient?: ((client: HttpClient.HttpClient) => Effect.Effect<HttpClient.HttpClient>) | undefined
readonly transformClient?: ((client: HttpClient.HttpClient.With<E, R>) => Effect.Effect<HttpClient.HttpClient.With<E, R>>) | undefined
} = {}
): ${name} => {
): ${name}<E, R> => {
${helpers.join("\n ")}
const decodeSuccess = <A>(response: HttpClientResponse.HttpClientResponse) =>
response.json as Effect.Effect<A, HttpClientError.HttpClientError>
Expand Down Expand Up @@ -1001,7 +1001,7 @@ const commonSource = `const unexpectedStatus = (response: HttpClientResponse.Htt
)
const withResponse = <Config extends OperationConfig>(config: Config | undefined) => (
f: (response: HttpClientResponse.HttpClientResponse) => Effect.Effect<any, any>,
): (request: HttpClientRequest.HttpClientRequest) => Effect.Effect<any, any> => {
): (request: HttpClientRequest.HttpClientRequest) => Effect.Effect<any, any, R> => {
const withOptionalResponse = (
config?.includeResponse
? (response: HttpClientResponse.HttpClientResponse) => Effect.map(f(response), (a) => [a, response])
Expand Down Expand Up @@ -1086,8 +1086,8 @@ const sseRequestSource = (_importName: string) =>
request: HttpClientRequest.HttpClientRequest
): Stream.Stream<
{ readonly event: string; readonly id: string | undefined; readonly data: Type },
HttpClientError.HttpClientError | SchemaError | Sse.Retry | Sse.SseError,
DecodingServices
E | HttpClientError.HttpClientError | SchemaError | Sse.Retry | Sse.SseError,
R | DecodingServices
> =>
executeStreamRequest(request).pipe(
Effect.map((response) => response.stream),
Expand All @@ -1101,8 +1101,8 @@ const sseEventRequestSource = `const sseEventRequest = <S extends Sse.EventCodec
request: HttpClientRequest.HttpClientRequest
): Stream.Stream<
S["Type"],
HttpClientError.HttpClientError | SchemaError | Sse.Retry | Sse.SseError,
S["DecodingServices"]
E | HttpClientError.HttpClientError | SchemaError | Sse.Retry | Sse.SseError,
R | S["DecodingServices"]
> =>
executeStreamRequest(request).pipe(
Effect.map((response) => response.stream),
Expand All @@ -1112,15 +1112,15 @@ const sseEventRequestSource = `const sseEventRequest = <S extends Sse.EventCodec
)`

const binaryRequestSource =
`const binaryRequest = (request: HttpClientRequest.HttpClientRequest): Stream.Stream<Uint8Array, HttpClientError.HttpClientError> =>
`const binaryRequest = (request: HttpClientRequest.HttpClientRequest): Stream.Stream<Uint8Array, E | HttpClientError.HttpClientError, R> =>
executeStreamRequest(request).pipe(
Effect.map((response) => response.stream),
Stream.unwrap
)`

// Type-only mode helpers (no schema decoding)
const sseRequestSourceTs =
`const sseRequest = (request: HttpClientRequest.HttpClientRequest): Stream.Stream<unknown, HttpClientError.HttpClientError> =>
`const sseRequest = (request: HttpClientRequest.HttpClientRequest): Stream.Stream<unknown, E | HttpClientError.HttpClientError, R> =>
executeStreamRequest(request).pipe(
Effect.map((response) => response.stream),
Stream.unwrap,
Expand All @@ -1131,7 +1131,7 @@ const sseRequestSourceTs =
)`

const binaryRequestSourceTs =
`const binaryRequest = (request: HttpClientRequest.HttpClientRequest): Stream.Stream<Uint8Array, HttpClientError.HttpClientError> =>
`const binaryRequest = (request: HttpClientRequest.HttpClientRequest): Stream.Stream<Uint8Array, E | HttpClientError.HttpClientError, R> =>
executeStreamRequest(request).pipe(
Effect.map((response) => response.stream),
Stream.unwrap
Expand Down Expand Up @@ -1179,7 +1179,7 @@ const operationSignatures = (
const parameters = [...args, `options: { ${fields} }${configOptional && !optionsRequired ? " | undefined" : ""}`]
return ` <Config extends OperationConfig | undefined = undefined>(${
parameters.join(", ")
}): Effect.Effect<WithOptionalResponse<${success}, ${config}>, ${errors.join(" | ")}>;`
}): Effect.Effect<WithOptionalResponse<${success}, ${config}>, ${errors.join(" | ")}, R>;`
}
// Infer the whole config from a required property before allowing omitted config.
// An optional property would otherwise discard undefined during inference.
Expand Down
Loading
Loading