diff --git a/langs/ts/ts.go b/langs/ts/ts.go index 3d7e4c4..89ef720 100644 --- a/langs/ts/ts.go +++ b/langs/ts/ts.go @@ -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 } @@ -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" } @@ -137,128 +150,6 @@ const ( file = `// DO NOT EDIT - Code generated by firemodel {{firemodelVersion}}. import { firestore } from 'firebase'; -type Omit = Pick>; - -export interface Query - extends firestore.Query { - where( - fieldPath: string | firestore.FieldPath, - opStr: firestore.WhereFilterOp, - value: any, - ): Query; - orderBy( - fieldPath: string | firestore.FieldPath, - directionStr?: firestore.OrderByDirection, - ): Query; - limit(limit: number): Query; - startAt(snapshot: DocumentSnapshot): Query; - startAt(...fieldValues: any[]): Query; - startAfter(snapshot: DocumentSnapshot): Query; - startAfter(...fieldValues: any[]): Query; - endBefore(snapshot: DocumentSnapshot): Query; - endBefore(...fieldValues: any[]): Query; - endAt(snapshot: DocumentSnapshot): Query; - endAt(...fieldValues: any[]): Query; - get(options?: firestore.GetOptions): Promise>; - onSnapshot(observer: { - next?: (snapshot: QuerySnapshot) => void; - error?: (error: Error) => void; - complete?: () => void; - }): () => void; - onSnapshot( - options: firestore.SnapshotListenOptions, - observer: { - next?: (snapshot: QuerySnapshot) => void; - error?: (error: Error) => void; - complete?: () => void; - }, - ): () => void; - onSnapshot( - onNext: (snapshot: QuerySnapshot) => void, - onError?: (error: Error) => void, - onCompletion?: () => void, - ): () => void; - onSnapshot( - options: firestore.SnapshotListenOptions, - onNext: (snapshot: QuerySnapshot) => void, - onError?: (error: Error) => void, - onCompletion?: () => void, - ): () => void; -} - - -export interface DocumentSnapshot - extends firestore.DocumentSnapshot { - data(options?: firestore.SnapshotOptions): DataType | undefined; -} -export interface QueryDocumentSnapshot - extends firestore.QueryDocumentSnapshot { - data(options?: firestore.SnapshotOptions): DataType | undefined; -} -export interface QuerySnapshot - extends firestore.QuerySnapshot { - readonly docs: QueryDocumentSnapshot[]; -} -export interface DocumentSnapshotExpanded { - exists: firestore.DocumentSnapshot['exists']; - ref: firestore.DocumentSnapshot['ref']; - id: firestore.DocumentSnapshot['id']; - metadata: firestore.DocumentSnapshot['metadata']; - data: DataType; -} -export interface QuerySnapshotExpanded { - metadata: { - hasPendingWrites: firestore.QuerySnapshot['metadata']['hasPendingWrites']; - fromCache: firestore.QuerySnapshot['metadata']['fromCache']; - }; - size: firestore.QuerySnapshot['size']; - empty: firestore.QuerySnapshot['empty']; - docs: { - [docId: string]: DocumentSnapshotExpanded; - }; -} -export interface DocumentReference - extends firestore.DocumentReference { - set(data: DataType, options?: firestore.SetOptions): Promise; - get(options?: firestore.GetOptions): Promise>; - onSnapshot(observer: { - next?: (snapshot: DocumentSnapshot) => void; - error?: (error: firestore.FirestoreError) => void; - complete?: () => void; - }): () => void; - onSnapshot( - options: firestore.SnapshotListenOptions, - observer: { - next?: (snapshot: DocumentSnapshot) => void; - error?: (error: Error) => void; - complete?: () => void; - }, - ): () => void; - onSnapshot( - onNext: (snapshot: DocumentSnapshot) => void, - onError?: (error: Error) => void, - onCompletion?: () => void, - ): () => void; - onSnapshot( - options: firestore.SnapshotListenOptions, - onNext: (snapshot: DocumentSnapshot) => void, - onError?: (error: Error) => void, - onCompletion?: () => void, - ): () => void; -} - - - -export interface CollectionReference - extends Query, - Omit { - add(data: DataType): Promise; -} -export interface Collection { - [id: string]: DocumentSnapshotExpanded; -} - - // tslint:disable-next-line:no-namespace export namespace {{.Options | getSchemaOption "ts" "namespace" "firemodel"}} { type URL = string; @@ -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}} @@ -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 { + data(options?: SnapshotOptions): D | undefined; + } + export interface QueryDocumentSnapshot extends DocumentSnapshot { + data(options?: SnapshotOptions): T; + } + export interface QuerySnapshot { + readonly docs: QueryDocumentSnapshot[]; + forEach(callback: (result: QueryDocumentSnapshot) => void, thisArg?: any): void; + } + + // References + Queries + export interface DocumentReference { + onSnapshot(observer: { + next?: (snapshot: DocumentSnapshot) => void; + error?: (error: FirestoreError) => void; + complete?: () => void; + }): () => void; + onSnapshot( + options: SnapshotListenOptions, + observer: { + next?: (snapshot: DocumentSnapshot) => void; + error?: (error: Error) => void; + complete?: () => void; + }, + ): () => void; + onSnapshot( + onNext: (snapshot: DocumentSnapshot) => void, + onError?: (error: Error) => void, + onCompletion?: () => void, + ): () => void; + onSnapshot( + options: SnapshotListenOptions, + onNext: (snapshot: DocumentSnapshot) => void, + onError?: (error: Error) => void, + onCompletion?: () => void, + ): () => void; + } + export interface Query { + onSnapshot(observer: { + next?: (snapshot: QuerySnapshot) => void; + error?: (error: Error) => void; + complete?: () => void; + }): () => void; + onSnapshot( + options: SnapshotListenOptions, + observer: { + next?: (snapshot: QuerySnapshot) => void; + error?: (error: Error) => void; + complete?: () => void; + }, + ): () => void; + onSnapshot( + onNext: (snapshot: QuerySnapshot) => void, + onError?: (error: Error) => void, + onCompletion?: () => void, + ): () => void; + onSnapshot( + options: SnapshotListenOptions, + onNext: (snapshot: QuerySnapshot) => void, + onError?: (error: Error) => void, + onCompletion?: () => void, + ): () => void; + } + export interface CollectionReference extends Query {} + } +}` ) diff --git a/testfixtures/firemodel/TestFiremodelFromSchema/ts/firebase.d.ts b/testfixtures/firemodel/TestFiremodelFromSchema/ts/firebase.d.ts new file mode 100644 index 0000000..f796807 --- /dev/null +++ b/testfixtures/firemodel/TestFiremodelFromSchema/ts/firebase.d.ts @@ -0,0 +1,72 @@ +import * as FIREBASE from 'firebase'; + +declare module 'firebase' { + namespace firestore { + // Snapshots + export interface DocumentSnapshot { + data(options?: SnapshotOptions): D | undefined; + } + export interface QueryDocumentSnapshot extends DocumentSnapshot { + data(options?: SnapshotOptions): T; + } + export interface QuerySnapshot { + readonly docs: QueryDocumentSnapshot[]; + forEach(callback: (result: QueryDocumentSnapshot) => void, thisArg?: any): void; + } + + // References + Queries + export interface DocumentReference { + onSnapshot(observer: { + next?: (snapshot: DocumentSnapshot) => void; + error?: (error: FirestoreError) => void; + complete?: () => void; + }): () => void; + onSnapshot( + options: SnapshotListenOptions, + observer: { + next?: (snapshot: DocumentSnapshot) => void; + error?: (error: Error) => void; + complete?: () => void; + }, + ): () => void; + onSnapshot( + onNext: (snapshot: DocumentSnapshot) => void, + onError?: (error: Error) => void, + onCompletion?: () => void, + ): () => void; + onSnapshot( + options: SnapshotListenOptions, + onNext: (snapshot: DocumentSnapshot) => void, + onError?: (error: Error) => void, + onCompletion?: () => void, + ): () => void; + } + export interface Query { + onSnapshot(observer: { + next?: (snapshot: QuerySnapshot) => void; + error?: (error: Error) => void; + complete?: () => void; + }): () => void; + onSnapshot( + options: SnapshotListenOptions, + observer: { + next?: (snapshot: QuerySnapshot) => void; + error?: (error: Error) => void; + complete?: () => void; + }, + ): () => void; + onSnapshot( + onNext: (snapshot: QuerySnapshot) => void, + onError?: (error: Error) => void, + onCompletion?: () => void, + ): () => void; + onSnapshot( + options: SnapshotListenOptions, + onNext: (snapshot: QuerySnapshot) => void, + onError?: (error: Error) => void, + onCompletion?: () => void, + ): () => void; + } + export interface CollectionReference extends Query {} + } +} \ No newline at end of file diff --git a/testfixtures/firemodel/TestFiremodelFromSchema/ts/firemodel.ts b/testfixtures/firemodel/TestFiremodelFromSchema/ts/firemodel.ts index 786dd82..920fab2 100644 --- a/testfixtures/firemodel/TestFiremodelFromSchema/ts/firemodel.ts +++ b/testfixtures/firemodel/TestFiremodelFromSchema/ts/firemodel.ts @@ -1,128 +1,6 @@ // DO NOT EDIT - Code generated by firemodel (dev). import { firestore } from 'firebase'; -type Omit = Pick>; - -export interface Query - extends firestore.Query { - where( - fieldPath: string | firestore.FieldPath, - opStr: firestore.WhereFilterOp, - value: any, - ): Query; - orderBy( - fieldPath: string | firestore.FieldPath, - directionStr?: firestore.OrderByDirection, - ): Query; - limit(limit: number): Query; - startAt(snapshot: DocumentSnapshot): Query; - startAt(...fieldValues: any[]): Query; - startAfter(snapshot: DocumentSnapshot): Query; - startAfter(...fieldValues: any[]): Query; - endBefore(snapshot: DocumentSnapshot): Query; - endBefore(...fieldValues: any[]): Query; - endAt(snapshot: DocumentSnapshot): Query; - endAt(...fieldValues: any[]): Query; - get(options?: firestore.GetOptions): Promise>; - onSnapshot(observer: { - next?: (snapshot: QuerySnapshot) => void; - error?: (error: Error) => void; - complete?: () => void; - }): () => void; - onSnapshot( - options: firestore.SnapshotListenOptions, - observer: { - next?: (snapshot: QuerySnapshot) => void; - error?: (error: Error) => void; - complete?: () => void; - }, - ): () => void; - onSnapshot( - onNext: (snapshot: QuerySnapshot) => void, - onError?: (error: Error) => void, - onCompletion?: () => void, - ): () => void; - onSnapshot( - options: firestore.SnapshotListenOptions, - onNext: (snapshot: QuerySnapshot) => void, - onError?: (error: Error) => void, - onCompletion?: () => void, - ): () => void; -} - - -export interface DocumentSnapshot - extends firestore.DocumentSnapshot { - data(options?: firestore.SnapshotOptions): DataType | undefined; -} -export interface QueryDocumentSnapshot - extends firestore.QueryDocumentSnapshot { - data(options?: firestore.SnapshotOptions): DataType | undefined; -} -export interface QuerySnapshot - extends firestore.QuerySnapshot { - readonly docs: QueryDocumentSnapshot[]; -} -export interface DocumentSnapshotExpanded { - exists: firestore.DocumentSnapshot['exists']; - ref: firestore.DocumentSnapshot['ref']; - id: firestore.DocumentSnapshot['id']; - metadata: firestore.DocumentSnapshot['metadata']; - data: DataType; -} -export interface QuerySnapshotExpanded { - metadata: { - hasPendingWrites: firestore.QuerySnapshot['metadata']['hasPendingWrites']; - fromCache: firestore.QuerySnapshot['metadata']['fromCache']; - }; - size: firestore.QuerySnapshot['size']; - empty: firestore.QuerySnapshot['empty']; - docs: { - [docId: string]: DocumentSnapshotExpanded; - }; -} -export interface DocumentReference - extends firestore.DocumentReference { - set(data: DataType, options?: firestore.SetOptions): Promise; - get(options?: firestore.GetOptions): Promise>; - onSnapshot(observer: { - next?: (snapshot: DocumentSnapshot) => void; - error?: (error: firestore.FirestoreError) => void; - complete?: () => void; - }): () => void; - onSnapshot( - options: firestore.SnapshotListenOptions, - observer: { - next?: (snapshot: DocumentSnapshot) => void; - error?: (error: Error) => void; - complete?: () => void; - }, - ): () => void; - onSnapshot( - onNext: (snapshot: DocumentSnapshot) => void, - onError?: (error: Error) => void, - onCompletion?: () => void, - ): () => void; - onSnapshot( - options: firestore.SnapshotListenOptions, - onNext: (snapshot: DocumentSnapshot) => void, - onError?: (error: Error) => void, - onCompletion?: () => void, - ): () => void; -} - - - -export interface CollectionReference - extends Query, - Omit { - add(data: DataType): Promise; -} -export interface Collection { - [id: string]: DocumentSnapshotExpanded; -} - - // tslint:disable-next-line:no-namespace export namespace example { type URL = string; @@ -158,7 +36,7 @@ export namespace example { /** A Test is a test model. */ export interface ITestModel { /** TODO: Add documentation to nested_collection in firemodel schema. */ - nestedCollection: CollectionReference; + nestedCollection: firestore.CollectionReference; /** The name. */ name?: string; /** The age. */ @@ -172,7 +50,7 @@ export namespace example { /** TODO: Add documentation to data in firemodel schema. */ data?: firestore.Blob; /** TODO: Add documentation to friend in firemodel schema. */ - friend?: DocumentReference; + friend?: firestore.DocumentReference; /** TODO: Add documentation to location in firemodel schema. */ location?: firestore.GeoPoint; /** TODO: Add documentation to colors in firemodel schema. */ @@ -192,7 +70,7 @@ export namespace example { /** TODO: Add documentation to refs in firemodel schema. */ refs?: firestore.DocumentReference[]; /** TODO: Add documentation to model_refs in firemodel schema. */ - modelRefs?: DocumentReference[]; + modelRefs?: firestore.DocumentReference[]; /** TODO: Add documentation to meta in firemodel schema. */ meta?: { [key: string]: any; }; /** TODO: Add documentation to meta_strs in firemodel schema. */