Skip to content

Commit 4008cbc

Browse files
committed
refactor: change function signature
1 parent 10423d4 commit 4008cbc

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

README.md

+7-12
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ This provides a processor class which you can pass to your firestore onWrite cal
88
import {
99
FirestoreOnWriteProcessor,
1010
FirestoreOnWriteProcess,
11-
} from "firebase-extension-utilities/firestore-onwrite-processor";
11+
} from "../firestore-onwrite-processor";
12+
13+
// this can be async if needed
14+
const processFn = ({ input }) => {
15+
// do stuff here
16+
return { output };
17+
};
1218

1319
const myProcess = new FirestoreOnWriteProcess({
1420
id: "myProcessId",
1521
fieldDependencyArray: ["input"],
16-
processFn: ({ input }) => {
17-
// do stuff here
18-
return { output };
19-
},
2022
});
2123

2224
const myProcessor = new FirestoreOnWriteProcessor({
@@ -28,13 +30,6 @@ export const myFunction = functions.firestore
2830
.onWrite(process);
2931
```
3032

31-
### TODO:
32-
33-
- change signature of above, process function should be main argument and then should pass `options` which include optional id etc.
34-
3533
### Used in:
3634

3735
(nda)
38-
## distributed-cloud-tasks
39-
40-
**(TODO)**

jest.config.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const packageJson = require("./package.json");
22

33
module.exports = {
4-
name: packageJson.name,
54
displayName: packageJson.name,
65
preset: "ts-jest",
76
testMatch: ["**/*.test.ts"],

src/firestore-onwrite-processor/index.test.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ type WrappedFirebaseFunction = WrappedFunction<
2727
const firestoreObserver = jest.fn((_x: any) => {});
2828
let collectionName: string;
2929

30+
const processFn = (data: { input: string }) => {
31+
return { output: "foo" };
32+
};
33+
3034
const processes = [
31-
new Process<{ input: string }, { output: string }>({
35+
new Process<{ input: string }, { output: string }>(processFn, {
3236
id: "test",
3337
fieldDependencyArray: ["input"],
34-
processFn: async (data: { input: string }) => {
35-
return { output: "foo" };
36-
},
3738
}),
3839
];
3940

src/firestore-onwrite-processor/process.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,25 @@ export class Process<
44
TInput extends Record<string, FirestoreField>,
55
TOutput extends Record<string, FirestoreField>,
66
> {
7+
processFn: (data: TInput) => Promise<TOutput>;
78
id: string;
89
fieldDependencyArray: string[];
9-
processFn: (data: TInput) => Promise<TOutput>;
10-
errorFn: (e: unknown) => Promise<string | void>;
10+
errorFn?: (e: unknown) => Promise<string | void>;
1111
shouldProcess?: (oldData: TInput, newData: TInput) => boolean;
1212

13-
constructor(options: {
14-
id: string;
15-
fieldDependencyArray: string[];
16-
processFn: (data: TInput) => Promise<TOutput>;
17-
shouldProcess?: (oldData: TInput, newData: TInput) => boolean;
18-
statusField?: string;
19-
}) {
13+
constructor(
14+
processFn: (data: TInput) => TOutput | Promise<TOutput>,
15+
options: {
16+
id: string;
17+
fieldDependencyArray: string[];
18+
shouldProcess?: (oldData: TInput, newData: TInput) => boolean;
19+
errorFn?: (e: unknown) => Promise<string | void>;
20+
},
21+
) {
2022
this.id = options.id;
2123
this.fieldDependencyArray = options.fieldDependencyArray;
22-
this.processFn = options.processFn;
24+
this.processFn = async (data: TInput) => processFn(data);
25+
this.errorFn = options.errorFn;
2326
this.shouldProcess = function (oldData: TInput, newData: TInput) {
2427
if (options.shouldProcess) {
2528
if (!options.shouldProcess(oldData, newData)) {

0 commit comments

Comments
 (0)