以前我简单说明过hashids 的类型定义,以下是集成使用## 环境准备
详细介绍可以参考 javascript:void(0)
集成hashids
- dockerfile 安装hashid
使用luarocks
FROM openresty/openresty:1.21.4.1rc3-1-alpine-fat
RUN /usr/local/openresty/luajit/bin/luarocks install hashids
代码集成
- 添加类型定义
hashids.d.ts
declare module "hashids" {
interface Hashids {
encode(...args: number[]):string
decode(hashid:string):number[]
}
interface myHashidsConstructor {
new: (this: void, saltKey: string,length?:number) => Hashids;
}
var id: myHashidsConstructor
export = {
...id
}
}
- ts 配置调整
"compilerOptions": {
"outDir": "./lua_code",
"types": ["openresty-lua-types"],
"lib": ["esnext","DOM"],
"module":"commonjs",
"target": "esnext",
"skipLibCheck": true, /* Skip type checking all .d.ts files. */
"esModuleInterop": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"allowSyntheticDefaultImports": true,
"strict": true
},
- 代码集成
还是以前的添加了hashids 生成的
indexpage.ts
import myid = require("hashids")
let hashids = myid.new("dalongdemo",15)
ngx.say(hashids.encode(100,200,200))
运行
- docker build
docker-compose build
- 编译效果
- 构建
yarn dev
- 启动
docker-compose up -d
- 效果