Skip to content

Commit

Permalink
(fix, typescript): multipart form upload on Node 19+ (#4056)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Jul 16, 2024
1 parent 189d1ae commit 6eb4e6b
Show file tree
Hide file tree
Showing 428 changed files with 2,325 additions and 2,282 deletions.
73 changes: 60 additions & 13 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
12 changes: 12 additions & 0 deletions generators/typescript/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.33.0] - 2024-07-15

- Fix: This release comes with numerous improvements to multipart uploads:

1. `Fetcher.ts` no longer depends on form-data and formdata-node which reduces
the size of the SDK for all consumers that are not leveraging multipart form
data uploads.
2. The SDK now accepts `fs.ReadStream`, `Blob` and `File` as inputs and handles
parsing them appropriately.
3. By accepting a `Blob` as a file parameter, the SDK now supports sending the
filename when making a request.

## [0.32.0] - 2024-07-15

- Feature: The `reference.md` is now generated for every SDK.
Expand Down
2 changes: 1 addition & 1 deletion generators/typescript/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.32.0
0.33.0
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,13 @@ export class GeneratedDefaultEndpointRequest implements GeneratedEndpointRequest

public getFetcherRequestArgs(
context: SdkContext
): Pick<Fetcher.Args, "headers" | "queryParameters" | "body" | "contentType"> {
): Pick<Fetcher.Args, "headers" | "queryParameters" | "body" | "contentType" | "requestType"> {
return {
headers: this.getHeaders(context),
queryParameters: this.queryParams.getReferenceTo(context),
body: this.getSerializedRequestBodyWithNullCheck(context),
contentType: "application/json"
contentType: "application/json",
requestType: "json"
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface GeneratedEndpointRequest {
getEndpointParameters(context: SdkContext): OptionalKind<ParameterDeclarationStructure & { docs?: string }>[];
getFetcherRequestArgs: (
context: SdkContext
) => Pick<Fetcher.Args, "headers" | "queryParameters" | "body" | "contentType">;
) => Pick<Fetcher.Args, "headers" | "queryParameters" | "body" | "contentType" | "requestType">;
getReferenceToRequestBody: (context: SdkContext) => ts.Expression | undefined;
getReferenceToQueryParameter: (queryParameterKey: string, context: SdkContext) => ts.Expression;
getExampleEndpointParameters({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ export class GeneratedFileUploadEndpointRequest implements GeneratedEndpointRequ
value: context.externalDependencies.fs.ReadStream._getReferenceToType()
})
);

types.push(
this.maybeWrapFileArray({
property,
value: context.externalDependencies.blob.Blob._getReferenceToType()
})
);
},
browser: () => {
types.push(
Expand Down Expand Up @@ -252,7 +259,7 @@ export class GeneratedFileUploadEndpointRequest implements GeneratedEndpointRequ
GeneratedFileUploadEndpointRequest.FORM_DATA_VARIABLE_NAME,
undefined,
undefined,
context.coreUtilities.formDataUtils._instantiate()
context.coreUtilities.formDataUtils.newFormData()
)
],
ts.NodeFlags.Const
Expand Down Expand Up @@ -282,11 +289,13 @@ export class GeneratedFileUploadEndpointRequest implements GeneratedEndpointRequ
GeneratedFileUploadEndpointRequest.FORM_DATA_REQUEST_OPTIONS_VARIABLE_NAME,
undefined,
undefined,
context.coreUtilities.formDataUtils.getRequest({
referencetoFormData: ts.factory.createIdentifier(
GeneratedFileUploadEndpointRequest.FORM_DATA_VARIABLE_NAME
)
})
ts.factory.createAwaitExpression(
context.coreUtilities.formDataUtils.getRequest({
referencetoFormData: ts.factory.createIdentifier(
GeneratedFileUploadEndpointRequest.FORM_DATA_VARIABLE_NAME
)
})
)
)
],
ts.NodeFlags.Const
Expand All @@ -299,12 +308,13 @@ export class GeneratedFileUploadEndpointRequest implements GeneratedEndpointRequ

public getFetcherRequestArgs(
context: SdkContext
): Pick<Fetcher.Args, "headers" | "queryParameters" | "body" | "contentType"> {
): Pick<Fetcher.Args, "headers" | "queryParameters" | "body" | "contentType" | "requestType"> {
return {
headers: this.getHeaders(context),
queryParameters: this.queryParams != null ? this.queryParams.getReferenceTo(context) : undefined,
requestType: "file",
body: context.coreUtilities.formDataUtils.getBody({
referencetoFormDataRequest: ts.factory.createIdentifier(
referencetoFormData: ts.factory.createIdentifier(
GeneratedFileUploadEndpointRequest.FORM_DATA_REQUEST_OPTIONS_VARIABLE_NAME
)
})
Expand All @@ -321,7 +331,7 @@ export class GeneratedFileUploadEndpointRequest implements GeneratedEndpointRequ
endpoint: this.endpoint,
additionalSpreadHeaders: [
context.coreUtilities.formDataUtils.getHeaders({
referencetoFormDataRequest: ts.factory.createIdentifier(
referencetoFormData: ts.factory.createIdentifier(
GeneratedFileUploadEndpointRequest.FORM_DATA_REQUEST_OPTIONS_VARIABLE_NAME
)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function appendPropertyToFormData({
file: (property) => {
const FOR_LOOP_ITEM_VARIABLE_NAME = "_file";

let statement = context.coreUtilities.formDataUtils.append({
let statement = context.coreUtilities.formDataUtils.appendFile({
referencetoFormData: referenceToFormData,
key: property.key.wireValue,
value: ts.factory.createIdentifier(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export declare namespace Fetcher {
withCredentials: boolean;
timeoutInSeconds: ts.Expression;
maxRetries?: ts.Expression;
requestType?: "json" | "file" | "bytes" | "other";
responseType?: "json" | "blob" | "streaming" | "text";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class FetcherImpl extends CoreUtility implements Fetcher {
body: "body",
timeoutMs: "timeoutMs",
withCredentials: "withCredentials",
requestType: "requestType",
responseType: "responseType",
abortSignal: "abortSignal"
},
Expand Down Expand Up @@ -106,6 +107,14 @@ export class FetcherImpl extends CoreUtility implements Fetcher {
)
);
}
if (args.requestType != null && args.responseType !== "json") {
properties.push(
ts.factory.createPropertyAssignment(
this.Fetcher.Args.properties.requestType,
ts.factory.createStringLiteral(args.requestType)
)
);
}
if (args.body != null) {
properties.push(ts.factory.createPropertyAssignment(this.Fetcher.Args.properties.body, args.body));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { ts } from "ts-morph";

export interface FormDataUtils {
_instantiate: () => ts.NewExpression;
newFormData: () => ts.AwaitExpression;

append: (args: { referencetoFormData: ts.Expression; key: string; value: ts.Expression }) => ts.Statement;
appendFile: (args: {
referencetoFormData: ts.Expression;
key: string;
value: ts.Expression;
filename?: ts.Expression;
}) => ts.Statement;

getBody: (args: { referencetoFormData: ts.Expression }) => ts.Expression;
getHeaders: (args: { referencetoFormData: ts.Expression }) => ts.Expression;
getRequest: (args: { referencetoFormData: ts.Expression }) => ts.Expression;
getBody: (args: { referencetoFormDataRequest: ts.Expression }) => ts.Expression;
getHeaders: (args: { referencetoFormDataRequest: ts.Expression }) => ts.Expression;
}
Loading

0 comments on commit 6eb4e6b

Please sign in to comment.