|
1 |
| -const helper = require('./helper'); |
2 |
| - |
3 |
| -async function api(baseUrl, config, method, params) { |
4 |
| - const url = new URL(baseUrl); |
5 |
| - const auth = Buffer.from(`${config.username}:${config.apiKey}`).toString( |
6 |
| - 'base64' |
7 |
| - ); |
8 |
| - const authHeader = `Basic ${auth}`; |
9 |
| - const options = { method, headers: { Authorization: authHeader } }; |
10 |
| - if (method === 'POST') { |
11 |
| - options.body = new helper.FormData(); |
12 |
| - Object.keys(params).forEach((key) => { |
13 |
| - let data = params[key]; |
14 |
| - if (Array.isArray(data)) { |
15 |
| - data = JSON.stringify(data); |
16 |
| - } |
17 |
| - options.body.append(key, data); |
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
18 | 9 | });
|
19 |
| - } else if (params) { |
20 |
| - Object.entries(params).forEach(([key, value]) => { |
21 |
| - url.searchParams.append(key, value); |
| 10 | +}; |
| 11 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 12 | +exports.api = void 0; |
| 13 | +const types_1 = require("./types"); |
| 14 | +const fetch = require('isomorphic-fetch'); |
| 15 | +const FormData = require('isomorphic-form-data'); |
| 16 | +function api(baseUrl, config, method, params) { |
| 17 | + return __awaiter(this, void 0, void 0, function* () { |
| 18 | + const url = new URL(baseUrl); |
| 19 | + const auth = Buffer.from(`${config.username}:${config.apiKey}`).toString('base64'); |
| 20 | + const authHeader = `Basic ${auth}`; |
| 21 | + const options = { |
| 22 | + method, |
| 23 | + headers: { Authorization: authHeader }, |
| 24 | + }; |
| 25 | + if (method === 'POST') { |
| 26 | + options.body = new FormData(); |
| 27 | + Object.keys(params).forEach((key) => { |
| 28 | + let data = params[key]; |
| 29 | + if (Array.isArray(data)) { |
| 30 | + data = JSON.stringify(data); |
| 31 | + } |
| 32 | + options.body.append(key, data); |
| 33 | + }); |
| 34 | + } |
| 35 | + else if (params) { |
| 36 | + Object.entries(params).forEach(([key, value]) => { |
| 37 | + url.searchParams.append(key, value); |
| 38 | + }); |
| 39 | + } |
| 40 | + const response = yield fetch(url.href, options); |
| 41 | + try { |
| 42 | + return response.json(); |
| 43 | + } |
| 44 | + catch (e) { |
| 45 | + if (e instanceof SyntaxError) { |
| 46 | + // We probably got a non-JSON response from the server. |
| 47 | + // We should inform the user of the same. |
| 48 | + let message = 'Server Returned a non-JSON response.'; |
| 49 | + if (response.status === 404) { |
| 50 | + message += ` Maybe endpoint: ${method} ${response.url.replace(config.apiURL, '')} doesn't exist.`; |
| 51 | + } |
| 52 | + else { |
| 53 | + message += ' Please check the API documentation.'; |
| 54 | + } |
| 55 | + const error = new types_1.ZulipErrorResponse(message); |
| 56 | + error.res = response; |
| 57 | + throw error; |
| 58 | + } |
| 59 | + throw e; |
| 60 | + } |
22 | 61 | });
|
23 |
| - } |
24 |
| - const response = await helper.fetch(url.href, options); |
25 |
| - try { |
26 |
| - return response.json(); |
27 |
| - } catch (e) { |
28 |
| - if (e instanceof SyntaxError) { |
29 |
| - // We probably got a non-JSON response from the server. |
30 |
| - // We should inform the user of the same. |
31 |
| - let message = 'Server Returned a non-JSON response.'; |
32 |
| - if (response.status === 404) { |
33 |
| - message += ` Maybe endpoint: ${method} ${response.url.replace( |
34 |
| - config.apiURL, |
35 |
| - '' |
36 |
| - )} doesn't exist.`; |
37 |
| - } else { |
38 |
| - message += ' Please check the API documentation.'; |
39 |
| - } |
40 |
| - const error = new Error(message); |
41 |
| - error.res = response; |
42 |
| - throw error; |
43 |
| - } |
44 |
| - throw e; |
45 |
| - } |
46 | 62 | }
|
47 |
| - |
48 |
| -module.exports = api; |
| 63 | +exports.api = api; |
0 commit comments