Skip to content
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

Support usage and access #2356

Merged
merged 12 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 1 addition & 1 deletion packages/rlc-common/src/helpers/nameUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const ReservedModelNames: ReservedName[] = [
{ name: "package", reservedFor: [NameType.Parameter] },
{ name: "private", reservedFor: [NameType.Parameter] },
{ name: "protected", reservedFor: [NameType.Parameter] },
{ name: "public", reservedFor: [NameType.Parameter] },
{ name: "public", reservedFor: [NameType.Parameter, NameType.Operation] },
qiaozha marked this conversation as resolved.
Show resolved Hide resolved
{ name: "requestoptions", reservedFor: [NameType.Parameter] },
{ name: "require", reservedFor: [NameType.Parameter] },
{ name: "return", reservedFor: [NameType.Parameter] },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ options:
azureSdkForJs: false
isModularLibrary: true
hierarchyClient: false
generateOrphanModels: true
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any documentation we need to update for this new setting?

Copy link
Member Author

Choose a reason for hiding this comment

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

"emitter-output-dir": "{project-root}/../generated/typespec-ts"
packageDetails:
name: "@msinternal/openai_modular"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export interface ListWidgetsPagesResults {
results: Widget[];
}

// @public (undocumented)
export interface NonReferencedModel {
prop1: number;
prop2: string;
}

// @public
export interface PagedAsyncIterableIterator<TElement, TPage = TElement[], TPageSettings extends PageSettings = PageSettings> {
[Symbol.asyncIterator](): PagedAsyncIterableIterator<TElement, TPage, TPageSettings>;
Expand All @@ -56,6 +62,12 @@ export interface Widget {
weight: number;
}

// @public (undocumented)
export interface WidgetError {
code: number;
message: string;
}

// @public (undocumented)
export interface WidgetsAnalyzeWidgetOptions extends OperationOptions {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ export {
} from "./WidgetServiceClient.js";
export {
Widget,
WidgetError,
ListWidgetsPagesResults,
CreateWidget,
UpdateWidget,
AnalyzeResult,
NonReferencedModel,
WidgetsListWidgetsOptions,
WidgetsListWidgetsPagesOptions,
WidgetsQueryWidgetsPagesOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

export {
Widget,
WidgetError,
ListWidgetsPagesResults,
CreateWidget,
UpdateWidget,
AnalyzeResult,
NonReferencedModel,
} from "./models.js";
export {
WidgetsListWidgetsOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ export interface Widget {
color: "red" | "blue";
}

export interface WidgetError {
/** The HTTP error code. */
code: number;
/** A human-readable message describing the error. */
message: string;
}

export interface ListWidgetsPagesResults {
/** The current page of results. */
results: Widget[];
Expand All @@ -34,3 +41,10 @@ export interface UpdateWidget {
export interface AnalyzeResult {
summary: string;
}

export interface NonReferencedModel {
/** The weight of the widget. This is an int32, but must be greater than zero. */
prop1: number;
/** The color of the widget. */
prop2: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ export {
} from "./WidgetServiceClient.js";
export {
Widget,
WidgetError,
ListWidgetsPagesResults,
CreateWidget,
UpdateWidget,
AnalyzeResult,
NonReferencedModel,
WidgetsListWidgetsOptions,
WidgetsListWidgetsPagesOptions,
WidgetsQueryWidgetsPagesOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

export {
Widget,
WidgetError,
ListWidgetsPagesResults,
CreateWidget,
UpdateWidget,
AnalyzeResult,
NonReferencedModel,
} from "./models.js";
export {
WidgetsListWidgetsOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ export interface Widget {
color: "red" | "blue";
}

export interface WidgetError {
/** The HTTP error code. */
code: number;
/** A human-readable message describing the error. */
message: string;
}

export interface ListWidgetsPagesResults {
/** The current page of results. */
results: Widget[];
Expand All @@ -34,3 +41,10 @@ export interface UpdateWidget {
export interface AnalyzeResult {
summary: string;
}

export interface NonReferencedModel {
/** The weight of the widget. This is an int32, but must be greater than zero. */
prop1: number;
/** The color of the widget. */
prop2: string;
}
8 changes: 8 additions & 0 deletions packages/typespec-test/test/widget_dpg/spec/client.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import "@azure-tools/typespec-client-generator-core";
import "./main.tsp";

using Azure.ClientGenerator.Core;

// Some models from routes with suppressed visibility are still desired for custom public surface.
@@access(WidgetService.NonReferencedModel, Access.public);
@@usage(WidgetService.NonReferencedModel, Usage.input | Usage.output);
qiaozha marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions packages/typespec-test/test/widget_dpg/spec/main.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ model CreateWidget {
color: "red" | "blue";
}

model NonReferencedModel {
@doc("The weight of the widget. This is an int32, but must be greater than zero.")
prop1: int32;
@doc("The color of the widget.")
prop2: string;
}

model UpdateWidget {
@doc("The UUID of this widget. This is generated automatically by the service.")
@path
Expand Down
1 change: 1 addition & 0 deletions packages/typespec-test/test/widget_dpg/tspconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ options:
multiClient: false
azureSdkForJs: false
isModularLibrary: true
generateOrphanModels: true
"emitter-output-dir": "{project-root}/generated/typespec-ts"
packageDetails:
name: "@msinternal/widget_dpg"
Expand Down
4 changes: 3 additions & 1 deletion packages/typespec-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ export async function $onEmit(context: EmitContext) {
buildRootIndex(modularCodeModel, subClient, rootIndexFile);
}

removeUnusedInterfaces(project);
if (!emitterOptions.generateOrphanModels) {
removeUnusedInterfaces(project);
}

for (const file of project.getSourceFiles()) {
await emitContentByBuilder(
Expand Down
2 changes: 2 additions & 0 deletions packages/typespec-ts/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Options } from "prettier";

export interface EmitterOptions extends RLCOptions {
branded?: boolean;
generateOrphanModels?: boolean;
qiaozha marked this conversation as resolved.
Show resolved Hide resolved
}

export const RLCOptionsSchema: JSONSchemaType<EmitterOptions> = {
Expand Down Expand Up @@ -83,6 +84,7 @@ export const RLCOptionsSchema: JSONSchemaType<EmitterOptions> = {
hierarchyClient: { type: "boolean", nullable: true },
branded: { type: "boolean", nullable: true },
flavor: { type: "string", nullable: true },
generateOrphanModels: { type: "boolean", nullable: true },
moduleKind: {
type: "string",
nullable: true,
Expand Down
8 changes: 8 additions & 0 deletions packages/typespec-ts/test/commands/cadl-ranch-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ export const nonBrandedRlcTsps: TypeSpecRanchConfig[] = [
];

export const modularTsps: TypeSpecRanchConfig[] = [
{
outputPath: "azure/clientGeneratorCore/access",
inputPath: "azure/client-generator-core/access"
},
{
outputPath: "azure/clientGeneratorCore/usage",
inputPath: "azure/client-generator-core/usage"
},
{
outputPath: "parameters/body-optionality",
inputPath: "parameters/body-optionality"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,6 @@ describe("Access Client", () => {
}
});

it("should get internal decorator in internal operation", async () => {
try {
const result = await client
.path(
"/azure/client-generator-core/access/internalOperation/internalDecoratorInInternal"
)
.get({ queryParameters: { name: "myname" } });
assert.strictEqual(result.status, "200");
assert.strictEqual(result.body.name, "myname");
} catch (err) {
assert.fail(err as string);
}
});

it("should get public decorator in internal operation", async () => {
try {
const result = await client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
NoDecoratorInInternal200Response,
InternalDecoratorInInternal200Response,
PublicDecoratorInInternal200Response,
Public200Response,
PublicOperation200Response,
Internal200Response,
Operation200Response,
Discriminator200Response,
Expand Down Expand Up @@ -56,7 +56,7 @@ export interface PublicDecoratorInInternal {
}

export interface Public {
get(options: PublicParameters): StreamableMethod<Public200Response>;
get(options: PublicParameters): StreamableMethod<PublicOperation200Response>;
}

export interface Internal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface PublicDecoratorInInternal200Response extends HttpResponse {
}

/** The request has succeeded. */
export interface Public200Response extends HttpResponse {
export interface PublicOperation200Response extends HttpResponse {
status: "200";
body: SharedModelOutput;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { assert } from "chai";
import { AccessClient } from "./generated/azure/clientGeneratorCore/access/src/index.js";
describe("Azure ClientGeneratorCore Access Client", () => {
let client: AccessClient;

beforeEach(() => {
client = new AccessClient({
allowInsecureConnection: true,
retryOptions: {
maxRetries: 0
}
});
});

it("should get no decorator in public operation", async () => {
try {
const result = await client.noDecoratorInPublic("myname");
assert.equal(result.name, "myname");
} catch (err) {
assert.fail(err as string);
}
});

it("should get public decorator in public operation", async () => {
try {
const result = await client.publicDecoratorInPublic("myname");
assert.equal(result.name, "myname");
} catch (err) {
assert.fail(err as string);
}
});

it("should get no decorator in internal operation", async () => {
try {
const result = await client.noDecoratorInInternal("myname");
assert.equal(result.name, "myname");
} catch (err) {
assert.fail(err as string);
}
});

it("should get internal decorator in internal operation", async () => {
try {
const result = await client.internalDecoratorInInternal("myname");
assert.equal(result.name, "myname");
} catch (err) {
assert.fail(err as string);
}
});

it("should get public decorator in internal operation", async () => {
try {
const result = await client.publicDecoratorInInternal("myname");
assert.equal(result.name, "myname");
} catch (err) {
assert.fail(err as string);
}
});

it("should get public shared model in operation", async () => {
try {
const result = await client.publicOperation("myname");
assert.equal(result.name, "myname");
} catch (err) {
assert.fail(err as string);
}
});

it("should get internal shared model in operation", async () => {
try {
const result = await client.internal("myname");
assert.equal(result.name, "myname");
} catch (err) {
assert.fail(err as string);
}
});

it("should get relative model in operation", async () => {
try {
const result = await client.operation("myname");
assert.equal(result.name, "Madge");
assert.equal(result.inner.name, "Madge");
} catch (err) {
assert.fail(err as string);
}
});

it("should get relative model in discriminator", async () => {
try {
const result = await client.discriminator("myname");
assert.equal(result.name, "Madge");
assert.equal(result.kind, "real");
} catch (err) {
assert.fail(err as string);
}
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { assert } from "chai";
import { UsageClient } from "./generated/azure/clientGeneratorCore/usage/src/index.js";
describe("Azure ClientGeneratorCore Usage Client", () => {
let client: UsageClient;

beforeEach(() => {
client = new UsageClient({
allowInsecureConnection: true,
retryOptions: {
maxRetries: 0
}
});
});

it("should post input usage model in operation", async () => {
try {
const result = await client.inputToInputOutput({ name: "Madge" });
assert.isUndefined(result);
} catch (err) {
assert.fail(err as string);
}
});

it("should get usage model in operation", async () => {
try {
const result = await client.outputToInputOutput();
assert.strictEqual(result.name, "Madge");
} catch (err) {
assert.fail(err as string);
}
});
});
Loading
Loading