Skip to content

Commit 7828f7a

Browse files
authored
chore: add ts configuration (#868)
And convert pubsub to Typescript.
1 parent 82cca54 commit 7828f7a

File tree

10 files changed

+56
-16
lines changed

10 files changed

+56
-16
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ cat_docs_command = cat ./docs/_API-header.md ./docs/_API-body.md > ./docs/API.md
1212

1313
build:
1414
rm -rf ./dist
15-
./node_modules/.bin/fedx-scripts babel src --out-dir dist --source-maps --ignore **/*.test.jsx,**/*.test.js,**/setupTest.js --copy-files
15+
./node_modules/.bin/fedx-scripts babel src --out-dir dist --source-maps --extensions '.js,.jsx,.ts,.tsx' --ignore **/*.test.jsx,**/*.test.js,**/*.test.tsx,**/*.test.ts,**/setupTest.js,**/*.d.ts --copy-files
1616
@# --copy-files will bring in everything else that wasn't processed by babel. Remove what we don't want.
1717
@find dist -name '*.test.js*' -delete
18+
@find dist -name '*.test.ts*' -delete
19+
@find dist -name '*.d.ts' -delete
1820
rm ./dist/setupTest.js
21+
@# Generate .d.ts type declaration files from TypeScript sources
22+
./node_modules/.bin/tsc --project tsconfig.build.json
1923
cp ./package.json ./dist/package.json
2024
cp ./LICENSE ./dist/LICENSE
2125
cp ./README.md ./dist/README.md

babel.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
const { createConfig } = require('@openedx/frontend-build');
22

3-
module.exports = createConfig('babel-preserve-modules');
3+
const config = createConfig('babel-preserve-modules');
4+
config.presets.push('@babel/preset-typescript');
5+
module.exports = config;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.0.0-semantically-released",
44
"description": "Foundational application framework for Open edX micro-frontend applications.",
55
"main": "index.js",
6+
"types": "index.d.ts",
67
"publishConfig": {
78
"access": "public"
89
},
@@ -11,7 +12,7 @@
1112
"build": "make build",
1213
"docs": "jsdoc -c jsdoc.json",
1314
"docs-watch": "nodemon -w src -w docs/template -w README.md -e js,jsx --exec npm run docs",
14-
"lint": "fedx-scripts eslint --ext .js --ext .jsx .",
15+
"lint": "fedx-scripts eslint --ext .js --ext .jsx --ext .ts --ext .tsx .",
1516
"i18n_extract": "fedx-scripts formatjs extract",
1617
"snapshot": "fedx-scripts jest --updateSnapshot",
1718
"start": "fedx-scripts webpack-dev-server --progress",

src/analytics/SegmentAnalyticsService.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class SegmentAnalyticsService {
174174
* @param {*} [traits]
175175
* @returns {Promise} Promise that will resolve once the document readyState is complete
176176
*/
177+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
177178
identifyAnonymousUser(traits) { // eslint-disable-line no-unused-vars
178179
if (!this.segmentInitialized) {
179180
return Promise.resolve();
@@ -182,7 +183,7 @@ class SegmentAnalyticsService {
182183
// but we still have a user id associated in segment, reset the local segment state
183184
// This has to be wrapped in the analytics.ready() callback because the analytics.user()
184185
// function isn't available until the analytics.js package has finished initializing.
185-
return new Promise((resolve, reject) => { // eslint-disable-line no-unused-vars
186+
return new Promise((resolve, reject) => { // eslint-disable-line no-unused-vars, @typescript-eslint/no-unused-vars
186187
global.analytics.ready(() => {
187188
if (global.analytics.user().id()) {
188189
global.analytics.reset();

src/auth/AxiosJwtTokenService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export default class AxiosJwtTokenService {
124124
}
125125

126126
try {
127-
return await this.refresh();
127+
return await this.refresh(); // eslint-disable-line @typescript-eslint/return-await
128128
} catch (e) {
129129
// TODO: Fix these. They're still using loggingService as a singleton.
130130
logFrontendAuthError(this.loggingService, e);

src/global.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
declare module 'pubsub-js' {
2+
const PubSub: {
3+
subscribe(type: string, callback: (message: string, data?: unknown) => void): string;
4+
unsubscribe(token: string): void;
5+
publish(type: string, data?: unknown): boolean;
6+
};
7+
export default PubSub;
8+
}

src/pubSub.js renamed to src/pubSub.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,32 @@
2020
import PubSub from 'pubsub-js';
2121

2222
/**
23+
* Subscribes to a PubSub event.
2324
*
24-
* @param {string} type
25-
* @param {function} callback
26-
* @returns {string} A subscription token that can be passed to `unsubscribe`
25+
* @param type - The event type to subscribe to
26+
* @param callback - The callback function invoked when the event is published
27+
* @returns A subscription token that can be passed to {@link unsubscribe}
2728
*/
28-
export function subscribe(type, callback) {
29+
export function subscribe(type: string, callback: (message: string, data: unknown) => void): string {
2930
return PubSub.subscribe(type, callback);
3031
}
3132

3233
/**
34+
* Unsubscribes from a PubSub event.
3335
*
34-
* @param {string} token A subscription token provided by `subscribe`
36+
* @param token - A subscription token provided by {@link subscribe}
3537
*/
36-
export function unsubscribe(token) {
37-
return PubSub.unsubscribe(token);
38+
export function unsubscribe(token: string): void {
39+
PubSub.unsubscribe(token);
3840
}
3941

4042
/**
43+
* Publishes a PubSub event.
4144
*
42-
* @param {string} type
43-
* @param {Object} data
45+
* @param type - The event type to publish
46+
* @param data - The data to pass to subscribers
47+
* @returns Whether the event was published successfully
4448
*/
45-
export function publish(type, data) {
49+
export function publish(type: string, data?: unknown): boolean {
4650
return PubSub.publish(type, data);
4751
}

src/react/PageWrap.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable react/prop-types */
2-
// eslint-disable-next-line no-unused-vars
2+
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
33
import React, { useEffect } from 'react';
44
import { useLocation } from 'react-router-dom';
55

tsconfig.build.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "dist",
6+
"declaration": true,
7+
"emitDeclarationOnly": true
8+
},
9+
"include": ["src/**/*"],
10+
"exclude": ["dist", "node_modules", "src/**/*.test.*", "src/setupTest.*"]
11+
}

tsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "@edx/typescript-config",
3+
"compilerOptions": {
4+
"rootDir": ".",
5+
"outDir": "dist"
6+
},
7+
"include": ["src/**/*", "example/**/*", "__mocks__/**/*", ".*.js", "*.js", "*.jsx", "*.ts", "*.tsx"],
8+
"exclude": ["dist", "node_modules"]
9+
}

0 commit comments

Comments
 (0)