|
2 | 2 | * Interface to send requests – thin wrapper around fetch() |
3 | 3 | * |
4 | 4 | * send, get and post return a Promise that resolves with a Response object |
5 | | - * from which can be retrieved Body's contents. |
| 5 | + * from which can be retrieved Body's contents. They all receive the same params: |
| 6 | + * |
| 7 | + * @param {string} url - relative URL of the API endpoint, e.g. 'updates/create.json' |
| 8 | + * @param {object} [data] - any piece of data to send alongside the request |
| 9 | + * @param {object} [settings] - any additional options to pass to GlobalFetch.fetch() |
| 10 | + * |
| 11 | + * Note: This util encodes data according to the multipart/form-data type, since that's |
| 12 | + * what most of our current endpoints expect, and it plays well with PHP. For newer |
| 13 | + * endpoints, esp. Node ones, it might make more sense to JSON.stringify() the data object |
| 14 | + * and pass it as a string through settings.body, and JSON.parse() that payload on |
| 15 | + * the server. Additions welcome! |
6 | 16 | */ |
7 | 17 |
|
8 | 18 | class Request { |
@@ -51,13 +61,15 @@ class Request { |
51 | 61 |
|
52 | 62 | /** |
53 | 63 | * Split non-primitive values into primitive chunks with formatted keys to be used |
54 | | - * with FormData.append |
| 64 | + * with FormData.append() |
55 | 65 | * |
56 | | - * data is an array of primitives, arrays and objects |
| 66 | + * @param {object} [data] - map of primitives, arrays and objects |
57 | 67 | * |
58 | | - * Example: |
| 68 | + * E.g.: |
59 | 69 | * formatDataPieces({ values: [{ a: 'a', b: 'b' }] }) |
60 | 70 | * => [ ['values[0][a]', 'a'], ['values[0][b]', 'b'] ] |
| 71 | + * |
| 72 | + * TODO: Add tests |
61 | 73 | */ |
62 | 74 | function formatDataPieces(data) { |
63 | 75 | const dataPieces = []; |
|
0 commit comments