openresty-lua-types openresty typescript 类型定义

openresty-lua-types 是利用了TypeScriptToLua 搞一个一个openresty ts 定义,原想是自己搞一个,但是看到社区已经有类似的实现了
值得参考学习下,以下是一个简单的试用

参考代码

  • package.json
 
{
  "name": "myopenresty-lua",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "build": "tstl",
    "dev": "tstl --watch"
  },
  "devDependencies": {
    "openresty-lua-types": "^1.0.3",
    "typescript": "^4.6.3",
    "typescript-to-lua": "^1.4.3"
  }
}

tsconfig.json 

{
  "$schema": "https://raw.githubusercontent.com/TypeScriptToLua/vscode-typescript-to-lua/master/tsconfig-schema.json",
   "include": [
     "src/*"
    ],
   "compilerOptions": {
     "outDir": "./dist",
     "target":"ESNext",
     "types": ["openresty-lua-types"],
     "lib": ["esnext","DOM"],
     "moduleResolution": "node",
     "allowSyntheticDefaultImports": true,
     "strict": true
   },
   "tstl": {
     "noHeader": true,  // 去掉头
     "noImplicitSelf":true, // 禁用self 
     "buildMode": "library",
     "luaTarget": "JIT"
   }
 }

src/myapp.ts

ngx.say("dalongdmo","appdemo")
ngx.exit(ngx.HTTP_OK)
let method = ngx.req.get_method()
if (method === "GET") {
    console.log("do get ")
}

自动提示效果

 

 


构建效果

 

 

说明

基于TypeScriptToLua + openresty-lua-types 开发openresty 模块很好很强大,值得使用

参考资料

https://github.com/TypeScriptToLua/TypeScriptToLua
https://github.com/andrei-markeev/openresty-lua-types
https://github.com/openresty/lua-nginx-module
https://www.npmjs.com/package/openresty-lua-types

posted on 2022-04-17 23:18  荣锋亮  阅读(49)  评论(0编辑  收藏  举报

导航