Skip to content
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

refactor: rename types and add modular namespace #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"geokit": "^1.1.0"
},
"optionalDependencies": {
"@firebase/firestore": ">= 3.0.0 < 4.0.0",
"@google-cloud/firestore": ">= 5.0.0 < 6.0.0",
"firebase": ">= 9.0.0 < 10.0.0"
},
Expand Down
29 changes: 22 additions & 7 deletions src/api/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import {findGeoPoint} from '../utils';
* @return The document encoded as GeoDocument object.
*/
export function encodeDocumentAdd(
documentData: GeoFirestoreTypes.DocumentData,
documentData:
| GeoFirestoreTypes.DocumentData
| GeoFirestoreTypes.modular.WithFieldValue<any>,
customKey?: string
): GeoFirestoreTypes.GeoDocumentData {
):
| GeoFirestoreTypes.GeoDocumentData
| GeoFirestoreTypes.modular.WithFieldValue<any> {
if (Object.prototype.toString.call(documentData) !== '[object Object]') {
throw new Error('document must be an object');
}
Expand All @@ -29,9 +33,14 @@ export function encodeDocumentAdd(
* @return The document encoded as GeoDocument object.
*/
export function encodeDocumentSet(
documentData: GeoFirestoreTypes.DocumentData,
documentData:
| GeoFirestoreTypes.DocumentData
| GeoFirestoreTypes.modular.WithFieldValue<any>,
options?: GeoFirestoreTypes.SetOptions
): GeoFirestoreTypes.GeoDocumentData | GeoFirestoreTypes.DocumentData {
):
| GeoFirestoreTypes.GeoDocumentData
| GeoFirestoreTypes.DocumentData
| GeoFirestoreTypes.modular.WithFieldValue<any> {
if (Object.prototype.toString.call(documentData) !== '[object Object]') {
throw new Error('document must be an object');
}
Expand All @@ -52,9 +61,13 @@ export function encodeDocumentSet(
* @return The document encoded as GeoDocument object.
*/
export function encodeDocumentUpdate(
documentData: GeoFirestoreTypes.UpdateData,
documentData:
| GeoFirestoreTypes.UpdateData
| GeoFirestoreTypes.modular.WithFieldValue<any>,
customKey?: string
): GeoFirestoreTypes.UpdateData {
):
| GeoFirestoreTypes.UpdateData
| GeoFirestoreTypes.modular.WithFieldValue<any> {
if (Object.prototype.toString.call(documentData) !== '[object Object]') {
throw new Error('document must be an object');
}
Expand All @@ -70,7 +83,9 @@ export function encodeDocumentUpdate(
* @return The document encoded as GeoDocument object.
*/
export function encodeGeoDocument(
geopoint: GeoFirestoreTypes.cloud.GeoPoint | GeoFirestoreTypes.web.GeoPoint,
geopoint:
| GeoFirestoreTypes.admin.GeoPoint
| GeoFirestoreTypes.compat.GeoPoint,
documentData: GeoFirestoreTypes.DocumentData
): GeoFirestoreTypes.GeoDocumentData {
validateLocation(geopoint);
Expand Down
18 changes: 9 additions & 9 deletions src/api/query-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import {calculateDistance, generateQuery} from '../utils';
* @param queryCriteria The query criteria of geo based queries, includes field such as center, radius, and limit.
*/
export function geoQueryGet(
query: GeoFirestoreTypes.cloud.Query | GeoFirestoreTypes.web.Query,
query: GeoFirestoreTypes.admin.Query | GeoFirestoreTypes.compat.Query,
queryCriteria: GeoFirestoreTypes.QueryCriteria,
options: GeoFirestoreTypes.web.GetOptions = {source: 'default'}
options: GeoFirestoreTypes.compat.GetOptions = {source: 'default'}
): Promise<GeoQuerySnapshot> {
const isWeb =
Object.prototype.toString.call(
(query as GeoFirestoreTypes.web.CollectionReference).firestore
(query as GeoFirestoreTypes.compat.CollectionReference).firestore
.enablePersistence
) === '[object Function]';

Expand All @@ -35,8 +35,8 @@ export function geoQueryGet(
} else {
query = queryCriteria.limit ? query.limit(queryCriteria.limit) : query;
const promise = isWeb
? (query as GeoFirestoreTypes.web.Query).get(options)
: (query as GeoFirestoreTypes.web.Query).get();
? (query as GeoFirestoreTypes.compat.Query).get(options)
: (query as GeoFirestoreTypes.compat.Query).get();

return promise.then(snapshot => new GeoQuerySnapshot(snapshot));
}
Expand All @@ -45,20 +45,20 @@ export function geoQueryGet(
* A `GeoJoinerGet` aggregates multiple `get` results.
*/
export class GeoQueryGet {
private _docs: Map<string, GeoFirestoreTypes.web.QueryDocumentSnapshot> =
private _docs: Map<string, GeoFirestoreTypes.compat.QueryDocumentSnapshot> =
new Map();

/**
* @param snapshots An array of snpashots from a Firestore Query `get` call.
* @param _queryCriteria The query criteria of geo based queries, includes field such as center, radius, and limit.
*/
constructor(
snapshots: GeoFirestoreTypes.web.QuerySnapshot[],
snapshots: GeoFirestoreTypes.compat.QuerySnapshot[],
private _queryCriteria: GeoFirestoreTypes.QueryCriteria
) {
validateQueryCriteria(_queryCriteria);

snapshots.forEach((snapshot: GeoFirestoreTypes.web.QuerySnapshot) => {
snapshots.forEach((snapshot: GeoFirestoreTypes.compat.QuerySnapshot) => {
snapshot.docs.forEach(doc => {
const distance = calculateDistance(
this._queryCriteria.center,
Expand Down Expand Up @@ -107,7 +107,7 @@ export class GeoQueryGet {
docs.map((doc, index) => {
return {doc, newIndex: index, oldIndex: -1, type: 'added'};
}),
} as GeoFirestoreTypes.web.QuerySnapshot,
} as GeoFirestoreTypes.compat.QuerySnapshot,
this._queryCriteria.center
);
}
Expand Down
18 changes: 9 additions & 9 deletions src/api/query-on-snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {GeoFirestoreTypes} from '../definitions';
import {calculateDistance, generateQuery} from '../utils';

interface DocMap {
change: GeoFirestoreTypes.web.DocumentChange;
change: GeoFirestoreTypes.compat.DocumentChange;
distance: number;
emitted: boolean;
}
Expand All @@ -20,7 +20,7 @@ interface DocMap {
* @param queryCriteria The query criteria of geo based queries, includes field such as center, radius, and limit.
*/
export function geoQueryOnSnapshot(
query: GeoFirestoreTypes.cloud.Query | GeoFirestoreTypes.web.Query,
query: GeoFirestoreTypes.admin.Query | GeoFirestoreTypes.compat.Query,
queryCriteria: GeoFirestoreTypes.QueryCriteria
): (
onNext: (snapshot: GeoQuerySnapshot) => void,
Expand All @@ -39,7 +39,7 @@ export function geoQueryOnSnapshot(
).unsubscribe();
} else {
query = queryCriteria.limit ? query.limit(queryCriteria.limit) : query;
return (query as GeoFirestoreTypes.web.Query).onSnapshot(
return (query as GeoFirestoreTypes.compat.Query).onSnapshot(
snapshot => onNext(new GeoQuerySnapshot(snapshot)),
onError
);
Expand Down Expand Up @@ -68,14 +68,14 @@ export class GeoQueryOnSnapshot {
* @param _onError A callback to be called if the listen fails or is cancelled. No further callbacks will occur.
*/
constructor(
private _queries: GeoFirestoreTypes.web.Query[],
private _queries: GeoFirestoreTypes.compat.Query[],
private _queryCriteria: GeoFirestoreTypes.QueryCriteria,
private _onNext: (snapshot: GeoQuerySnapshot) => void,
private _onError: (error: Error) => void = () => {}
) {
validateQueryCriteria(_queryCriteria);
this._queriesResolved = new Array(_queries.length).fill(0);
_queries.forEach((value: GeoFirestoreTypes.web.Query, index) => {
_queries.forEach((value: GeoFirestoreTypes.compat.Query, index) => {
const subscription = value.onSnapshot(
snapshot => this._processSnapshot(snapshot, index),
error => (this._error = error)
Expand Down Expand Up @@ -131,7 +131,7 @@ export class GeoQueryOnSnapshot {
let deductIndexBy = 0;
const docChanges = Array.from(this._docs.values()).map(
(value: DocMap, index: number) => {
const result: GeoFirestoreTypes.web.DocumentChange = {
const result: GeoFirestoreTypes.compat.DocumentChange = {
type: value.change.type,
doc: value.change.doc,
oldIndex: value.emitted ? value.change.newIndex : -1,
Expand Down Expand Up @@ -173,7 +173,7 @@ export class GeoQueryOnSnapshot {
}
return reduced;
}, []),
} as GeoFirestoreTypes.web.QuerySnapshot,
} as GeoFirestoreTypes.compat.QuerySnapshot,
this._queryCriteria.center
)
);
Expand Down Expand Up @@ -203,11 +203,11 @@ export class GeoQueryOnSnapshot {
* @param index Index of query who's snapshot has been triggered.
*/
private _processSnapshot(
snapshot: GeoFirestoreTypes.web.QuerySnapshot,
snapshot: GeoFirestoreTypes.compat.QuerySnapshot,
index: number
): void {
const docChanges = Array.isArray(snapshot.docChanges)
? (snapshot.docChanges as any as GeoFirestoreTypes.web.DocumentChange[])
? (snapshot.docChanges as any as GeoFirestoreTypes.compat.DocumentChange[])
: snapshot.docChanges();
if (!this._firstRoundResolved) this._queriesResolved[index] = 1;
if (docChanges.length) {
Expand Down
22 changes: 11 additions & 11 deletions src/api/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ export class GeoQuerySnapshot {
*/
constructor(
private _querySnapshot:
| GeoFirestoreTypes.web.QuerySnapshot
| GeoFirestoreTypes.cloud.QuerySnapshot,
| GeoFirestoreTypes.compat.QuerySnapshot
| GeoFirestoreTypes.admin.QuerySnapshot,
private _center?:
| GeoFirestoreTypes.cloud.GeoPoint
| GeoFirestoreTypes.web.GeoPoint
| GeoFirestoreTypes.admin.GeoPoint
| GeoFirestoreTypes.compat.GeoPoint
) {
if (_center) {
// Validate the _center coordinates
validateLocation(_center);
}

this._docs = (
_querySnapshot as GeoFirestoreTypes.cloud.QuerySnapshot
).docs.map((snapshot: GeoFirestoreTypes.cloud.QueryDocumentSnapshot) =>
_querySnapshot as GeoFirestoreTypes.admin.QuerySnapshot
).docs.map((snapshot: GeoFirestoreTypes.admin.QueryDocumentSnapshot) =>
generateGeoQueryDocumentSnapshot(snapshot, _center)
);
}

/** The native `QuerySnapshot` instance. */
get native():
| GeoFirestoreTypes.cloud.QuerySnapshot
| GeoFirestoreTypes.web.QuerySnapshot {
| GeoFirestoreTypes.admin.QuerySnapshot
| GeoFirestoreTypes.compat.QuerySnapshot {
return this._querySnapshot;
}

Expand Down Expand Up @@ -68,10 +68,10 @@ export class GeoQuerySnapshot {
docChanges(): GeoFirestoreTypes.DocumentChange[] {
const docChanges = Array.isArray(this._querySnapshot.docChanges)
? (this._querySnapshot
.docChanges as any as GeoFirestoreTypes.web.DocumentChange[])
.docChanges as any as GeoFirestoreTypes.compat.DocumentChange[])
: this._querySnapshot.docChanges();
return (docChanges as GeoFirestoreTypes.web.DocumentChange[]).map(
(change: GeoFirestoreTypes.web.DocumentChange) => {
return (docChanges as GeoFirestoreTypes.compat.DocumentChange[]).map(
(change: GeoFirestoreTypes.compat.DocumentChange) => {
return {
doc: generateGeoQueryDocumentSnapshot(change.doc, this._center),
newIndex: change.newIndex,
Expand Down
4 changes: 3 additions & 1 deletion src/api/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export function validateLimit(limit: number, flag = false): boolean {
* @param flag Tells function to send up boolean if not valid instead of throwing an error.
*/
export function validateLocation(
location: GeoFirestoreTypes.web.GeoPoint | GeoFirestoreTypes.cloud.GeoPoint,
location:
| GeoFirestoreTypes.compat.GeoPoint
| GeoFirestoreTypes.admin.GeoPoint,
flag = false
): boolean {
let error: string;
Expand Down
Loading