Trans.nvim/lua/Trans/core/backend.lua

87 lines
2.4 KiB
Lua
Raw Normal View History

2023-03-24 00:56:36 +08:00
local Trans = require 'Trans'
2023-03-14 18:17:07 +08:00
---@class TransBackend
---@field no_wait? boolean whether need to wait for the result
---@field name string
---@field display_text string?
---@field conf table? @User specific config
2023-03-14 18:17:07 +08:00
---@class TransBackendOnline: TransBackend
---@field uri string @request uri
---@field method 'get' | 'post' @request method
---@field formatter fun(body: table, data: TransData): TransResult|false|nil transform response body to TransResult
---@field get_query fun(data: TransData): table<string, any> @get query table
---@field error_message? fun(errorCode) @get error message
-- -@field header table<string, string>|fun(data: TransData): table<string, string> @request header
2023-03-14 18:17:07 +08:00
---@class TransBackendOffline: TransBackend
---@field query fun(data: TransData)
2023-03-22 21:55:08 +08:00
2023-03-18 21:56:12 +08:00
---@class TransBackendCore
2023-05-06 11:38:37 +08:00
local M = {
---@type table<string, TransBackend> backendname -> backend source
sources = {},
2023-05-06 11:38:37 +08:00
}
local m_util = {}
2023-07-13 10:15:38 +08:00
-- INFO :Template method for online query
---@param data TransData @data
---@param backend TransBackendOnline @backend
function M.do_query(data, backend)
local name = backend.name
local formatter = backend.formatter
-- local header = type(backend.header) == 'function' and backend.header(data) or backend.header
local function handle(output)
local status, body = pcall(vim.json.decode, output.body)
if not status or not body then
data.result[name] = false
return
end
data.result[name] = formatter(body, data)
end
Trans.curl[backend.method](backend.uri, {
query = backend.get_query(data),
callback = handle,
--- FIXME :
header = header,
})
end
2023-07-13 10:15:38 +08:00
-- TODO :Implement all of utility functions
M.util = m_util
2023-07-13 10:15:38 +08:00
-- INFO :Parse configuration file
local path = Trans.conf.dir .. '/Trans.json'
local file = io.open(path, 'r')
local user_conf = {}
if file then
local content = file:read '*a'
user_conf = vim.json.decode(content) or user_conf
file:close()
end
2023-07-13 10:15:38 +08:00
-- WARNING : [Breaking change] 'Trans.json' should use json object instead of array
---@class Trans
---@field backend TransBackendCore
return setmetatable(M, {
__index = function(self, name)
---@type TransBackend
local backend = require('Trans.backend.' .. name)
backend.conf = user_conf[name]
self.sources[name] = backend
return backend
end,
})