Skip to content

Commit

Permalink
Merge pull request #70 from tylim88/new-features
Browse files Browse the repository at this point in the history
cleanup and rename setting
  • Loading branch information
tylim88 authored Jul 18, 2022
2 parents 9f6b347 + 9237764 commit 39d7977
Show file tree
Hide file tree
Showing 40 changed files with 282 additions and 333 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# HISTORY

## v1.5.0 19-July-2022

- rename setting `allFieldsPossiblyUndefined` as `allFieldsPossiblyReadAsUndefined`
- remove onSnapshot onCompletion parameter

## v1.3.0 6-May-2022

- orderBy('\_name') cursor now only accept full doc path, if input type is string, require const assertion or else display "Please use const assertion" error message.
Expand Down
2 changes: 1 addition & 1 deletion codeForDoc/src/possiblyUndefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type abc2 = MetaTypeCreator<
'abc',
string,
null, // no parent
{ allFieldsPossiblyUndefined: true }
{ allFieldsPossiblyReadAsUndefined: true }
>

const docRef2 = getFirelord<abc2>()('abc').doc('efg')
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "firelordjs",
"version": "1.4.8",
"version": "1.5.0",
"description": "🔥 Extremely High Precision Typescript Wrapper for Firestore Web, Providing Unparalleled Type Safe and Dev Experience",
"source": "src/index.ts",
"main": "dist/src/index.js",
Expand All @@ -19,7 +19,7 @@
"test": "firebase emulators:exec --only firestore \"rm -rf coverage && jest --forceExit\"",
"parcel": "npm un firebase && npm i firebase && rm -rf dist && tsc --emitDeclarationOnly true && parcel build && npm i -D firebase",
"build": "rm -rf dist && tsc",
"link": "npm link && npm link firelordjs",
"link": "npm unlink firelordjs && npm link && npm link firelordjs",
"dev": "---------------------dev------------------------",
"d-link": "npm run build && npm run link",
"d-test": "tsc && npm test",
Expand Down
9 changes: 4 additions & 5 deletions src/batch/delete.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { OriWriteBatch, OriDocumentReference, WriteBatchDelete } from '../types'

export const deleteCreator = ((writeBatch: OriWriteBatch) =>
(reference: OriDocumentReference) => {
const ref = writeBatch.delete(reference)
return ref
}) as unknown as (writeBatch: OriWriteBatch) => WriteBatchDelete
export const deleteCreator = (writeBatch: OriWriteBatch) =>
((reference: OriDocumentReference) => {
return writeBatch.delete(reference)
}) as WriteBatchDelete
2 changes: 1 addition & 1 deletion src/batch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const writeBatch = (
): WriteBatch => {
const batch = writeBatch_(
// @ts-expect-error
firestore || getFirestore() // ! type messed up, after adding firestore of testing type, weird
firestore || getFirestore()
)
return Object.freeze({
commit: () => batch.commit(),
Expand Down
8 changes: 3 additions & 5 deletions src/batch/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
WriteBatchSet,
} from '../types'

export const setCreator = ((writeBatch: OriWriteBatch) =>
(
export const setCreator = (writeBatch: OriWriteBatch) =>
((
reference: OriDocumentReference,
data: OriDocumentData,
options?: OriSetOptions
Expand All @@ -16,6 +16,4 @@ export const setCreator = ((writeBatch: OriWriteBatch) =>
? writeBatch.set(reference, data, options)
: writeBatch.set(reference, data)
return ref
}) as unknown as setCreator

type setCreator = (writeBatch: OriWriteBatch) => WriteBatchSet
}) as WriteBatchSet
6 changes: 3 additions & 3 deletions src/batch/update.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { OriWriteBatch, OriDocumentReference, WriteBatchUpdate } from '../types'
import { flatten } from '../utils'

export const updateCreator = ((writeBatch: OriWriteBatch) =>
(reference: OriDocumentReference, data: Record<string, unknown>) => {
export const updateCreator = (writeBatch: OriWriteBatch) =>
((reference: OriDocumentReference, data: Record<string, unknown>) => {
const ref = writeBatch.update(
reference,
// @ts-expect-error
flatten(data)
)
return ref
}) as unknown as (writeBatch: OriWriteBatch) => WriteBatchUpdate
}) as WriteBatchUpdate
9 changes: 6 additions & 3 deletions src/equal/refEqual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import { DocumentReference, CollectionReference, MetaType } from '../types'
* @returns true if the references point to the same location in the same
* Firestore database.
*/
export const refEqual = (
left: DocumentReference<MetaType> | CollectionReference<MetaType>,
right: DocumentReference<MetaType> | CollectionReference<MetaType>
export const refEqual = <
T extends DocumentReference<MetaType> | CollectionReference<MetaType>,
U extends T
>(
left: T,
right: U
) => {
return refEqual_(
// @ts-expect-error
Expand Down
10 changes: 6 additions & 4 deletions src/equal/snapshotEqual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { DocumentSnapshot, QuerySnapshot, MetaType } from '../types'
* @param right - A snapshot to compare.
* @returns true if the snapshots are equal.
*/
export const snapshotEqual = (
export const snapshotEqual = <
// ! DocumentSnapshot<User> does not extends DocumentSnapshot<MetaType>...why? same case with QuerySnapshot
// eslint-disable-next-line @typescript-eslint/no-explicit-any
left: DocumentSnapshot<any> | QuerySnapshot<any>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
right: DocumentSnapshot<any> | QuerySnapshot<any>
T extends DocumentSnapshot<any> | QuerySnapshot<any>,
U extends T
>(
left: T,
right: U
) => {
return snapshotEqual_(
// @ts-expect-error
Expand Down
7 changes: 2 additions & 5 deletions src/onSnapshot/onSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('test onSnapshot', () => {
)
})
})
it('test one doc functionality and type with options', done => {
it('test one doc functionality and type', done => {
const docRef = userRef.doc('onSnapshotWithOptionsOneDocTest')
const data = generateRandomData()
expect.hasAssertions()
Expand All @@ -59,16 +59,13 @@ describe('test onSnapshot', () => {
unsub()
done()
},
() => {
//
},
() => {
//
}
)
})
})
it('test naked query functionality and type', done => {
it('test naked query functionality and type with options', done => {
const docId = 'onSnapshotNakedQueryTest'
const docRef = userRef.doc(docId)
const data = generateRandomData()
Expand Down
57 changes: 11 additions & 46 deletions src/onSnapshot/onSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import {
OriFirestoreError,
OriSnapshotListenOptions,
OriQuery,
OriUnsubscribe,
Unsubscribe,
DocumentSnapshot,
QuerySnapshot,
FirestoreError,
SnapshotListenOptions,
} from '../types'

export const isOptions = (
Expand All @@ -25,30 +27,23 @@ export const isOptions = (
export const onSnapshot: OnSnapshot = (
reference,
onNext,
onError,
onCompletion?: (() => void) | OriSnapshotListenOptions,
onError?: ((error: FirestoreError) => void) | OriSnapshotListenOptions,
options?: OriSnapshotListenOptions
) => {
const newOnError = isOptions(onError) ? undefined : onError
const newOncCompletion = isOptions(onCompletion) ? undefined : onCompletion
const newOptions =
options ||
(isOptions(onError) ? onError : undefined) ||
(isOptions(onCompletion) ? onCompletion : undefined)
const newOptions = options || (isOptions(onError) ? onError : undefined)

return newOptions
? onSnapshot_(reference as OriQuery, newOptions, {
// @ts-expect-error
next: onNext,
error: newOnError,
complete: newOncCompletion,
})
: onSnapshot_(
reference as OriQuery,
// @ts-expect-error
onNext,
newOnError,
newOncCompletion
newOnError
)
}

Expand All @@ -66,8 +61,6 @@ type OnSnapshot = {
* is available.
* @param onError - An optional callback to be called if the listen fails or is
* cancelled. No further callbacks will occur.
* @param onCompletion - optional callback, but will not be called since streams are
* never ending.
* @param options - Options controlling the listen behavior.
* @returns An unsubscribe function that can be called to cancel
* the snapshot listener.
Expand All @@ -79,37 +72,9 @@ type OnSnapshot = {
? DocumentSnapshot<T>
: QuerySnapshot<T>
) => void,
onError?: (error: OriFirestoreError) => void,
onCompletion?: () => void,
options?: OriSnapshotListenOptions
): OriUnsubscribe
/**
* Attaches a listener for `DocumentSnapshot` events. You may either pass
* individual `onNext` and `onError` callbacks or pass a single observer
* object with `next` and `error` callbacks.
*
* NOTE: Although an `onCompletion` callback can be provided, it will
* never be called because the snapshot stream is never-ending.
*
* @param reference - A reference to the document to listen to.
* @param onNext - A callback to be called every time a new `DocumentSnapshot`
* is available.
* @param onError - An optional callback to be called if the listen fails or is
* cancelled. No further callbacks will occur.
* @param options - Options controlling the listen behavior.
* @returns An unsubscribe function that can be called to cancel
* the snapshot listener.
*/
<T extends MetaType, Ref extends Query<T> | DocumentReference<T>>(
reference: Ref extends never ? Ref : Query<T> | DocumentReference<T>,
onNext: (
snapshot: Ref extends DocumentReference<T>
? DocumentSnapshot<T>
: QuerySnapshot<T>
) => void,
onError?: (error: OriFirestoreError) => void,
options?: OriSnapshotListenOptions
): OriUnsubscribe
onError?: (error: FirestoreError) => void,
options?: SnapshotListenOptions
): Unsubscribe
/**
* Attaches a listener for `DocumentSnapshot` events. You may either pass
* individual `onNext` and `onError` callbacks or pass a single observer
Expand All @@ -132,6 +97,6 @@ type OnSnapshot = {
? DocumentSnapshot<T>
: QuerySnapshot<T>
) => void,
options?: OriSnapshotListenOptions
): OriUnsubscribe
options?: SnapshotListenOptions
): Unsubscribe
}
2 changes: 1 addition & 1 deletion src/operations/addDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ export const addDoc = <T extends MetaType>(
// @ts-expect-error
reference,
data
) as unknown as Promise<DocumentReference<T>>
) as Promise<DocumentReference<T>>
}
12 changes: 9 additions & 3 deletions src/operations/deleteDoc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { deleteDoc as deleteDoc_ } from 'firebase/firestore'
import { OriDocumentReference, Delete } from '../types'

/**
Deletes the document referred to by the specified DocumentReference.
@param reference — A reference to the document to delete.
@returns A Promise resolved once the document has been successfully deleted from the backend (note that it won't resolve while you're offline).
*/
export const deleteDoc = ((reference: OriDocumentReference) => {
const ref = deleteDoc_(reference)
return ref
}) as unknown as Delete
return deleteDoc_(reference)
}) as Delete
32 changes: 10 additions & 22 deletions src/operations/getDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
getDocFromCache as getDocFromCache_,
getDocFromServer as getDocFromServer_,
} from 'firebase/firestore'
import { Get } from '../types'
import { Get, OriDocumentReference, DocumentSnapshot, MetaType } from '../types'
/**
Reads the document referred to by this DocumentReference.
Expand All @@ -14,13 +14,9 @@ Note: getDoc() attempts to provide up-to-date data when possible by waiting for
@returns
A Promise resolved with a DocumentSnapshot containing the current document contents.
*/
// @ts-expect-error
export const getDoc: Get = reference => {
return getDoc_(
// @ts-expect-error
reference
)
}
export const getDoc: Get = ((reference: OriDocumentReference) => {
return getDoc_(reference) as Promise<DocumentSnapshot<MetaType>>
}) as Get

/**
* Reads the document referred to by this `DocumentReference` from cache.
Expand All @@ -29,24 +25,16 @@ export const getDoc: Get = reference => {
* @returns A `Promise` resolved with a `DocumentSnapshot` containing the
* current document contents.
*/
// @ts-expect-error
export const getDocFromCache: Get = reference => {
return getDocFromCache_(
// @ts-expect-error
reference
)
}
export const getDocFromCache = ((reference: OriDocumentReference) => {
return getDocFromCache_(reference) as Promise<DocumentSnapshot<MetaType>>
}) as Get
/**
* Reads the document referred to by this `DocumentReference` from the server.
* Returns an error if the network is not available.
*
* @returns A `Promise` resolved with a `DocumentSnapshot` containing the
* current document contents.
*/
// @ts-expect-error
export const getDocFromServer: Get = async reference => {
return getDocFromServer_(
// @ts-expect-error
reference
)
}
export const getDocFromServer = ((reference: OriDocumentReference) => {
return getDocFromServer_(reference) as Promise<DocumentSnapshot<MetaType>>
}) as Get
Loading

0 comments on commit 39d7977

Please sign in to comment.