Skip to content

Commit 78995cf

Browse files
committed
basic ts migration
1 parent ce85fda commit 78995cf

File tree

14 files changed

+76
-16
lines changed

14 files changed

+76
-16
lines changed

rollup.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import pkg from './package.json';
66
export default [
77
// browser-friendly UMD build
88
{
9-
input: 'src/index.js',
9+
input: 'src/index.ts',
1010
output: {
1111
name: 'DiscourseJS',
1212
file: pkg.browser,
@@ -22,7 +22,7 @@ export default [
2222
// an array for the `output` option, where we can specify
2323
// `file` and `format` for each target)
2424
{
25-
input: 'src/index.js',
25+
input: 'src/index.ts',
2626
external: [...Object.keys(pkg.dependencies || {})],
2727
output: [
2828
{ file: pkg.main, format: 'cjs' },

src/index.js renamed to src/index.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,19 @@ const resources = {
2727
};
2828

2929
export default class Discourse {
30-
constructor(userApiKey, baseUrl, apiKey = null) {
30+
_BASE_URL: string;
31+
_USER_API_KEY: string;
32+
_API_KEY: string | null;
33+
_API_USERNAME: string | null;
34+
isUsingAdminAPI: string;
35+
36+
constructor(
37+
userApiKey: string,
38+
baseUrl: string,
39+
apiKey: string | null = null,
40+
) {
3141
this._BASE_URL = baseUrl;
32-
this._USER_API_Key = userApiKey;
42+
this._USER_API_KEY = userApiKey;
3343

3444
// Admin User API
3545
this._API_KEY = apiKey;
@@ -40,7 +50,17 @@ export default class Discourse {
4050
}
4151

4252
config = (
43-
{ userApiKey, baseUrl, apiUsername, apiKey } = {
53+
{
54+
userApiKey,
55+
baseUrl,
56+
apiUsername,
57+
apiKey,
58+
}: {
59+
userApiKey?: string | null,
60+
baseUrl?: string | null,
61+
apiKey: string | null,
62+
apiUsername: string | null,
63+
} = {
4464
apiUsername: null,
4565
apiKey: null,
4666
},
@@ -75,7 +95,10 @@ export default class Discourse {
7595
: createBody(body);
7696
};
7797

78-
get = ({ path, headers } = {}) => {
98+
get = ({
99+
path,
100+
headers,
101+
}: { path?: string, headers?: { [key: string]: string } } = {}) => {
79102
return this.request({
80103
method: 'GET',
81104
headers,

src/resources/Categories.js renamed to src/resources/Categories.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { buildQueryString } from '../utils';
22

33
export default function Categories(discourse) {
44
this.getCategory = async (
5-
{ cat_id, latest, ...inputs } = { latest: false },
5+
{ cat_id, latest, ...inputs }: { cat_id?: number, latest: boolean } = {
6+
latest: false,
7+
},
68
) => {
79
if (!cat_id) {
810
throw new Error(
@@ -19,7 +21,14 @@ export default function Categories(discourse) {
1921
};
2022

2123
this.getSubcategory = async (
22-
{ cat_id, subcat_id, latest, ...inputs } = { latest: false },
24+
{
25+
cat_id,
26+
subcat_id,
27+
latest,
28+
...inputs
29+
}: { cat_id?: number, subcat_id?: number, latest: boolean } = {
30+
latest: false,
31+
},
2332
) => {
2433
if (!cat_id || !subcat_id) {
2534
throw new Error(
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/resources/Posts.js renamed to src/resources/Posts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default function Posts(discourse) {
1313
});
1414

1515
if (url) {
16-
const body = {};
16+
const body: { [key: string]: string } = {};
1717

1818
// Remove the imageUri from the inputs as it's not used in the next request.
1919
delete inputs.imageUri;

src/resources/Preferences.js renamed to src/resources/Preferences.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
export default function Preferences(discourse) {
2-
this.pickAvatar = async ({ username, upload_id } = {}) => {
2+
this.pickAvatar = async ({
3+
username,
4+
upload_id,
5+
}: { username?: string, upload_id?: number } = {}) => {
36
return discourse.put({
47
path: `u/${username}/preferences/avatar/pick`,
58
body: {

src/resources/Tags.js renamed to src/resources/Tags.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { buildQueryString } from '../utils';
22

33
export default function Tags(discourse) {
4-
this.getTopic = async ({ tag, ...inputs } = {}) => {
4+
this.getTopic = async ({ tag, ...inputs }: { tag?: string } = {}) => {
55
return discourse.get({
66
path: buildQueryString(`tags/${tag}.json`, inputs),
77
});
@@ -12,7 +12,7 @@ export default function Tags(discourse) {
1212
category,
1313
subcategory,
1414
...inputs
15-
} = {}) => {
15+
}: { tag?: string, category?: number, subcategory?: number } = {}) => {
1616
return discourse.get({
1717
path: buildQueryString(
1818
`tags/c/${category}/${subcategory ? `${subcategory}/` : ''}${tag}.json`,

src/resources/Topics.js renamed to src/resources/Topics.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import { buildQueryString } from '../utils';
22

33
export default function Topics(discourse) {
4-
this.getTopic = async ({ id, reverse, ...inputs } = {}) => {
4+
this.getTopic = async ({
5+
id,
6+
reverse,
7+
...inputs
8+
}: { id?: number, reverse?: string } = {}) => {
59
return discourse.get({
610
path: buildQueryString(`t/${id}${reverse ? '/last' : ''}.json`, inputs),
711
});
812
};
913

10-
this.getTopicPosts = async ({ id, posts, ...inputs } = {}) => {
14+
this.getTopicPosts = async ({
15+
id,
16+
posts,
17+
...inputs
18+
}: { id?: number, posts?: any } = {}) => {
1119
const params = {
1220
post_ids: posts,
1321
...inputs,
@@ -18,7 +26,7 @@ export default function Topics(discourse) {
1826
});
1927
};
2028

21-
this.deleteTopic = async ({ id, ...inputs } = {}) => {
29+
this.deleteTopic = async ({ id, ...inputs }: { id?: number } = {}) => {
2230
return discourse.delete({
2331
path: buildQueryString(`t/${id}`, inputs),
2432
});

0 commit comments

Comments
 (0)