|
1 |
| ---[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]] |
2 |
| -local ____exports = {} |
| 1 | +local M = {} |
3 | 2 | local curl = require("plenary.curl")
|
4 | 3 | local job = require("plenary.job")
|
5 | 4 | local notify = require("forem-nvim.notify")
|
6 | 5 | local article = require("forem-nvim.article")
|
7 |
| -____exports.key = function() return vim.env.FOREM_API_KEY end |
8 |
| -local baseUrl = "https://dev.to/api" |
9 |
| -local function handleAsyncError(response) |
10 |
| - notify.error("Error: " .. tostring(response.body.error)) |
| 6 | + |
| 7 | +function M.key() |
| 8 | + return vim.env.FOREM_API_KEY |
11 | 9 | end
|
12 |
| -____exports.handleError = function(response, onSuccess) |
13 |
| - local startStatus = string.sub(response.status, 1, 2) |
14 |
| - local ____temp_0 |
15 |
| - if startStatus == "20" then |
16 |
| - ____temp_0 = onSuccess(response.body) |
17 |
| - else |
18 |
| - ____temp_0 = notify.error("Error: " .. tostring(response.body.error)) |
19 |
| - end |
20 |
| - return ____temp_0 |
| 10 | + |
| 11 | +local BASE_URL = "https://dev.to/api" |
| 12 | + |
| 13 | +local function handle_async_error(response) |
| 14 | + notify.error("Error: " .. tostring(response.body.error)) |
| 15 | +end |
| 16 | + |
| 17 | +function M.handle_error(response, on_success) |
| 18 | + local start_status = string.sub(response.status, 1, 2) |
| 19 | + |
| 20 | + if start_status == "20" then |
| 21 | + on_success(response.body) |
| 22 | + else |
| 23 | + notify.error("Error: " .. tostring(response.body.error)) |
| 24 | + end |
21 | 25 | end
|
22 |
| -local function request(requestFunction, endpoint, options) |
23 |
| - local parameters = vim.tbl_extend( |
24 |
| - "force", |
25 |
| - { |
26 |
| - url = baseUrl .. endpoint, |
27 |
| - headers = { |
28 |
| - ["api-key"] = ____exports.key(), |
29 |
| - content_type = "application/json", |
30 |
| - accept = "application/vnd.forem.api-v1+json" |
31 |
| - } |
32 |
| - }, |
33 |
| - options |
| 26 | + |
| 27 | +local function request(request_function, endpoint, options) |
| 28 | + local parameters = vim.tbl_extend( |
| 29 | + "force", |
| 30 | + { |
| 31 | + url = BASE_URL .. endpoint, |
| 32 | + headers = { |
| 33 | + ["api-key"] = M.key(), |
| 34 | + content_type = "application/json", |
| 35 | + accept = "application/vnd.forem.api-v1+json" |
| 36 | + } |
| 37 | + }, |
| 38 | + options |
| 39 | + ) |
| 40 | + local response = request_function(parameters) |
| 41 | + if response.body then |
| 42 | + return vim.tbl_extend( |
| 43 | + "force", |
| 44 | + response, |
| 45 | + { body = vim.fn.json_decode(response.body) } |
34 | 46 | )
|
35 |
| - local response = requestFunction(parameters) |
36 |
| - if response.body then |
37 |
| - return vim.tbl_extend( |
38 |
| - "force", |
39 |
| - response, |
40 |
| - {body = vim.fn.json_decode(response.body)} |
41 |
| - ) |
42 |
| - end |
43 |
| - return response |
| 47 | + end |
| 48 | + return response |
44 | 49 | end
|
45 |
| -local function requestAsync(method, endpoint, options, onSuccess, onError) |
46 |
| - return job:new({ |
47 |
| - command = "curl", |
48 |
| - args = { |
49 |
| - "-X", |
50 |
| - method, |
51 |
| - "-H", |
52 |
| - "Content-Type: application/json", |
53 |
| - "-H", |
54 |
| - "Accept: application/vnd.forem.api-v1+json", |
55 |
| - "-H", |
56 |
| - "api-key: " .. tostring(____exports.key()), |
57 |
| - "-d", |
58 |
| - vim.fn.json_encode(options), |
59 |
| - baseUrl .. endpoint |
60 |
| - }, |
61 |
| - on_exit = function(job, code) |
62 |
| - vim.schedule(function() |
63 |
| - local result = table.concat( |
64 |
| - job:result(), |
65 |
| - "\n" |
66 |
| - ) |
67 |
| - local response = vim.fn.json_decode(result) |
68 |
| - if code == 0 then |
69 |
| - onSuccess(response) |
70 |
| - return |
71 |
| - end |
72 |
| - handleAsyncError(response) |
73 |
| - if onError then |
74 |
| - onError(response) |
75 |
| - end |
76 |
| - end) |
| 50 | + |
| 51 | +local function request_async(method, endpoint, options, on_success, on_error) |
| 52 | + -- TODO: Check if this could be done with `vim.system` |
| 53 | + return job:new({ |
| 54 | + command = "curl", |
| 55 | + args = { |
| 56 | + "-X", |
| 57 | + method, |
| 58 | + "-H", |
| 59 | + "Content-Type: application/json", |
| 60 | + "-H", |
| 61 | + "Accept: application/vnd.forem.api-v1+json", |
| 62 | + "-H", |
| 63 | + "api-key: " .. tostring(M.key()), |
| 64 | + "-d", |
| 65 | + vim.fn.json_encode(options), |
| 66 | + BASE_URL .. endpoint |
| 67 | + }, |
| 68 | + on_exit = function(this_job, code) |
| 69 | + vim.schedule(function() |
| 70 | + local result = table.concat( |
| 71 | + this_job:result(), |
| 72 | + "\n" |
| 73 | + ) |
| 74 | + local response = vim.fn.json_decode(result) |
| 75 | + if code == 0 then |
| 76 | + on_success(response) |
| 77 | + return |
77 | 78 | end
|
78 |
| - }):start() |
| 79 | + handle_async_error(response) |
| 80 | + if on_error then |
| 81 | + on_error(response) |
| 82 | + end |
| 83 | + end) |
| 84 | + end |
| 85 | + }):start() |
79 | 86 | end
|
80 |
| -local function get(endpoint, onSuccess, onError) |
81 |
| - return requestAsync( |
82 |
| - "GET", |
83 |
| - endpoint, |
84 |
| - {}, |
85 |
| - onSuccess, |
86 |
| - onError |
87 |
| - ) |
| 87 | + |
| 88 | +local function get(endpoint, on_success, on_error) |
| 89 | + return request_async( |
| 90 | + "GET", |
| 91 | + endpoint, |
| 92 | + {}, |
| 93 | + on_success, |
| 94 | + on_error |
| 95 | + ) |
88 | 96 | end
|
| 97 | + |
89 | 98 | local function put(endpoint, body)
|
90 |
| - return request(curl.put, endpoint, {body = body}) |
| 99 | + return request(curl.put, endpoint, { body = body }) |
91 | 100 | end
|
| 101 | + |
92 | 102 | local function post(endpoint, body)
|
93 |
| - return request(curl.post, endpoint, {body = body}) |
| 103 | + return request(curl.post, endpoint, { body = body }) |
94 | 104 | end
|
95 |
| -____exports.myArticles = function(onSuccess, onError) return get("/articles/me/all", onSuccess, onError) end |
96 |
| -____exports.saveArticle = function(id, content) return put( |
| 105 | + |
| 106 | +function M.my_articles(on_success, on_error) return get("/articles/me/all", on_success, on_error) end |
| 107 | + |
| 108 | +function M.save_article(id, content) |
| 109 | + return put( |
97 | 110 | "/articles/" .. tostring(id),
|
98 |
| - vim.fn.json_encode({article = {body_markdown = content}}) |
99 |
| -) end |
100 |
| -____exports.newArticle = function(title) return post( |
| 111 | + vim.fn.json_encode({ article = { body_markdown = content } }) |
| 112 | + ) |
| 113 | +end |
| 114 | + |
| 115 | +function M.new_article(title) |
| 116 | + return post( |
101 | 117 | "/articles",
|
102 |
| - vim.fn.json_encode({article = {body_markdown = article.getTemplate(title)}}) |
103 |
| -) end |
104 |
| -____exports.feed = function(onSuccess, onError) return get("/articles", onSuccess, onError) end |
105 |
| -____exports.getArticle = function(id, onSuccess, onError) return get( |
| 118 | + vim.fn.json_encode({ article = { body_markdown = article.get_template(title) } }) |
| 119 | + ) |
| 120 | +end |
| 121 | + |
| 122 | +function M.feed(on_success, on_error) return get("/articles", on_success, on_error) end |
| 123 | + |
| 124 | +function M.get_article(id, on_success, on_error) |
| 125 | + return get( |
106 | 126 | "/articles/" .. tostring(id),
|
107 |
| - onSuccess, |
108 |
| - onError |
109 |
| -) end |
110 |
| -____exports.getArticleByPath = function(path, onSuccess, onError) return get("/articles/" .. path, onSuccess, onError) end |
111 |
| -return ____exports |
| 127 | + on_success, |
| 128 | + on_error |
| 129 | + ) |
| 130 | +end |
| 131 | + |
| 132 | +function M.get_article_by_path(path, on_success, on_error) return get("/articles/" .. path, on_success, on_error) end |
| 133 | + |
| 134 | +return M |
0 commit comments