firebase / firebase/firebase-admin-node
Allow generic in onDocumentUpdated, onDocumentCreated, etc...
- Lingua principale
- TypeScript
- Stelle
- 1.7k
- Fork
- 419
- Merge medio
- 3g 10h
- PR unite (30g)
- 16
Descrizione
Problem:
Making a wrapper class where types can be inferred is not possible.
Goal:
```ts
onDocumentUpdated( // note the modified declaration below
`${Col.payments}/{paymentID}`,
handlers.authorizePayment,
);
```
with changes to the declaration such as:
```
// firestore.d.ts
export declare function onDocumentUpdated(document: Document, handler: (event: FirestoreEvent> | undefined, ParamsOf>) => any | Promise): CloudFunction | undefined, ParamsOf>>;
```
Use case:
It will be possible to write things like:
```ts
export type UpdateEvent = FirestoreEvent> | undefined>;
class AuthorizePaymentOnValidatedTrigger implements OnUpdate {
handle(event: UpdateEvent) {
// ...
}
}
```
-----
Currently I'm using a wrapper:
```ts
/// Used to create firebase triggers without having to cast
export abstract class Triggers {
static onDocumentUpdated(path: string, handler: OnUpdateHandler) {
return onDocumentUpdated(path, (event) => handler.handle(event as UpdateEvent));
}
static onDocumentCreated(path: string, handler: OnCreateHandler) {
return onDocumentCreated(path, (event) => handler.handle(event as CreateEvent));
}
static onDocumentDeleted(path: string, handler: OnDeleteHandler) {
return onDocumentDeleted(path, (event) => handler.handle(event as DeleteEvent));
}
}
export type UpdateEvent = FirestoreEvent>>;
export interface OnUpdateHandler {
handle(event: UpdateEvent): Promise;
}
export type CreateEvent = FirestoreEvent>;
export interface OnCreateHandler {
handle(event: CreateEvent): Promise;
}
export type DeleteEvent = FirestoreEvent>;
export interface OnDeleteHandler {
handle(deleted: DeleteEvent): Promise;
}
```
-----
together with #2202 would allow:
```ts
handle(event: UpdateEvent) {
const { before, after } = event.data;
}
```
instead of
```ts
handle(event: UpdateEvent) {
const data = event.data;
if (!data) return;
const before = data.before.data() as Payment;
const after = data.after.data() as Payment;
}
```
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Inizia con le dichiarazioni onDocumentUpdated, onDocumentCreated e onDocumentDeleted in firestore.d.ts, usando la firma generica proposta come requisito. Verifica come sono attualmente rappresentati i percorsi dei documenti, i tipi di evento degli handler e i dati dei documenti; il lavoro è completato quando le API dei trigger supportano event handler generici tipizzati come mostrato, senza wrapper cast, insieme al lavoro correlato a #2202.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- firebase, typescript
- Ambito
- backend-api-design, databases
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100