Skip to content

Commit

Permalink
Update rest init template to include emitters and basic code (#5716)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin authored Jan 23, 2025
1 parent e654ba1 commit 9517d45
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@typespec/compiler"
---

Updated Rest init template to include additional emitters(client, server) and a basic sample.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ coverage.xml
.pytest_cache/
test-results.xml
test-results/
__snapshots__/

# Translations
*.mo
Expand Down
48 changes: 45 additions & 3 deletions packages/compiler/.scripts/build-init-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,52 @@ const builtInTemplates: Record<string, InitTemplate> = {
title: "Generic REST API",
description: "Create a project representing a generic REST API",
compilerVersion: minCompilerVersion,
libraries: ["@typespec/http", "@typespec/rest", "@typespec/openapi3"],
config: {
emit: ["@typespec/openapi3"],
libraries: ["@typespec/http", "@typespec/rest", "@typespec/openapi", "@typespec/openapi3"],
emitters: {
"@typespec/openapi3": {
selected: true,
options: {
"emitter-output-dir": "{output-dir}/schema",
},
},
"@typespec/http-client-csharp": {
description: "CSharp Client emitter",
options: {
"emitter-output-dir": "{output-dir}/clients/csharp",
},
},
"@typespec/http-client-java": {
description: "Java Client emitter",
options: {
"emitter-output-dir": "{output-dir}/clients/java",
},
},
"@typespec/http-client-js": {
description: "JavaScript Client emitter",
options: {
"emitter-output-dir": "{output-dir}/clients/js",
},
},
"@typespec/http-client-python": {
description: "Python Client emitter",
options: {
"emitter-output-dir": "{output-dir}/clients/python",
},
},
"@typespec/http-server-csharp": {
description: "CSharp server stubs",
options: {
"emitter-output-dir": "{output-dir}/server",
},
},
"@typespec/http-server-js": {
description: "Javascript server stubs",
options: {
"emitter-output-dir": "{output-dir}/server",
},
},
},
files: [...(await localDir("rest"))],
},
"library-ts": {
title: "TypeSpec Library (With TypeScript)",
Expand Down
9 changes: 9 additions & 0 deletions packages/compiler/templates/__snapshots__/rest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# MacOS
.DS_Store

# Default TypeSpec output
tsp-output/
dist/

# Dependency directories
node_modules/
40 changes: 40 additions & 0 deletions packages/compiler/templates/__snapshots__/rest/main.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import "@typespec/http";

using TypeSpec.Http;
@service({
title: "Widget Service",
})
namespace DemoService;

model Widget {
@visibility("read", "update")
@path
id: string;

weight: int32;
color: "red" | "blue";
}

@error
model Error {
code: int32;
message: string;
}

@route("/widgets")
@tag("Widgets")
interface Widgets {
/** List widgets */
@get list(): Widget[] | Error;
/** Read widgets */
@get read(@path id: string): Widget | Error;
/** Create a widget */
@post create(...Widget): Widget | Error;
/** Update a widget */
@patch update(...Widget): Widget | Error;
/** Delete a widget */
@delete delete(@path id: string): void | Error;

/** Analyze a widget */
@route("{id}/analyze") @post analyze(@path id: string): string | Error;
}
20 changes: 20 additions & 0 deletions packages/compiler/templates/__snapshots__/rest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "rest",
"version": "0.1.0",
"type": "module",
"peerDependencies": {
"@typespec/compiler": "latest",
"@typespec/http": "latest",
"@typespec/rest": "latest",
"@typespec/openapi": "latest",
"@typespec/openapi3": "latest"
},
"devDependencies": {
"@typespec/compiler": "latest",
"@typespec/http": "latest",
"@typespec/rest": "latest",
"@typespec/openapi": "latest",
"@typespec/openapi3": "latest"
},
"private": true
}
16 changes: 16 additions & 0 deletions packages/compiler/templates/__snapshots__/rest/tspconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# extends: ../tspconfig.yaml # Extend another config file
# emit: # Emitter name
# - "<emitter-name"
# options: # Emitter options
# <emitter-name>:
# "<option-name>": "<option-value>"
# environment-variables: # Environment variables which can be used to interpolate emitter options
# <variable-name>:
# default: "<variable-default>"
# parameters: # Parameters which can be used to interpolate emitter options
# <param-name>:
# default: "<param-default>"
# trace: # Trace areas to enable tracing
# - "<trace-name>"
# warn-as-error: true # Treat warnings as errors
# output-dir: "{project-root}/_generated" # Configure the base output directory for all emitters
40 changes: 40 additions & 0 deletions packages/compiler/templates/rest/main.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import "@typespec/http";

using TypeSpec.Http;
@service({
title: "Widget Service",
})
namespace DemoService;

model Widget {
@visibility("read", "update")
@path
id: string;

weight: int32;
color: "red" | "blue";
}

@error
model Error {
code: int32;
message: string;
}

@route("/widgets")
@tag("Widgets")
interface Widgets {
/** List widgets */
@get list(): Widget[] | Error;
/** Read widgets */
@get read(@path id: string): Widget | Error;
/** Create a widget */
@post create(...Widget): Widget | Error;
/** Update a widget */
@patch update(...Widget): Widget | Error;
/** Delete a widget */
@delete delete(@path id: string): void | Error;

/** Analyze a widget */
@route("{id}/analyze") @post analyze(@path id: string): string | Error;
}
56 changes: 51 additions & 5 deletions packages/compiler/templates/scaffolding.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,59 @@
"libraries": [
"@typespec/http",
"@typespec/rest",
"@typespec/openapi",
"@typespec/openapi3"
],
"config": {
"emit": [
"@typespec/openapi3"
]
}
"emitters": {
"@typespec/openapi3": {
"selected": true,
"options": {
"emitter-output-dir": "{output-dir}/schema"
}
},
"@typespec/http-client-csharp": {
"description": "CSharp Client emitter",
"options": {
"emitter-output-dir": "{output-dir}/clients/csharp"
}
},
"@typespec/http-client-java": {
"description": "Java Client emitter",
"options": {
"emitter-output-dir": "{output-dir}/clients/java"
}
},
"@typespec/http-client-js": {
"description": "JavaScript Client emitter",
"options": {
"emitter-output-dir": "{output-dir}/clients/js"
}
},
"@typespec/http-client-python": {
"description": "Python Client emitter",
"options": {
"emitter-output-dir": "{output-dir}/clients/python"
}
},
"@typespec/http-server-csharp": {
"description": "CSharp server stubs",
"options": {
"emitter-output-dir": "{output-dir}/server"
}
},
"@typespec/http-server-js": {
"description": "Javascript server stubs",
"options": {
"emitter-output-dir": "{output-dir}/server"
}
}
},
"files": [
{
"path": "rest/main.tsp",
"destination": "main.tsp"
}
]
},
"library-ts": {
"title": "TypeSpec Library (With TypeScript)",
Expand Down
6 changes: 6 additions & 0 deletions packages/compiler/test/e2e/init-templates.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,17 @@ describe("Init templates e2e tests", () => {
await rm(snapshotFolder, { recursive: true, force: true });
});

it("rest", () => scaffoldTemplateSnapshot("rest"));
it("emitter-ts", () => scaffoldTemplateSnapshot("emitter-ts"));
it("library-ts", () => scaffoldTemplateSnapshot("library-ts"));
});

describe("validate templates", () => {
it("validate rest template", async () => {
const fixture = await scaffoldTemplateForTest("rest");
await fixture.checkCommand("npm", ["install"]);
await fixture.checkCommand("npx", ["tsp", "compile", "."]);
});
it("validate emitter-ts template", async () => {
const fixture = await scaffoldTemplateForTest("emitter-ts");
await fixture.checkCommand("npm", ["install"]);
Expand Down
1 change: 1 addition & 0 deletions packages/playground-website/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default mergeConfig(
defineConfig({
test: {
exclude: [...configDefaults.exclude, "dist-dev/**/*"],
testTimeout: 10_000,
},
}),
);

0 comments on commit 9517d45

Please sign in to comment.