Skip to content

Commit

Permalink
consolidate make firestore event fns and simplify typings
Browse files Browse the repository at this point in the history
  • Loading branch information
blidd-google committed Apr 3, 2024
1 parent a99f8ea commit 34761bd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 115 deletions.
68 changes: 16 additions & 52 deletions spec/v2/providers/firestore.spec.ts
Expand Up @@ -40,8 +40,6 @@ const eventBase = {
datacontenttype: "application/protobuf",
dataschema:
"https://github.com/googleapis/google-cloudevents/blob/main/proto/google/events/cloud/firestore/v1/data.proto",
authtype: "unknown",
authid: "1234",
id: "379ad868-5ef9-4c84-a8ba-f75f1b056663",
source: "projects/my-project/databases/my-db/documents/d",
subject: "documents/foo/fGRodw71mHutZ4wGDuT8",
Expand Down Expand Up @@ -90,8 +88,8 @@ function makeAuthEvent(data?: any): firestore.RawFirestoreAuthEvent {
return {
...eventBase,
data,
userId: "userId",
authType: "user",
authid: "userId",
authtype: "unknown",
} as firestore.RawFirestoreAuthEvent;
}

Expand Down Expand Up @@ -919,37 +917,25 @@ describe("firestore", () => {

expect(event.data.data()).to.deep.eq({ hello: "delete world" });
});
});

describe("makeFirestoreAuthEvent", () => {
it("should make event from an event without data", () => {
const event = firestore.makeFirestoreAuthEvent(
firestore.createdEventType,
makeAuthEvent(),
firestore.makeParams("foo/fGRodw71mHutZ4wGDuT8", new PathPattern("foo/{bar}"))
);

expect(event.data).to.eq(undefined);
});

it("should make event from a created event", () => {
const event = firestore.makeFirestoreAuthEvent(
firestore.createdEventType,
it("should make event from a created event with auth context", () => {
const event = firestore.makeFirestoreEvent(
firestore.createdEventWithAuthContextType,
makeAuthEvent(makeEncodedProtobuf(createdProto)),
firestore.makeParams("foo/fGRodw71mHutZ4wGDuT8", new PathPattern("foo/{bar}"))
);

expect(event.data.data()).to.deep.eq({ hello: "create world" });
});

it("should make event from a deleted event", () => {
const event = firestore.makeFirestoreAuthEvent(
firestore.deletedEventType,
makeAuthEvent(makeEncodedProtobuf(deletedProto)),
it("should include auth fields if provided in raw event", () => {
const event = firestore.makeFirestoreEvent(
firestore.createdEventWithAuthContextType,
makeAuthEvent(makeEncodedProtobuf(createdProto)),
firestore.makeParams("foo/fGRodw71mHutZ4wGDuT8", new PathPattern("foo/{bar}"))
);

expect(event.data.data()).to.deep.eq({ hello: "delete world" });
expect(event).to.include({ authId: "userId", authType: "unknown" });
});
});

Expand Down Expand Up @@ -984,35 +970,13 @@ describe("firestore", () => {
});
});

describe("makeChangedFirestoreEvent", () => {
it("should make event from an event without data", () => {
const event = firestore.makeChangedFirestoreAuthEvent(
makeAuthEvent(),
firestore.makeParams("foo/fGRodw71mHutZ4wGDuT8", new PathPattern("foo/{bar}"))
);

expect(event.data).to.eq(undefined);
});
it("should include auth fields if provided in raw event", () => {
const event = firestore.makeChangedFirestoreEvent(
makeAuthEvent(makeEncodedProtobuf(writtenProto)),
firestore.makeParams("foo/fGRodw71mHutZ4wGDuT8", new PathPattern("foo/{bar}"))
);

it("should make event from an updated event", () => {
const event = firestore.makeChangedFirestoreEvent(
makeAuthEvent(makeEncodedProtobuf(updatedProto)),
firestore.makeParams("foo/fGRodw71mHutZ4wGDuT8", new PathPattern("foo/{bar}"))
);

expect(event.data.before.data()).to.deep.eq({ hello: "old world" });
expect(event.data.after.data()).to.deep.eq({ hello: "new world" });
});

it("should make event from a written event", () => {
const event = firestore.makeChangedFirestoreEvent(
makeAuthEvent(makeEncodedProtobuf(writtenProto)),
firestore.makeParams("foo/fGRodw71mHutZ4wGDuT8", new PathPattern("foo/{bar}"))
);

expect(event.data.before.data()).to.deep.eq({});
expect(event.data.after.data()).to.deep.eq({ hello: "a new world" });
});
expect(event).to.include({ authId: "userId", authType: "unknown" });
});

describe("makeEndpoint", () => {
Expand Down
106 changes: 43 additions & 63 deletions src/v2/providers/firestore.ts
Expand Up @@ -233,11 +233,16 @@ export function onDocumentWrittenWithAuthContext<Document extends string>(
export function onDocumentWrittenWithAuthContext<Document extends string>(
documentOrOpts: Document | DocumentOptions<Document>,
handler: (
event: FirestoreAuthEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>
event: FirestoreAuthEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>
) => any | Promise<any>
): CloudFunction<
FirestoreAuthEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>
> {
): CloudFunction<FirestoreAuthEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>> {
// const fn = (
// event: FirestoreEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>> & {
// foo: string;
// }
// ): any => {
// return event;
// };
return onChangedOperation(writtenEventWithAuthContextType, documentOrOpts, handler);
}

Expand Down Expand Up @@ -579,11 +584,13 @@ export function makeParams(document: string, documentPattern: PathPattern) {
/** @internal */
export function makeFirestoreEvent<Params>(
eventType: string,
event: RawFirestoreEvent,
event: RawFirestoreEvent | RawFirestoreAuthEvent,
params: Params
): FirestoreEvent<QueryDocumentSnapshot | undefined, Params> {
):
| FirestoreEvent<QueryDocumentSnapshot | undefined, Params>
| FirestoreAuthEvent<QueryDocumentSnapshot | undefined, Params> {
const data = event.data
? eventType === createdEventType
? eventType === createdEventType || eventType === createdEventWithAuthContextType
? createSnapshot(event)
: createBeforeSnapshot(event)
: undefined;
Expand All @@ -595,38 +602,28 @@ export function makeFirestoreEvent<Params>(

delete (firestoreEvent as any).datacontenttype;
delete (firestoreEvent as any).dataschema;
return firestoreEvent;
}

/** @internal */
export function makeFirestoreAuthEvent<Params>(
eventType: string,
event: RawFirestoreAuthEvent,
params: Params
): FirestoreAuthEvent<QueryDocumentSnapshot | undefined, Params> {
const data = event.data
? eventType === createdEventType
? createSnapshot(event)
: createBeforeSnapshot(event)
: undefined;
const firestoreEvent: FirestoreAuthEvent<QueryDocumentSnapshot | undefined, Params> = {
...event,
params,
data,
authType: event.authtype,
authId: event.authid,
};
if ("authtype" in event) {
const eventWithAuth = {
...firestoreEvent,
authType: event.authtype,
authId: event.authid,
};
delete (eventWithAuth as any).authtype;
delete (eventWithAuth as any).authid;
return eventWithAuth;
}

delete (firestoreEvent as any).datacontenttype;
delete (firestoreEvent as any).dataschema;
return firestoreEvent;
}

/** @internal */
export function makeChangedFirestoreEvent<Params>(
event: RawFirestoreEvent,
event: RawFirestoreEvent | RawFirestoreAuthEvent,
params: Params
): FirestoreEvent<Change<QueryDocumentSnapshot> | undefined, Params> {
):
| FirestoreEvent<Change<DocumentSnapshot> | undefined, Params>
| FirestoreAuthEvent<Change<DocumentSnapshot> | undefined, Params> {
const data = event.data
? Change.fromObjects(createBeforeSnapshot(event), createSnapshot(event))
: undefined;
Expand All @@ -637,25 +634,18 @@ export function makeChangedFirestoreEvent<Params>(
};
delete (firestoreEvent as any).datacontenttype;
delete (firestoreEvent as any).dataschema;
return firestoreEvent;
}

export function makeChangedFirestoreAuthEvent<Params>(
event: RawFirestoreAuthEvent,
params: Params
): FirestoreAuthEvent<Change<QueryDocumentSnapshot> | undefined, Params> {
const data = event.data
? Change.fromObjects(createBeforeSnapshot(event), createSnapshot(event))
: undefined;
const firestoreEvent: FirestoreAuthEvent<Change<QueryDocumentSnapshot> | undefined, Params> = {
...event,
params,
data,
authType: event.authtype,
authId: event.authid,
};
delete (firestoreEvent as any).datacontenttype;
delete (firestoreEvent as any).dataschema;
if ("authtype" in event) {
const eventWithAuth = {
...firestoreEvent,
authType: event.authtype,
authId: event.authid,
};
delete (eventWithAuth as any).authtype;
delete (eventWithAuth as any).authid;
return eventWithAuth;
}

return firestoreEvent;
}

Expand Down Expand Up @@ -704,9 +694,7 @@ export function makeEndpoint(
/** @internal */
export function onOperation<
Document extends string,
Event extends
| FirestoreEvent<QueryDocumentSnapshot, ParamsOf<Document>>
| FirestoreAuthEvent<QueryDocumentSnapshot, ParamsOf<Document>>
Event extends FirestoreEvent<QueryDocumentSnapshot, ParamsOf<Document>>
>(
eventType: string,
documentOrOpts: Document | DocumentOptions<Document>,
Expand All @@ -721,10 +709,7 @@ export function onOperation<
typeof document === "string" ? document : document.value()
);
const params = makeParams(event.document, documentPattern) as unknown as ParamsOf<Document>;
const firestoreEvent =
"authid" in raw
? makeFirestoreAuthEvent(eventType, event, params)
: makeFirestoreEvent(eventType, event, params);
const firestoreEvent = makeFirestoreEvent(eventType, event, params);
return wrapTraceContext(withInit(handler))(firestoreEvent);
};

Expand All @@ -738,9 +723,7 @@ export function onOperation<
/** @internal */
export function onChangedOperation<
Document extends string,
Event extends
| FirestoreEvent<Change<QueryDocumentSnapshot>, ParamsOf<Document>>
| FirestoreAuthEvent<Change<QueryDocumentSnapshot>, ParamsOf<Document>>
Event extends FirestoreEvent<Change<DocumentSnapshot>, ParamsOf<Document>>
>(
eventType: string,
documentOrOpts: Document | DocumentOptions<Document>,
Expand All @@ -755,10 +738,7 @@ export function onChangedOperation<
typeof document === "string" ? document : document.value()
);
const params = makeParams(event.document, documentPattern) as unknown as ParamsOf<Document>;
const firestoreEvent =
"authid" in raw
? makeChangedFirestoreAuthEvent(event, params)
: makeChangedFirestoreEvent(event, params);
const firestoreEvent = makeChangedFirestoreEvent(event, params);
return wrapTraceContext(withInit(handler))(firestoreEvent);
};

Expand Down

0 comments on commit 34761bd

Please sign in to comment.