Skip to content

Commit

Permalink
Remove Firestore TS definitions (#52)
Browse files Browse the repository at this point in the history
* ♻️

* use new firestore generic references

* fixtures

* write the definitions file

* check in fixture
  • Loading branch information
ZakTax authored Nov 16, 2018
1 parent 29c3bda commit f37bd29
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 249 deletions.
212 changes: 88 additions & 124 deletions langs/ts/ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@ func (m *Modeler) Model(schema *firemodel.Schema, sourceCoder firemodel.SourceCo
}
defer f.Close()

// d, err := os.Create("firebase.d.ts")
d, err := sourceCoder.NewFile("firebase.d.ts")
if err != nil {
return errors.Wrapf(err, "firemodel/ts: create typescript definition file")
}
defer d.Close()

if err := tpl.Execute(f, schema); err != nil {
return errors.Wrapf(err, "firemodel/ts: generating typescript")
}

_, err = d.Write([]byte(definitions))
if err != nil {
return errors.Wrapf(err, "firemodel/ts: writing typescript definitions")
}

return nil
}

Expand Down Expand Up @@ -71,7 +84,7 @@ func toTypescriptType(firetype firemodel.SchemaFieldType) string {
return "firestore.Blob"
case *firemodel.Reference:
if firetype.T != nil {
return fmt.Sprintf("DocumentReference<%s>", interfaceName(firetype.T.Name))
return fmt.Sprintf("firestore.DocumentReference<%s>", interfaceName(firetype.T.Name))
} else {
return "firestore.DocumentReference"
}
Expand Down Expand Up @@ -137,128 +150,6 @@ const (
file = `// DO NOT EDIT - Code generated by firemodel {{firemodelVersion}}.
import { firestore } from 'firebase';
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export interface Query<DataType = firestore.DocumentData>
extends firestore.Query {
where(
fieldPath: string | firestore.FieldPath,
opStr: firestore.WhereFilterOp,
value: any,
): Query<DataType>;
orderBy(
fieldPath: string | firestore.FieldPath,
directionStr?: firestore.OrderByDirection,
): Query<DataType>;
limit(limit: number): Query<DataType>;
startAt(snapshot: DocumentSnapshot): Query<DataType>;
startAt(...fieldValues: any[]): Query<DataType>;
startAfter(snapshot: DocumentSnapshot): Query<DataType>;
startAfter(...fieldValues: any[]): Query<DataType>;
endBefore(snapshot: DocumentSnapshot): Query<DataType>;
endBefore(...fieldValues: any[]): Query<DataType>;
endAt(snapshot: DocumentSnapshot): Query<DataType>;
endAt(...fieldValues: any[]): Query<DataType>;
get(options?: firestore.GetOptions): Promise<QuerySnapshot<DataType>>;
onSnapshot(observer: {
next?: (snapshot: QuerySnapshot<DataType>) => void;
error?: (error: Error) => void;
complete?: () => void;
}): () => void;
onSnapshot(
options: firestore.SnapshotListenOptions,
observer: {
next?: (snapshot: QuerySnapshot<DataType>) => void;
error?: (error: Error) => void;
complete?: () => void;
},
): () => void;
onSnapshot(
onNext: (snapshot: QuerySnapshot<DataType>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
onSnapshot(
options: firestore.SnapshotListenOptions,
onNext: (snapshot: QuerySnapshot<DataType>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
}
export interface DocumentSnapshot<DataType = firestore.DocumentData>
extends firestore.DocumentSnapshot {
data(options?: firestore.SnapshotOptions): DataType | undefined;
}
export interface QueryDocumentSnapshot<DataType = firestore.DocumentData>
extends firestore.QueryDocumentSnapshot {
data(options?: firestore.SnapshotOptions): DataType | undefined;
}
export interface QuerySnapshot<DataType = firestore.DocumentData>
extends firestore.QuerySnapshot {
readonly docs: QueryDocumentSnapshot<DataType>[];
}
export interface DocumentSnapshotExpanded<DataType = firestore.DocumentData> {
exists: firestore.DocumentSnapshot['exists'];
ref: firestore.DocumentSnapshot['ref'];
id: firestore.DocumentSnapshot['id'];
metadata: firestore.DocumentSnapshot['metadata'];
data: DataType;
}
export interface QuerySnapshotExpanded<DataType = firestore.DocumentData> {
metadata: {
hasPendingWrites: firestore.QuerySnapshot['metadata']['hasPendingWrites'];
fromCache: firestore.QuerySnapshot['metadata']['fromCache'];
};
size: firestore.QuerySnapshot['size'];
empty: firestore.QuerySnapshot['empty'];
docs: {
[docId: string]: DocumentSnapshotExpanded<DataType>;
};
}
export interface DocumentReference<DataType = firestore.DocumentData>
extends firestore.DocumentReference {
set(data: DataType, options?: firestore.SetOptions): Promise<void>;
get(options?: firestore.GetOptions): Promise<DocumentSnapshot<DataType>>;
onSnapshot(observer: {
next?: (snapshot: DocumentSnapshot<DataType>) => void;
error?: (error: firestore.FirestoreError) => void;
complete?: () => void;
}): () => void;
onSnapshot(
options: firestore.SnapshotListenOptions,
observer: {
next?: (snapshot: DocumentSnapshot<DataType>) => void;
error?: (error: Error) => void;
complete?: () => void;
},
): () => void;
onSnapshot(
onNext: (snapshot: DocumentSnapshot<DataType>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
onSnapshot(
options: firestore.SnapshotListenOptions,
onNext: (snapshot: DocumentSnapshot<DataType>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
}
export interface CollectionReference<DataType = firestore.DocumentData>
extends Query<DataType>,
Omit<firestore.CollectionReference, keyof Query> {
add(data: DataType): Promise<DocumentReference>;
}
export interface Collection<DataType = firestore.DocumentData> {
[id: string]: DocumentSnapshotExpanded<DataType>;
}
// tslint:disable-next-line:no-namespace
export namespace {{.Options | getSchemaOption "ts" "namespace" "firemodel"}} {
type URL = string;
Expand Down Expand Up @@ -295,7 +186,7 @@ export namespace {{.Options | getSchemaOption "ts" "namespace" "firemodel"}} {
{{- else }}
/** TODO: Add documentation to {{.Name}} in firemodel schema. */
{{- end}}
{{.Name | ToLowerCamel}}: CollectionReference<{{.Type.Name | interfaceName | ToCamel}}>;
{{.Name | ToLowerCamel}}: firestore.CollectionReference<{{.Type.Name | interfaceName | ToCamel}}>;
{{- end}}
{{- range .Fields}}
Expand Down Expand Up @@ -352,4 +243,77 @@ export namespace {{.Options | getSchemaOption "ts" "namespace" "firemodel"}} {
{{.Name}} = '{{.Name | ToScreamingSnake}}',
{{- end}}
}`

definitions = `import * as FIREBASE from 'firebase';
declare module 'firebase' {
namespace firestore {
// Snapshots
export interface DocumentSnapshot<T = DocumentData> {
data(options?: SnapshotOptions): D | undefined;
}
export interface QueryDocumentSnapshot<T = DocumentData> extends DocumentSnapshot {
data(options?: SnapshotOptions): T;
}
export interface QuerySnapshot<T = DocumentData> {
readonly docs: QueryDocumentSnapshot<T>[];
forEach(callback: (result: QueryDocumentSnapshot<T>) => void, thisArg?: any): void;
}
// References + Queries
export interface DocumentReference<T = DocumentData> {
onSnapshot(observer: {
next?: (snapshot: DocumentSnapshot<T>) => void;
error?: (error: FirestoreError) => void;
complete?: () => void;
}): () => void;
onSnapshot(
options: SnapshotListenOptions,
observer: {
next?: (snapshot: DocumentSnapshot<T>) => void;
error?: (error: Error) => void;
complete?: () => void;
},
): () => void;
onSnapshot(
onNext: (snapshot: DocumentSnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
onSnapshot(
options: SnapshotListenOptions,
onNext: (snapshot: DocumentSnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
}
export interface Query<T = DocumentData> {
onSnapshot(observer: {
next?: (snapshot: QuerySnapshot<T>) => void;
error?: (error: Error) => void;
complete?: () => void;
}): () => void;
onSnapshot(
options: SnapshotListenOptions,
observer: {
next?: (snapshot: QuerySnapshot<T>) => void;
error?: (error: Error) => void;
complete?: () => void;
},
): () => void;
onSnapshot(
onNext: (snapshot: QuerySnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
onSnapshot(
options: SnapshotListenOptions,
onNext: (snapshot: QuerySnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
}
export interface CollectionReference<T = DocumentData> extends Query<T> {}
}
}`
)
72 changes: 72 additions & 0 deletions testfixtures/firemodel/TestFiremodelFromSchema/ts/firebase.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as FIREBASE from 'firebase';

declare module 'firebase' {
namespace firestore {
// Snapshots
export interface DocumentSnapshot<T = DocumentData> {
data(options?: SnapshotOptions): D | undefined;
}
export interface QueryDocumentSnapshot<T = DocumentData> extends DocumentSnapshot {
data(options?: SnapshotOptions): T;
}
export interface QuerySnapshot<T = DocumentData> {
readonly docs: QueryDocumentSnapshot<T>[];
forEach(callback: (result: QueryDocumentSnapshot<T>) => void, thisArg?: any): void;
}

// References + Queries
export interface DocumentReference<T = DocumentData> {
onSnapshot(observer: {
next?: (snapshot: DocumentSnapshot<T>) => void;
error?: (error: FirestoreError) => void;
complete?: () => void;
}): () => void;
onSnapshot(
options: SnapshotListenOptions,
observer: {
next?: (snapshot: DocumentSnapshot<T>) => void;
error?: (error: Error) => void;
complete?: () => void;
},
): () => void;
onSnapshot(
onNext: (snapshot: DocumentSnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
onSnapshot(
options: SnapshotListenOptions,
onNext: (snapshot: DocumentSnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
}
export interface Query<T = DocumentData> {
onSnapshot(observer: {
next?: (snapshot: QuerySnapshot<T>) => void;
error?: (error: Error) => void;
complete?: () => void;
}): () => void;
onSnapshot(
options: SnapshotListenOptions,
observer: {
next?: (snapshot: QuerySnapshot<T>) => void;
error?: (error: Error) => void;
complete?: () => void;
},
): () => void;
onSnapshot(
onNext: (snapshot: QuerySnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
onSnapshot(
options: SnapshotListenOptions,
onNext: (snapshot: QuerySnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void,
): () => void;
}
export interface CollectionReference<T = DocumentData> extends Query<T> {}
}
}
Loading

0 comments on commit f37bd29

Please sign in to comment.