firebase / firebase/firebase-admin-node

Allow generic in onDocumentUpdated, onDocumentCreated, etc...

Ouverte
#2,203 0 commentaires 1 réaction 0 personnes assignées Voir sur GitHub
Langage dominant
TypeScript
Étoiles
1.7k
Forks
419
Merge moyen
3 j 10 h
PR mergées (30 j)
16

Description

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;
}
```

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.