Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi committed Nov 19, 2024
1 parent 4ffc5a0 commit f57ad8d
Show file tree
Hide file tree
Showing 23 changed files with 380 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -615,15 +615,19 @@ export class GeneratedSdkClientClassImpl implements GeneratedSdkClientClass {
ts.factory.createTypeReferenceNode("Promise", [signature.returnTypeWithoutPromise])
)
: getTextOfTsNode(
context.coreUtilities.apiPromise._getReferenceToType(signature.returnTypeWithoutPromise)
context.coreUtilities.promiseUtils.APIPromise._getReferenceToType(
signature.returnTypeWithoutPromise
)
),
scope: Scope.Public,
isAsync: true,
isAsync: context.neverThrowErrors, // if not neverThrowErrors we return an `APIPromise`
statements: context.neverThrowErrors
? statements.map(getTextOfTsNode)
: [ts.factory.createReturnStatement(context.coreUtilities.apiPromise.from(statements))].map(
getTextOfTsNode
),
: [
ts.factory.createReturnStatement(
context.coreUtilities.promiseUtils.APIPromise.from(statements)
)
].map(getTextOfTsNode),
overloads: overloads.map((overload, index) => ({
docs: index === 0 && docs != null ? ["\n" + docs] : undefined,
parameters: overload.parameters,
Expand All @@ -632,7 +636,9 @@ export class GeneratedSdkClientClassImpl implements GeneratedSdkClientClass {
ts.factory.createTypeReferenceNode("Promise", [overload.returnTypeWithoutPromise])
)
: getTextOfTsNode(
context.coreUtilities.apiPromise._getReferenceToType(overload.returnTypeWithoutPromise)
context.coreUtilities.promiseUtils.APIPromise._getReferenceToType(
overload.returnTypeWithoutPromise
)
)
}))
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ export class GeneratedThrowingEndpointResponse implements GeneratedEndpointRespo
}

private getReturnResponseIfOk(context: SdkContext): ts.Statement {

return ts.factory.createIfStatement(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier(GeneratedThrowingEndpointResponse.RESPONSE_VARIABLE_NAME),
Expand Down Expand Up @@ -421,7 +422,7 @@ export class GeneratedThrowingEndpointResponse implements GeneratedEndpointRespo
ts.NodeFlags.Const
)
),
ts.factory.createReturnStatement(
this.getSuccessResponse(
ts.factory.createObjectLiteralExpression(
[
ts.factory.createPropertyAssignment(
Expand Down Expand Up @@ -481,55 +482,52 @@ export class GeneratedThrowingEndpointResponse implements GeneratedEndpointRespo
throw new Error(`Unknown response type: ${type}`);
}
});
return [
ts.factory.createReturnStatement(
context.coreUtilities.streamUtils.Stream._construct({
stream: ts.factory.createPropertyAccessChain(
ts.factory.createIdentifier(GeneratedThrowingEndpointResponse.RESPONSE_VARIABLE_NAME),
return [this.getSuccessResponse(context.coreUtilities.streamUtils.Stream._construct({
stream: ts.factory.createPropertyAccessChain(
ts.factory.createIdentifier(GeneratedThrowingEndpointResponse.RESPONSE_VARIABLE_NAME),
undefined,
ts.factory.createIdentifier(
context.coreUtilities.fetcher.APIResponse.SuccessfulResponse.body
)
),
eventShape,
signal: getAbortSignalExpression({
abortSignalReference: this.clientClass.getReferenceToAbortSignal.bind(this.clientClass)
}),
parse: ts.factory.createArrowFunction(
[ts.factory.createToken(ts.SyntaxKind.AsyncKeyword)],
undefined,
[
ts.factory.createParameterDeclaration(
undefined,
ts.factory.createIdentifier(
context.coreUtilities.fetcher.APIResponse.SuccessfulResponse.body
)
),
eventShape,
signal: getAbortSignalExpression({
abortSignalReference: this.clientClass.getReferenceToAbortSignal.bind(this.clientClass)
}),
parse: ts.factory.createArrowFunction(
[ts.factory.createToken(ts.SyntaxKind.AsyncKeyword)],
undefined,
[
ts.factory.createParameterDeclaration(
undefined,
undefined,
undefined,
GeneratedStreamingEndpointImplementation.DATA_PARAMETER_NAME
)
],
undefined,
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
ts.factory.createBlock(
[
ts.factory.createReturnStatement(
context.sdkEndpointTypeSchemas
.getGeneratedEndpointTypeSchemas(this.packageId, this.endpoint.name)
.deserializeStreamData({
context,
referenceToRawStreamData: ts.factory.createIdentifier(
GeneratedStreamingEndpointImplementation.DATA_PARAMETER_NAME
)
})
)
],
true
)
GeneratedStreamingEndpointImplementation.DATA_PARAMETER_NAME
)
})
],
undefined,
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
ts.factory.createBlock(
[
ts.factory.createReturnStatement(
context.sdkEndpointTypeSchemas
.getGeneratedEndpointTypeSchemas(this.packageId, this.endpoint.name)
.deserializeStreamData({
context,
referenceToRawStreamData: ts.factory.createIdentifier(
GeneratedStreamingEndpointImplementation.DATA_PARAMETER_NAME
)
})
)
],
true
)
)
}))
];
}
return [
ts.factory.createReturnStatement(
this.getSuccessResponse(
generatedEndpointTypeSchemas.deserializeResponse(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier(GeneratedThrowingEndpointResponse.RESPONSE_VARIABLE_NAME),
Expand All @@ -541,6 +539,34 @@ export class GeneratedThrowingEndpointResponse implements GeneratedEndpointRespo
];
}

private getSuccessResponse(body: ts.Expression): ts.ReturnStatement {
return ts.factory.createReturnStatement(
ts.factory.createObjectLiteralExpression(
[
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier("ok"),
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier(GeneratedThrowingEndpointResponse.RESPONSE_VARIABLE_NAME),
ts.factory.createIdentifier("ok")
)
),
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier("body"),
body
),
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier("headers"),
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier(GeneratedThrowingEndpointResponse.RESPONSE_VARIABLE_NAME),
ts.factory.createIdentifier("headers")
)
)
],
true
)
);
}

private getReferenceToResponseHeaders(context: SdkContext): ts.Expression {
return ts.factory.createBinaryExpression(
ts.factory.createPropertyAccessExpression(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Runtime } from "./runtime/Runtime";
import { StreamUtils } from "./stream-utils/StreamUtils";
import { Utils } from "./utils/Utils";
import { Zurg } from "./zurg/Zurg";
import { APIPromise } from "./api-promise/APIPromise";
import { PromiseUtils } from "./promise/PromiseUtils";

export interface CoreUtilities {
zurg: Zurg;
Expand All @@ -21,5 +21,5 @@ export interface CoreUtilities {
runtime: Runtime;
pagination: Pagination;
utils: Utils;
apiPromise: APIPromise;
promiseUtils: PromiseUtils;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { DependencyManager } from "../dependency-manager/DependencyManager";
import { ExportedDirectory, ExportsManager } from "../exports-manager";
import { ImportsManager } from "../imports-manager";
import { getReferenceToExportViaNamespaceImport } from "../referencing";
import { APIPromiseImpl } from "./api-promise/APIPromiseImpl";
import { AuthImpl } from "./auth/AuthImpl";
import { BaseCoreUtilitiesImpl } from "./base/BaseCoreUtilitiesImpl";
import { CallbackQueueImpl } from "./callback-queue/CallbackQueueImpl";
Expand All @@ -28,6 +27,7 @@ import { RuntimeImpl } from "./runtime/RuntimeImpl";
import { StreamingUtilsImpl } from "./stream-utils/StreamUtilsImpl";
import { UtilsImpl } from "./utils/UtilsImpl";
import { ZurgImpl } from "./zurg/ZurgImpl";
import { PromiseUtilsImpl } from "./promise/PromiseUtilsImpl";

export declare namespace CoreUtilitiesManager {
namespace getCoreUtilities {
Expand Down Expand Up @@ -55,7 +55,7 @@ export class CoreUtilitiesManager {
runtime: new RuntimeImpl({ getReferenceToExport }),
pagination: new PaginationImpl({ getReferenceToExport }),
utils: new UtilsImpl({ getReferenceToExport }),
apiPromise: new APIPromiseImpl({ getReferenceToExport })
promiseUtils: new PromiseUtilsImpl({ getReferenceToExport })
};
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ts } from "ts-morph";

export interface PromiseUtils {
readonly APIPromise: {
_getReferenceToType: (response: ts.TypeNode) => ts.TypeNode;
from: (statements: ts.Statement[]) => ts.Expression;
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { AbsoluteFilePath, RelativeFilePath } from "@fern-api/fs-utils";
import { ts } from "ts-morph";
import { DependencyManager } from "../../dependency-manager/DependencyManager";
import { CoreUtility } from "../CoreUtility";
import { PromiseUtils } from "./PromiseUtils";

export class PromiseUtilsImpl extends CoreUtility implements PromiseUtils {
public readonly MANIFEST = {
name: "promise",
repoInfoForTesting: {
path: RelativeFilePath.of("generators/typescript/utils/core-utilities/fetcher/src/promise")
},
originalPathOnDocker: AbsoluteFilePath.of("/assets/fetcher/promise"),
pathInCoreUtilities: [{ nameOnDisk: "api-promise", exportDeclaration: { exportAll: true } }],
addDependencies: (dependencyManager: DependencyManager): void => {
return;
}
};

public APIPromise = {
_getReferenceToType: this.withExportedName(
"APIPromise",
(APIResponse) => (responseType: ts.TypeNode) =>
ts.factory.createTypeReferenceNode(APIResponse.getEntityName(), [responseType])
),
from: (body: ts.Statement[]): ts.Expression => {
return ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier("core"),
ts.factory.createIdentifier("APIPromise"),
),
ts.factory.createIdentifier("from")
),
undefined,
[
ts.factory.createCallExpression(
ts.factory.createParenthesizedExpression(
ts.factory.createArrowFunction(
[ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword)],
undefined,
[],
undefined,
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
ts.factory.createBlock(body, true)
)
),
undefined,
[]
)
]
);
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export * from "./form-data-utils";
export * from "./runtime";
export * from "./stream";
export * from "./pagination";
export * from "./api-promise";
export * from "./promise";
Loading

0 comments on commit f57ad8d

Please sign in to comment.