蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

独立输入与实现:sdk-probe.cjs

JAVASCRIPTWindows / Node24.22.1 / SwaggerUI5.34.1 / Edge156 / Python3.12 / 固定生成器历史兼容版本sdk-probe.cjs
sdk-probe.cjs8 行
const path=require('node:path');
const assert=require('node:assert/strict');
const fs=require('node:fs');
const {promisify}=require('node:util');
const execFile=promisify(require('node:child_process').execFile);
const {createApp}=require('./server.cjs');
(async()=>{const ctx=createApp(),server=await new Promise(r=>{const s=ctx.app.listen(0,'127.0.0.1',()=>r(s))}),base='http://127.0.0.1:'+server.address().port;try{const {BooksApi,Configuration}=require('./generated/typescript-fetch/dist');const client=new BooksApi(new Configuration({basePath:base+'/api/v1',accessToken:ctx.fixture.token()}));const list=await client.listBooks({limit:1});assert.equal(list.data.length,1);assert.equal(list.total,3);const one=await client.getBook({bookId:1});assert(one.title.includes('蓝色天空网'));let missing=false;try{await client.getBook({bookId:999999})}catch(e){missing=e.response.status===404}assert(missing);const created=await client.createBook({bookInput:{title:'TypeScript真实生成客户端',author:'MBSKY模拟',isbn:'9780000000033',category:'编程'}});assert.equal(created.id,4);await client.deleteBook({bookId:created.id});const customSDK=require('./generated/typescript-custom/dist');const customClient=new customSDK.BooksApi(new customSDK.Configuration({basePath:base+'/api/v1',accessToken:ctx.fixture.token()}));const customList=await customClient.listBooks({limit:1});assert.equal(customList.data.length,1);const customBook=await customClient.getBook({bookId:2});assert.equal(customBook.id,2);const mainSDK=require('./generated/typescript-fetch/dist');const productAPI=new mainSDK.ProductsApi(new mainSDK.Configuration({basePath:base+'/api/v1',accessToken:ctx.fixture.token()}));const products=await productAPI.listProducts({limit:1});assert.equal(products.total,2);const product=await productAPI.createProduct({productInput:{name:'TS客户端实际产品',priceCents:1234,category:'books'}});assert.equal(product.id,3);await productAPI.deleteProduct({productId:product.id});const py=process.env.MBSKY_SDK_PYTHON;if(!py)throw Error('Set MBSKY_SDK_PYTHON to a Python3.12 interpreter with this SDK requirements installed');const result=await execFile(py,['-E','-s','-B','-X','utf8',path.join(__dirname,'python-sdk-probe.py'),base],{cwd:__dirname,timeout:30000,encoding:'utf8'});assert.equal(result.stderr,'');const python=JSON.parse(result.stdout);assert(python.passed);const observation={passed:true,product_client:{total:products.total,created_id:product.id,price_cents:product.priceCents,deleted:true},custom_template_client:{list_count:customList.data.length,detail_id:customBook.id,actually_called:true},typescript:{list_count:list.data.length,total:list.total,detail:one.title,created_id:created.id,missing_status:404,deleted:true},python,actual_request_count:ctx.requests.length,scope:'Actual generated TS/Python SDK requests to own HTTP service; Java/client stubs not runtime accepted',source_scripts_executed:false};console.log(JSON.stringify(observation))}finally{server.closeAllConnections();await new Promise(r=>server.close(r))}})().catch(e=>{console.error(e.stack);process.exitCode=1});
本例需在 Windows / Node24.22.1 / SwaggerUI5.34.1 / Edge156 / Python3.12 / 固定生成器历史兼容版本 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows / Node24.22.1 / SwaggerUI5.34.1 / Edge156 / Python3.12 / 固定生成器历史兼容版本

入口文件:projects/swagger-lab/sdk-probe.cjs

预期结果与验证条件观察提示
  • 按本章对应实际结果核对,未运行的其它环境仍待处理

请结合实际环境逐项观察。

附件与关联工程包下载

下载课程关联工程包:Swagger

课程关联工程包(登记关联13章) · 含当前章节或实例 · mbskycom202611a548.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习