Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ export class DynamicSnippetsGenerator extends AbstractDynamicSnippetsGenerator<
return super.generateSync(request);
}

public async generateSnippetAst(request: FernIr.dynamic.EndpointSnippetRequest): Promise<AbstractAstNode> {
return super.generateSnippetAst(request);
public async generateSnippetAst(
request: FernIr.dynamic.EndpointSnippetRequest,
options?: { skipClientInstantiation?: boolean }
): Promise<AbstractAstNode> {
return super.generateSnippetAst(request, options);
}

protected createSnippetGenerator(context: DynamicSnippetsGeneratorContext): EndpointSnippetGenerator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ruby } from "@fern-api/ruby-ast";
import { DynamicSnippetsGeneratorContext } from "./context/DynamicSnippetsGeneratorContext";

const CLIENT_VAR_NAME = "client";
const INSTANCE_CLIENT_VAR_NAME = "@client";

export class EndpointSnippetGenerator {
private context: DynamicSnippetsGeneratorContext;
Expand Down Expand Up @@ -53,6 +54,9 @@ export class EndpointSnippetGenerator {
request: FernIr.dynamic.EndpointSnippetRequest;
options?: Options;
}): Promise<ruby.AstNode> {
if (options?.skipClientInstantiation) {
return this.buildCodeBlockWithoutClient({ endpoint, snippet: request });
}
return this.buildCodeBlock({ endpoint, snippet: request });
}

Expand All @@ -71,6 +75,22 @@ export class EndpointSnippetGenerator {
});
}

/**
* Builds a code block without client instantiation.
* Used for wire tests where the client is already instantiated in the test setup.
*/
private buildCodeBlockWithoutClient({
endpoint,
snippet
}: {
endpoint: FernIr.dynamic.Endpoint;
snippet: FernIr.dynamic.EndpointSnippetRequest;
}): ruby.AstNode {
return ruby.codeblock((writer) => {
writer.writeNodeStatement(this.callMethodOnExistingClient({ endpoint, snippet }));
});
}

private constructClient({
endpoint,
snippet
Expand Down Expand Up @@ -362,6 +382,50 @@ export class EndpointSnippetGenerator {
return ruby.invokeMethod(invokeMethodArgs);
}

/**
* Calls a method on an existing client instance variable (@client).
* Used for wire tests where the client is already instantiated in the test setup.
*/
private callMethodOnExistingClient({
endpoint,
snippet
}: {
endpoint: FernIr.dynamic.Endpoint;
snippet: FernIr.dynamic.EndpointSnippetRequest;
}): ruby.MethodInvocation {
const invokeMethodArgs: ruby.MethodInvocation.Args = {
on: ruby.codeblock(INSTANCE_CLIENT_VAR_NAME),
method: this.getMethod({ endpoint }),
arguments_: []
};

switch (endpoint.request.type) {
case "inlined":
invokeMethodArgs.keywordArguments = this.getMethodArgsForInlinedRequest({
request: endpoint.request,
snippet
});
break;
case "body":
invokeMethodArgs.keywordArguments = this.getMethodArgsForBodyRequest({
request: endpoint.request,
snippet
});
break;
default:
assertNever(endpoint.request);
}

// Add request_options with additional_headers for unmapped headers (e.g., X-Test-Id)
const requestOptions = this.getRequestOptions({ endpoint, snippet });
if (requestOptions != null) {
invokeMethodArgs.keywordArguments = invokeMethodArgs.keywordArguments ?? [];
invokeMethodArgs.keywordArguments.push(requestOptions);
}

return ruby.invokeMethod(invokeMethodArgs);
}

/**
* Builds request_options from snippet headers for per-request options.
* This is used when generating snippets for wire tests where headers like X-Test-Id
Expand Down
Loading
Loading