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
27 changes: 1 addition & 26 deletions packages/typespec-ts/src/framework/hooks/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,13 @@ class BinderImp implements Binder {
private project: Project;
private dependencies: Record<string, ReferenceableSymbol>;
private staticHelpers: Map<string, StaticHelperMetadata>;
private useSubpathImports: boolean;

constructor(project: Project, options: BinderOptions = {}) {
this.project = project;

provideDependencies(options.dependencies);
this.staticHelpers = options.staticHelpers ?? new Map();
this.dependencies = useDependencies();
this.useSubpathImports = options.useSubpathImports ?? false;
}

trackDeclaration(refkey: unknown, name: string, sourceFile: SourceFile): string {
Expand Down Expand Up @@ -185,27 +183,6 @@ class BinderImp implements Binder {
return importSpecifier;
}

/**
* Returns the #platform/ subpath import specifier for a static helper file
* that has a polyfill variant (-browser.mts or -react-native.mts sibling),
* or undefined if subpath imports are disabled or no variant exists.
* e.g. "src/static-helpers/serialization/get-binary-response.ts"
* -> "#platform/static-helpers/serialization/get-binary-response"
*/
private getPlatformImportSpecifier(declarationSourceFile: SourceFile): string | undefined {
if (!this.useSubpathImports) return undefined;
const filePath = declarationSourceFile.getFilePath();
const srcIndex = filePath.indexOf("/src/");
if (srcIndex === -1) return undefined;
// Check if a -browser.mts or -react-native.mts sibling exists
const basePath = filePath.replace(/\.ts$/, "");
const hasBrowserVariant = this.project.getSourceFile(basePath + "-browser.mts");
const hasReactNativeVariant = this.project.getSourceFile(basePath + "-react-native.mts");
if (!hasBrowserVariant && !hasReactNativeVariant) return undefined;
const relativePath = filePath.substring(srcIndex + "/src/".length);
return "#platform/" + relativePath.replace(/\.ts$/, "");
}

/**
* Applies all tracked imports to their respective source files.
*/
Expand Down Expand Up @@ -316,9 +293,7 @@ class BinderImp implements Binder {

if (file !== declarationSourceFile) {
this.trackReference(declarationKey, file);
// Use #platform/ subpath import specifier for static helpers in warp packages
const platformSpecifier = this.getPlatformImportSpecifier(declarationSourceFile);
const importTarget = platformSpecifier ?? declarationSourceFile;
const importTarget = declarationSourceFile;
const importDec = this.addImport(file, importTarget, name);
name = importDec.alias ?? name;
}
Expand Down
17 changes: 1 addition & 16 deletions packages/typespec-ts/src/framework/load-static-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ModularEmitterOptions } from "../modular/interfaces.js";
import { resolveProjectRoot } from "../utils/resolve-project-root.js";
import { refkey } from "./refkey.js";
export const SourceFileSymbol = Symbol("SourceFile");

export interface StaticHelperMetadata {
name: string;
kind: "function" | "interface" | "typeAlias" | "class" | "enum";
Expand Down Expand Up @@ -94,22 +95,6 @@ export async function loadStaticHelpers(
const addedFile = project.createSourceFile(targetPath, contents, {
overwrite: true,
});
addedFile.getImportDeclarations().map((i) => {
// Rewrite relative platform-types imports to #platform/ specifiers
// so that browser/react-native variants are resolved via subpath imports.
// Only rewrite imports to the default variant (not -browser/-react-native variants
// which are already platform-specific direct imports).
const specifier = i.getModuleSpecifierValue();
if (
specifier.startsWith(".") &&
specifier.includes("platform-types") &&
!specifier.includes("-browser") &&
!specifier.includes("-react-native")
) {
i.setModuleSpecifier("#platform/static-helpers/platform-types");
}
});

for (const entry of Object.values(helpers)) {
if (!addedFile.getFilePath().endsWith(entry.location)) {
continue;
Expand Down
3 changes: 0 additions & 3 deletions packages/typespec-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
CreateRecorderHelpers,
MultipartHelpers,
PagingHelpers,
PlatformTypeHelpers,
PollingHelpers,
SerializationHelpers,
SimplePollerHelpers,
Expand Down Expand Up @@ -139,7 +138,6 @@ export async function $onEmit(context: EmitContext) {
...SimplePollerHelpers,
...UrlTemplateHelpers,
...MultipartHelpers,
...PlatformTypeHelpers,
...CloudSettingHelpers,
...XmlHelpers,
...(resolvedEmitterOptions.generateTest ? CreateRecorderHelpers : {}),
Expand All @@ -165,7 +163,6 @@ export async function $onEmit(context: EmitContext) {
dependencies: {
...extraDependencies,
},
useSubpathImports: true,
});
provideSdkTypes(dpgContext);

Expand Down
11 changes: 8 additions & 3 deletions packages/typespec-ts/src/metadata/build-package-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,6 @@ export function updatePackageFile(
// Update Core Client dependency
if (needsCoreClientUpdate) {
delete deps["@azure/core-client"];
if (!("@azure-rest/core-client" in deps)) {
deps["@azure-rest/core-client"] = "^2.3.1";
}
packageInfo.dependencies = deps;
}

Expand Down Expand Up @@ -161,6 +158,14 @@ export function updatePackageFile(
metadata.constantPaths = [...nonUserAgentPaths, ...newUserAgentPaths];
}

// Always update @azure/core-rest-pipeline and @azure-rest/core-client to the latest
// versions because our imports rely on APIs from those latest package versions.
packageInfo.dependencies = {
...packageInfo.dependencies,
"@azure/core-rest-pipeline": "^1.24.0",
"@azure-rest/core-client": "^2.7.0",
};

return {
path: "package.json",
content: JSON.stringify(packageInfo, null, 2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ export function getAzureMonorepoDependencies(config: AzureMonorepoInfoConfig) {
// revert this change after sdk repo update.
const runtimeDeps = {
...dependencies,
"@azure-rest/core-client": "^2.3.1",
"@azure-rest/core-client": "^2.7.0",
...(hasLro && {
"@azure/abort-controller": "^2.1.2",
}),
"@azure/core-auth": "^1.9.0",
...(hasLro && {
"@azure/core-lro": "^3.1.0",
}),
"@azure/core-rest-pipeline": "^1.20.0",
"@azure/core-rest-pipeline": "^1.24.0",
"@azure/core-util": "^1.12.0",
"@azure/logger": "^1.2.0",
tslib: "catalog:",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,11 @@ function getEntryPointInformation(config: PackageCommonInfoConfig) {
module: "./dist/esm/index.js",
types: "./dist/commonjs/index.d.ts",
browser: "./dist/browser/index.js",
imports: {
"#platform/*": {
browser: "./src/*-browser.mts",
default: "./src/*.ts",
} as Record<string, string>,
},
exports: resolveWarpExports(config.exports, config.generateReactNativeTarget),
};

if (config.generateReactNativeTarget) {
result["react-native"] = "./dist/react-native/index.js";
(result["imports"]["#platform/*"] as Record<string, string>)["react-native"] =
"./src/*-react-native.mts";
// Reorder so react-native comes before default
const importsEntry = result["imports"]["#platform/*"] as Record<string, string>;
result["imports"]["#platform/*"] = {
browser: importsEntry["browser"],
"react-native": importsEntry["react-native"],
default: importsEntry["default"],
};
}

return result;
Expand Down
10 changes: 3 additions & 7 deletions packages/typespec-ts/src/modular/build-root-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ import { SdkContext } from "../utils/interfaces.js";
import { NameType, normalizeName } from "../utils/name-utils.js";
import { getMethodHierarchiesMap } from "../utils/operation-util.js";
import { partitionAndEmitExports } from "./build-subpath-index.js";
import { AzureCoreDependencies } from "./external-dependencies.js";
import { getClassicalClientName } from "./helpers/naming-helpers.js";
import { isLroOnlyOperation } from "./helpers/operation-helpers.js";
import { ModularEmitterOptions } from "./interfaces.js";
import {
CloudSettingHelpers,
MultipartHelpers,
PagingHelpers,
PlatformTypeHelpers,
} from "./static-helpers-metadata.js";
import { CloudSettingHelpers, MultipartHelpers, PagingHelpers } from "./static-helpers-metadata.js";

export function buildRootIndex(
context: SdkContext,
Expand Down Expand Up @@ -179,7 +175,7 @@ function exportFileContentsType(context: SdkContext, rootIndexFile: SourceFile)
rootIndexFile,
[
resolveReference(MultipartHelpers.FileContents),
resolveReference(PlatformTypeHelpers.NodeReadableStream),
resolveReference(AzureCoreDependencies["NodeReadableStream"]),
],
true,
);
Expand Down
10 changes: 10 additions & 0 deletions packages/typespec-ts/src/modular/external-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ export const AzureCoreDependencies: CoreDependencies = {
name: "ErrorResponse",
module: "@azure-rest/core-client",
},
getBinaryStreamResponse: {
kind: "externalDependency",
module: "@azure-rest/core-client",
name: "getBinaryStreamResponse",
},
NodeReadableStream: {
kind: "externalDependency",
module: "@azure/core-rest-pipeline",
name: "NodeReadableStream",
},
};

export const AzureIdentityDependencies = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
KnownCollectionFormat,
ServiceOperation,
} from "../../utils/operation-util.js";
import { AzurePollingDependencies } from "../external-dependencies.js";
import { AzureCoreDependencies, AzurePollingDependencies } from "../external-dependencies.js";
import {
buildModelDeserializer,
buildPropertyDeserializer,
Expand All @@ -70,7 +70,6 @@ import {
} from "../serialization/serialize-utils.js";
import {
PagingHelpers,
PlatformTypeHelpers,
PollingHelpers,
SerializationHelpers,
StorageCompatHelpers,
Expand Down Expand Up @@ -1043,7 +1042,7 @@ export function getOperationFunction(
statements.push(`const ${streamableMethodVarName} = _${name}Send(${sendParameterList});`);
const binaryHelper =
wrapReturn && wrapReturnIsBinary
? SerializationHelpers.getBinaryStreamResponse
? AzureCoreDependencies["getBinaryStreamResponse"]
: SerializationHelpers.getBinaryResponse;
statements.push(
`const ${resultVarName} = await ${resolveReference(binaryHelper)}(${streamableMethodVarName});`,
Expand Down Expand Up @@ -3003,7 +3002,7 @@ export function buildNonModelResponseTypeDeclaration(
let typeBody: string;

if (isBinary) {
const nodeReadableStreamRef = resolveReference(PlatformTypeHelpers.NodeReadableStream);
const nodeReadableStreamRef = resolveReference(AzureCoreDependencies["NodeReadableStream"]);
typeBody = `{
/**
* BROWSER ONLY
Expand Down
13 changes: 0 additions & 13 deletions packages/typespec-ts/src/modular/static-helpers-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ export const SerializationHelpers = {
name: "getBinaryResponse",
location: "serialization/get-binary-response.ts",
},
getBinaryStreamResponse: {
kind: "function",
name: "getBinaryStreamResponse",
location: "serialization/get-binary-stream-response.ts",
},
areAllPropsUndefined: {
kind: "function",
name: "areAllPropsUndefined",
Expand Down Expand Up @@ -150,14 +145,6 @@ export const MultipartHelpers = {
},
} as const;

export const PlatformTypeHelpers = {
NodeReadableStream: {
kind: "typeAlias",
name: "NodeReadableStream",
location: "platform-types.ts",
},
} as const;

export const CloudSettingHelpers = {
AzureClouds: {
kind: "enum",
Expand Down
2 changes: 1 addition & 1 deletion packages/typespec-ts/src/utils/imports-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function buildRuntimeImports(): Imports {
restClient: {
type: "restClient",
specifier: "@azure-rest/core-client",
version: "^2.0.0",
version: "^2.7.0",
},
coreAuth: {
type: "coreAuth",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodeReadableStream } from "./platform-types.js";
import { NodeReadableStream } from "@azure/core-rest-pipeline";

/**
* Valid values for the contents of a binary file.
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading