Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
3 changes: 2 additions & 1 deletion packages/typespec-ts/src/modular/buildCodeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Model,
ModelProperty,
Namespace,
NoTarget,
Operation,
Program,
Scalar,
Expand Down Expand Up @@ -1260,7 +1261,7 @@ function emitStdScalar(
format: {
propertyName: newScalar?.name ?? ""
},
target: newScalar ?? scalar
target: NoTarget
Comment thread
MaryGao marked this conversation as resolved.
});
return { type: "integer", format: newScalar.format };
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export function getTypeExpression(
case "uint8":
case "numeric":
case "safeint":
if (type.encode === "string") {
return "string";
}
return "number";
case "endpoint":
case "plainDate":
Expand Down
4 changes: 2 additions & 2 deletions packages/typespec-ts/src/utils/modelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ function getSchemaForStdScalar(
format: {
propertyName: relevantProperty?.name ?? ""
},
target: relevantProperty ?? type
target: relevantProperty ?? NoTarget
});
return applyIntrinsicDecorators(program, type, {
type: "number",
Expand All @@ -1243,7 +1243,7 @@ function getSchemaForStdScalar(
format: {
propertyName: relevantProperty?.name ?? ""
},
target: relevantProperty ?? type
target: relevantProperty ?? NoTarget
});
return applyIntrinsicDecorators(program, type, {
type: "number",
Expand Down
8 changes: 8 additions & 0 deletions packages/typespec-ts/test/commands/cadl-ranch-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ export const rlcTsps = [
{
outputPath: "client/structure/client-operation-group",
inputPath: "client/structure/client-operation-group"
},
{
outputPath: "encode/numeric",
inputPath: "encode/numeric"
}
];

Expand Down Expand Up @@ -562,6 +566,10 @@ export const modularTsps = [
{
outputPath: "client/structure/client-operation-group",
inputPath: "client/structure/client-operation-group"
},
{
outputPath: "encode/numeric",
inputPath: "encode/numeric"
}
];

Expand Down
47 changes: 47 additions & 0 deletions packages/typespec-ts/test/integration/encodeNumeric.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { assert } from "chai";
import EncodeNumericClientFactory, {
NumericClient
} from "./generated/encode/numeric/src/index.js";
describe("EncodeNumericClient Rest Client", () => {
let client: NumericClient;

beforeEach(() => {
client = EncodeNumericClientFactory({
allowInsecureConnection: true
});
});

describe("property", () => {
it(`should post safeint property`, async () => {
const result = await client
.path(`/encode/numeric/property/safeint`)
.post({
body: {
value: "10000000000"
}
});
assert.strictEqual(result.status, "200");
assert.strictEqual(result.body.value, "10000000000");
});

it(`should post uint32 property`, async () => {
const result = await client.path(`/encode/numeric/property/uint32`).post({
body: {
value: "1"
}
});
assert.strictEqual(result.status, "200");
assert.strictEqual(result.body.value, "1");
});

it(`should post uint8 property`, async () => {
const result = await client.path(`/encode/numeric/property/uint8`).post({
body: {
value: "255"
}
});
assert.strictEqual(result.status, "200");
assert.strictEqual(result.body.value, "255");
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
!/src
/src/**
!/src/index.d.ts
!/.gitignore
!/tspconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Client } from '@azure-rest/core-client';
import { ClientOptions } from '@azure-rest/core-client';
import { HttpResponse } from '@azure-rest/core-client';
import { RequestParameters } from '@azure-rest/core-client';
import { StreamableMethod } from '@azure-rest/core-client';

declare function createClient(options?: NumericClientOptions): NumericClient;
export default createClient;

export declare type NumericClient = Client & {
path: Routes;
};

export declare interface NumericClientOptions extends ClientOptions {
}

export declare interface PropertySafeintAsString200Response extends HttpResponse {
status: "200";
body: SafeintAsStringPropertyOutput;
}

export declare interface PropertySafeintAsStringBodyParam {
body: SafeintAsStringProperty;
}

export declare type PropertySafeintAsStringParameters = PropertySafeintAsStringBodyParam & RequestParameters;

export declare interface PropertyUint32AsStringOptional200Response extends HttpResponse {
status: "200";
body: Uint32AsStringPropertyOutput;
}

export declare interface PropertyUint32AsStringOptionalBodyParam {
body: Uint32AsStringProperty;
}

export declare type PropertyUint32AsStringOptionalParameters = PropertyUint32AsStringOptionalBodyParam & RequestParameters;

export declare interface PropertyUint8AsString200Response extends HttpResponse {
status: "200";
body: Uint8AsStringPropertyOutput;
}

export declare interface PropertyUint8AsStringBodyParam {
body: Uint8AsStringProperty;
}

export declare type PropertyUint8AsStringParameters = PropertyUint8AsStringBodyParam & RequestParameters;

export declare interface Routes {
(path: "/encode/numeric/property/safeint"): SafeintAsString;
(path: "/encode/numeric/property/uint32"): Uint32AsStringOptional;
(path: "/encode/numeric/property/uint8"): Uint8AsString;
}

export declare interface SafeintAsString {
post(options: PropertySafeintAsStringParameters): StreamableMethod<PropertySafeintAsString200Response>;
}

export declare interface SafeintAsStringProperty {
value: string;
}

export declare interface SafeintAsStringPropertyOutput {
value: string;
}

export declare interface Uint32AsStringOptional {
post(options: PropertyUint32AsStringOptionalParameters): StreamableMethod<PropertyUint32AsStringOptional200Response>;
}

export declare interface Uint32AsStringProperty {
value?: string;
}

export declare interface Uint32AsStringPropertyOutput {
value?: string;
}

export declare interface Uint8AsString {
post(options: PropertyUint8AsStringParameters): StreamableMethod<PropertyUint8AsString200Response>;
}

export declare interface Uint8AsStringProperty {
value: string;
}

export declare interface Uint8AsStringPropertyOutput {
value: string;
}

export { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
emit:
- "@azure-tools/typespec-ts"
options:
"@azure-tools/typespec-ts":
"emitter-output-dir": "{project-root}"
generateMetadata: true
addCredentials: false
azureSdkForJs: false
isTypeSpecTest: true
enableOperationGroup: true
packageDetails:
name: "@msinternal/encode-numeric"
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { assert } from "chai";
import { NumericClient } from "./generated/encode/numeric/src/index.js";
describe("EncodeNumericClient Rest Client", () => {
let client: NumericClient;

beforeEach(() => {
client = new NumericClient({
endpoint: "http://localhost:3002",
allowInsecureConnection: true,
retryOptions: {
maxRetries: 0
}
});
});

describe("query", () => {
it(`should get safeint numeric`, async () => {
const result = await client.property.safeintAsString({
value: "10000000000"
});
assert.strictEqual(result.value, "10000000000");
});

it(`should get uint32 numeric`, async () => {
const result = await client.property.uint32AsStringOptional({
value: "1"
});
assert.strictEqual(result.value, "1");
});

it(`should get uint8 numeric`, async () => {
const result = await client.property.uint8AsString({ value: "255" });
assert.strictEqual(result.value, "255");
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
!/src
/src/**
!/src/index.d.ts
!/.gitignore
!/tspconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ClientOptions } from '@azure-rest/core-client';
import { OperationOptions } from '@azure-rest/core-client';
import { Pipeline } from '@azure/core-rest-pipeline';

export declare class NumericClient {
private _client;
readonly pipeline: Pipeline;
constructor(options?: NumericClientOptionalParams);
readonly property: PropertyOperations;
}

export declare interface NumericClientOptionalParams extends ClientOptions {
}

export declare interface PropertyOperations {
safeintAsString: (value: SafeintAsStringProperty, options?: PropertySafeintAsStringOptionalParams) => Promise<SafeintAsStringProperty>;
uint32AsStringOptional: (value: Uint32AsStringProperty, options?: PropertyUint32AsStringOptionalOptionalParams) => Promise<Uint32AsStringProperty>;
uint8AsString: (value: Uint8AsStringProperty, options?: PropertyUint8AsStringOptionalParams) => Promise<Uint8AsStringProperty>;
}

export declare interface PropertySafeintAsStringOptionalParams extends OperationOptions {
}

export declare interface PropertyUint32AsStringOptionalOptionalParams extends OperationOptions {
}

export declare interface PropertyUint8AsStringOptionalParams extends OperationOptions {
}

export declare interface SafeintAsStringProperty {
value: string;
}

export declare interface Uint32AsStringProperty {
value?: string;
}

export declare interface Uint8AsStringProperty {
value: string;
}

export { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
emit:
- "@azure-tools/typespec-ts"
options:
"@azure-tools/typespec-ts":
"emitter-output-dir": "{project-root}"
generateMetadata: true
generateTest: false
addCredentials: false
azureSdkForJs: false
isTypeSpecTest: true
enableOperationGroup: true
isModularLibrary: true
hierarchyClient: false
packageDetails:
name: "@msinternal/encode-numeric"
10 changes: 5 additions & 5 deletions packages/typespec-ts/test/modularUnit/scenarios.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ type EmitterFunction = (
const OUTPUT_CODE_BLOCK_TYPES: Record<string, EmitterFunction> = {
// Snapshot of a particular interface named {name} in the models file
"(ts|typescript) models interface {name}": async (tsp, { name }, namedUnknownArgs) => {
const configs = namedUnknownArgs ? (namedUnknownArgs["configs"] as Record<string, string>): {};
const configs = namedUnknownArgs ? (namedUnknownArgs["configs"] as Record<string, string>) : {};
const result = await emitModularModelsFromTypeSpec(tsp, configs);
return result!.getInterfaceOrThrow(name ?? "No name specified!").getText();
},

// Snapshot of a particular function named {name} in the models file
"(ts|typescript) models function {name}": async (tsp, { name }, namedUnknownArgs) => {
const configs = namedUnknownArgs ? (namedUnknownArgs["configs"] as Record<string, string>): {};
const configs = namedUnknownArgs ? (namedUnknownArgs["configs"] as Record<string, string>) : {};
const result = await emitModularModelsFromTypeSpec(tsp, configs);

if (result === undefined) {
Expand All @@ -48,8 +48,8 @@ const OUTPUT_CODE_BLOCK_TYPES: Record<string, EmitterFunction> = {
},

// Snapshot of the entire models file
"(ts|typescript) models": async (tsp, {}, namedUnknownArgs) => {
const configs = namedUnknownArgs ? (namedUnknownArgs["configs"] as Record<string, string>): {};
"(ts|typescript) models": async (tsp, { }, namedUnknownArgs) => {
const configs = namedUnknownArgs ? (namedUnknownArgs["configs"] as Record<string, string>) : {};
const result = await emitModularModelsFromTypeSpec(tsp, configs);

if (result === undefined) {
Expand Down Expand Up @@ -104,7 +104,7 @@ const OUTPUT_CODE_BLOCK_TYPES: Record<string, EmitterFunction> = {
return result![0]!.getFunctionOrThrow(name!).getText();
},

"(ts|typescript) samples": async (tsp, {}, namedUnknownArgs) => {
"(ts|typescript) samples": async (tsp, { }, namedUnknownArgs) => {
if (!namedUnknownArgs || !namedUnknownArgs["examples"]) {
throw new Error(`Expected 'examples' to be passed in as an argument`);
}
Expand Down
Loading