{"id":535185,"date":"2021-08-30T01:55:10","date_gmt":"2021-08-29T17:55:10","guid":{"rendered":"http:\/\/4563.org\/?p=535185"},"modified":"2021-08-30T01:55:10","modified_gmt":"2021-08-29T17:55:10","slug":"%e4%b8%ba-http-%e8%af%b7%e6%b1%82%e7%9a%84-json-%e5%93%8d%e5%ba%94%e8%87%aa%e5%8a%a8%e7%94%9f%e6%88%90-ts-%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=535185","title":{"rendered":"\u4e3a HTTP \u8bf7\u6c42\u7684 JSON \u54cd\u5e94\u81ea\u52a8\u751f\u6210 TS \u7c7b\u578b"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1> \u4e3a HTTP \u8bf7\u6c42\u7684 JSON \u54cd\u5e94\u81ea\u52a8\u751f\u6210 TS \u7c7b\u578b <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ThomasTrainset <\/span>  <span><i><\/i> 15<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4f7f\u7528 TypeScript \u5f00\u53d1\u524d\u7aef\u9879\u76ee\uff0c\u5b8c\u5584\u7684\u7c7b\u578b\u6279\u6ce8\u662f\u975e\u5e38\u63d0\u5347\u5f00\u53d1\u6548\u7387\u7684\u3002\u7136\u800c\uff0c\u5f53\u9047\u5230 Restful\uff0c\u4f3c\u4e4e\u53ea\u80fd\u4e3a Restful \u8fd4\u56de\u7684 JSON \u6570\u636e\u624b\u52a8\u4e66\u5199\u7c7b\u578b\uff0c\u968f\u7740\u63a5\u53e3\u8d8a\u6765\u8d8a\u591a\uff0c\u624b\u5199\u7c7b\u578b\u662f\u7e41\u7410\u4e14\u4f4e\u6548\u7684\u3002 \u6709\u6ca1\u6709\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u62ff\u5230\u8fd4\u56de\u6570\u636e\u7684\u7c7b\u578b\u5462\uff1f<\/p>\n<h2>JSON \u7c7b\u578b\u6587\u4ef6\u751f\u6210<\/h2>\n<h3>JSON \u7c7b\u578b<\/h3>\n<p>Json \u4e2d\u6570\u636e\u7c7b\u578b\u6709 6 \u79cd: string \u3001number \u3001boolean \u3001array \u3001object \u3001null<\/p>\n<p>\u5176\u4e2d string \u3001number \u3001boolean \u7684\u7c7b\u578b\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <code>typeof<\/code> \u5224\u522b\u7c7b\u578b\u3002<\/p>\n<p>null \u6709\u4e9b\u590d\u6742\uff0c\u5b83\u53ef\u80fd\u662f\u5176\u4ed6 5 \u4e2d\u7c7b\u578b\u4e2d\u7684\u4e00\u79cd\uff0c\u65e0\u6cd5\u5224\u65ad\u5177\u4f53\u662f\u4ec0\u4e48\u7c7b\u578b\uff0c\u56e0\u800c\u53ea\u80fd\u586b\u5145 any<\/p>\n<p>\u5bf9\u4e8e object\uff0c\u5b83\u53ef\u80fd\u7531 Json \u7684 6 \u79cd\u6570\u636e\u7ed3\u6784\u7ec4\u6210\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u904d\u5386\u7684\u65b9\u5f0f\uff0c\u6765\u5224\u65ad value \u7684\u7c7b\u578b<\/p>\n<p>\u800c\u5bf9\u4e8e array\uff0carray \u4e2d\u7684\u6bcf\u4e00\u9879\u6570\u636e\u7ed3\u6784\u5e94\u5f53\u90fd\u662f\u76f8\u540c\u7684\uff0c\u56e0\u800c\u53ea\u9700\u8981\u53d6\u51fa\u7b2c\u4e00\u9879\u8fdb\u884c\u5904\u7406\uff0c\u5904\u7406\u903b\u8f91\u4e0e\u4e0a\u8ff0\u51e0\u79cd\u7c7b\u578b\u76f8\u540c\u3002<\/p>\n<h3>\u6587\u4ef6\u751f\u6210<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528 node fs api\uff0c\u5229\u7528\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\uff0c\u5c06 JSON \u7c7b\u578b\u5904\u7406\u540e\uff0c\u8f93\u51fa\u5230\u7c7b\u578b\u6587\u4ef6\u4e2d\u3002\u8fd9\u6837\u7b80\u5355\u4e14\u6709\u6548\uff0c\u4f46\u4e0d\u90a3\u4e48\u4f18\u96c5\uff0c\u4e14\u6613\u51fa\u9519\u3002<\/p>\n<p>\u53ef\u4ee5\u501f\u52a9 ts-morph \u8fd9\u4e2a\u5e93\uff0c\u6765\u5b8c\u6210\u7c7b\u578b\u7684\u751f\u6210\u548c\u5bfc\u51fa\u3002<\/p>\n<p>ts-morph \u4f7f\u7528\u4f2a\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code> const project = createProject()  project.addInterface({ name, value }).setIsExport(true)  saveProject(project)  <\/code><\/pre>\n<p>\u76f8\u6bd4 fs API\uff0cts-morph \u4f7f\u7528\u66f4\u7b80\u5355<\/p>\n<h2>Restful \u6574\u5408<\/h2>\n<p>\u53ef\u4ee5\u6839\u636e JSON \u6570\u636e\u751f\u6210\u7c7b\u578b\u6587\u4ef6\u540e\uff0c\u5f88\u5bb9\u6613\u60f3\u5230\uff0c\u5728\u8bf7\u6c42\u5e93\u7684\u62e6\u622a\u5668\u4e2d\uff0c\u62e6\u622a\u54cd\u5e94\uff0c\u6267\u884c JSON \u7c7b\u578b\u6587\u4ef6\u751f\u6210\u3002\u4f46\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u524d\u7aef\u9879\u76ee\u4e2d\uff0cNode API \u4e0d\u80fd\u4f7f\u7528\uff0c\u56e0\u4e3a\u4f60\u7684\u4ee3\u7801\u662f\u8fd0\u884c\u5728\u6d4f\u89c8\u5668\u7684\u3002\u90a3\u4e48\u600e\u4e48\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f<\/p>\n<h3>\u7c7b\u578b\u751f\u6210\u5668\u811a\u672c<\/h3>\n<p>\u65e2\u7136\u524d\u7aef\u9879\u76ee\u4e2d\u4e0d\u80fd\u96c6\u6210 JSON \u7c7b\u578b\u6587\u4ef6\u751f\u6210\u5de5\u5177\uff0c\u90a3\u4e48\u53ef\u4ee5\u7f16\u5199 Node \u811a\u672c\u6765\u89e3\u51b3\u95ee\u9898\u3002\u540e\u7aef\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3\u540e\uff0c\u524d\u7aef\u65b0\u589e\u4e00\u4e2a\u63a5\u53e3\uff0c\u811a\u672c\u914d\u7f6e\u6587\u4ef6\u4e5f\u8981\u6ce8\u518c\u4e00\u4e2a\u63a5\u53e3\uff0c\u6700\u540e\u8fd0\u884c\u4e00\u4e0b\u811a\u672c\u5373\u53ef\u3002<\/p>\n<p>\u90a3\u4e48\u770b\u770b\u811a\u672c\u9700\u8981\u5b8c\u6210\u54ea\u4e9b\u529f\u80fd\u3002<\/p>\n<p>\u9996\u5148\u811a\u672c\u9700\u8981\u96c6\u6210\u4e00\u4e2a\u8bf7\u6c42\u5e93\uff0c\u7528\u4ee5\u53d1\u8d77\u8bf7\u6c42\uff0c\u63a5\u6536\u670d\u52a1\u7aef\u7684 JSON \u6570\u636e\u3002<\/p>\n<p>\u7136\u540e\u8fd8\u8981\u96c6\u6210\u4e0a\u9762\u7684 JSON \u7c7b\u578b\u6587\u4ef6\u751f\u6210\u811a\u672c\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u8fd8\u9700\u8981\u7ef4\u62a4\u4e00\u4efd\u914d\u7f6e\u6587\u4ef6\uff0c\u6587\u4ef6\u4e2d\u8981\u6709\u8bf7\u6c42\u53c2\u6570\u5217\u8868\uff0c\u7528\u4ee5\u52a8\u6001\u751f\u6210\u7c7b\u578b\u6587\u4ef6\u3002\u4e3a\u4e86\u907f\u514d\u540c\u65f6\u53d1\u8d77\u7684\u8bf7\u6c42\u6570\u91cf\u592a\u591a\uff0c\u5bfc\u81f4\u7535\u8111\u6b7b\u673a\uff0c\u6216\u8005\u670d\u52a1\u7aef\u5b95\u673a\uff0c\u8fd8\u8981\u5bf9\u8bf7\u6c42\u8fdb\u884c\u5e76\u53d1\u63a7\u5236\u3002<\/p>\n<p>\u6bcf\u6b21\u6267\u884c\u811a\u672c\uff0c\u6240\u6709\u8bf7\u6c42\u90fd\u4f1a\u518d\u53d1\u9001\u4e00\u904d\uff0c\u6240\u4ee5\u8fd8\u8981\u8003\u8651\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u751f\u6210\uff0c\u518d\u53bb\u8bf7\u6c42\u3002<\/p>\n<p>\u8003\u8651\u5230\u53ef\u7ef4\u62a4\u6027\uff0c\u5efa\u8bae\u5355\u72ec\u7ef4\u62a4\u4e00\u4e2a URL \u7684\u6620\u5c04\u6587\u4ef6\uff0c\u5728 Node \u811a\u672c\u548c\u524d\u7aef\u9879\u76ee\uff0c\u5f15\u7528 URL \u6587\u4ef6\u7684 URL \u5730\u5740\u3002<\/p>\n<p>\u6709\u4e86\u8fd9\u6837\u4e00\u4e2a\u811a\u672c\uff0c\u6bcf\u6b21\u65b0\u589e\u4e00\u4e2a\u63a5\u53e3\u65f6\uff0c\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u4e00\u4e0b\u63a5\u53e3\u548c\u8bf7\u6c42\u53c2\u6570\uff0c\u7136\u540e\u624b\u52a8\u6267\u884c\u4e00\u4e0b\u811a\u672c\u3002\u8fd9\u6837\u4e5f\u4e0d\u592a\u65b9\u4fbf\uff0c\u53ef\u4ee5\u4f7f\u7528 chokidar \u76d1\u542c\u6587\u4ef6\u53d8\u66f4\uff0c\u4f7f\u7528 shelljs \u6765\u6267\u884c\u811a\u672c\u3002<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u4e0a\u9762\u7684\u6b65\u9aa4\u7e41\u7410\u4e14\u590d\u6742\uff0c\u7ef4\u62a4\u8fd9\u6837\u4e00\u4e2a\u590d\u6742\u914d\u7f6e\u6587\u4ef6\uff0c\u4f1a\u8ba9\u4eba\u671b\u800c\u5374\u6b65\u3002\u5e76\u4e14\u8fd9\u6837\u7684\u914d\u7f6e\u6587\u4ef6\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u7684\u8bf7\u6c42\uff0c\u6d89\u53ca\u5230\u7684 Token \u6821\u9a8c\uff0cPost \u7684 Body \u5904\u7406\uff0c\u54cd\u5e94\u7684 Data \u7684\u5904\u7406\u7b49\u7b49\u90fd\u8981\u533a\u522b\u4e0e\u524d\u7aef\u9879\u76ee\uff0c\u518d\u5355\u72ec\u5904\u7406\u4e00\u904d\u3002<\/p>\n<p>\u6709\u6ca1\u6709\u66f4\u597d\u7684\u529e\u6cd5\uff0c\u6765\u5b8c\u6210\u7c7b\u578b\u751f\u6210\u7684\u76ee\u7684\uff1f<\/p>\n<h3>Server-Clinet \u7c7b\u578b\u751f\u6210\u5668<\/h3>\n<p>\u5199\u8fd9\u6837\u4e00\u4e2a\u811a\u672c\uff0c\u4e3b\u8981\u7684\u96be\u70b9\u5728\u4e8e Node \u811a\u672c\u600e\u4e48\u4fbf\u6377\u7684\u62ff\u5230\u524d\u7aef\u9879\u76ee\u7684\u54cd\u5e94\u6570\u636e\uff0c\u4e5f\u5c31\u662f\u524d\u7aef\u62ff\u5230\u6570\u636e\u540e\u600e\u4e48\u901a\u77e5\u5230\u811a\u672c\uff1f<\/p>\n<p>\u8fd9\u4e48\u4e00\u60f3\uff0c\u4e8b\u60c5\u5c31\u7b80\u5355\u4e86\uff0c\u5982\u679c Node \u811a\u672c\u4e2d\u5f00\u542f\u4e00\u4e2a HTTP Server\uff0c\u524d\u7aef\u62ff\u5230\u6570\u636e\u540e\uff0c\u518d\u5411 HTTP Server \u53d1\u8d77\u4e00\u4e2a POST \u8bf7\u6c42\uff0c\u5c06\u4e00\u4e9b\u53c2\u6570\u643a\u5e26\u8fc7\u53bb\uff0c\u6307\u6325 HTTP Server \u5411\u76ee\u6807\u76ee\u5f55\u751f\u6210\u7c7b\u578b\u6587\u4ef6\u5373\u53ef\u3002<\/p>\n<p>\u4f46\u8fd9\u4e00\u5957\u6d41\u7a0b\u8fd8\u6709\u4e2a\u7f3a\u70b9\uff0c\u7c7b\u578b\u6587\u4ef6\u662f\u201c\u8fd0\u884c\u65f6\u201d\u751f\u6210\u7684\uff0c\u751f\u6210\u7c7b\u578b\u6587\u4ef6\u524d\uff0c\u9700\u8981\u524d\u7aef\u9879\u76ee\u5148\u8c03\u7528\u4e00\u6b21\u8bf7\u6c42\u3002\u4f46\u662f\uff0c\u8fd9\u4e00\u70b9\u7f3a\u70b9\u65e0\u4f24\u5927\u96c5\uff0c\u5f00\u53d1\u4ee3\u7801\u65f6\uff0c\u80af\u5b9a\u9700\u8981\u5148\u6d4b\u8bd5\u63a5\u53e3\u80fd\u4e0d\u80fd\u901a\u4ec0\u4e48\u7684\u3002<\/p>\n<h2>\u5de5\u5177\u94fe<\/h2>\n<p>\u57fa\u4e8e\u51e0\u5929\u7684\u5c1d\u8bd5\uff0c\u6211\u5f00\u53d1\u4e86\u51e0\u4e2a\u5e93\uff0c\u5b8c\u6210\u4e86\u8fd9\u6837\u4e00\u4ef6\u4e8b\u60c5\uff0c\u6700\u540e\u770b demo \u7684\u6548\u679c\uff0c\u8fd8\u4e0d\u9519\u3002<\/p>\n<h3>Demo \u9879\u76ee<\/h3>\n<p>\u6211\u57fa\u4e8e Vite React TypeScript \u5199\u4e86\u4e00\u4e2a demo \u9879\u76ee\uff1arestful-types-generate-example\u3002<\/p>\n<p>clone \u9879\u76ee\u540e\uff0c\u8fd0\u884c yarn \u5b89\u88c5, yarn dev \u542f\u52a8\u9879\u76ee\uff0c\u70b9\u51fb\u9875\u9762\u6309\u94ae\uff0c\u53d1\u8d77\u8bf7\u6c42\u540e\u5373\u53ef\u770b\u5230\u6548\u679c\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"https:\/\/p9-juejin.byteimg.com\/tos-cn-i-k3u1fbpfcp\/1c86f280ed4c49b8aeb5062ba1abb88c%7Etplv-k3u1fbpfcp-watermark.image\" alt=\"\u4e3a HTTP \u8bf7\u6c42\u7684 JSON \u54cd\u5e94\u81ea\u52a8\u751f\u6210 TS \u7c7b\u578b\" \/><\/p>\n<h3>JsonTypesGenerator<\/h3>\n<p>json-types-generator \u662f\u6839\u636e\u7b2c\u4e00\u5c0f\u8282\u4e2d\u4ecb\u7ecd\u7684\u539f\u7406\u5b8c\u6210\u7684<\/p>\n<p>\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b:<\/p>\n<pre><code> import jsonTypesGenerator from 'json-types-generator'  const json = { a: { b: 1, c: { d: true } } }  jsonTypesGenerator({    data: json,    outPutPath: '\/User\/xdoer\/types.ts',    rootInterfaceName: 'ChinaRegion',    customInterfaceName(key, value, data) {      if (key === 'a') return 'Province'      return key    }, }) <\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u5c06\u4f1a\u5728 <code>\/User\/xdoer\/types.ts<\/code> \u6587\u4ef6\u4e2d\u751f\u6210\u5bfc\u51fa interface \u4e3a <code>ChinaRegion<\/code> \u7684\u7c7b\u578b\u6587\u4ef6\uff0c\u4ea7\u751f\u7684\u4e2d\u95f4 inteface \u540d\u79f0\u4e3a <code>Province<\/code>\u3002\u4e0d\u4f20\u5165 customInterfaceName \u7684\u60c5\u51b5\u4e0b\uff0c\u4e2d\u95f4\u4ea7\u7269\u9ed8\u8ba4\u7684 interface \u540d\u79f0\u4e3a key \u7684\u5927\u5199<\/p>\n<pre><code>&lt;!----\/User\/xdoer\/types.ts----&gt;  export interface ChinaRegion {   a: Province }  export interface Province {   b: number   c: c }  export interface c {   d: boolean } <\/code><\/pre>\n<h3>ResponseTypesServer<\/h3>\n<p>response-types-server \u662f\u4e0a\u6587\u63d0\u5230\u7684 Server-Clinet \u7c7b\u578b\u751f\u6210\u5668 \u4e2d\u7684 Server \u90e8\u5206\u3002\u53ea\u9700\u8981\u5411\u8fd9\u4e2a Server \u53d1\u9001 POST \u8bf7\u6c42\uff0c\u5373\u53ef\u751f\u6210\u7c7b\u578b\u3002<\/p>\n<p>\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b:<\/p>\n<pre><code>import server from '@prequest\/response-types-server'  \/\/ \u9ed8\u8ba4\u5f00\u542f\u7684\u7aef\u53e3\u4e3a 10086 server()  \/\/ \u4f60\u53ef\u4ee5\u901a\u8fc7\u4f20\u53c2\u6307\u5b9a\u7aef\u53e3 server({ port: 10010 }) <\/code><\/pre>\n<p>\u53d1\u9001\u7684\u8bf7\u6c42\uff0c\u8def\u5f84\u4efb\u610f\uff0cPOST \u8bf7\u6c42\u53c2\u6570\u4e3a:<\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u7c7b\u578b<\/th>\n<th>\u542b\u4e49<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>outPutDir<\/td>\n<td>string<\/td>\n<td>\u7c7b\u578b\u6587\u4ef6\u8f93\u51fa\u76ee\u5f55<\/td>\n<\/tr>\n<tr>\n<td>outPutName<\/td>\n<td>string<\/td>\n<td>\u6587\u4ef6\u540d\u79f0<\/td>\n<\/tr>\n<tr>\n<td>overwrite<\/td>\n<td>boolean<\/td>\n<td>\u6587\u4ef6\u53ef\u590d\u5199<\/td>\n<\/tr>\n<tr>\n<td>data<\/td>\n<td>Json<\/td>\n<td>\u8981\u89e3\u6790\u7684 Json \u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>interfaceName<\/td>\n<td>string<\/td>\n<td>\u5bfc\u51fa\u7684\u63a5\u53e3\u540d\u79f0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>ResponseTypesClient<\/h3>\n<p>response-types-client \u662f\u4e0a\u6587\u63d0\u5230\u7684 Server-Clinet \u7c7b\u578b\u751f\u6210\u5668 \u4e2d\u7684 Client \u90e8\u5206\u3002\u5b83\u662f\u4e00\u4e2a\u4e2d\u95f4\u4ef6 Wrapper\uff0c\u53ea\u8981\u5c06\u5176\u6ce8\u518c\u5230\u8bf7\u6c42\u5e93\u4e2d\u95f4\u4ef6\u4e2d\uff0c\u5373\u53ef\u53d1\u8d77\u8bf7\u6c42\u3002<\/p>\n<p>\u4e0b\u9762\u7684 demo \u4f7f\u7528\u4e86\u6211\u81ea\u5df1\u5c01\u88c5\u7684\u8bf7\u6c42\u5e93 PreQuest\uff0c\u57fa\u4e8e Koa \u4e2d\u95f4\u4ef6\u6a21\u578b\u7684\u8bf7\u6c42\u5e93\u5e94\u8be5\u90fd\u53ef\u4ee5\u4f7f\u7528\uff0c\u6bd4\u5982\u8bf4 Umi-Request \u3002\u5bf9\u4e8e Axios\uff0c\u9700\u8981\u81ea\u5df1\u5728\u62e6\u622a\u5668\u4e2d\u5b9e\u73b0\uff0c\u4e5f\u975e\u5e38\u5bb9\u6613\u3002<\/p>\n<p>\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code> import { create, Request, Response } from '@prequest\/xhr' import generateMiddleware, { TypesGeneratorInject } from '@prequest\/response-types-client'  \/\/ \u751f\u6210\u4e2d\u95f4\u4ef6 const middleware = generateMiddleware&lt;Request, Response&gt;({    enable: process.env.NODE_ENV === 'development',    httpAgent: create({ path: 'http:\/\/localhost:10010\/' }),    outPutDir: 'src\/api-types'    parseResponse(res) {       \/\/ res \u5e94\u5f53\u8fd4\u56de\u63a5\u53e3 data \u6570\u636e       return res as any    },     typesGeneratorConfig(req, res) {      const { path } = req      const { data } = res            if (!path) throw new Error('path not found')       \/\/ \u6839\u636e\u8bf7\u6c42\u8def\u5f84\u751f\u6210\u6587\u4ef6\u540d\u548c\u7c7b\u578b\u5bfc\u51fa\u540d\u79f0      const outPutName = path.replace(\/.*\/(w+)\/, (_, __) =&gt; __)      const interfaceName = outPutName.replace(\/^[a-z]\/, g =&gt; g.toUpperCase())       return {        data,        outPutName,        interfaceName,        overwrite: true      } }})  \/\/ \u6ce8\u5165 TypesGeneratorInject\uff0c \u53ef\u5728\u8bf7\u6c42\u65f6\uff0c\u6839\u636e rewriteType \u53c2\u6570\u5f3a\u5236\u91cd\u65b0\u751f\u6210\u7c7b\u578b\u6587\u4ef6 export const prequest = create&lt;TypesGeneratorInject, {}&gt;({ baseURL: 'http:\/\/localhost:3000' })  \/\/ \u6ce8\u518c\u4e2d\u95f4\u4ef6 prequest.use(middleware) <\/code><\/pre>\n<h3>ResponseTypesGenerator<\/h3>\n<p>\u6b64\u5916\uff0c\u8fd8\u6709\u57fa\u4e8e\u4e0a\u6587 &#8220;\u7c7b\u578b\u751f\u6210\u5668\u811a\u672c&#8221; \u4e00\u8282\u4e2d\u7684\u539f\u7406\uff0c\u8fdb\u884c\u4e86\u4e00\u4e2a\u5931\u8d25\u7684\u5c1d\u8bd5:response-types-generator\uff0c\u4e5f\u4e00\u5e76\u653e\u5230\u8fd9\u91cc\uff0c\u611f\u5174\u8da3\u7684\u53ef\u4ee5\u770b\u770b<\/p>\n<h2>\u7ed3\u8bed<\/h2>\n<p>\u4ee5\u4e0a\u57fa\u4e8e\u6211\u6d45\u8584\u7684\u5b66\u8bc6\u8fdb\u884c\u7684\u4e00\u4e9b\u5bf9 Restful \u54cd\u5e94\u7684 JSON \u6570\u636e\u7c7b\u578b\u751f\u6210\u7684\u4e00\u4e9b\u63a2\u7d22\uff0c\u5982\u679c\u60a8\u53d1\u73b0\u4e86\u6587\u4e2d\u7684\u4e00\u4e9b\u9519\u8bef\u4e4b\u5904\uff0c\u6216\u8005\u6709\u66f4\u7b80\u4fbf\u7684\u65b9\u5f0f\u751f\u6210\u7c7b\u578b\u6587\u4ef6\uff0c\u6b22\u8fce\u5728\u8bc4\u8bba\u91cc\u63d0\u51fa\u6765\uff0c\u5927\u5bb6\u4e00\u8d77\u63a2\u8ba8\u3002<\/p>\n<p>\u539f\u6587\u9996\u53d1\u4e8e\u6211\u7684\u4e2a\u4eba\u535a\u5ba2\uff1aRestful-API \u7684\u4e00\u79cd\u52a8\u6001\u751f\u6210\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5 | \u6587\u4eab\u65e5\u5fd7 (aiyou.life)<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>) <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a HTTP \u8bf7\u6c42\u7684 JSON \u54cd&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/535185"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=535185"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/535185\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=535185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=535185"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=535185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}