Skip to content

Commit 6f09322

Browse files
add vietnamese translation (axios#55)
remove old font-family, let the browser decide fix a bug in inert.config.js add the ability to use a custom main title and custom sans font for any translations Co-authored-by: Jay <[email protected]>
1 parent b1af103 commit 6f09322

20 files changed

+1037
-15
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
[![Netlify Status](https://api.netlify.com/api/v1/badges/09768f21-08e2-487b-8c7a-7fb084bbf99d/deploy-status)](https://app.netlify.com/sites/axios-docs/deploys)
44
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.0-4baaaa.svg)](code_of_conduct.md)
55

6-
The documentation page was built using [`inert static site generator`](https://github.com/codemaster138/inert). It works like this: inside the `docs` folder, there is a folder named `_src` (because GitHub automatically hides all files and folders starting with `_` from viewers on GitHub Pages). Inside this folder, there are some configuration, templates, styles, and, most importantly, in the `posts` folder, multiple markdown files containing the documentation.
6+
The documentation page was built using [`inert static site generator`](https://github.com/codemaster138/inert). It works like this: Inside the root folder, there are some configuration, templates, styles, and, most importantly, in the `posts` folder, multiple markdown files containing the documentation.
77

88
To compile the documentation page, one must first install the inert CLI:
99

1010
```bash
1111
npm i -g inert-cli # yarn global add inert-cli
1212
```
1313

14-
Now, open a terminal, navigate into the folder `docs/_src` and run the following command:
14+
Now, open a terminal and run the following command:
1515

1616
```bash
1717
inert build
1818
```
1919

20-
Inert will convert the markdown files into HTML, insert them into the templates, compile the `SCSS` styles and write the output into the `docs` directory.
20+
Inert will convert the markdown files into HTML, insert them into the templates, compile the `SCSS` styles and write the output into the `public` directory.
2121

2222
This website was generously created by [@codemaster138](https://github.com/codemaster138)

inert.config.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const kuConfig = require("./ku.lang.js");
2323
const esConfig = require('./es.lang.js');
2424
const trConfig = require("./tr.lang.js");
2525
const krConfig = require("./kr.lang.js");
26+
const viConfig = require("./vi.lang.js");
2627
const faConfig = require("./fa.lang.js");
2728

2829
// List of languages
@@ -82,6 +83,12 @@ const langs = [
8283
prefix: "/kr/",
8384
config: krConfig,
8485
},
86+
{
87+
dir: "ltr",
88+
name: "Tiếng Việt",
89+
prefix: "/vi/",
90+
config: viConfig,
91+
},
8592
{
8693
dir: "rtl",
8794
name: "فارسی",
@@ -208,7 +215,10 @@ module.exports = {
208215
...langs
209216
.map((lang) => [
210217
singleHTMLBuild(lang.config),
211-
writeFile(`:${lang.prefix === '/' ? 'output:/index.html' : lang.prefix.slice(1, -1)}Output:/index.html`),
218+
writeFile(lang.prefix === '/'
219+
? ':output:/index.html'
220+
: `:${lang.prefix.slice(1, -1)}Output:/index.html`
221+
),
212222
])
213223
.flat(),
214224
],

posts/vi/api_intro.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: 'Axios API'
3+
description: 'Bản tham khảo Axios API'
4+
prev_title: 'POST Request'
5+
prev_link: '/vi/docs/post_example'
6+
next_title: 'Axios Instance'
7+
next_link: '/vi/docs/instance'
8+
---
9+
10+
Có thể truyền cấu hình có liên quan vào `axios` để tạo ra request.
11+
12+
##### axios(config)
13+
14+
```js
15+
// Gửi một POST request
16+
axios({
17+
method: 'post',
18+
url: '/user/12345',
19+
data: {
20+
lastName: ''
21+
firstName: 'Thường Kiệt',
22+
}
23+
});
24+
```
25+
26+
```js
27+
// Truy xuất ảnh từ xa bằng GET request trong node.js
28+
axios({
29+
method: 'get',
30+
url: 'http://bit.ly/2mTM3nY',
31+
responseType: 'stream'
32+
})
33+
.then(function (response) {
34+
response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
35+
});
36+
```
37+
38+
##### axios(url[, config])
39+
40+
```js
41+
// Gửi một GET request (chức năng mặc định)
42+
axios('/user/12345');
43+
```
44+
45+
### Alias cho phương thức request
46+
47+
Để cho tiện, thư viện đã cung cấp sẵn alias cho tất cả phương thức request được hỗ trợ.
48+
49+
##### axios.request(config)
50+
##### axios.get(url[, config])
51+
##### axios.delete(url[, config])
52+
##### axios.head(url[, config])
53+
##### axios.options(url[, config])
54+
##### axios.post(url[, data[, config]])
55+
##### axios.put(url[, data[, config]])
56+
##### axios.patch(url[, data[, config]])
57+
58+
**LƯU Ý:** Khi sử dụng các phương thức alias, các thuộc tính `url`, `method`, và `data` đều không cần phải được chỉ định trong cấu hình.

posts/vi/cancellation.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: 'Bãi bỏ Request'
3+
prev_title: 'Xử trí lỗi'
4+
prev_link: '/vi/docs/handling_errors'
5+
next_title: 'Phần thân URL-Encoding'
6+
next_link: '/vi/docs/urlencoded'
7+
---
8+
9+
Bạn có thể bãi bỏ request bằng cách sử dụng *token bãi bỏ*
10+
11+
> API token bãi bỏ trong axios là dựa trên [đề xuất cancelable promise](https://github.com/tc39/proposal-cancelable-promises) đã bị thu hồi.
12+
13+
Bạn có thể tạo ra một token bãi bỏ bằng cách sử dụng phương thức factory `CancelToken.source` như sau:
14+
15+
```js
16+
const CancelToken = axios.CancelToken;
17+
const source = CancelToken.source();
18+
19+
axios.get('/user/12345', {
20+
cancelToken: source.token
21+
}).catch(function (thrown) {
22+
if (axios.isCancel(thrown)) {
23+
console.log('Request đã được bãi bỏ', thrown.message);
24+
} else {
25+
// xử trí lỗi
26+
}
27+
});
28+
29+
axios.post('/user/12345', {
30+
name: 'tên mới'
31+
}, {
32+
cancelToken: source.token
33+
})
34+
35+
// bãi bỏ request (tham số message là tùy chọn)
36+
source.cancel('Thao tác đã được người dùng bãi bỏ.');
37+
```
38+
39+
Bạn còn có thể tạo ra một token bãi bỏ bằng cách truyền một hàm thực thi vào constructor `CancelToken`:
40+
41+
```js
42+
const CancelToken = axios.CancelToken;
43+
let cancel;
44+
45+
axios.get('/user/12345', {
46+
cancelToken: new CancelToken(function executor(c) {
47+
// Hàm thực thi nhận vào một tham số là hàm bãi bỏ, lưu nó lại
48+
cancel = c;
49+
})
50+
});
51+
52+
// bãi bỏ request
53+
cancel();
54+
```
55+
56+
> Lưu ý: bạn có thể dùng chung một token bãi bỏ để bãi bỏ nhiều request.

posts/vi/config_defaults.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: 'Cấu hình Mặc định'
3+
prev_title: 'Kết cấu Response'
4+
prev_link: '/vi/docs/res_schema'
5+
next_title: 'Bộ đón chặn'
6+
next_link: '/vi/docs/interceptors'
7+
---
8+
9+
## Cấu hình Mặc định
10+
11+
Bạn có thể chỉ định cấu hình mặc định sẽ được áp dụng cho mọi request.
12+
13+
### Mặc định cho toàn cục
14+
15+
```js
16+
axios.defaults.baseURL = 'https://api.example.com';
17+
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
18+
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
19+
```
20+
21+
### Mặc định cho instance tự đặt
22+
23+
```js
24+
// Đặt cấu hình mặc định lúc tạo ra instance
25+
const instance = axios.create({
26+
baseURL: 'https://api.example.com'
27+
});
28+
29+
// Sửa đổi cấu hình trị mặc định sau khi tạo ra instance
30+
instance.defaults.headers.common['Authorization'] = AUTH_TOKEN;
31+
```
32+
33+
### Thứ tự ưu tiên của cấu hình
34+
35+
Cấu hình sẽ được hợp lại theo thứ tự ưu tiên. Thứ tự lần lượt là mặc định của thư viện được đặt trong [lib/defaults.js](https://github.com/axios/axios/blob/master/lib/defaults.js#L28), tiếp đến là thuộc tính `defaults` của instance, rồi cuối cùng là đối số `config` cho request. Cái đi sau sẽ được ưu tiên hơn cái đi trước. Sau đây là một ví dụ.
36+
37+
```js
38+
// Tạo ra một instance dùng cấu hình mặc định do thư viện cung cấp
39+
// Tại thời điểm này giá trị cấu hình timeout là `0` giống như trong mặc định của thư viện
40+
const instance = axios.create();
41+
42+
// Override giá trị timeout mặc định của thư viện
43+
// Giờ tất cả request mà dùng instance này thì sẽ chờ 2,5 giây trước khi tính hết thời gian
44+
instance.defaults.timeout = 2500;
45+
46+
// Override giá trị timeout cho riêng request này vì ta biết trước là nó tốn thời gian dài
47+
instance.get('/request-lâu', {
48+
timeout: 5000
49+
});
50+
```

posts/vi/example.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
title: 'Ví dụ cơ bản'
3+
description: 'Ví dụ nhỏ về cách sử dụng Axios'
4+
prev_title: 'Giới thiệu'
5+
prev_link: '/vi/docs/intro'
6+
next_title: 'POST Request'
7+
next_link: '/vi/docs/post_example'
8+
---
9+
10+
## Lưu ý: Cách dùng với CommonJS
11+
Để có được định nghĩa kiểu dữ liệu TypeScript (cho intellisense / autocomplete) trong khi sử dụng phép import của CommonJS (tức `require()`), hãy sử dụng phương pháp sau đây:
12+
13+
```js
14+
const axios = require('axios').default;
15+
16+
// Giờ axios.<phương thức> sẽ cung cấp autocomplete và định nghĩa kiểu dữ liệu tham số
17+
```
18+
19+
# Ví dụ
20+
21+
Phát đi một `GET` request
22+
23+
```js
24+
const axios = require('axios');
25+
26+
// Tạo một request để truy xuất người dùng ứng với ID cho sẵn:
27+
axios.get('/user?ID=12345')
28+
.then(function (response) {
29+
// xử trí khi thành công
30+
console.log(response);
31+
})
32+
.catch(function (error) {
33+
// xử trí khi bị lỗi
34+
console.log(error);
35+
})
36+
.then(function () {
37+
// luôn luôn được thực thi
38+
});
39+
40+
// Cái request bên trên cũng có thể được làm bằng cách sau, tùy ý
41+
axios.get('/user', {
42+
params: {
43+
ID: 12345
44+
}
45+
})
46+
.then(function (response) {
47+
console.log(response);
48+
})
49+
.catch(function (error) {
50+
console.log(error);
51+
})
52+
.then(function () {
53+
// luôn luôn được thực thi
54+
});
55+
56+
// Nếu muốn sử dụng async/await thì viết như sau
57+
async function getUser() {
58+
try {
59+
const response = await axios.get('/user?ID=12345');
60+
console.log(response);
61+
} catch (error) {
62+
console.error(error);
63+
}
64+
}
65+
```
66+
67+
> **LƯU Ý:** `async/await` là một phần của ECMAScript 2017, không được hỗ trợ trong Internet Explorer và những trình duyệt cũ, cho nên khi sử dụng thì phải để ý.

posts/vi/handling_errors.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: 'Xử trí lỗi'
3+
prev_title: 'Bộ đón chặn'
4+
prev_link: '/vi/docs/interceptors'
5+
next_title: 'Bãi bỏ Request'
6+
next_link: '/vi/docs/cancellation'
7+
---
8+
9+
```js
10+
axios.get('/user/12345')
11+
.catch(function (error) {
12+
if (error.response) {
13+
// Request đã được tạo ra và server đã hồi đáp với một mã trạng thái
14+
// nằm ra ngoài tầm 2xx
15+
console.log(error.response.data);
16+
console.log(error.response.status);
17+
console.log(error.response.headers);
18+
} else if (error.request) {
19+
// Request đã được tạo ra nhưng không nhận được hồi đáp nào
20+
// Trong trình duyệt, `error.request` là instance của XMLHttpRequest
21+
// còn trong node.js thì nó là instance của http.ClientRequest
22+
console.log(error.request);
23+
} else {
24+
// Điều gì đó đã xảy ra trong bước thiết lập request rồi gây nên lỗi
25+
console.log('Lỗi', error.message);
26+
}
27+
console.log(error.config);
28+
});
29+
```
30+
31+
Sử dụng tùy chọn cấu hình `validateStatus`, bạn có thể định nghĩa mã HTTP nào nên được tính là lỗi để ném ra.
32+
33+
```js
34+
axios.get('/user/12345', {
35+
validateStatus: function (status) {
36+
return status < 500; // Chỉ phân giải nếu như mã trạng thái thấp hơn 500
37+
}
38+
})
39+
```
40+
41+
Sử dụng phương thức `toJSON` để lấy đối tượng có thêm nhiều thông tin hơn về lỗi HTTP.
42+
43+
```js
44+
axios.get('/user/12345')
45+
.catch(function (error) {
46+
console.log(error.toJSON());
47+
});
48+
```

posts/vi/instance.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: 'Axios instance'
3+
prev_title: 'Axios API'
4+
prev_link: '/vi/docs/api_intro'
5+
next_title: 'Cấu hình Request'
6+
next_link: '/vi/docs/req_config'
7+
---
8+
9+
### Tạo ra instance
10+
11+
Bạn có thể tạo ra một instance mới của axios bằng cấu hình tự đặt.
12+
13+
##### axios.create([config])
14+
15+
```js
16+
const instance = axios.create({
17+
baseURL: 'https://tên-miền-nào-đó.com/api/',
18+
timeout: 1000,
19+
headers: {'X-Custom-Header': 'foobar'}
20+
});
21+
```
22+
23+
### Phương thức instance
24+
25+
Các phương thức instance sẵn có đều được liệt kê dưới đây. Cấu hình được chỉ định thì sẽ được hợp chung với cấu hình instance.
26+
27+
##### axios#request(config)
28+
##### axios#get(url[, config])
29+
##### axios#delete(url[, config])
30+
##### axios#head(url[, config])
31+
##### axios#options(url[, config])
32+
##### axios#post(url[, data[, config]])
33+
##### axios#put(url[, data[, config]])
34+
##### axios#patch(url[, data[, config]])
35+
##### axios#getUri([config])

0 commit comments

Comments
 (0)