Skip to content

Commit 54bac9e

Browse files
mitrocjakubdrozdek
mitroc
authored andcommitted
Translate 'Fetch' page (#22)
1 parent ac3ec9c commit 54bac9e

File tree

8 files changed

+170
-170
lines changed

8 files changed

+170
-170
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
async function getUsers(names) {
3-
/* your code */
3+
/* twój kod */
44
}

5-network/01-fetch/01-fetch-users/_js.view/test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
describe("getUsers", function() {
22

3-
it("gets users from GitHub", async function() {
4-
let users = await getUsers(['iliakan', 'remy', 'no.such.users']);
3+
it("pobiera użytkowników GitHuba", async function() {
4+
let users = await getUsers(['iliakan', 'remy', 'nieistniejący.użytkownik']);
55
assert.equal(users[0].login, 'iliakan');
66
assert.equal(users[1].login, 'remy');
77
assert.equal(users[2], null);

5-network/01-fetch/01-fetch-users/solution.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

2-
To fetch a user we need: `fetch('https://api.github.com/users/USERNAME')`.
2+
Do pobrania użytkownika wykorzystamy: `fetch('https://api.github.com/users/USERNAME')`.
33

4-
If the response has status `200`, call `.json()` to read the JS object.
4+
Jeżeli odpowiedź zostanie zwrócona ze statusem `200`, wywołamy metodę `.json()`, aby móc odczytać javascriptowy obiekt.
55

6-
Otherwise, if a `fetch` fails, or the response has non-200 status, we just return `null` in the resulting arrray.
6+
Jeżeli natomiast `fetch` się nie powiedzie lub status odpowiedzi będzie inny niz 200, wówczas w tablicy wynikowej zwracamy po prostu `null`.
77

8-
So here's the code:
8+
Kod wygląda następująco:
99

1010
```js demo
1111
async function getUsers(names) {
@@ -33,8 +33,8 @@ async function getUsers(names) {
3333
}
3434
```
3535

36-
Please note: `.then` call is attached directly to `fetch`, so that when we have the response, it doesn't wait for other fetches, but starts to read `.json()` immediately.
36+
Zauważ, że metoda `.then` jest dołączona bezpośrednio do `fetch`, więc nie czeka ona na kolejne żądania, lecz jak tylko otrzyma odpowiedź, natychmiast odczytuje ją przy użyciu metody `.json()`.
3737

38-
If we used `await Promise.all(names.map(name => fetch(...)))`, and call `.json()` on the results, then it would wait for all fetches to respond. By adding `.json()` directly to each `fetch`, we ensure that individual fetches start reading data as JSON without waiting for each other.
38+
Gdybyśmy jednak użyli `await Promise.all(names.map(name => fetch(...)))` i wywołali metodę `.json()` dopiero na rezultacie, wówczas musiałaby ona czekać, aż wszystkie żądania zwrócą swoje odpowiedzi. Dołączając `.json()` bezpośrednio do każdego zapytania `fetch` możemy być pewni, że pojedyncze zapytania zaczną odczytywać dane jako JSON, bez czekania nawzajem na siebie.
3939

40-
That's an example of how low-level Promise API can still be useful even if we mainly use `async/await`.
40+
Jest to przykład tego, jak przydatne może być niskopoziomowe Promise API, nawet jeżeli głównie korzystamy z `async/await`.
+8-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# Fetch users from GitHub
1+
# Pobierz użytkowników z GitHuba
22

3-
Create an async function `getUsers(names)`, that gets an array of GitHub logins, fetches the users from GitHub and returns an array of GitHub users.
3+
Stwórz asynchroniczną funkcję `getUsers(names)`, która otrzymuje tablicę z loginami do GitHuba, a następnie zwraca tablicę z odpowiadającymi im użytkownikami.
44

5-
The GitHub url with user information for the given `USERNAME` is: `https://api.github.com/users/USERNAME`.
5+
Informacje o użytkowniku przypisanym do `USERNAME`, znajdują się pod adresem: `https://api.github.com/users/USERNAME`.
66

7-
There's a test example in the sandbox.
7+
W naszym środowisku izolowanym znajduje się przykład testowy.
88

9-
Important details:
9+
Ważne informacje:
1010

11-
1. There should be one `fetch` request per user.
12-
2. Requests shouldn't wait for each other. So that the data arrives as soon as possible.
13-
3. If any request fails, or if there's no such user, the function should return `null` in the resulting array.
11+
1. Można wykonać tylko jedno żądanie `fetch` o dane użytkownika.
12+
2. Żądania nie powinny na siebie oczekiwać. Chodzi o to, aby dane dotarły jak najszybciej.
13+
3. Jeżeli żądanie się nie powiedzie lub nie będzie użytkownika o podanej nazwie, funkcja powinna zwrócić `null` w tablicy wynikowej.

5-network/01-fetch/article.md

+99-99
Large diffs are not rendered by default.

5-network/01-fetch/post.view/server.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let router = new Router();
88

99
router.post('/user', async (ctx) => {
1010
ctx.body = {
11-
message: "User saved."
11+
message: "Zapisano użytkownika."
1212
};
1313
});
1414

@@ -17,7 +17,7 @@ router.post('/image', async (ctx) => {
1717
limit: '1mb'
1818
});
1919
ctx.body = {
20-
message: `Image saved, size:${body.length}.`
20+
message: `Zapisano obraz, rozmiar:${body.length}.`
2121
};
2222
});
2323

5-network/02-formdata/article.md

+48-48
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11

22
# FormData
33

4-
This chapter is about sending HTML forms: with or without files, with additional fields and so on.
4+
W niniejszym rozdziale omówimy wysyłkę formularzy HTML: z plikami lub bez, z dodatkowymi polami i tak dalej.
55

6-
[FormData](https://xhr.spec.whatwg.org/#interface-formdata) objects can help with that. As you might have guessed, it's the object to represent HTML form data.
6+
Pomoże nam w tym obiekt [FormData](https://xhr.spec.whatwg.org/#interface-formdata). Jak zapewne się domyślasz, jest to obiekt reprezentujący dane formularza HTML.
77

8-
The constructor is:
8+
Konstruktor wygląda następująco:
99
```js
1010
let formData = new FormData([form]);
1111
```
1212

13-
If HTML `form` element is provided, it automatically captures its fields.
13+
Przechwyci on automatycznie wszystkie pola formularza HTML na stronie.
1414

15-
The special thing about `FormData` is that network methods, such as `fetch`, can accept a `FormData` object as a body. It's encoded and sent out with `Content-Type: form/multipart`.
15+
`FormData` posiada tę szczególną cechę, że metody sieciowe takie jak `fetch` mogą przyjmować obiekt `FormData` jako ciało. Jest on wówczas kodowany i wysyłany jako `Content-Type: form/multipart`.
1616

17-
From the server point of view, that looks like a usual form submission.
17+
Z perspektywy serwera wygląda to jak zwykłe przesłanie formularza.
1818

19-
## Sending a simple form
19+
## Wysyłanie prostego formularza
2020

21-
Let's send a simple form first.
21+
Na początek wyślijmy prosty formularz.
2222

23-
As you can see, that's almost one-liner:
23+
Jak widać, to niemal jedna linijka:
2424

2525
```html run autorun
2626
<form id="formElem">
27-
<input type="text" name="name" value="John">
28-
<input type="text" name="surname" value="Smith">
27+
<input type="text" name="name" value="Jan">
28+
<input type="text" name="surname" value="Kowalski">
2929
<input type="submit">
3030
</form>
3131

@@ -47,48 +47,48 @@ As you can see, that's almost one-liner:
4747
</script>
4848
```
4949

50-
In this example, the server code is not presented, as it's beyound our scope. The server accepts the POST request and replies "User saved".
50+
Kod serwera jest poza naszym zakresem zainteresowania, zatem nie pokazujemy go w tym przykładzie. W każdym razie serwer akceptuje żądanie POST i odpowiada komunikatem: "Użytkownik zapisany".
5151

52-
## FormData Methods
52+
## Metody FormData
5353

54-
We can modify fields in `FormData` with methods:
54+
Pola w `FormData` możemy zmieniać następującymi metodami:
5555

56-
- `formData.append(name, value)` - add a form field with the given `name` and `value`,
57-
- `formData.append(name, blob, fileName)` - add a field as if it were `<input type="file">`, the third argument `fileName` sets file name (not form field name), as it it were a name of the file in user's filesystem,
58-
- `formData.delete(name)` - remove the field with the given `name`,
59-
- `formData.get(name)` - get the value of the field with the given `name`,
60-
- `formData.has(name)` - if there exists a field with the given `name`, returns `true`, otherwise `false`
56+
- `formData.append(name, value)` - dodaj pole formularza o nazwie `name` i wartości `value`,
57+
- `formData.append(name, blob, fileName)` - dodaj pole tak jakby było znacznikiem `<input type="file">`; trzeci argument `fileName` ustawia nazwę pliku (nie nazwę formularza), tak jakby była nazwą pliku w systemie plików użytkownika,
58+
- `formData.delete(name)` - usuń pole `name`,
59+
- `formData.get(name)` - pobierz wartość pola `name`,
60+
- `formData.has(name)` - jeżeli istnieje pole `name`, zwróć `true`; w innym przypadku zwróć `false`
6161

62-
A form is technically allowed to have many fields with the same `name`, so multiple calls to `append` add more same-named fields.
62+
Formularz, z technicznego punktu widzenia, może mieć pól o nazwie `name`, tak więc wiele wywołań metody `append` doda wiele pól o tej samej nazwie.
6363

64-
There's also method `set`, with the same syntax as `append`. The difference is that `.set` removes all fields with the given `name`, and then appends a new field. So it makes sure there's only field with such `name`, the rest is just like `append`:
64+
Istnieje również metoda `set`, która ma taką samą składnię jak `append`. Różnica polega na tym, że `.set` usuwa wszystkie pola o nazwie `name`, a następnie dodaje nowe pole. Dzięki temu zapewnia, że istnieje tylko jedno pole o nazwie `name`. Pozostała część wygląda jak w metodzie `append`:
6565

6666
- `formData.set(name, value)`,
6767
- `formData.set(name, blob, fileName)`.
6868

69-
Also we can iterate over formData fields using `for..of` loop:
69+
Możemy również iterować po polach `formData`, używając pętli `for..of`:
7070

7171
```js run
7272
let formData = new FormData();
7373
formData.append('key1', 'value1');
7474
formData.append('key2', 'value2');
7575

76-
// List key/value pairs
76+
// Wylicz pary klucz/wartość
7777
for(let [name, value] of formData) {
78-
alert(`${name} = ${value}`); // key1=value1, then key2=value2
78+
alert(`${name}=${value}`); // key1=value1 oraz key2=value2
7979
}
8080
```
8181

82-
## Sending a form with a file
82+
## Wysyłanie formularza z plikiem
8383

84-
The form is always sent as `Content-Type: form/multipart`, this encoding allows to send files. So, `<input type="file">` fields are sent also, similar to a usual form submission.
84+
Formularz jest zawsze wysyłany jako `Content-Type: form/multipart`, gdyż takie kodowanie pozwala na wysyłkę plików. Tak więc pola `<input type="file">` są również wysyłane, podobnie jak ma to miejsce w zwykłym przesłaniu formularza.
8585

86-
Here's an example with such form:
86+
Oto przykład takiego formularza:
8787

8888
```html run autorun
8989
<form id="formElem">
90-
<input type="text" name="firstName" value="John">
91-
Picture: <input type="file" name="picture" accept="image/*">
90+
<input type="text" name="firstName" value="Jan">
91+
Obraz: <input type="file" name="picture" accept="image/*">
9292
<input type="submit">
9393
</form>
9494

@@ -110,21 +110,21 @@ Here's an example with such form:
110110
</script>
111111
```
112112

113-
## Sending a form with Blob data
113+
## Wysyłanie formularza z danymi typu Blob
114114

115-
As we've seen in the chapter <info:fetch>, it's easy to send dynamically generated binary data e.g. an image, as `Blob`. We can supply it directly as `fetch` parameter `body`.
115+
W rozdziale pt. "<info:fetch>" widzieliśmy, że wysyłka dynamicznie generowanych danych binarnych, np. obrazu jako `Blob`, jest dość prosta. Możemy go umieścić jako parametr `body` funkcji `fetch`.
116116

117-
In practice though, it's often convenient to send an image not separately, but as a part of the form, with additional fields, such as "name" and other metadata.
117+
W praktyce jednak często wygodniej jest wysłać obraz nie osobno, ale jako część formularza, z dodatkowymi polami, takimi jak "nazwa” i inne metadane.
118118

119-
Also, servers are usually more suited to accept multipart-encoded forms, rather than raw binary data.
119+
Ponadto serwery są zwykle lepiej przystosowane do akceptowania formularzy zakodowanych w postaci wieloczęściowej (ang. *multipart*) niż surowych danych binarnych.
120120

121-
This example submits an image from `<canvas>`, along with some other fields, as a form, using `FormData`:
121+
W tym przykładzie wysyłamy w formularzu obraz ze znacznika `<canvas>` wraz z innymi polami, używając do tego `FormData`:
122122

123123
```html run autorun height="90"
124124
<body style="margin:0">
125125
<canvas id="canvasElem" width="100" height="80" style="border:1px solid"></canvas>
126126

127-
<input type="button" value="Submit" onclick="submit()">
127+
<input type="button" value="Prześlij" onclick="submit()">
128128

129129
<script>
130130
canvasElem.onmousemove = function(e) {
@@ -138,7 +138,7 @@ This example submits an image from `<canvas>`, along with some other fields, as
138138
139139
*!*
140140
let formData = new FormData();
141-
formData.append("firstName", "John");
141+
formData.append("firstName", "Jan");
142142
formData.append("image", imageBlob, "image.png");
143143
*/!*
144144
@@ -154,36 +154,36 @@ This example submits an image from `<canvas>`, along with some other fields, as
154154
</body>
155155
```
156156

157-
Please note how the image `Blob` is added:
157+
Zwróć uwagę, w jaki sposób dodawany jest obraz jako `Blob`:
158158

159159
```js
160-
formData.append("image", imageBlob, "image.png");
160+
formData.append("image", imageBlob, "obraz.png");
161161
```
162162

163-
That's same as if there were `<input type="file" name="image">` in the form, and the visitor submitted a file named `"image.png"` (3rd argument) with the data `imageBlob` (2nd argument) from their filesystem.
163+
To tak, jakby w formularzu był znacznik `<input type="file" name="image">`, a użytkownik załadował z systemu plików plik o nazwie `"obraz.png"` (trzeci argument) jako `imageBlob` (drugi argument).
164164

165-
The server reads form data and the file, as if it were a regular form submission.
165+
Serwer odczytuje dane formularza i plik, tak jakby było to zwykłe przesyłanie formularza.
166166

167-
## Summary
167+
## Podsumowanie
168168

169-
[FormData](https://xhr.spec.whatwg.org/#interface-formdata) objects are used to capture HTML form and submit it using `fetch` or another network method.
169+
Obiekty klasy [FormData](https://xhr.spec.whatwg.org/#interface-formdata) służą do przechwycenia formularza HTML i przesłania go za pomocą `fetch` lub innej funkcji sieciowej.
170170

171-
We can either create `new FormData(form)` from an HTML form, or create a object without a form at all, and then append fields with methods:
171+
Możemy albo utworzyć `new FormData(form)` na podstawie formularza HTML, albo stworzyć obiekt bez formularza, a następnie dołączyć do niego pola metodami:
172172

173173
- `formData.append(name, value)`
174174
- `formData.append(name, blob, fileName)`
175175
- `formData.set(name, value)`
176176
- `formData.set(name, blob, fileName)`
177177

178-
Let's note two peculiarities here:
178+
Zwróćmy uwagę na dwie osobliwości:
179179

180-
1. The `set` method removes fields with the same name, `append` doesn't. That's the only difference between them.
181-
2. To send a file, 3-argument syntax is needed, the last argument is a file name, that normally is taken from user filesystem for `<input type="file">`.
180+
1. Metoda `set` usuwa zduplikowane pola o tej samej nazwie, a `append` nie. To jedynia różnica między nimi.
181+
2. Aby wysłać plik, potrzebna jest trójargumentowa składnia, gdzie ostatnim argumentem jest nazwa pliku, zwykle pobierana z systemu plików na potrzeby `<input type="file">`.
182182

183-
Other methods are:
183+
Inne metody to:
184184

185185
- `formData.delete(name)`
186186
- `formData.get(name)`
187187
- `formData.has(name)`
188188

189-
That's it!
189+
I to by było na tyle!

5-network/02-formdata/post.view/server.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let router = new Router();
99

1010
router.post('/user', async (ctx) => {
1111
ctx.body = {
12-
message: "User saved"
12+
message: "Użytkownik zapisany"
1313
};
1414
});
1515

@@ -34,7 +34,7 @@ router.post('/image-form', async (ctx) => {
3434
});
3535

3636
ctx.body = {
37-
message: `Image saved, firstName: ${fields.firstName}, Image size:${files[0].length}, fileName: ${files[0].filename}`
37+
message: `Obraz zapisany, imię: ${fields.firstName}, rozmiar obrazu:${files[0].length}, nazwa pliku: ${files[0].filename}.`
3838
};
3939
});
4040

@@ -61,7 +61,7 @@ router.post('/user-avatar', async (ctx) => {
6161
});
6262

6363
ctx.body = {
64-
message: `User with picture, firstName: ${fields.firstName}, picture size:${files[0].length}`
64+
message: `Użytkownik ze zdjęciem, imię: ${fields.firstName}, rozmiar obrazu:${files[0].length}.`
6565
};
6666
});
6767

0 commit comments

Comments
 (0)