--- title: ES6 Class Add ServiceStack Reference --- In addition to [TypeScript](/typescript-add-servicestack-reference) support for generating typed Data Transfer Objects (DTOs), JavaScript is now supported in the form of [JSDoc](https://jsdoc.app) annotated typed ES6 classes that can be referenced natively from [JavaScript Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules). ### Reference directly in JavaScript Modules Unlike TypeScript, the JavaScript ES6 class DTOs can be referenced directly in a browser as-is, removing the need to keep your DTOs in sync with extra tooling by direct referencing them in a JavaScript Module: ```html ``` Then to make typed API Requests from web pages, you need only need to reference an ES Module (.mjs) build of the dependency-free [@servicestack/client](https://github.com/ServiceStack/servicestack-client) library which can be sourced directly from a npm CDN: ```html ``` ### Import Maps Although we recommend using an [importmap](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap) to specify where to load **@servicestack/client** from, e.g: ```html ``` ### ImportMap in Razor Pages or MVC Razor Pages or MVC projects can use `@Html.ImportMap()` in **_Layout.cshtml** to use different builds for development and production, e.g: ```csharp @if (Context.Request.Headers.UserAgent.Any(x => x.Contains("Safari") && !x.Contains("Chrome"))) { } @Html.ImportMap(new() { ["@servicestack/client"] = ("/js/servicestack-client.mjs", "/js/servicestack-client.min.mjs"), }) ``` ### Usage This lets us reference the **@servicestack/client** package name in our source code instead of its physical location: ```html
``` ```html ``` ### Enable static analysis and intelli-sense For better IDE intelli-sense during development, save the annotated Typed DTOs to disk with the [x dotnet tool](/dotnet-tool): :::sh x mjs ::: Then reference it instead to enable IDE static analysis when calling Typed APIs from JavaScript: ```js import { Hello } from '/js/dtos.mjs' client.api(new Hello({ name })) ``` To also enable static analysis for **@servicestack/client**, install the dependency-free library as a dev dependency: :::sh npm install -D @servicestack/client ::: Where only its TypeScript definitions are used by the IDE during development to enable its type-checking and intelli-sense. ::include npx-get-dtos.md:: ### Rich intelli-sense support Where you'll be able to benefit from rich intelli-sense support in smart IDEs like [Rider](https://www.jetbrains.com/rider/) for both the client library: ![](/img/pages/mix/init-rider-ts-client.png) As well as your App's server generated DTOs: ![](/img/pages/release-notes/v6.6/mjs-intellisense.png) ## Add ServiceStack Reference A new ServiceStack reference containing the APIs typed DTOs can be added using the **BaseUrl** of the ServiceStack App, e.g: :::sh `x mjs https://localhost:5001` ::: ### Update ServiceStack References All existing ServiceStack References can later be updated with: :::sh x mjs ::: ## DTO Customization Options In most cases you'll just use the generated JavaScript DTO's as-is, however you can further customize how the DTOs are generated by overriding the default options. The header in the generated DTOs show the different options JavaScript types support with their defaults. Default values are shown with the comment prefix of `//`. To override a value, remove the `//` and specify the value to the right of the `:`. Any uncommented value will be sent to the server to override any server defaults. The DTO comments allows for customizations for how DTOs are generated. The default options that were used to generate the DTOs are repeated in the header comments of the generated DTOs, options that are preceded by a TypeScript comment `//` are defaults from the server, any uncommented value will be sent to the server to override any server defaults. ```js /* Options: Date: 2025-06-04 09:52:13 Version: 8.80 Tip: To override a DTO option, remove "//" prefix before updating BaseUrl: https://blazor-vue.web-templates.io //AddServiceStackTypes: True //AddDocAnnotations: True //AddDescriptionAsComments: True //IncludeTypes: //ExcludeTypes: //DefaultImports: */ ``` We'll go through and cover each of the above options to see how they affect the generated DTO's: ### Change Default Server Configuration The above defaults are also overridable on the ServiceStack Server by modifying the `NativeTypesFeature` Plugin, e.g: ```csharp //Server example in CSharp var nativeTypes = this.GetPlugin(); nativeTypes.MetadataTypesConfig.AddDescriptionAsComments = false; ... ``` We'll go through and cover each of the above options to see how they affect the generated DTO's: ### IncludeTypes Is used as a Whitelist to specify only the types you would like to have code-generated: ``` /* Options: IncludeTypes: Hello, HelloResponse ``` Will only generate `Hello` and `HelloResponse` DTOs: ```csharp export class Hello { /** @param {{name?:string}} [init] */ constructor(init) { Object.assign(this, init) } /** @type {string} */ name; getTypeName() { return 'Hello' } getMethod() { return 'POST' } createResponse() { return new HelloResponse() } } export class HelloResponse { /** @param {{result?:string,responseStatus?:ResponseStatus}} [init] */ constructor(init) { Object.assign(this, init) } /** @type {string} */ result; /** @type {ResponseStatus} */ responseStatus; } ``` #### Include Generic Types Use .NET's Type Name to include Generic Types, i.e. the Type name separated by the backtick followed by the number of generic arguments, e.g: ``` IncludeTypes: IReturn`1,MyPair`2 ``` #### Include Request DTO and its dependent types You can include a Request DTO and all its dependent types with a `.*` suffix on the Request DTO, e.g: ``` /* Options: IncludeTypes: GetTechnology.* ``` Which will include the `GetTechnology` Request DTO, the `GetTechnologyResponse` Response DTO and all Types that they both reference. #### Include All Types within a C# namespace If your DTOs are grouped into different namespaces they can be all included using the `/*` suffix, e.g: ``` /* Options: IncludeTypes: MyApp.ServiceModel.Admin/* ``` This will include all DTOs within the `MyApp.ServiceModel.Admin` C# namespace. #### Include All Services in a Tag Group Services [grouped by Tag](/api-design#group-services-by-tag) can be used in the `IncludeTypes` where tags can be specified using braces in the format `{tag}` or `{tag1,tag2,tag3}`, e.g: ``` /* Options: IncludeTypes: {web,mobile} ``` Or individually: ``` /* Options: IncludeTypes: {web},{mobile} ``` ### ExcludeTypes Is used as a Blacklist to specify which types you would like excluded from being generated: ``` /* Options: ExcludeTypes: GetTechnology,GetTechnologyResponse ``` Will exclude `GetTechnology` and `GetTechnologyResponse` DTOs from being generated. ### Cache When using `/types/mjs` directly from a `script` tag, the server will cache the result by default when not running in [DebugMode](/debugging#debugmode). This caching process can be disabled if required by using **?cache=false**.