Skip to content

feat: Add typescript support #1954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ jobs:
uses: mansona/npm-lockfile-version@v1
with:
version: 2
check-types:
name: Check types
timeout-minutes: 5
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm ci
- name: Check types
run: npm run test:types
build:
runs-on: ubuntu-latest
timeout-minutes: 30
Expand Down
2 changes: 1 addition & 1 deletion babel-jest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const babelJest = require('babel-jest');

module.exports = babelJest.createTransformer({
presets: [["@babel/preset-env", {
presets: ["@babel/preset-typescript", ["@babel/preset-env", {
"targets": {
"node": "14"
},
Expand Down
14 changes: 7 additions & 7 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ const transformRuntime = ["@babel/plugin-transform-runtime", {
}];

const PRESETS = {
'browser': [["@babel/preset-env", {
'browser': ["@babel/preset-typescript",["@babel/preset-env", {
"targets": "> 0.25%, not dead"
}]],
'weapp': [["@babel/preset-env", {
'weapp': ["@babel/preset-typescript",["@babel/preset-env", {
"targets": "> 0.25%, not dead"
}], '@babel/react'],
'node': [["@babel/preset-env", {
'node': ["@babel/preset-typescript",["@babel/preset-env", {
"targets": { "node": "14" }
}]],
'react-native': ['module:metro-react-native-babel-preset'],
'react-native': ["@babel/preset-typescript", 'module:metro-react-native-babel-preset'],
};
const PLUGINS = {
'browser': [transformRuntime, '@babel/plugin-transform-flow-comments', '@babel/plugin-proposal-class-properties', 'inline-package-json',
Expand Down Expand Up @@ -79,7 +79,7 @@ function compileTask(stream) {
}

gulp.task('compile', function() {
return compileTask(gulp.src('src/*.js'));
return compileTask(gulp.src('src/*.*(js|ts)'));
});

gulp.task('browserify', function(cb) {
Expand Down Expand Up @@ -137,7 +137,7 @@ gulp.task('minify-weapp', function() {

gulp.task('watch', function() {
if (BUILD === 'browser') {
const watcher = gulp.watch('src/*.js', { ignoreInitial: false }, gulp.series('compile', 'browserify', 'minify'));
const watcher = gulp.watch('src/*.*(js|ts)', { ignoreInitial: false }, gulp.series('compile', 'browserify', 'minify'));
watcher.on('add', function(path) {
console.log(`File ${path} was added`);
});
Expand All @@ -146,5 +146,5 @@ gulp.task('watch', function() {
});
return watcher;
}
return compileTask(watch('src/*.js', { ignoreInitial: false, verbose: true }));
return compileTask(watch('src/*.*(js|ts)', { ignoreInitial: false, verbose: true }));
});
3,427 changes: 2,479 additions & 948 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
"lib/",
"LICENSE",
"NOTICE",
"README.md"
"README.md",
"types/index.d.ts"
],
"browser": {
"react-native": false
},
"types": "types",
"dependencies": {
"@babel/runtime-corejs3": "7.22.3",
"@definitelytyped/dtslint": "0.0.163",
"idb-keyval": "6.2.1",
"react-native-crypto-js": "1.0.0",
"uuid": "9.0.0",
Expand All @@ -45,6 +48,7 @@
"@babel/plugin-transform-runtime": "7.21.4",
"@babel/preset-env": "7.21.5",
"@babel/preset-react": "7.18.6",
"@babel/preset-typescript": "7.22.5",
"@parse/minami": "1.0.0",
"@saithodev/semantic-release-backmerge": "2.1.3",
"@semantic-release/changelog": "6.0.3",
Expand Down Expand Up @@ -95,10 +99,12 @@
},
"scripts": {
"build": "node build_releases.js",
"build:types": "tsc",
"release": "node build_releases.js && npm publish",
"test": "cross-env PARSE_BUILD=node jest",
"test:mongodb": "npm run test:mongodb:runnerstart && npm run integration",
"test:mongodb:runnerstart": "mongodb-runner start",
"test:types": "dtslint --expectOnly types",
"posttest:mongodb": "mongodb-runner stop",
"lint": "eslint --cache src/ integration/",
"lint:fix": "eslint --fix --cache src/ integration/",
Expand Down
5 changes: 3 additions & 2 deletions src/Analytics.js → src/Analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import CoreManager from './CoreManager';
* @returns {Promise} A promise that is resolved when the round-trip
* to the server completes.
*/
export function track(name: string, dimensions: { [key: string]: string }): Promise {

export function track(name: string, dimensions: { [key: string]: string }): Promise<any> {
name = name || '';
name = name.replace(/^\s*/, '');
name = name.replace(/\s*$/, '');
Expand All @@ -62,7 +63,7 @@ export function track(name: string, dimensions: { [key: string]: string }): Prom
}

const DefaultController = {
track(name, dimensions) {
track(name: string, dimensions: { [key: string]: string }) {
const path = 'events/' + name;
const RESTController = CoreManager.getRESTController();
return RESTController.request('POST', path, { dimensions: dimensions });
Expand Down
File renamed without changes.
7 changes: 5 additions & 2 deletions src/EventEmitter.js → src/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
* This is a simple wrapper to unify EventEmitter implementations across platforms.
*/

let emitter: any = null;
if (process.env.PARSE_BUILD === 'react-native') {
let EventEmitter = require('react-native/Libraries/vendor/emitter/EventEmitter');
if (EventEmitter.default) {
EventEmitter = EventEmitter.default;
}
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
module.exports = EventEmitter;
emitter = EventEmitter;
} else {
module.exports = require('events').EventEmitter;
emitter = require('events').EventEmitter;
}
module.exports = emitter;
export default emitter;
13 changes: 7 additions & 6 deletions src/EventuallyQueue.js → src/EventuallyQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const EventuallyQueue = {
* @static
* @see Parse.Object#saveEventually
*/
save(object: ParseObject, serverOptions: SaveOptions = {}): Promise {
save(object: ParseObject, serverOptions: SaveOptions = {}): Promise<void> {
return this.enqueue('save', object, serverOptions);
},

Expand All @@ -64,7 +64,7 @@ const EventuallyQueue = {
* @static
* @see Parse.Object#destroyEventually
*/
destroy(object: ParseObject, serverOptions: RequestOptions = {}): Promise {
destroy(object: ParseObject, serverOptions: RequestOptions = {}): Promise<void> {
return this.enqueue('destroy', object, serverOptions);
},

Expand Down Expand Up @@ -98,7 +98,7 @@ const EventuallyQueue = {
action: string,
object: ParseObject,
serverOptions: SaveOptions | RequestOptions
): Promise {
): Promise<any> {
const queueData = await this.getQueue();
const queueId = this.generateQueueId(action, object);

Expand Down Expand Up @@ -142,7 +142,7 @@ const EventuallyQueue = {
* @returns {Promise<Array>}
* @static
*/
async getQueue(): Promise<Array> {
async getQueue(): Promise<any[]> {
if (dirtyCache) {
queueCache = JSON.parse((await this.load()) || '[]');
dirtyCache = false;
Expand Down Expand Up @@ -188,7 +188,7 @@ const EventuallyQueue = {
* @returns {Promise} A promise that is fulfilled when queue is cleared.
* @static
*/
clear(): Promise {
clear(): Promise<any> {
queueCache = [];
return this.store([]);
},
Expand All @@ -214,7 +214,7 @@ const EventuallyQueue = {
* @returns {number}
* @static
*/
async length(): number {
async length(): Promise<number> {
const queueData = await this.getQueue();
return queueData.length;
},
Expand Down Expand Up @@ -373,3 +373,4 @@ const EventuallyQueue = {
};

module.exports = EventuallyQueue;
export default EventuallyQueue;
20 changes: 18 additions & 2 deletions src/LiveQuerySubscription.js → src/LiveQuerySubscription.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import EventEmitter from './EventEmitter';
import CoreManager from './CoreManager';
import { resolvingPromise } from './promiseUtils';
import ParseQuery from './ParseQuery';
import ParseObject from './ParseObject';

/**
* Creates a new LiveQuery Subscription.
Expand Down Expand Up @@ -93,7 +95,14 @@ class Subscription extends EventEmitter {
* @param {string} query - query to subscribe to
* @param {string} sessionToken - optional session token
*/
constructor(id, query, sessionToken) {
id: string;
query: ParseQuery;
sessionToken: string;
subscribePromise: resolvingPromise<any>
unsubscribePromise: resolvingPromise<any>
subscribed: boolean;
emit: any;
constructor(id: string, query: ParseQuery, sessionToken: string) {
super();
this.id = id;
this.query = query;
Expand All @@ -112,14 +121,21 @@ class Subscription extends EventEmitter {
*
* @returns {Promise}
*/
unsubscribe(): Promise {
unsubscribe(): Promise<any> {
return CoreManager.getLiveQueryController()
.getDefaultLiveQueryClient()
.then(liveQueryClient => {
this.emit('close');
return liveQueryClient.unsubscribe(this);
});
}

on(
event: 'error' | 'open' | 'create' | 'update' | 'enter' | 'leave' | 'delete' | 'close',
listener: (object: ParseObject) => void,
) {
return super.on(event, listener);
}
}

export default Subscription;
14 changes: 7 additions & 7 deletions src/ObjectStateMutations.js → src/ObjectStateMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function setServerData(serverData: AttributeMap, attributes: AttributeMap
}
}

export function setPendingOp(pendingOps: Array<OpsMap>, attr: string, op: ?Op) {
export function setPendingOp(pendingOps: Array<OpsMap>, attr: string, op?: Op) {
const last = pendingOps.length - 1;
if (op) {
pendingOps[last][attr] = op;
Expand All @@ -56,7 +56,7 @@ export function pushPendingState(pendingOps: Array<OpsMap>) {
pendingOps.push({});
}

export function popPendingState(pendingOps: Array<OpsMap>): OpsMap {
export function popPendingState(pendingOps: Array<OpsMap>): OpsMap | undefined {
const first = pendingOps.shift();
if (!pendingOps.length) {
pendingOps[0] = {};
Expand All @@ -83,15 +83,15 @@ export function estimateAttribute(
serverData: AttributeMap,
pendingOps: Array<OpsMap>,
className: string,
id: ?string,
id: string | undefined,
attr: string
): mixed {
): any {
let value = serverData[attr];
for (let i = 0; i < pendingOps.length; i++) {
if (pendingOps[i][attr]) {
if (pendingOps[i][attr] instanceof RelationOp) {
if (id) {
value = pendingOps[i][attr].applyTo(value, { className: className, id: id }, attr);
value = (pendingOps[i][attr] as RelationOp).applyTo(value, { className: className, id: id }, attr);
}
} else {
value = pendingOps[i][attr].applyTo(value);
Expand All @@ -105,7 +105,7 @@ export function estimateAttributes(
serverData: AttributeMap,
pendingOps: Array<OpsMap>,
className: string,
id: ?string
id?: string
): AttributeMap {
const data = {};
let attr;
Expand All @@ -116,7 +116,7 @@ export function estimateAttributes(
for (attr in pendingOps[i]) {
if (pendingOps[i][attr] instanceof RelationOp) {
if (id) {
data[attr] = pendingOps[i][attr].applyTo(
data[attr] = (pendingOps[i][attr] as RelationOp).applyTo(
data[attr],
{ className: className, id: id },
attr
Expand Down
Loading