Skip to content

feat(typescript): [DRAFT] creating websocket util files #7508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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 @@ -777,6 +777,11 @@ export class GeneratedSdkClientClassImpl implements GeneratedSdkClientClass {
}

if (this.generatedWebsocketImplementation != null) {
// we need to import the factory for the client
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not certain this is how to import this from here.

this.importsManager.addImport("../utils/createWebSocket.js", {
namedImports: ["createWebSocket"]
});

const signature = this.generatedWebsocketImplementation.getSignature(context);
const classStatements = this.generatedWebsocketImplementation.getClassStatements(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export class GeneratedDefaultWebsocketImplementation implements GeneratedWebsock
private static readonly CONNECT_ARGS_PRIVATE_MEMBER = "args";
private static readonly DEBUG_PROPERTY_NAME = "debug";
private static readonly HEADERS_PROPERTY_NAME = "headers";
private static readonly HEADERS_VARIABLE_NAME = "websocketHeaders";

private static readonly RECONNECT_ATTEMPTS_PROPERTY_NAME = "reconnectAttempts";
private static readonly GENERATED_VERSION_PROPERTY_NAME = "fernSdkVersion";
Expand Down Expand Up @@ -230,97 +229,47 @@ export class GeneratedDefaultWebsocketImplementation implements GeneratedWebsock
)
)
),

ts.factory.createVariableStatement(
undefined,
ts.factory.createVariableDeclarationList(
[
ts.factory.createVariableDeclaration(
ts.factory.createIdentifier(GeneratedDefaultWebsocketImplementation.HEADERS_VARIABLE_NAME),
ts.factory.createIdentifier("clientOptions"),
undefined,
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("Record"), [
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)
]),
ts.factory.createObjectLiteralExpression()
undefined,
this.createClientOptionsExpression()
)
],
ts.NodeFlags.Let
ts.NodeFlags.Const
)
),
...(this.generatedSdkClientClass.shouldGenerateCustomAuthorizationHeaderHelperMethod()
? [
ts.factory.createExpressionStatement(
ts.factory.createBinaryExpression(
ts.factory.createIdentifier(
GeneratedDefaultWebsocketImplementation.HEADERS_VARIABLE_NAME
),
ts.factory.createToken(ts.SyntaxKind.EqualsToken),
ts.factory.createObjectLiteralExpression(
[
ts.factory.createSpreadAssignment(
ts.factory.createIdentifier(
GeneratedDefaultWebsocketImplementation.HEADERS_VARIABLE_NAME
)
),
ts.factory.createSpreadAssignment(
ts.factory.createAwaitExpression(
ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createThis(),
GeneratedSdkClientClassImpl.CUSTOM_AUTHORIZATION_HEADER_HELPER_METHOD_NAME
),
undefined,
[]
)
)
)
],
true
)
)
)
]
: []),
...(this.channel.headers ?? []).map((header) => {
return ts.factory.createIfStatement(
ts.factory.createBinaryExpression(
this.getReferenceToArg(header.name.wireValue),
ts.factory.createToken(ts.SyntaxKind.ExclamationEqualsToken),
ts.factory.createNull()
),
ts.factory.createBlock([
ts.factory.createExpressionStatement(
ts.factory.createBinaryExpression(
ts.factory.createElementAccessExpression(
ts.factory.createIdentifier(
GeneratedDefaultWebsocketImplementation.HEADERS_VARIABLE_NAME
),
ts.factory.createStringLiteral(header.name.wireValue)
),
ts.factory.createToken(ts.SyntaxKind.EqualsToken),
this.getReferenceToArg(header.name.wireValue)
)
ts.factory.createVariableStatement(
undefined,
ts.factory.createVariableDeclarationList(
[
ts.factory.createVariableDeclaration(
ts.factory.createIdentifier("url"),
undefined,
undefined,
this.buildFullUrl(this.getBaseUrl(this.channel, context), this.channel, context)
)
])
);
}),
ts.factory.createExpressionStatement(
ts.factory.createBinaryExpression(
ts.factory.createIdentifier(GeneratedDefaultWebsocketImplementation.HEADERS_VARIABLE_NAME),
ts.factory.createToken(ts.SyntaxKind.EqualsToken),
ts.factory.createObjectLiteralExpression(
[
ts.factory.createSpreadAssignment(
ts.factory.createIdentifier(
GeneratedDefaultWebsocketImplementation.HEADERS_VARIABLE_NAME
)
),
ts.factory.createSpreadAssignment(
this.getReferenceToArg(GeneratedDefaultWebsocketImplementation.HEADERS_PROPERTY_NAME)
)
],
true
)
],
ts.NodeFlags.Const
)
),
ts.factory.createVariableStatement(
undefined,
ts.factory.createVariableDeclarationList(
[
ts.factory.createVariableDeclaration(
ts.factory.createIdentifier("reconnectingOptions"),
undefined,
undefined,
this.createReconnectingOptionsExpression()
)
],
ts.NodeFlags.Const
)
),
ts.factory.createVariableStatement(
Expand All @@ -331,7 +280,7 @@ export class GeneratedDefaultWebsocketImplementation implements GeneratedWebsock
ts.factory.createIdentifier("socket"),
undefined,
undefined,
this.getReferenceToWebsocket(context)
this.createReconnectingWebSocketExpression()
)
],
ts.NodeFlags.Const
Expand All @@ -347,34 +296,96 @@ export class GeneratedDefaultWebsocketImplementation implements GeneratedWebsock
];
}

private getReferenceToWebsocket(context: SdkContext): ts.Expression {
return context.coreUtilities.websocket.ReconnectingWebSocket._connect({
url: this.buildFullUrl(this.getBaseUrl(this.channel, context), this.channel, context),
protocols: ts.factory.createArrayLiteralExpression([]),
options: ts.factory.createObjectLiteralExpression([
ts.factory.createPropertyAssignment(
"debug",
ts.factory.createBinaryExpression(
this.getReferenceToArg(GeneratedDefaultWebsocketImplementation.DEBUG_PROPERTY_NAME),
ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
ts.factory.createFalse()
private createClientOptionsExpression(): ts.Expression {
return ts.factory.createObjectLiteralExpression([
ts.factory.createPropertyAssignment(
"debug",
ts.factory.createBinaryExpression(
this.getReferenceToArg(GeneratedDefaultWebsocketImplementation.DEBUG_PROPERTY_NAME),
ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
ts.factory.createFalse()
)
),
ts.factory.createSpreadAssignment(
ts.factory.createPropertyAccessExpression(
ts.factory.createThis(),
GeneratedSdkClientClassImpl.OPTIONS_PRIVATE_MEMBER
)
),
ts.factory.createPropertyAssignment("headers", this.createHeadersObjectExpression())
]);
}

private createHeadersObjectExpression(): ts.Expression {
const headerProperties: ts.ObjectLiteralElementLike[] = [];

// Add args.headers spread
headerProperties.push(
ts.factory.createSpreadAssignment(
this.getReferenceToArg(GeneratedDefaultWebsocketImplementation.HEADERS_PROPERTY_NAME)
)
);

// Add Authorization header if provided: ...(args.Authorization ? { Authorization: args.Authorization } : {})
headerProperties.push(
ts.factory.createSpreadAssignment(
ts.factory.createParenthesizedExpression(
ts.factory.createConditionalExpression(
this.getReferenceToArg("Authorization"),
ts.factory.createToken(ts.SyntaxKind.QuestionToken),
ts.factory.createObjectLiteralExpression([
ts.factory.createPropertyAssignment(
"Authorization",
this.getReferenceToArg("Authorization")
)
]),
ts.factory.createToken(ts.SyntaxKind.ColonToken),
ts.factory.createObjectLiteralExpression()
)
),
ts.factory.createPropertyAssignment(
"maxRetries",
ts.factory.createBinaryExpression(
this.getReferenceToArg(
GeneratedDefaultWebsocketImplementation.RECONNECT_ATTEMPTS_PROPERTY_NAME
),
ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
ts.factory.createNumericLiteral(
GeneratedDefaultWebsocketImplementation.DEFAULT_NUM_RECONNECT_ATTEMPTS
)
)
)
);

return ts.factory.createObjectLiteralExpression(headerProperties, true);
}

private createReconnectingOptionsExpression(): ts.Expression {
return ts.factory.createObjectLiteralExpression([
ts.factory.createPropertyAssignment(
"debug",
ts.factory.createBinaryExpression(
this.getReferenceToArg(GeneratedDefaultWebsocketImplementation.DEBUG_PROPERTY_NAME),
ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
ts.factory.createFalse()
)
),
ts.factory.createPropertyAssignment(
"maxRetries",
ts.factory.createBinaryExpression(
this.getReferenceToArg(GeneratedDefaultWebsocketImplementation.RECONNECT_ATTEMPTS_PROPERTY_NAME),
ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
ts.factory.createNumericLiteral(
GeneratedDefaultWebsocketImplementation.DEFAULT_NUM_RECONNECT_ATTEMPTS
)
)
]),
headers: ts.factory.createIdentifier(GeneratedDefaultWebsocketImplementation.HEADERS_VARIABLE_NAME)
});
)
]);
}

private createReconnectingWebSocketExpression(): ts.Expression {
return ts.factory.createNewExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier("core"),
ts.factory.createIdentifier("ReconnectingWebSocket")
),
undefined,
[
ts.factory.createIdentifier("createWebSocket"), // websocketFactory
ts.factory.createIdentifier("url"), // url variable
ts.factory.createIdentifier("clientOptions"), // clientOptions variable
ts.factory.createIdentifier("reconnectingOptions") // reconnectingOptions variable
]
);
}

private buildFullUrl(url: ts.Expression, channel: WebSocketChannel, context: SdkContext): ts.Expression {
Expand Down
1 change: 1 addition & 0 deletions generators/typescript/sdk/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@fern-typescript/sdk-error-generator": "workspace:*",
"@fern-typescript/sdk-error-schema-generator": "workspace:*",
"@fern-typescript/sdk-inlined-request-schema-generator": "workspace:*",
"@fern-typescript/sdk-client-utils-generator": "workspace:*",
"@fern-typescript/websocket-type-schema-generator": "workspace:*",
"@fern-typescript/type-generator": "workspace:*",
"@fern-typescript/type-reference-converters": "workspace:*",
Expand Down
Loading
Loading