Skip to content

Commit 724a928

Browse files
committed
feature: Add typescript integration.
1 parent 45b8cb2 commit 724a928

36 files changed

+2088
-452
lines changed

package-lock.json

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
"@babel/core": "^7.12.10",
1818
"@babel/plugin-transform-runtime": "^7.12.10",
1919
"@babel/preset-env": "^7.12.10",
20+
"@types/isomorphic-fetch": "0.0.35",
21+
"@types/isomorphic-form-data": "^2.0.0",
22+
"@types/node": "^14.14.35",
2023
"chai": "^4.2.0",
2124
"eslint": "^7.15.0",
2225
"eslint-config-airbnb": "^18.2.1",
@@ -26,10 +29,11 @@
2629
"eslint-plugin-react": "^7.21.5",
2730
"mocha": "^8.2.1",
2831
"prettier": "^2.2.1",
29-
"sinon": "^9.2.1"
32+
"sinon": "^9.2.1",
33+
"typescript": "^4.2.3"
3034
},
3135
"scripts": {
32-
"build": "babel --delete-dir-on-start -d lib/ src/",
36+
"build": "tsc && babel --delete-dir-on-start -d lib/ src/",
3337
"prepare": "npm run build",
3438
"lint": "eslint . && prettier --loglevel=warn --check .",
3539
"lint:fix": "eslint --fix . && prettier --loglevel=warn --write .",

src/api.js

+60-45
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,63 @@
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());
189
});
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+
}
2261
});
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-
}
4662
}
47-
48-
module.exports = api;
63+
exports.api = api;

src/api.ts

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Config } from './zuliprc';
2+
import { ZulipErrorResponse } from './types';
3+
const fetch = require('isomorphic-fetch');
4+
const FormData = require('isomorphic-form-data');
5+
6+
export type HttpMethod =
7+
| "GET"
8+
| "HEAD"
9+
| "POST"
10+
| "PUT"
11+
| "DELETE"
12+
| "TRACE"
13+
| "OPTIONS"
14+
| "CONNECT"
15+
| "PATCH";
16+
17+
export async function api(baseUrl: string, config: Config, method: HttpMethod, params: any): Promise<any> {
18+
const url: URL = new URL(baseUrl);
19+
const auth: string = Buffer.from(`${config.username}:${config.apiKey}`).toString(
20+
'base64'
21+
);
22+
const authHeader: string = `Basic ${auth}`;
23+
const options: any = {
24+
method,
25+
headers: { Authorization: authHeader },
26+
};
27+
if (method === 'POST') {
28+
options.body = new FormData();
29+
Object.keys(params).forEach((key) => {
30+
let data: string = params[key];
31+
if (Array.isArray(data)) {
32+
data = JSON.stringify(data);
33+
}
34+
options.body.append(key, data);
35+
});
36+
} else if (params) {
37+
Object.entries(params).forEach(([key, value]) => {
38+
url.searchParams.append(key, value as string);
39+
});
40+
}
41+
const response: Response = await fetch(url.href, options);
42+
try {
43+
return response.json();
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: string = 'Server Returned a non-JSON response.';
49+
if (response.status === 404) {
50+
message += ` Maybe endpoint: ${method} ${response.url.replace(
51+
config.apiURL,
52+
''
53+
)} doesn't exist.`;
54+
} else {
55+
message += ' Please check the API documentation.';
56+
}
57+
const error: ZulipErrorResponse = new ZulipErrorResponse(message);
58+
error.res = response;
59+
throw error;
60+
}
61+
throw e;
62+
}
63+
}

0 commit comments

Comments
 (0)