Skip to content

Commit 9cda691

Browse files
committedAug 15, 2018
feat(types): add typescript jsdoc to core
1 parent 9ad55ff commit 9cda691

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// import { firestore } from '../../../node_modules/firebase/index'
2+
3+
type TODO = any
4+
type EmptyObject = {}
5+
6+
type DocumentData = Record<string, any>
7+
8+
interface OperationsType<T> {
9+
set: (target: Record<string, any>, key: string, value: T) => T
10+
add: (array: TODO[], index: number, data: firebase.firestore.DocumentData) => T
11+
remove: (array: TODO[], index: number) => any
12+
}

‎packages/@posva/vuefire-core/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"url": "https://github.com/vuejs/vuefire/issues"
2828
},
2929
"devDependencies": {
30+
"firebase": "^5.3.1",
3031
"typescript": "^3.0.1"
3132
}
3233
}

‎packages/@posva/vuefire-core/src/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,16 @@ function subscribeToDocument({ ref, target, path, depth, resolve, ops }, options
221221
}
222222
}
223223

224+
/* TODO do not use an object
225+
*
226+
* @param {*} vm
227+
* @param {string} key
228+
* @param {firebase.firestore.DocumentReference} document
229+
* @param {*} resolve
230+
* @param {*} reject
231+
* @param {OperationsType<any>} ops
232+
* @param {*} options
233+
*/
224234
export function bindDocument({ vm, key, document, resolve, reject, ops }, options) {
225235
// TODO warning check if key exists?
226236
// const boundRefs = Object.create(null)

‎packages/@posva/vuefire-core/src/utils.js

+54-1
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,55 @@
1+
/**
2+
* @typedef {firebase.firestore.DocumentReference | firebase.firestore.CollectionReference} Reference
3+
*/
4+
/**
5+
*
6+
* @param {firebase.firestore.DocumentSnapshot} doc
7+
* @return {DocumentData}
8+
*/
19
export function createSnapshot(doc) {
210
// defaults everything to false, so no need to set
311
return Object.defineProperty(doc.data(), 'id', {
412
value: doc.id,
513
})
614
}
715

16+
/**
17+
*
18+
* @param {any} o
19+
* @returns {boolean}
20+
*/
821
function isObject(o) {
922
return o && typeof o === 'object'
1023
}
1124

25+
/**
26+
*
27+
* @param {any} o
28+
* should be o is Date https://github.com/Microsoft/TypeScript/issues/26297
29+
* @returns {boolean}
30+
*/
1231
function isTimestamp(o) {
1332
return o.toDate
1433
}
1534

35+
/**
36+
*
37+
* @param {*} o
38+
* @returns {boolean}
39+
*/
1640
function isRef(o) {
1741
return o && o.onSnapshot
1842
}
1943

20-
export function extractRefs(doc, oldDoc, path = '', result = [{}, {}]) {
44+
/**
45+
*
46+
* @param {firebase.firestore.DocumentData} doc
47+
* @param {firebase.firestore.DocumentData} [oldDoc]
48+
* @param {string} [path]
49+
* @param {[firebase.firestore.DocumentData, Record<string, Reference>]} result
50+
* @returns {[firebase.firestore.DocumentData, Record<string, Reference>]}
51+
*/
52+
export function extractRefs(doc, oldDoc = {}, path = '', result = [{}, {}]) {
2153
// must be set here because walkGet can return null or undefined
2254
oldDoc = oldDoc || {}
2355
const idDescriptor = Object.getOwnPropertyDescriptor(doc, 'id')
@@ -53,7 +85,15 @@ export function extractRefs(doc, oldDoc, path = '', result = [{}, {}]) {
5385
}, result)
5486
}
5587

88+
/**
89+
* @template T any
90+
* @template K any
91+
* @param {(arg: T) => K} fn
92+
* @param {() => T} argFn
93+
* @returns {() => K | undefined}
94+
*/
5695
export function callOnceWithArg(fn, argFn) {
96+
/** @type {boolean | undefined} */
5797
let called
5898
return () => {
5999
if (!called) {
@@ -63,10 +103,23 @@ export function callOnceWithArg(fn, argFn) {
63103
}
64104
}
65105

106+
/**
107+
*
108+
* @param {Record<string, any>} obj
109+
* @param {string} path
110+
* @returns {any}
111+
*/
66112
export function walkGet(obj, path) {
67113
return path.split('.').reduce((target, key) => target[key], obj)
68114
}
69115

116+
/**
117+
*
118+
* @param {Record<string, any>} obj
119+
* @param {string} path
120+
* @param {any} value
121+
* @returns
122+
*/
70123
export function walkSet(obj, path, value) {
71124
// path can be a number
72125
const keys = ('' + path).split('.')

‎packages/@posva/vuefire-core/tsconfig.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
2-
"include": ["src/**/*.js", "src/*.js"],
2+
"include": [
3+
"../../../node_modules/firebase/index.d.ts",
4+
"declarations.d.ts",
5+
"src/index.js",
6+
"src/utils.js"
7+
],
38
"compilerOptions": {
49
/* Basic Options */
510
"target": "esnext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
@@ -22,7 +27,7 @@
2227
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
2328

2429
/* Strict Type-Checking Options */
25-
// "strict": true /* Enable all strict type-checking options. */,
30+
"strict": true /* Enable all strict type-checking options. */,
2631
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
2732
// "strictNullChecks": true, /* Enable strict null checks. */
2833
// "strictFunctionTypes": true, /* Enable strict checking of function types. */

‎yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -2901,7 +2901,7 @@ find-up@^3.0.0:
29012901
dependencies:
29022902
locate-path "^3.0.0"
29032903

2904-
firebase@^5.2.0:
2904+
firebase@^5.2.0, firebase@^5.3.1:
29052905
version "5.3.1"
29062906
resolved "https://registry.yarnpkg.com/firebase/-/firebase-5.3.1.tgz#99df1a9b101d12cd765ef010bb015fdb0b76850b"
29072907
dependencies:

0 commit comments

Comments
 (0)
Please sign in to comment.