Skip to content
Draft
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
4 changes: 2 additions & 2 deletions packages/rlc-common/src/buildObjectTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,8 @@ function getPropertySignatures(
function isBinaryArray(schema: Schema): boolean {
return Boolean(
isArraySchema(schema) &&
(schema.items?.typeName?.includes("NodeJS.ReadableStream") ||
schema.items?.outputTypeName?.includes("NodeJS.ReadableStream"))
(schema.items?.typeName?.includes("NodeReadableStream") ||
schema.items?.outputTypeName?.includes("NodeReadableStream"))
);
}

Expand Down
19 changes: 19 additions & 0 deletions packages/rlc-common/src/buildParameterTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,25 @@ export function buildParameterTypes(model: RLCModel) {
}
]);
}
// Add NodeReadableStream import if binary types are used in parameters
if (parametersFile.getFullText().includes("NodeReadableStream")) {
const platformTypesModuleSpecifier = model.options?.azureSdkForJs
? "#platform/static-helpers/platform-types.js"
: getImportModuleName(
{
cjsName: `./static-helpers/platform-types`,
esModulesName: `./static-helpers/platform-types.js`
},
model
);
parametersFile.addImportDeclarations([
{
isTypeOnly: true,
namedImports: ["NodeReadableStream"],
moduleSpecifier: platformTypesModuleSpecifier
}
]);
}
return { path: filePath, content: parametersFile.getFullText() };
}

Expand Down
20 changes: 20 additions & 0 deletions packages/rlc-common/src/buildSchemaType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "./buildObjectTypes.js";
import { RLCModel, SchemaContext } from "./interfaces.js";
import { getImportSpecifier } from "./helpers/importsUtil.js";
import { getImportModuleName } from "./helpers/nameConstructors.js";

/**
* Generates types to represent schema definitions in the swagger
Expand Down Expand Up @@ -75,6 +76,25 @@ export function generateModelFiles(
}
]);
}
// Add NodeReadableStream import if binary types are used in models
if (modelsFile.getFullText().includes("NodeReadableStream")) {
const platformTypesModuleSpecifier = model.options?.azureSdkForJs
? "#platform/static-helpers/platform-types.js"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
? "#platform/static-helpers/platform-types.js"
? "#platform/static-helpers/platform-types"

: getImportModuleName(
{
cjsName: `./static-helpers/platform-types`,
esModulesName: `./static-helpers/platform-types.js`
},
model
);
modelsFile.addImportDeclarations([
{
isTypeOnly: true,
namedImports: ["NodeReadableStream"],
moduleSpecifier: platformTypesModuleSpecifier
}
]);
}
return { path: filePath, content: modelsFile.getFullText() };
}
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/typespec-ts/src/utils/modelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ import { getModelNamespaceName } from "./namespaceUtils.js";
import { reportDiagnostic } from "../lib.js";

export const BINARY_TYPE_UNION =
"string | Uint8Array | ReadableStream<Uint8Array> | NodeJS.ReadableStream";
"string | Uint8Array | ReadableStream<Uint8Array> | NodeReadableStream";

export const BINARY_AND_FILE_TYPE_UNION = `${BINARY_TYPE_UNION} | File`;

Expand Down
35 changes: 22 additions & 13 deletions packages/typespec-ts/test/unit/bytesGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ describe("bytes", () => {
parameters?.content!,
`
import type { RequestParameters } from "@azure-rest/core-client";
import type { NodeReadableStream } from "./static-helpers/platform-types.js";

export interface UploadFileViaBodyBodyParam {
/** Value may contain any sequence of octets */
body:
| string
| Uint8Array
| ReadableStream<Uint8Array>
| NodeJS.ReadableStream;
| NodeReadableStream;
}

export interface UploadFileViaBodyMediaTypesParam {
Expand Down Expand Up @@ -59,14 +60,15 @@ describe("bytes", () => {
parameters?.content!,
`
import type { RequestParameters } from "@azure-rest/core-client";
import type { NodeReadableStream } from "./static-helpers/platform-types.js";

export interface UploadFileViaBodyBodyParam {
/** Value may contain any sequence of octets */
body:
| string
| Uint8Array
| ReadableStream<Uint8Array>
| NodeJS.ReadableStream;
| NodeReadableStream;
}

export interface UploadFileViaBodyMediaTypesParam {
Expand Down Expand Up @@ -118,14 +120,15 @@ describe("bytes", () => {
await assertEqualContent(
parameters?.content!,
`import type { RequestParameters } from "@azure-rest/core-client";
import type { NodeReadableStream } from "./static-helpers/platform-types.js";

export interface ReadBodyParam {
/** Value may contain any sequence of octets */
body:
| string
| Uint8Array
| ReadableStream<Uint8Array>
| NodeJS.ReadableStream;
| NodeReadableStream;
}

export type ReadParameters = ReadBodyParam & RequestParameters;
Expand Down Expand Up @@ -188,6 +191,7 @@ describe("bytes", () => {
parameters?.content!,
`
import type { RequestParameters } from "@azure-rest/core-client";
import type { NodeReadableStream } from "./static-helpers/platform-types.js";

export interface UploadFileBodyParam {
/** Value may contain any sequence of octets */
Expand All @@ -201,7 +205,7 @@ describe("bytes", () => {
| string
| Uint8Array
| ReadableStream<Uint8Array>
| NodeJS.ReadableStream
| NodeReadableStream
| File;
filename?: string;
contentType?: string;
Expand All @@ -212,7 +216,7 @@ describe("bytes", () => {
| string
| Uint8Array
| ReadableStream<Uint8Array>
| NodeJS.ReadableStream
| NodeReadableStream
| File;
filename?: string;
contentType?: string;
Expand Down Expand Up @@ -250,6 +254,7 @@ describe("bytes", () => {
parameters?.content!,
`
import type { RequestParameters } from "@azure-rest/core-client";
import type { NodeReadableStream } from "./static-helpers/platform-types.js";

export interface UploadFileBodyParam {
/** Value may contain any sequence of octets */
Expand All @@ -263,7 +268,7 @@ describe("bytes", () => {
| string
| Uint8Array
| ReadableStream<Uint8Array>
| NodeJS.ReadableStream
| NodeReadableStream
| File;
filename?: string;
contentType?: string;
Expand All @@ -274,7 +279,7 @@ describe("bytes", () => {
| string
| Uint8Array
| ReadableStream<Uint8Array>
| NodeJS.ReadableStream
| NodeReadableStream
| File;
filename?: string;
contentType?: string;
Expand Down Expand Up @@ -359,6 +364,8 @@ describe("bytes", () => {
await assertEqualContent(
models.inputModelFile?.content!,
`
import type { NodeReadableStream } from "./static-helpers/platform-types.js";

export interface FooNamePartDescriptor {
name: "name";
body: string;
Expand All @@ -370,7 +377,7 @@ describe("bytes", () => {
| string
| Uint8Array
| ReadableStream<Uint8Array>
| NodeJS.ReadableStream
| NodeReadableStream
| File;
filename?: string;
contentType?: string;
Expand All @@ -382,7 +389,7 @@ describe("bytes", () => {
| string
| Uint8Array
| ReadableStream<Uint8Array>
| NodeJS.ReadableStream
| NodeReadableStream
| File;
filename?: string;
contentType?: string;
Expand All @@ -394,7 +401,7 @@ describe("bytes", () => {
| string
| Uint8Array
| ReadableStream<Uint8Array>
| NodeJS.ReadableStream
| NodeReadableStream
| File;
filename?: string;
contentType?: string;
Expand All @@ -406,7 +413,7 @@ describe("bytes", () => {
| string
| Uint8Array
| ReadableStream<Uint8Array>
| NodeJS.ReadableStream
| NodeReadableStream
| File
| number;
filename?: string;
Expand Down Expand Up @@ -525,10 +532,11 @@ describe("bytes", () => {
`
import type { RequestParameters } from "@azure-rest/core-client";
import type { SchemaContentTypeValues } from "./models.js";
import type { NodeReadableStream } from "./static-helpers/platform-types.js";

export interface ReadBodyParam {
/** Value may contain any sequence of octets */
body: string | Uint8Array | ReadableStream<Uint8Array> | NodeJS.ReadableStream;
body: string | Uint8Array | ReadableStream<Uint8Array> | NodeReadableStream;
}

export interface ReadMediaTypesParam {
Expand All @@ -552,14 +560,15 @@ describe("bytes", () => {
parameters?.content!,
`
import type { RequestParameters } from "@azure-rest/core-client";
import type { NodeReadableStream } from "./static-helpers/platform-types.js";

export interface ReadBodyParam {
/** Value may contain any sequence of octets */
body:
| string
| Uint8Array
| ReadableStream<Uint8Array>
| NodeJS.ReadableStream;
| NodeReadableStream;
}

export interface ReadMediaTypesParam {
Expand Down
Loading