Coder Social home page Coder Social logo

c33t33 / cadl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from microsoft/typespec

0.0 0.0 0.0 4.76 MB

Home Page: https://microsoft.github.io/cadl

License: MIT License

JavaScript 2.38% C# 0.74% TypeScript 95.25% CSS 0.57% HTML 0.87% Dockerfile 0.04% Nunjucks 0.16%

cadl's Introduction

Cadl

Try Cadl Online

Cadl is a language for describing cloud service APIs and generating other API description languages, client and service code, documentation, and other assets. Cadl provides highly extensible core language primitives that can describe API shapes common among REST, GraphQL, gRPC, and other protocols.

You can try a work-in-progress build of the compiler by following the steps in the Getting Started section below. Please feel free to file issues for any issues you encounter while using the preview.

Packages

Name Changelog Latest Next
Core functionality
@cadl-lang/compiler Changelog
Cadl Libraries
@cadl-lang/rest Changelog
@cadl-lang/openapi Changelog
@cadl-lang/openapi3 Changelog
@cadl-lang/versioning Changelog
Cadl Tools
@cadl-lang/prettier-plugin-cadl Changelog
cadl-vs Changelog
cadl-vscode Changelog
tmlanguage-generator Changelog

@next version of the package are the latest versions available on the main branch.

Try Cadl without installing anything

You can try Cadl on the web without installing anything.

Getting Started

Using Docker

See docker documentation

Using Node & Npm

  1. Install Node.js 16 LTS and ensure you are able to run the npm command in a command prompt:

    npm --version

    It is recommended to have npm 7+. To update npm run npm install -g npm

  2. Create a folder for your new Cadl project

  3. Via init command: Run npx -p @cadl-lang/compiler cadl init > Select openapi3 library template.

  4. Alternatively manually: In a command prompt, run the following commands:

    cd path\to\cadl\project
    npm init -y
    npm install -g @cadl-lang/compiler
    npm install @cadl-lang/rest @cadl-lang/openapi3
    

    This will create a package.json file for your Cadl project and add the necessary Cadl dependencies to it.

  5. Install the Cadl extension for your editor of choice:

  6. Open the folder in your editor and create a new file main.cadl

  7. Follow our documentation to get started writing Cadl!

  8. Once you're ready to compile your Cadl to Swagger, save the file and type this at the command prompt in your project folder:

    npx cadl compile . --emit @cadl-lang/openapi3
    

    This will compile the Cadl files in the project folder into one output file: .\cadl-output\openapi.json.

  9. Using --emit every time can become tedious. You can create a project file to configure the default emitter.

Create a cadl-project.yaml file next to the package.json with this content:

emitters:
  "@cadl-lang/openapi3": true

After you should be able to just run npx cadl compile .

Troubleshooting

See common issues here

Usage

See full usage documentation by typing:

cadl --help

Compiling Cadl source to an OpenAPI 3.0 specification

Here is a very small Cadl example that uses the @cadl-lang/openapi3 library to generate OpenAPI 3.0 from Cadl.

sample.cadl

import "@cadl-lang/rest";

using Cadl.Http;

@server("https://example.com", "Single server endpoint")
@route("/example")
namespace Example {
  @get
  @route("/message")
  op getMessage(): string;
}

You can compile it to OpenAPI 3.0 by using the following command:

cadl compile sample.cadl --emit @cadl-lang/openapi3

Once it compiles, you can find the emitted OpenAPI document in `./cadl-output/openapi.json.

You can also pass in a directory instead of a file to cadl compile. That's equivalent to passing main.cadl in that directory.

Formatting Cadl files

Cadl provides an auto-formatter to keep your specs clean and organized. node_modules folders are automatically excluded by default

cadl format <patterns...>

# Format all the files in the current directory with the cadl extension.
cadl format **/*.cadl

# Exclude certain patterns. Either use `!` prefix or pass it via the `--exclude` or `-x` option.
cadl format **/*.cadl "!my-test-folder/**/*"
cadl format **/*.cadl --exclude "my-test-folder/**/*"

Installing VS Code Extension

cadl code install

This will download and install the latest VS Code extension. Use cadl code uninstall to remove it. Pass --insiders if you use VS Code Insiders edition.

If cadl-server cannot be found on PATH by VS Code in your setup, you can configure its location in VS Code settings. Search for "Cadl" in File -> Preferences -> Settings, and adjust cadl.cadl-server.path accordingly. You may need to restart VS Code after changing this. This should be the path to the @cadl-lang/compiler package. (e.g. ./node_modules/@cadl-lang/compiler)

You can also configure a project to use a local npm install of @cadl-lang/compiler. See local-cadl sample.

Installing Visual Studio Extension

cadl vs install

This will download and install the latest Visual Studio extension. Use cadl vs uninstall to remove it.

If cadl-server cannot be found on PATH by Visual Studio in your setup, you can configure its location by setting up the cadl.cadl-server.path entry in .vs/VSWorkspaceSettings.json. You may need to restart Visual Studio after changing this. This should be the path to the @cadl-lang/compiler package. (e.g. ./node_modules/@cadl-lang/compiler)

Installing nightly version

On every commit to the main branch, packages with changes are automatically published to npm with the @next tag. The packages section shows which version corresponds to the next tag for each package.

To use a nightly version of the packages, go over each one of the packages in the package.json file and update it to either the latest published @next version or @latest, whichever is the newest. You can also use the tag latest or next instead of an explicit version.

After updating the package.json file you can run npm update --force. Force is required as there might be some incompatible version requirement.

Example

// Stable setup
"dependencies": {
  "@cadl-lang/compiler": "~0.30.0",
  "@cadl-lang/rest": "~0.14.0",
  "@cadl-lang/openapi": "~0.9.0",
}

// Consume next version
// In this example: compiler and openapi have changes but rest library has none
"dependencies": {
  "@cadl-lang/compiler": "~0.31.0-dev.5",
  "@cadl-lang/rest": "~0.14.0", // No changes to @cadl-lang/rest library so need to stay the latest.
  "@cadl-lang/openapi": "~0.10.0-dev.2",
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.