Skip to content

Commit 2c5c7d1

Browse files
feat: allow hooks for global and bulk smart action (#622)
BREAKING CHANGE: record is no longer send to the hook middleware & values option on smart action is no longer supported
1 parent 545ceaa commit 2c5c7d1

File tree

3 files changed

+70
-10
lines changed

3 files changed

+70
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"dependencies": {
2828
"@babel/runtime": "7.10.1",
2929
"bluebird": "2.9.25",
30-
"forest-express": "9.0.0-beta.9",
30+
"forest-express": "9.0.0-beta.11",
3131
"http-errors": "1.7.2",
3232
"lodash": "4.17.21",
3333
"moment": "2.24.0",

types/index.d.ts

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,69 @@ export interface DatabaseConfiguration {
3232
}
3333
}
3434

35+
export interface ForestRequest extends Request {
36+
user: User,
37+
}
38+
39+
// Base attributes for actions requests (content of request.data.body.attributes)
40+
interface ActionRequestAttributes {
41+
collection_name: string,
42+
ids: string[],
43+
parent_collection_name: string,
44+
parent_collection_id: string,
45+
parent_association_name: string,
46+
all_records: boolean,
47+
all_records_subset_query: Query,
48+
all_records_ids_excluded: string[],
49+
smart_action_id: string,
50+
}
51+
52+
// Base body from requests for action routes / hooks
53+
interface ActionRequestBody {
54+
data: {
55+
attributes: ActionRequestAttributes,
56+
type: 'action-requests',
57+
},
58+
}
59+
60+
// Base body from requests for classic smart action routes
61+
interface SmartActionRequestBody {
62+
data: {
63+
attributes: ActionRequestAttributes & { values: Record<string, any> },
64+
type: 'custom-action-requests',
65+
},
66+
}
67+
68+
// Base body from requests for smart action hooks
69+
interface SmartActionHookRequestBody {
70+
data: {
71+
attributes: ActionRequestAttributes & {
72+
fields: SmartActionChangeHookField[],
73+
changedField: string,
74+
},
75+
type: 'custom-action-hook-requests',
76+
},
77+
}
78+
79+
// Concrete smart action request for classic smart action routes
80+
export interface SmartActionRequest extends ForestRequest {
81+
body: SmartActionRequestBody,
82+
}
83+
84+
// Request passed to smart action load hooks
85+
export interface SmartActionLoadHookRequest extends ForestRequest {
86+
body: ActionRequestBody,
87+
}
88+
89+
// Request passed to smart action change hooks
90+
export interface SmartActionChangeHookRequest extends ForestRequest {
91+
body: SmartActionHookRequestBody,
92+
}
93+
3594
// Everything related to Forest Authentication
3695

3796
export function ensureAuthenticated(request: Request, response: Response, next: NextFunction): void;
97+
3898
export interface User {
3999
renderingId: number;
40100
}
@@ -63,7 +123,7 @@ export class RecordGetter<T> extends AbstractRecordTool<T> {
63123

64124
export class RecordsGetter<T> extends AbstractRecordTool<T> {
65125
getAll(query: Query): Promise<(T & Document)[]>;
66-
getIdsFromRequest(request: Request): Promise<string[]>;
126+
getIdsFromRequest(request: SmartActionRequest | SmartActionLoadHookRequest | SmartActionChangeHookRequest): Promise<string[]>;
67127
}
68128

69129
export class RecordsCounter<M extends Model<any>> extends AbstractRecordTool<M> {
@@ -222,16 +282,16 @@ export interface SmartActionLoadHookField extends SmartActionHookField {
222282
position: number,
223283
}
224284

225-
export interface SmartActionLoadHook<T = any> {
226-
(context: { fields: SmartActionLoadHookField[], record: T & Document }): SmartActionLoadHookField[]
285+
export interface SmartActionLoadHook {
286+
(context: { fields: SmartActionLoadHookField[], request: SmartActionLoadHookRequest }): SmartActionLoadHookField[]
227287
}
228288

229289
export interface SmartActionChangeHookField extends SmartActionHookField {
230290
previousValue: any,
231291
}
232292

233-
export interface SmartActionChangeHook<T = any> {
234-
(context: { fields: SmartActionChangeHookField[], record: T, changedField: SmartActionChangeHookField }): SmartActionChangeHookField[]
293+
export interface SmartActionChangeHook {
294+
(context: { fields: SmartActionChangeHookField[], changedField: SmartActionChangeHookField, request: SmartActionChangeHookRequest }): SmartActionChangeHookField[]
235295
}
236296

237297
export interface SmartActionHooks {

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4769,10 +4769,10 @@ for-in@^1.0.2:
47694769
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"
47704770
integrity sha1-gQaNKVqBQuwKxybG4iAMMPttXoA=
47714771

4772-
4773-
version "9.0.0-beta.9"
4774-
resolved "https://registry.yarnpkg.com/forest-express/-/forest-express-9.0.0-beta.9.tgz#c92f7ea63ddb160667c419a60bfb35b8b97187fd"
4775-
integrity sha512-P6hSk0JZNee3e/eCTbVxzhQd5te+Q77xBuvv4jN5sg60S7ZbYLKNtgm9rpyLjs1iclLZMx/sHn/EbyHb48drwA==
4772+
4773+
version "9.0.0-beta.11"
4774+
resolved "https://registry.yarnpkg.com/forest-express/-/forest-express-9.0.0-beta.11.tgz#99b553c60ac6da76567c13f38fb5a78ad17d0e55"
4775+
integrity sha512-ufU/dIe9WTlQGCR2KJIS/wAXvvEb300Xk2GL1iXHSr4uny8zFlnPXwZlZHdWSC8FQZlnclvUj1xfsanf/WWt1A==
47764776
dependencies:
47774777
"@babel/runtime" "7.10.1"
47784778
base32-encode "1.1.1"

0 commit comments

Comments
 (0)