Skip to content

Commit

Permalink
Migrate to typescript (#16)
Browse files Browse the repository at this point in the history
* partial migration

* chore: configured eslint and tsc

* chore: tests working

* chore: set up for publishing
  • Loading branch information
samuelgozi authored Apr 27, 2020
1 parent d5e06c3 commit 26d9a6d
Show file tree
Hide file tree
Showing 31 changed files with 1,619 additions and 1,266 deletions.
12 changes: 11 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{
"presets": [["@babel/preset-env", { "useBuiltIns": "usage", "corejs": 3 }]]
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
],
"@babel/preset-typescript"
]
}
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true

[*.js]
[*.{js,ts}]
quote_type = single

[{package.json,.*rc,*.yml}]
Expand Down
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
47 changes: 26 additions & 21 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
{
"env": {
"es6": true,
"browser": true,
"node": true,
"jest/globals": true
},
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module"
},
"extends": ["eslint:recommended", "plugin:jest/recommended"],
"rules": {
"no-console": ["error", { "allow": ["error"] }],
"indent": ["error", "tab", { "SwitchCase": 1 }],
"linebreak-style": ["error", "unix"],
"quotes": ["error", "single", { "avoidEscape": true }],
"semi": ["error", "always"],
"no-mixed-spaces-and-tabs": ["warn", "smart-tabs"]
},
"plugins": ["babel", "jest"]
"env": {
"es6": true,
"browser": true,
"node": true,
"jest/globals": true
},
"extends": [
"eslint:recommended",
"plugin:jest/recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"no-console": ["error", { "allow": ["error"] }],
"indent": ["error", "tab", { "SwitchCase": 1 }],
"linebreak-style": ["error", "unix"],
"quotes": ["error", "single", { "avoidEscape": true }],
"semi": ["error", "always"],
"no-mixed-spaces-and-tabs": ["warn", "smart-tabs"],
"prefer-rest-params": "warn",
"prefer-const": "warn",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-use-before-define": "warn",
"@typescript-eslint/explicit-function-return-type": "off"
},
"plugins": ["jest"]
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ typings/

# Firebase configuration used for testing
firebaseConfig.js

# build directory
dist
1 change: 0 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"singleQuote": true,
"useTabs": true,
"printWidth": 120,
"trailingComma": "none",
"arrowParens": "avoid"
}
21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,33 @@
"name": "firebase-firestore-lite",
"version": "0.6.2",
"description": "A lightweight cloud firestore library for the browser",
"main": "src/index.js",
"main": "dist/mod.js",
"types": "dist/mod.d.ts",
"author": "Samuel Elgozi <samu.elgozi@gmail.com>",
"license": "MIT",
"files": [
"src"
"dist"
],
"repository": {
"type": "git",
"url": "https://github.com/samuelgozi/firebase-firestore-lite.git"
},
"scripts": {
"test": "jest"
"build": "tsc",
"lint": "eslint . --ext .ts",
"test": "jest",
"prepublishOnly": "tsc"
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"babel-eslint": "^10.1.0",
"core-js": "^3.6.4",
"@babel/preset-env": "^7.9.5",
"@babel/preset-typescript": "^7.9.0",
"@typescript-eslint/eslint-plugin": "^2.29.0",
"@typescript-eslint/parser": "^2.29.0",
"eslint": "^6.8.0",
"eslint-plugin-babel": "^5.3.0",
"eslint-plugin-jest": "^23.8.2",
"firebase-auth-lite": "^0.5.4",
"jest": "^25.1.0",
"jest-fetch-mock": "^3.0.3",
"regenerator-runtime": "^0.13.5"
"typescript": "^3.8.3"
}
}
30 changes: 0 additions & 30 deletions src/Document.js

This file was deleted.

64 changes: 64 additions & 0 deletions src/Document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { isRawDocument, decode } from './utils';
import Database from './mod';

export interface FirebaseMap {
/** The map's fields */
fields?: {
[key: string]: any;
};
}

export interface FirebaseDocument extends FirebaseMap {
/** The full resource name of the document */
name: string;
/** A timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds */
createTime: string;
/** A timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds */
updateTime: string;
}

export interface Meta {
/** The database instance used to create this document */
db: Database;
/** The ID of the document inside the collection */
id: string;
/** The path to the document relative to the database root */
path: string;
/** The full resource name of the document */
name: string;
/** A timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds */
createTime: string;
/** A timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds */
updateTime: string;
}

/**
* Wrapper around a fetched objects that represent a Firestore document.
* It is supposed to be used as a regular JS object but has a hidden
* property that holds the meta data of the document.
*
* That property is called `__meta__`, it should not be modified, and is non-enumerable.
* It is used internally to identify the document when writing the
* data to the database.
*/
export class Document {
__meta__: Meta;

constructor(rawDoc: FirebaseDocument, db: Database) {
if (db === undefined) throw Error('Argument "db" is required but missing');
if (!isRawDocument(rawDoc)) throw Error('Invalid Firestore Document');

const { name, createTime, updateTime } = rawDoc;
const meta = {
db,
name,
createTime,
updateTime,
path: name.replace(db.rootPath, ''),
id: name.split('/').pop()
};

Object.defineProperty(this, '__meta__', { value: meta });
Object.assign(this, decode(rawDoc, db));
}
}
16 changes: 0 additions & 16 deletions src/GeoPoint.js

This file was deleted.

23 changes: 23 additions & 0 deletions src/GeoPoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** Represents a firebase GeoPoint value */
export default class GeoPoint {
constructor(public latitude: number, public longitude: number) {
if (typeof latitude !== 'number')
throw Error('The latitude argument should be of type number');
if (typeof latitude !== 'number')
throw Error('The longitude argument should be of type number');
if (latitude >= 90 && latitude <= -90)
throw Error(
"GeoPoint's latitude should be within the range of -90.0 and 90.0"
);
if (longitude >= 180 && longitude <= -180)
throw Error(
"GeoPoint's longitude should be within the range of -180.0 and 180.0"
);
}

toJSON() {
return {
geoPointValue: { ...(this as object) }
};
}
}
29 changes: 0 additions & 29 deletions src/List.js

This file was deleted.

51 changes: 51 additions & 0 deletions src/List.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Document, FirebaseDocument } from './Document';
import Reference from './Reference';

interface FirebaseList {
documents: FirebaseDocument[];
nextPageToken: string;
}

export interface FirebaseListOptions {
pageSize?: number;
pageToken?: string;
orderBy?: string;
showMissing?: boolean;
}

/**
* Represents a collection list response, with functionality
* for getting the next page when available.
* @param {Object} rawList The response "raw" list object.
* @param {Reference} ref A reference to the collection.
* @param {Object} options Any options that were passed at first to the get request.
*/
export class List {
ref: Reference;
options: any;
documents: Document[];

constructor(
rawList: FirebaseList,
ref: Reference,
options: FirebaseListOptions = {}
) {
if (ref === undefined)
throw Error('The "reference" argument is required when creating a List');
if (!ref.isCollection)
throw Error('The reference in a list should point to a collection');

const { documents, nextPageToken } = rawList;
this.ref = ref;
this.options = options;
this.documents = documents
? documents.map(rawDoc => new Document(rawDoc, ref.db))
: [];
this.options.pageToken = nextPageToken;
}

/** Fetches the next page in the query */
getNextPage() {
return this.ref.get(this.options) as Promise<List>;
}
}
Loading

0 comments on commit 26d9a6d

Please sign in to comment.