Sure, here’s the translation: “我可以从Angular服务创建Swagger或其他API文档吗?”

huangapple go评论72阅读模式
英文:

Can I create a swagger or other API documentation from Angular Services?

问题

在创建后端之前,我创建了一个Angular应用程序。是否有一个可以扫描整个应用程序服务以及其http请求并生成swagger文档,以便后端开发人员使用的服务/npm包/库等?

我知道有一些工具可以反向生成Angular服务(从swagger生成),但我已经有了代码,我想为我正在使用/期望的端点创建文档。

英文:

I created an Angular app before creating a backend. Is there a service/npm package/library/etc I can use that scans the whole app services and its http requests and generate a swagger so the backend dev uses that?

I know there are some tools to do it the other way around (generate Angular Services out of a swagger) but I already have the code, I want to create the documentation for the endpoints I am using/expecting

答案1

得分: 0

Here are the translated parts:

{
  "title": "My Angular App API Documentation",
  "sourceDir": "src/app",
  "exportFormats": ["swagger"]
}
  • 注释服务
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

/**
 * 用于与我的API交互的服务。
 */
@Injectable({
  providedIn: 'root'
})
export class MyApiService {
  /**
   * 检索用户列表。
   * @returns 一个发出用户列表的可观察对象。
   */
  getUsers() {
    return this.http.get<User[]>('/api/users');
  }

  constructor(private http: HttpClient) { }
}
  • 生成swagger.json
compodoc -p tsconfig.app.json
  • 然后可以将输出文件提供给后端开发人员。
英文:

compodoc should be able to to Generate json by following JSDoc annotations added to Angular service methods. https://compodoc.github.io/compodoc-demo-todomvc-angular/index.html which then can be fed to swagger.

npm install -g @compodoc/compodoc

Then configure compdoc

{
  &quot;title&quot;: &quot;My Angular App API Documentation&quot;,
  &quot;sourceDir&quot;: &quot;src/app&quot;,
  &quot;exportFormats&quot;: [&quot;swagger&quot;]
}

Annotate services

import { HttpClient } from &#39;@angular/common/http&#39;;
import { Injectable } from &#39;@angular/core&#39;;

/**
 * A service for interacting with the My API.
 */
@Injectable({
  providedIn: &#39;root&#39;
})
export class MyApiService {
  /**
   * Retrieves the list of users.
   * @returns An observable that emits the list of users.
   */
  getUsers() {
    return this.http.get&lt;User[]&gt;(&#39;/api/users&#39;);
  }

  constructor(private http: HttpClient) { }
}

To generate swagger.json

compodoc -p tsconfig.app.json

Output file can be then given to BE guys?

huangapple
  • 本文由 发表于 2023年3月31日 02:34:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定