Skip to content

Commit

Permalink
Merge pull request #25 from vicanso/main
Browse files Browse the repository at this point in the history
version 0.1.13
  • Loading branch information
vicanso authored Jul 2, 2023
2 parents 289064c + f55e287 commit 4ed2f10
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 7 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

All notable changes to this project will be documented in this file.

## [0.1.13] - 2023-07-02

### Bug Fixes

- Fix parse file of header

### Features

- Support import from curl command

### Miscellaneous Tasks

- Update install cli script

### Refactor

- Enhance query string format

## [0.1.12] - 2023-06-29

### Bug Fixes
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ build:
clean:
cd src-tauri && cargo clean
install-cli:
cargo install tauri-cli --version 1.3.0
cargo install tauri-cli --version 1.4.0
orm:
cd src-tauri && sea-orm-cli generate entity --with-serde=both \
-u "sqlite:///~/Library/Application Support/com.bigtree.cyberapi/my_db.db" \
-o src/entities
version:
git cliff --unreleased --tag 0.1.12 --prepend CHANGELOG.md
git cliff --unreleased --tag 0.1.13 --prepend CHANGELOG.md
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"naive-ui": "^2.34.4",
"pinia": "^2.1.4",
"pretty-bytes": "^6.1.0",
"shellwords": "^1.0.1",
"ulid": "^2.3.0",
"vue": "^3.3.4",
"vue-i18n": "^9.2.2",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cyberapi"
version = "0.1.12"
version = "0.1.13"
description = "API tool based on tauri, it is smaller and faster."
authors = ["tree.xie@outlook.com"]
license = "Apache License 2.0"
Expand Down
22 changes: 21 additions & 1 deletion src/commands/import_api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Promise } from "bluebird";
import { get, uniq, forEach, has } from "lodash-es";

import dayjs from "dayjs";
import { ulid } from "ulid";
import { SettingType } from "../stores/api_setting";
import { APIFolder, createAPIFolder, newDefaultAPIFolder } from "./api_folder";
import {
Expand All @@ -16,6 +17,10 @@ import {
Variable,
VariableCategory,
} from "./variable";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import parseCurl from "../helpers/curl";
import { i18nCommon } from "../i18n";

interface PostManSetting {
name: string;
Expand Down Expand Up @@ -266,6 +271,21 @@ export async function importAPI(params: {
settings: [],
folders: [],
};
if (params.fileData.startsWith("curl")) {
const req = parseCurl(params.fileData);
const id = ulid();

await createAPISetting({
category: SettingType.HTTP,
collection: params.collection,
name: i18nCommon("untitled"),
id,
setting: JSON.stringify(req),
createdAt: dayjs().format(),
updatedAt: dayjs().format(),
});
return [id];
}
const json = JSON.parse(params.fileData);
const environments: Variable[] = [];
if (has(json, "item")) {
Expand Down
4 changes: 2 additions & 2 deletions src/components/ExDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ const ImportEditor = defineComponent({
const topIDList = await importAPI({
category: currentTab.value,
collection: props.collection,
fileData: fileData.value,
fileData: fileData.value.trim(),
});
// 如果指定了目录
if (props.folder && topIDList.length) {
Expand Down Expand Up @@ -239,7 +239,7 @@ const ImportEditor = defineComponent({
>
<NTabPane
name={ImportCategory.Text}
tab="JSON"
tab="JSON / curl"
onVnodeMounted={() => {
this.initEditor();
}}
Expand Down
161 changes: 161 additions & 0 deletions src/helpers/curl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import * as words from "shellwords";
import { forEach } from "lodash-es";
import { isJSON } from "./util";

export default function(s) {
const params = parse(s);
let contentType = "";
const headers = [];
forEach(params.header, (value, key) => {
if (key.toLowerCase() === "content-type") {
contentType = value;
return;
}
headers.push({
key,
value,
enabled: true,
});
});
if (!contentType && isJSON(params.body)) {
contentType = "application/json";
}
const index = contentType.indexOf(";");
if (index !== -1) {
contentType = contentType.substring(0, index);
}
const result = new URL(params.url);
const query = [];
result.searchParams.forEach((value, key) => {
query.push({
key,
value,
enabled: true,
});
});
return {
method: params.method,
uri: `${result.origin}${result.pathname}`,
body: params.body,
contentType,
headers,
query,
}
}

function parse(s) {
if (0 != s.indexOf('curl ')) return
var args = rewrite(words.split(s))
var out = { method: 'GET', header: {} }
var state = ''

args.forEach(function (arg) {
switch (true) {
case isURL(arg):
out.url = arg
break;

case arg == '-A' || arg == '--user-agent':
state = 'user-agent'
break;

case arg == '-H' || arg == '--header':
state = 'header'
break;

case arg == '-d' || arg == '--data' || arg == '--data-ascii':
state = 'data'
break;

case arg == '-u' || arg == '--user':
state = 'user'
break;

case arg == '-I' || arg == '--head':
out.method = 'HEAD'
break;

case arg == '-X' || arg == '--request':
state = 'method'
break;

case arg == '-b' || arg == '--cookie':
state = 'cookie'
break;

case arg == '--compressed':
out.header['Accept-Encoding'] = out.header['Accept-Encoding'] || 'deflate, gzip'
break;

case !!arg:
switch (state) {
case 'header':
var field = parseField(arg)
out.header[field[0]] = field[1]
state = ''
break;
case 'user-agent':
out.header['User-Agent'] = arg
state = ''
break;
case 'data':
if (out.method == 'GET' || out.method == 'HEAD') out.method = 'POST'
out.header['Content-Type'] = out.header['Content-Type'] || 'application/x-www-form-urlencoded'
out.body = out.body
? out.body + '&' + arg
: arg
state = ''
break;
case 'user':
out.header['Authorization'] = 'Basic ' + window.btoa(arg)
state = ''
break;
case 'method':
out.method = arg
state = ''
break;
case 'cookie':
out.header['Set-Cookie'] = arg
state = ''
break;
}
break;
}
})

return out
}

/**
* Rewrite args for special cases such as -XPUT.
*/

function rewrite(args) {
return args.reduce(function (args, a) {
if (0 == a.indexOf('-X')) {
args.push('-X')
args.push(a.slice(2))
} else {
args.push(a)
}

return args
}, [])
}

/**
* Parse header field.
*/

function parseField(s) {
return s.split(/: ?(.+)/)
}

/**
* Check if `s` looks like a url.
*/

function isURL(s) {
// TODO: others at some point
return /^https?:\/\//.test(s)
}
1 change: 1 addition & 0 deletions src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export default {
keywordFilterPlaceholder: "Please enter a keyword",
selectFile: "Please select a file",
saveToDownloadSuccess: "File is save to download successful",
untitled: "Untitled",
},
dashboard: {
newCollection: "Create API group",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/uk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default {
keywordFilterPlaceholder: "Введіть ключове слово",
selectFile: "Виберіть файл",
saveToDownloadSuccess: "Файл збережено для успішного завантаження",
untitled: "Без назви",
},
dashboard: {
newCollection: "Створити групу API",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default {
keywordFilterPlaceholder: "请输入关键字",
selectFile: "请选择文件",
saveToDownloadSuccess: "文件已成功保存至下载目录",
untitled: "未命名",
},
dashboard: {
newCollection: "创建API分组",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3368,6 +3368,11 @@ shebang-regex@^3.0.0:
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172"
integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==

shellwords@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-1.0.1.tgz#c00c509665368b249382121d9eb257cbe3af123e"
integrity sha512-Fd5KAbmR0kf6GL4bYJTeHdSKW1mCu6rMxdZcZ4l/hD9wRpBB6RxA01TdmegXWzIhJARyYDFs8EAdnpAsRaDGWw==

slash@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
Expand Down

0 comments on commit 4ed2f10

Please sign in to comment.